/**
   * Requires selected population to be sorted by fitness.
   *
   * @param params refering parameter set
   * @param iterations number of iterations performed
   * @param selected selected population
   * @return true if the parameters seem ok or were corrected, false if new parameters must be
   *     produced
   */
  private boolean testAndCorrectNumerics(
      CMAParamSet params, int iterations, Population selected) { // not much left here
    boolean corrected = true;
    /* Flat Fitness, Test if function values are identical */
    if (iterations > 1 && (selected.size() > 1)) {
      // selected pop is sorted
      if (nearlySame(
          selected.getEAIndividual(0).getFitness(),
          selected.getEAIndividual(selected.size() - 1).getFitness())) {
        if (TRACE_1)
          System.err.println(
              "flat fitness landscape, consider reformulation of fitness, step-size increased");
        params.sigma *= Math.exp(0.2 + params.c_sig / params.d_sig);
        //    			sigma=0.1;
      }
    }

    if (!checkValidDouble(params.sigma)) {
      System.err.println("Error, unstable sigma!");
      corrected = false;
      //        	params.sigma=params.firstSigma; // MK TODO
      //        	System.err.println(
    }

    /* Align (renormalize) scale C (and consequently sigma) */
    /* e.g. for infinite stationary state simulations (noise
     * handling needs to be introduced for that) */
    double fac = 1.;
    double minEig = 1e-12;
    double maxEig = 1e8;
    if (Mathematics.max(params.eigenvalues) < minEig)
      fac = 1. / Math.sqrt(Mathematics.max(params.eigenvalues));
    else if (Mathematics.min(params.eigenvalues) > maxEig)
      fac = 1. / Math.sqrt(Mathematics.min(params.eigenvalues));

    if (fac != 1.) {
      //    		System.err.println("Scaling by " + fac);
      params.sigma /= fac;
      for (int i = 0; i < params.meanX.length; ++i) {
        params.pathC[i] *= fac;
        params.eigenvalues[i] *= fac * fac;
        for (int j = 0; j <= i; ++j) {
          params.mC.set(i, j, params.mC.get(i, j) * fac * fac);
          if (i != j) params.mC.set(j, i, params.mC.get(i, j));
        }
      }
    }
    return corrected;
  } // Test...
