Esempio n. 1
0
  /**
   * This method allows an optimizer to register a change in the optimizer.
   *
   * @param source The source of the event.
   * @param name Could be used to indicate the nature of the event.
   */
  @Override
  public void registerPopulationStateChanged(Object source, String name) {
    if (name.equals(Population.NEXT_GENERATION_PERFORMED)) {
      Population population = ((InterfaceOptimizer) source).getPopulation();
      double x = 100 / this.multiRuns;
      if (this.optimizationParameters.getTerminator() instanceof EvaluationTerminator) {
        double y =
            x
                / (double)
                    ((EvaluationTerminator) this.optimizationParameters.getTerminator())
                        .getFitnessCalls();
        currentProgress = (int) (this.currentRun * x + population.getFunctionCalls() * y);
      } else {
        currentProgress = (int) (this.currentRun * x);
      }
      updateStatus(currentProgress);

      // data to be stored in file
      double tmpd = 0;
      StringBuilder tmpLine = new StringBuilder("");
      tmpLine.append(population.getFunctionCalls());
      tmpLine.append("\t");
      tmpLine.append(population.getBestEAIndividual().getFitness(0));
      tmpLine.append("\t");
      for (int i = 0; i < population.size(); i++) {
        tmpd += population.get(i).getFitness(0) / (double) population.size();
      }
      tmpLine.append("\t");
      tmpLine.append(tmpd);
      tmpLine.append("\t");
      tmpLine.append(population.getWorstEAIndividual().getFitness(0));
      // tmpLine.append("\t");
      // tmpLine.append(this.optimizationParameters.getProblem().getAdditionalDataValue(population));
      this.writeToFile(tmpLine.toString());

      Double[] tmpData = new Double[2];
      tmpData[0] = (double) population.getFunctionCalls();
      // instead of adding simply the best fitness value i'll ask the problem what to show
      tmpData[1] = this.optimizationParameters.getProblem().getDoublePlotValue(population);
      if (this.plot != null) {
        if (this.continueFlag) {
          this.plot.setConnectedPoint(
              tmpData[0] + this.recentFunctionCalls, tmpData[1], 1000 + this.currentRun);
        } else {
          this.plot.setConnectedPoint(tmpData[0], tmpData[1], 1000 + this.currentRun);
        }
      }
      this.tmpData.add(tmpData);
    }
  }
Esempio n. 2
0
  /**
   * Make sure that the parameter sets of each population are updated (reinitialized) if a
   * population is reinitialized.
   *
   * @see InterfacePopulationChangedEventListener
   */
  @Override
  public void registerPopulationStateChanged(Object source, String name) {
    if (name.equals(Population.POPULATION_INITIALIZED)) {
      Population pop = (Population) source;

      CMAParamSet params = (CMAParamSet) (pop.getData(MutateESRankMuCMA.cmaParamsKey));
      int mu;
      if (pop.hasData(EvolutionStrategies.esMuParam)) {
        mu = (Integer) pop.getData(EvolutionStrategies.esMuParam);
      } else {
        System.err.println("Unknown mu in reinit! using lambda/2...");
        mu = pop.size() / 2;
      }
      pop.putData(
          MutateESRankMuCMA.cmaParamsKey,
          CMAParamSet.initCMAParams(params, mu, pop.size(), pop, params.firstSigma));
    }
  }
  /**
   * The subswarm is deactivated and the particles indices are returned. They are to be
   * reinitialized into the mainswarm.
   */
  @Override
  public int[] deactivateSubswarm(
      ParticleSubSwarmOptimization subswarm, ParticleSubSwarmOptimization mainswarm) {
    if (!subswarm.isActive()) {
      System.out.println("deactivateSubSwarm: try to deactivate inactive subswarm");
      return null;
    }

    // use the indizes of the deactivated particles for the reinitialized particles (important for
    // ANPSO)
    Population pop = subswarm.getPopulation();
    int[] particleIndices = new int[pop.size()];
    for (int i = 0; i < pop.size(); ++i) {
      AbstractEAIndividual indy = pop.getEAIndividual(i);
      // Integer index = (Integer)indy.getData("particleIndex");
      particleIndices[i] = indy.getIndividualIndex(); // index.intValue();
    }
    subswarm.SetActive(false);
    return particleIndices;
  }
 public boolean isConverged(Population pop) {
   Vector<Double> bests = (Vector<Double>) pop.getEAIndividual(0).getData(NichePSO.fitArchiveKey);
   if (bests.size() < haltingWindow) {
     return false;
   } else {
     List<Double> lst = bests.subList(bests.size() - haltingWindow, bests.size());
     bests = new Vector<>(haltingWindow);
     bests.addAll(lst);
     for (int i = 1; i < pop.size(); i++) {
       for (int k = 0; k < haltingWindow; k++) {
         Vector<Double> fitArch =
             (Vector<Double>) pop.getEAIndividual(i).getData(NichePSO.fitArchiveKey);
         int archIndex =
             fitArch.size()
                 - haltingWindow
                 + k; // index within the fitness archive of the current particle, which may be
                      // larger than the bests list - the tail is important
         if (archIndex >= 0 && (fitArch.get(archIndex) < bests.get(k))) {
           bests.set(k, fitArch.get(archIndex));
         }
       }
     }
   }
   // bests contains now the sequence of best fitness values across the last generations
   Double historicHWAgo = bests.get(0);
   boolean res = true;
   for (int i = 1; i < haltingWindow; i++) {
     // if historic[-hW] is worse than historic[-hW+i] return false
     Double historicIter = bests.get(i);
     // if the iterated value (the later one in history) has improved, there is no convergence.
     boolean improvementHappened =
         (historicIter < historicHWAgo); // testSecondForImprovement(historicHWAgo, historicIter));
     if (improvementHappened) {
       res = false;
       break;
     }
   }
   return res;
 }
Esempio n. 5
0
 /**
  * This method takes a population of individuals with an array of fitness values and calculates a
  * single fitness value to replace the former fitness array. Please note: The orignal fitness
  * values are lost this way, so please use the individual.setData() method if you still want to
  * access the original fitness values.
  *
  * @param pop The population to process.
  */
 @Override
 public void convertMultiObjective2SingleObjective(Population pop) {
   for (int i = 0; i < pop.size(); i++) {
     this.convertSingleIndividual(pop.get(i));
   }
 }