/** This method will optimize */ @Override public void optimize() { AbstractEAIndividual indy; Population original = (Population) this.population.clone(); double[] fitness; for (int i = 0; i < this.population.size(); i++) { indy = this.population.get(i); double tmpD = indy.getMutationProbability(); indy.setMutationProbability(1.0); indy.mutate(); indy.setMutationProbability(tmpD); } this.optimizationProblem.evaluate(this.population); for (int i = 0; i < this.population.size(); i++) { fitness = this.population.get(i).getFitness(); if (fitness[0] > this.currentFloodPeak) { this.population.remove(i); this.population.add(i, original.get(i)); } } this.currentFloodPeak -= this.drainRate; this.population.incrGeneration(); this.firePropertyChangedEvent(Population.NEXT_GENERATION_PERFORMED); }
/** * This method return the currently selected populations * * @return the selected populations */ public Population getSelectedPopulations() { Population result = new Population(); for (int i = 0; i < this.populationHistory.length; i++) { if (this.use[i]) { result.addPopulation(this.populationHistory[i]); } } this.currentProblem.evaluate(result); return result; }
/** * Initializes the CMA parameter set for given mu, lambda and a population. The initialSigma * parameter is used as initial sigma directly unless it is <0, in that case the average range is * used as initial sigma. The parameter instance is also added as listener to the population. * * @param params the CMA parameter set to be used - its data are overwritten * @param mu ES mu parameter * @param lambda ES lambda parameter * @param pop associated Population * @param initialSigma initial sigma or -1 to indicate the usage of average range * @return */ public static CMAParamSet initCMAParams( CMAParamSet params, int mu, int lambda, Population pop, double initialSigma) { initCMAParams( params, mu, lambda, pop.getBestEAIndividual().getDoublePosition(), ((InterfaceDataTypeDouble) pop.getEAIndividual(0)).getDoubleRange(), initialSigma); pop.addPopulationChangedEventListener(params); return params; }
@Override public void stopOptimizationPerformed(boolean normal, String stopMessage) { this.currentRun.put("stopMessage", stopMessage); this.currentRun.put( "totalFunctionCalls", this.currentParameters.getOptimizer().getPopulation().getFunctionCalls()); // ToDo: Figure out a sane way to do this. Having multirun > 1 increases SnakeYAML memory // consumption to beyond infinity // this.currentRun.put("generations", currentGenerations); Population pop = this.currentParameters.getOptimizer().getAllSolutions().getSolutions(); this.currentRun.put("solution", pop.getBestEAIndividual().getDoublePosition().clone()); this.currentRun.put("bestFitness", pop.getBestFitness().clone()); this.currentRun.put("meanFitness", pop.getMeanFitness().clone()); this.runs.add(currentRun); }
/** * 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); } }
@Override public void actionPerformed(ActionEvent event) { JFrame frame = new JFrame(); frame.setTitle( "The current best solution for " + optimizationParameters.getProblem().getName()); frame.setSize(400, 300); frame.setLocation(450, 250); Population pop = optimizationParameters.getOptimizer().getPopulation(); frame .getContentPane() .add( optimizationParameters .getProblem() .drawIndividual( pop.getGeneration(), pop.getFunctionCalls(), pop.getBestEAIndividual())); frame.validate(); frame.setVisible(true); }
@Override public void initializeByPopulation(Population pop, boolean reset) { this.setPopulation((Population) pop.clone()); if (reset) { this.getPopulation().initialize(); this.optimizationProblem.evaluate(this.getPopulation()); this.firePropertyChangedEvent(Population.NEXT_GENERATION_PERFORMED); } }
public void addPopulation2History(Population pop) { InterfaceOptimizationObjective[] tmpObj = null; if (this.show == null) { this.use = new boolean[1]; this.use[0] = true; this.show = new boolean[1]; this.show[0] = true; this.colors = new Color[1]; this.colors[0] = this.getColor4Index(0); } else { boolean[] newUse = new boolean[this.show.length + 1]; boolean[] newShow = new boolean[this.show.length + 1]; Color[] newColor = new Color[this.show.length + 1]; for (int i = 0; i < this.show.length; i++) { newUse[i] = this.use[i]; newShow[i] = this.show[i]; newColor[i] = this.colors[i]; } newUse[show.length] = true; newShow[show.length] = true; newColor[show.length] = this.getColor4Index(this.populationHistory.length); this.use = newUse; this.show = newShow; this.colors = newColor; } Population[] newPop = new Population[this.populationHistory.length + 1]; System.arraycopy(this.populationHistory, 0, newPop, 0, this.populationHistory.length); newPop[newPop.length - 1] = (Population) pop.clone(); newPop[newPop.length - 1].addPopulation(newPop[newPop.length - 1].getArchive()); newPop[newPop.length - 1].SetArchive(null); this.populationHistory = newPop; ArrayList fitness = new ArrayList(); ArrayList objectives = new ArrayList(); ArrayList constraint = new ArrayList(); if (this.currentProblem instanceof InterfaceMultiObjectiveDeNovoProblem) { tmpObj = ((InterfaceMultiObjectiveDeNovoProblem) this.currentProblem).getProblemObjectives(); } for (int j = 0; j < newPop[newPop.length - 1].size(); j++) { if (tmpObj != null) { double[] tmoF = new double[tmpObj.length]; for (int k = 0; k < tmpObj.length; k++) { tmoF[k] = (Double) newPop[newPop.length - 1].get(j).getData(tmpObj[k].getIdentName()); } objectives.add(tmoF); } fitness.add(newPop[newPop.length - 1].get(j).getFitness()); constraint.add(newPop[newPop.length - 1].get(j).getConstraintViolation()); } if (this.objectiveCache != null) { this.objectiveCache.add(objectives); } this.fitnessCache.add(fitness); this.constraintCache.add(constraint); }
/** * This method will initialize the optimizer with a given population * * @param reset If true the population is reset. */ @Override public void initializeByPopulation(Population pop, boolean reset) { this.population = (Population) pop.clone(); if (reset) { this.population.initialize(); this.optimizationProblem.evaluate(this.population); this.firePropertyChangedEvent(Population.NEXT_GENERATION_PERFORMED); } this.currentFloodPeak = this.initialFloodPeak; }
/** * 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) { int currentProgress; if (name.equals(Population.NEXT_GENERATION_PERFORMED)) { if (this.state.isVisible) { Population population = ((InterfaceOptimizer) source).getPopulation(); double x = 100; if (this.state.terminator instanceof EvaluationTerminator) { double y = x / (double) ((EvaluationTerminator) this.state.terminator).getFitnessCalls(); currentProgress = (int) (population.getFunctionCalls() * y); } else { currentProgress = 0; } updateStatus("Optimizing...", currentProgress); } else { // perhaps i could write it to file!? } } }
/** * 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; }
/** * 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)); } }
/** * 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)); } }
/** * This method establishes a fitness cache to give the plot methods easier and faster access to * the data * * @param reevaluate */ public void makeFitnessCache(boolean reevaluate) { InterfaceOptimizationObjective[] tmpObj = null; if (reevaluate) { // clear all archives, since problem dimension may have changed for (int i = 0; i < this.populationHistory.length; i++) { if (this.populationHistory[i].getArchive() != null) { this.populationHistory[i].addPopulation(this.populationHistory[i].getArchive()); this.populationHistory[i].SetArchive(null); } } Population pop = this.optimizer.getPopulation(); if (pop.getArchive() != null) { pop.addPopulation(pop.getArchive()); pop.SetArchive(null); } this.currentProblem.evaluate(pop); } this.fitnessCache = new ArrayList(); this.objectiveCache = null; this.constraintCache = new ArrayList(); if (this.currentProblem instanceof InterfaceMultiObjectiveDeNovoProblem) { this.objectiveCache = new ArrayList(); tmpObj = ((InterfaceMultiObjectiveDeNovoProblem) this.currentProblem).getProblemObjectives(); } this.paretoFront = new Population(); for (int i = 0; i < this.populationHistory.length; i++) { if (reevaluate) { ((AbstractMultiObjectiveOptimizationProblem) this.currentProblem).resetParetoFront(); this.currentProblem.evaluate(this.populationHistory[i]); } this.paretoFront.addPopulation(this.populationHistory[i]); ArrayList fitness = new ArrayList(); ArrayList objectives = new ArrayList(); ArrayList constraint = new ArrayList(); for (int j = 0; j < this.populationHistory[i].size(); j++) { if (tmpObj != null) { double[] tmoF = new double[tmpObj.length]; for (int k = 0; k < tmpObj.length; k++) { if (this.populationHistory[i].get(j) == null) { System.out.println("Individual " + i + " == null!"); } if (tmpObj[k] == null) { System.out.println("Objective " + k + " == null!"); } if (this.populationHistory[i].get(j).getData(tmpObj[k].getIdentName()) == null) { System.out.println("User Data " + k + " " + tmpObj[k].getIdentName() + " == null!"); } tmoF[k] = (Double) this.populationHistory[i].get(j).getData(tmpObj[k].getIdentName()); } objectives.add(tmoF); } fitness.add(this.populationHistory[i].get(j).getFitness()); constraint.add(this.populationHistory[i].get(j).getConstraintViolation()); } if (this.objectiveCache != null) { this.objectiveCache.add(objectives); } this.fitnessCache.add(fitness); this.constraintCache.add(constraint); } ArchivingAllDominating arch = new ArchivingAllDominating(); arch.addElementsToArchive(this.paretoFront); this.paretoFront = this.paretoFront.getArchive(); this.paretoFront.SetArchive(null); }
public void MOCCOOptimization() { boolean cont = true; InterfaceProcessElement tmpP; while (cont) { this.iteration++; while (stillWorking) { try { Thread.sleep(1000); } catch (java.lang.InterruptedException e) { } } if (this.state.originalProblem == null) { this.state.originalProblem = new TF1Problem(); tmpP = new MOCCOProblemInitialization(this); tmpP.initProcessElementParametrization(); while (!tmpP.isFinished()) { try { Thread.sleep(1000); } catch (java.lang.InterruptedException e) { } } this.state.currentProblem = (InterfaceOptimizationProblem) this.state.originalProblem.clone(); this.view.problemChanged(true); this.parameterPanel.removeAll(); tmpP = new MOCCOInitialPopulationSize(this); tmpP.initProcessElementParametrization(); while (!tmpP.isFinished()) { try { Thread.sleep(1000); } catch (java.lang.InterruptedException e) { } } this.state.initialPopulationSize = Math.max(1, this.state.initialPopulationSize); Population pop = new Population(); pop.setTargetSize(this.state.initialPopulationSize); this.state.currentProblem = (InterfaceOptimizationProblem) this.state.originalProblem.clone(); this.state.currentProblem.initializePopulation(pop); this.state.currentProblem.evaluate(pop); this.state.addPopulation2History(pop); this.view.problemChanged(true); } ((InterfaceMultiObjectiveDeNovoProblem) this.state.currentProblem) .deactivateRepresentationEdit(); this.updateStatus("Analysis/Redefinition", 33); tmpP = new MOCCOProblemRedefinition(this); tmpP.initProcessElementParametrization(); while (!tmpP.isFinished()) { try { Thread.sleep(1000); } catch (java.lang.InterruptedException e) { } } this.state.makeFitnessCache(true); this.state.currentProblem.initializeProblem(); this.state.makeBackup(); this.view.problemChanged(true); if (this.state.currentProblem.isMultiObjective()) { this.updateStatus("MO Strategy Selection", 50); tmpP = new MOCCOChooseMOStrategy(this); tmpP.initProcessElementParametrization(); while (!tmpP.isFinished()) { try { Thread.sleep(1000); } catch (java.lang.InterruptedException e) { } } switch (((MOCCOChooseMOStrategy) tmpP).getMOStrategy()) { case MOCCOChooseMOStrategy.STRATEGY_MOEA: { this.updateStatus("MOEA Parameterization", 75); tmpP = new MOCCOParameterizeMO(this); tmpP.initProcessElementParametrization(); while (!tmpP.isFinished()) { try { Thread.sleep(1000); } catch (java.lang.InterruptedException e) { } } break; } case MOCCOChooseMOStrategy.STRATEGY_STEP: { this.updateStatus("Reference Solution...", 75); tmpP = new MOCCOChooseReferenceSolution(this); tmpP.initProcessElementParametrization(); while (!tmpP.isFinished()) { try { Thread.sleep(1000); } catch (java.lang.InterruptedException e) { } } AbstractEAIndividual reference = ((MOCCOChooseReferenceSolution) tmpP).getReferenceSolution(); this.updateStatus("STEP Parameterization...", 90); tmpP = new MOCCOParameterizeSTEP(this); ((MOCCOParameterizeSTEP) tmpP).setReferenceSolution(reference); tmpP.initProcessElementParametrization(); while (!tmpP.isFinished()) { try { Thread.sleep(1000); } catch (java.lang.InterruptedException e) { } } break; } case MOCCOChooseMOStrategy.STRATEGY_REFP: { this.updateStatus("Reference Point...", 75); tmpP = new MOCCOChooseReferencePoint(this); tmpP.initProcessElementParametrization(); while (!tmpP.isFinished()) { try { Thread.sleep(1000); } catch (java.lang.InterruptedException e) { } } double[] reference = ((MOCCOChooseReferencePoint) tmpP).getReferencePoint(); this.updateStatus("Reference Point Parameterization...", 90); tmpP = new MOCCOParameterizeRefPoint(this); ((MOCCOParameterizeRefPoint) tmpP).setReferencePoint(reference); tmpP.initProcessElementParametrization(); while (!tmpP.isFinished()) { try { Thread.sleep(1000); } catch (java.lang.InterruptedException e) { } } break; } case MOCCOChooseMOStrategy.STRATEGY_TBCH: { this.updateStatus("Reference Point...", 75); tmpP = new MOCCOChooseReferencePoint(this); tmpP.initProcessElementParametrization(); while (!tmpP.isFinished()) { try { Thread.sleep(1000); } catch (java.lang.InterruptedException e) { } } double[] reference = ((MOCCOChooseReferencePoint) tmpP).getReferencePoint(); this.updateStatus("Tchebycheff Method Parameterization...", 90); tmpP = new MOCCOParameterizeTchebycheff(this); ((MOCCOParameterizeTchebycheff) tmpP).setReferencePoint(reference); tmpP.initProcessElementParametrization(); while (!tmpP.isFinished()) { try { Thread.sleep(1000); } catch (java.lang.InterruptedException e) { } } break; } case MOCCOChooseMOStrategy.STRATEGY_GDF: { this.updateStatus("Reference Solution...", 75); tmpP = new MOCCOChooseReferenceSolution(this); tmpP.initProcessElementParametrization(); while (!tmpP.isFinished()) { try { Thread.sleep(1000); } catch (java.lang.InterruptedException e) { } } AbstractEAIndividual reference = ((MOCCOChooseReferenceSolution) tmpP).getReferenceSolution(); this.updateStatus("Geoffrion-Dyer-Feinberg Method Parameterization...", 90); tmpP = new MOCCOParameterizeGDF(this); ((MOCCOParameterizeGDF) tmpP).setReferenceSolution(reference); tmpP.initProcessElementParametrization(); while (!tmpP.isFinished()) { try { Thread.sleep(1000); } catch (java.lang.InterruptedException e) { } } break; } default: { tmpP = new MOCCOParameterizeMO(this); while (!tmpP.isFinished()) { try { Thread.sleep(1000); } catch (java.lang.InterruptedException e) { } } break; } } } else { this.updateStatus("SO-Optimizer Parameterization", 66); tmpP = new MOCCOParameterizeSO(this); tmpP.initProcessElementParametrization(); while (!tmpP.isFinished()) { try { Thread.sleep(1000); } catch (java.lang.InterruptedException e) { } } } // now optimize this.updateStatus("Optimizing...", 0); this.startExperiment(); } }