Ejemplo n.º 2
0
  private void plotAll(Population pop) {
    // TODO
    double pos[], vel[];

    for (int i = 0; i < pop.size(); i++) {
      pos = ((TribesExplorer) pop.getEAIndividual(i)).getDoubleData();
      vel = ((TribesExplorer) pop.getEAIndividual(i)).getVelocity();
      plotIndy(pos, vel, i);
      //			hier weiter!
    }
  }
  /* update C */
  private void updateCov(
      CMAParamSet params,
      double[] newPathC,
      double[] newMeanX,
      double hsig,
      int mu,
      Population selected) {
    double newVal = 0;
    int dim = newMeanX.length;
    double ccv = getCCov(params.weights, mu, dim);
    if (ccv > 0) {
      double mcv = CMAParamSet.getMuCov(params.weights, mu);
      /* (only upper triangle!) */
      /* update covariance matrix */
      // System.out.println("CCov " + getCCov(selected) + " Cc " + getCc() + " muCov " +
      // getMuCov(selected));
      for (int i = 0; i < dim; ++i)
        for (int j = 0; j <= i; ++j) {
          //                	oldVal = mC.get(i,j);
          newVal =
              (1 - ccv) * params.mC.get(i, j)
                  + ccv
                      * (1. / mcv)
                      * (newPathC[i] * newPathC[j]
                          + (1 - hsig) * getCc() * (2. - getCc()) * params.mC.get(i, j));
          checkValidDouble(newVal);
          params.mC.set(i, j, newVal);
          if (isRankMu()) {
            for (int k = 0; k < mu; ++k) {
              /*
               * additional rank mu
               * update
               */
              //                    	double[] x_k =
              // ((InterfaceDataTypeDouble)selected.getEAIndividual(k)).getDoubleData();
              double[] x_k =
                  AbstractEAIndividual.getDoublePositionShallow(selected.getEAIndividual(k));
              newVal =
                  params.mC.get(i, j)
                      + ccv
                          * (1 - 1. / mcv)
                          * params.weights[k]
                          * (x_k[i] - params.meanX[i])
                          * (x_k[j] - params.meanX[j])
                          / (getSigma(params, i) * getSigma(params, j)); // TODO right sigmas?
              checkValidDouble(newVal);
              params.mC.set(i, j, newVal);
            }
          }
        }
      // fill rest of C
      for (int i = 0; i < dim; ++i) {
        for (int j = i + 1; j < dim; ++j) {

          params.mC.set(i, j, params.mC.get(j, i));
        }
      }
      if (params.mC.get(0, 1) != params.mC.get(1, 0)) {
        System.err.println("WARNING, C is not symmetric!");
      }
      //            maxsqrtdiagC = Math.sqrt(math.max(math.diag(C)));
      //            minsqrtdiagC = Math.sqrt(math.min(math.diag(C)));
    } // update of C
  }
  /**
   * Perform the main adaption of sigma and C using evolution paths. The evolution path is deduced
   * from the center of the selected population compared to the old mean value. See Hansen&Kern 04
   * for further information.
   *
   * @param oldGen
   * @param selectedP
   */
  public void adaptAfterSelection(Population oldGen, Population selectedP) {
    Population selectedSorted =
        selectedP.getSortedBestFirst(new AbstractEAIndividualComparator(-1));

    int mu, lambda;
    mu = selectedP.size();
    lambda = oldGen.size();
    int generation = oldGen.getGeneration();
    if (mu >= lambda) {
      // try to override by oldGen additional data:
      if (oldGen.hasData(EvolutionStrategies.esMuParam))
        mu = (Integer) oldGen.getData(EvolutionStrategies.esMuParam);
      if (oldGen.hasData(EvolutionStrategies.esLambdaParam))
        lambda = (Integer) oldGen.getData(EvolutionStrategies.esLambdaParam);
    }
    if (mu >= lambda) {
      mu = Math.max(1, lambda / 2);
      EVAERROR.errorMsgOnce(
          "Warning: invalid mu/lambda ratio! Setting mu to lambda/2 = "
              + mu
              + ", lambda = "
              + lambda);
    }
    CMAParamSet params;
    if (oldGen.getGeneration()
        <= 1) { // init new param set. At gen < 1 we shouldnt be called, but better do it once too
                // often
      if (oldGen.hasData(cmaParamsKey))
        params =
            CMAParamSet.initCMAParams(
                (CMAParamSet) oldGen.getData(cmaParamsKey),
                mu,
                lambda,
                oldGen,
                getInitSigma(oldGen));
      else params = CMAParamSet.initCMAParams(mu, lambda, oldGen, getInitSigma(oldGen));
    } else {
      if (!oldGen.hasData(cmaParamsKey)) {
        if (oldGen.getGeneration() > 1)
          EVAERROR.errorMsgOnce("Error: population lost cma parameters. Incompatible optimizer?");
        params = CMAParamSet.initCMAParams(mu, lambda, oldGen, getInitSigma(oldGen));
      } else params = (CMAParamSet) oldGen.getData(cmaParamsKey);
    }

    if (lambda == 1
        && (oldGen.size() == 1)
        && (selectedP.size() == 1)
        && (oldGen.getEAIndividual(0).equals(selectedP.getEAIndividual(0)))) {
      // nothing really happened, so do not adapt and just store default params
      lastParams = (CMAParamSet) params.clone();
      oldGen.putData(cmaParamsKey, params);
      selectedP.putData(cmaParamsKey, params);
      return;
    }

    if (TRACE_1) {
      System.out.println("WCMA adaptGenerational **********");
      //			System.out.println("newPop measures: " +
      // BeanInspector.toString(newPop.getPopulationMeasures()));
      System.out.println("mu_eff: " + CMAParamSet.getMuEff(params.weights, mu));
      System.out.println(params.toString());
      System.out.println("*********************************");
    }

    double[] newMeanX = calcMeanX(params.weights, selectedSorted);
    if (TRACE_1) System.out.println("newMeanX:  " + BeanInspector.toString(newMeanX));

    int dim = params.meanX.length;
    double[] BDz = new double[dim];
    for (int i = 0; i < dim; i++) {
        /* calculate xmean and BDz~N(0,C) */
      // Eq. 4 from HK04, most right term
      BDz[i] =
          Math.sqrt(CMAParamSet.getMuEff(params.weights, mu))
              * (newMeanX[i] - params.meanX[i])
              / getSigma(params, i);
    }
    //        if (TRACE_2) System.out.println("BDz is " + BeanInspector.toString(BDz));

    double[] newPathS = params.pathS.clone();
    double[] newPathC = params.pathC.clone();

    double[] zVect = new double[dim];
    /* calculate z := D^(-1) * B^(-1) * BDz into artmp, we could have stored z instead */
    for (int i = 0; i < dim; ++i) {
      double sum = 0.;
      for (int j = 0; j < dim; ++j) {
        sum += params.mB.get(j, i) * BDz[j]; // times B transposed, (Eq 4) in HK04
      }
      if (params.eigenvalues[i] < 0) {
        EVAERROR.errorMsgOnce(
            "Warning: negative eigenvalue in MutateESRankMuCMA! (possibly multiple cases)");
        zVect[i] = 0;
      } else {
        zVect[i] = sum / Math.sqrt(params.eigenvalues[i]);
        if (!checkValidDouble(zVect[i])) {
          System.err.println("Error, infinite zVect entry!");
          zVect[i] = 0; // TODO MK
        }
      }
    }

    /* cumulation for sigma (ps) using B*z */
    for (int i = 0; i < dim; ++i) {
      double sum = 0.;
      for (int j = 0; j < dim; ++j) sum += params.mB.get(i, j) * zVect[j];
      newPathS[i] =
          (1. - params.c_sig) * params.pathS[i]
              + Math.sqrt(params.c_sig * (2. - params.c_sig)) * sum;
      if (!checkValidDouble(newPathS[i])) {
        System.err.println("Error, infinite pathS!");
      }
    }
    //		System.out.println("pathS diff: " + BeanInspector.toString(Mathematics.vvSub(newPathS,
    // pathS)));
    //		System.out.println("newPathS is " + BeanInspector.toString(newPathS));

    double psNorm = Mathematics.norm(newPathS);

    double hsig = 0;
    if (psNorm / Math.sqrt(1. - Math.pow(1. - params.c_sig, 2. * generation)) / expRandStepLen
        < 1.4 + 2. / (dim + 1.)) {
      hsig = 1;
    }
    for (int i = 0; i < dim; ++i) {
      newPathC[i] =
          (1. - getCc()) * params.pathC[i] + hsig * Math.sqrt(getCc() * (2. - getCc())) * BDz[i];
      checkValidDouble(newPathC[i]);
    }

    // TODO missing: "remove momentum in ps"

    if (TRACE_1) {
      System.out.println("newPathC: " + BeanInspector.toString(newPathC));
      System.out.println("newPathS: " + BeanInspector.toString(newPathS));
    }

    if (TRACE_1) System.out.println("Bef: C is \n" + params.mC.toString());
    if (params.meanX == null) params.meanX = newMeanX;

    updateCov(params, newPathC, newMeanX, hsig, mu, selectedSorted);
    updateBD(params);

    if (TRACE_2) System.out.println("Aft: C is " + params.mC.toString());

    /* update of sigma */
    double sigFact = Math.exp(((psNorm / expRandStepLen) - 1) * params.c_sig / params.d_sig);
    if (Double.isInfinite(sigFact))
      params.sigma *= 10.; // in larger search spaces sigma tends to explode after init.
    else params.sigma *= sigFact;

    if (!testAndCorrectNumerics(params, generation, selectedSorted)) {
      // parameter seemingly exploded...
      params =
          CMAParamSet.initCMAParams(
              params,
              mu,
              lambda,
              params.meanX,
              ((InterfaceDataTypeDouble) oldGen.getEAIndividual(0)).getDoubleRange(),
              params.firstSigma);
    }

    if (TRACE_1) {
      System.out.println("sigma=" + params.sigma);
      System.out.print("psLen=" + (psNorm) + " ");
      outputParams(params, mu);
    }

    // take over data
    params.meanX = newMeanX;
    params.pathC = newPathC;
    params.pathS = newPathS;
    params.firstAdaptionDone = true;

    lastParams = (CMAParamSet) params.clone();
    oldGen.putData(cmaParamsKey, params);
    selectedP.putData(cmaParamsKey, params);
    //		if (TRACE_2) System.out.println("sampling around " + BeanInspector.toString(meanX));
  }