Without any loss of functionality, JE2Base can now also be compiled with Java 1.5.

This commit is contained in:
Andreas Dräger 2010-03-29 02:51:13 +00:00
parent 9848734f07
commit df9c625771
2 changed files with 313 additions and 249 deletions

View File

@ -6,8 +6,9 @@ import eva2.server.go.populations.Population;
import eva2.server.go.problems.InterfaceOptimizationProblem;
import eva2.tools.math.Jama.Matrix;
public class MutateESCovarianceMatrixAdaptionPlus extends MutateESCovarianceMatrixAdaption implements InterfaceMutationGenerational
{
public class MutateESCovarianceMatrixAdaptionPlus extends
MutateESCovarianceMatrixAdaption implements
InterfaceMutationGenerational {
protected double m_psuccess;
protected double m_cp;
protected double m_psuccesstarget = 0.44;
@ -15,13 +16,12 @@ public class MutateESCovarianceMatrixAdaptionPlus extends MutateESCovarianceMatr
protected double m_pthresh;
protected int m_lambda = 1;
public MutateESCovarianceMatrixAdaptionPlus() {
super();
}
public MutateESCovarianceMatrixAdaptionPlus(MutateESCovarianceMatrixAdaptionPlus mutator) {
public MutateESCovarianceMatrixAdaptionPlus(
MutateESCovarianceMatrixAdaptionPlus mutator) {
super(mutator);
m_psuccess = mutator.m_psuccess;
m_cp = mutator.m_cp;
@ -31,35 +31,42 @@ public class MutateESCovarianceMatrixAdaptionPlus extends MutateESCovarianceMatr
m_stepd = mutator.m_stepd;
}
/** This method will enable you to clone a given mutation operator
/**
* This method will enable you to clone a given mutation operator
*
* @return The clone
*/
public Object clone() {
return new MutateESCovarianceMatrixAdaptionPlus(this);
}
/** This method allows you to init the mutation operator
* @param individual The individual that will be mutated.
* @param opt The optimization problem.
/**
* This method allows you to init the mutation operator
*
* @param individual
* The individual that will be mutated.
* @param opt
* The optimization problem.
*/
public void init(AbstractEAIndividual individual, InterfaceOptimizationProblem opt) {
public void init(AbstractEAIndividual individual,
InterfaceOptimizationProblem opt) {
if (!(individual instanceof InterfaceESIndividual)) return;
if (!(individual instanceof InterfaceESIndividual))
return;
super.init(individual, opt);
m_psuccesstarget = 1.0 / (5 + Math.sqrt(m_lambda) / 2);
m_psuccess = m_psuccesstarget;
m_stepd = 1.0 + m_D / (2.0 * m_lambda);
m_cp = m_psuccesstarget * m_lambda / (2 + m_psuccesstarget * m_lambda);
m_c = 2.0 / (2.0 + m_D);
this.cov = 2.0/(6.0+Math.pow(m_D, 2)); //ATTN: differs from the standard CMA-ES
this.cov = 2.0 / (6.0 + Math.pow(m_D, 2)); // ATTN: differs from the
// standard CMA-ES
m_pthresh = 0.44;
}
protected void adaptStrategyGen(AbstractEAIndividual child,AbstractEAIndividual parent) {
protected void adaptStrategyGen(AbstractEAIndividual child,
AbstractEAIndividual parent) {
if (child.getFitness(0) <= parent.getFitness(0)) {
// updatecov
updateCovariance(child, parent);
@ -72,10 +79,12 @@ public class MutateESCovarianceMatrixAdaptionPlus extends MutateESCovarianceMatr
* @param parent
* @param child
*/
public void updateCovariance(AbstractEAIndividual child,AbstractEAIndividual parent) {
public void updateCovariance(AbstractEAIndividual child,
AbstractEAIndividual parent) {
double[] step = new double[m_D];
for (int i = 0; i < m_D; i++) {
step[i]=((InterfaceESIndividual)parent).getDGenotype()[i]-((InterfaceESIndividual)child).getDGenotype()[i];
step[i] = ((InterfaceESIndividual) parent).getDGenotype()[i]
- ((InterfaceESIndividual) child).getDGenotype()[i];
}
updateCovariance(step);
}
@ -87,6 +96,7 @@ public class MutateESCovarianceMatrixAdaptionPlus extends MutateESCovarianceMatr
}
updateCovariance(step);
}
/**
* @param step
*
@ -94,7 +104,9 @@ public class MutateESCovarianceMatrixAdaptionPlus extends MutateESCovarianceMatr
public void updateCovariance(double[] step) {
for (int i = 0; i < m_D; i++) {
if (m_psuccess < m_pthresh) {
m_PathS[i]=(1.0-m_c)*m_PathS[i]+Math.sqrt(m_c*(2.0-m_c))*(step[i])/m_SigmaGlobal;
m_PathS[i] = (1.0 - m_c) * m_PathS[i]
+ Math.sqrt(m_c * (2.0 - m_c)) * (step[i])
/ m_SigmaGlobal;
} else {
m_PathS[i] = (1.0 - m_c) * m_PathS[i];
}
@ -106,9 +118,7 @@ public class MutateESCovarianceMatrixAdaptionPlus extends MutateESCovarianceMatr
} else {
m_C = m_C.multi((1.0 - cov)).plus(
(Matrix.outer(m_PathS, m_PathS).plus(
m_C.multi(m_c*(2.0-m_c))
).multi(cov))
);
m_C.multi(m_c * (2.0 - m_c))).multi(cov)));
}
}
@ -123,7 +133,6 @@ public class MutateESCovarianceMatrixAdaptionPlus extends MutateESCovarianceMatr
m_lambda = mLambda;
}
@Override
public void crossoverOnStrategyParameters(AbstractEAIndividual indy1,
Population partners) {
@ -131,55 +140,81 @@ public class MutateESCovarianceMatrixAdaptionPlus extends MutateESCovarianceMatr
}
@Override
// @Override
public void adaptAfterSelection(Population oldPop, Population selectedPop) {
// TODO Auto-generated method stub
}
@Override
public void adaptGenerational(Population selectedPop, Population parentPop, Population newPop, boolean updateSelected) {
// @Override
public void adaptGenerational(Population selectedPop, Population parentPop,
Population newPop, boolean updateSelected) {
double rate = 0.;
for (int i = 0; i < parentPop.size(); i++) {
// calculate success rate
// System.out.println("new fit / old fit: " + BeanInspector.toString(newPop.getEAIndividual(i).getFitness()) + " , " + BeanInspector.toString(parentPop.getEAIndividual(i).getFitness()));
if (newPop.getEAIndividual(i).getFitness(0) < parentPop.getEAIndividual(i).getFitness(0)) rate++;
// System.out.println("new fit / old fit: " +
// BeanInspector.toString(newPop.getEAIndividual(i).getFitness()) +
// " , " +
// BeanInspector.toString(parentPop.getEAIndividual(i).getFitness()));
if (newPop.getEAIndividual(i).getFitness(0) < parentPop
.getEAIndividual(i).getFitness(0))
rate++;
}
rate = rate / parentPop.size();
if (updateSelected) for (int i = 0; i < selectedPop.size(); i++) { // applied to the old population as well in case of plus strategy
MutateESCovarianceMatrixAdaptionPlus mutator = (MutateESCovarianceMatrixAdaptionPlus)((AbstractEAIndividual)selectedPop.get(i)).getMutationOperator();
if (updateSelected)
for (int i = 0; i < selectedPop.size(); i++) { // applied to the old
// population as
// well in case of
// plus strategy
MutateESCovarianceMatrixAdaptionPlus mutator = (MutateESCovarianceMatrixAdaptionPlus) ((AbstractEAIndividual) selectedPop
.get(i)).getMutationOperator();
updateMutator(rate, mutator);
if(selectedPop.getEAIndividual(i).getFitness(0)<=parentPop.getEAIndividual(0).getFitness(0)){
mutator.adaptStrategyGen(selectedPop.getEAIndividual(i),parentPop.getEAIndividual(0));
if (selectedPop.getEAIndividual(i).getFitness(0) <= parentPop
.getEAIndividual(0).getFitness(0)) {
mutator.adaptStrategyGen(selectedPop.getEAIndividual(i),
parentPop.getEAIndividual(0));
}
// System.out.println("old pop step size " + mutator.getSigma()+ " (" + mutator+ ")");
// System.out.println("old pop step size " + mutator.getSigma()+
// " (" + mutator+ ")");
}
for (int i = 0; i < newPop.size(); i++) {
MutateESCovarianceMatrixAdaptionPlus mutator = (MutateESCovarianceMatrixAdaptionPlus)((AbstractEAIndividual)newPop.get(i)).getMutationOperator();
MutateESCovarianceMatrixAdaptionPlus mutator = (MutateESCovarianceMatrixAdaptionPlus) ((AbstractEAIndividual) newPop
.get(i)).getMutationOperator();
updateMutator(rate, mutator);
if(newPop.getEAIndividual(i).getFitness(0)<=parentPop.getEAIndividual(0).getFitness(0)){
mutator.adaptStrategyGen(newPop.getEAIndividual(i),parentPop.getEAIndividual(0));
if (newPop.getEAIndividual(i).getFitness(0) <= parentPop
.getEAIndividual(0).getFitness(0)) {
mutator.adaptStrategyGen(newPop.getEAIndividual(i), parentPop
.getEAIndividual(0));
}
// System.out.println("new pop step size " + mutator.getSigma()+ " (" + mutator+ ")");
// System.out.println("new pop step size " + mutator.getSigma()+
// " (" + mutator+ ")");
}
}
private void updateMutator(double rate, MutateESCovarianceMatrixAdaptionPlus mutator) {
private void updateMutator(double rate,
MutateESCovarianceMatrixAdaptionPlus mutator) {
mutator.updateStepSize(rate);
}
public double getM_psuccess() {
return m_psuccess;
}
public void updateStepSize(double psuccess) {
this.m_psuccess = (1 - m_cp) * m_psuccess + m_cp * psuccess;
m_SigmaGlobal=m_SigmaGlobal*Math.exp(1/m_stepd*(m_psuccess-m_psuccesstarget)/(1-m_psuccesstarget));
m_SigmaGlobal = m_SigmaGlobal
* Math.exp(1 / m_stepd * (m_psuccess - m_psuccesstarget)
/ (1 - m_psuccesstarget));
}
public String getName() {
return "CMA mutation for plus Strategies";
}
/** This method returns a global info string
/**
* This method returns a global info string
*
* @return description
*/
public static String globalInfo() {

View File

@ -26,27 +26,24 @@ import eva2.server.go.populations.SolutionSet;
import eva2.server.go.problems.AbstractOptimizationProblem;
import eva2.server.go.problems.InterfaceOptimizationProblem;
import eva2.tools.math.Mathematics;
import eva2.tools.math.RNG;
import eva2.tools.math.Jama.EigenvalueDecomposition;
import eva2.tools.math.Jama.Matrix;
public class MultiObjectiveCMAES implements InterfaceOptimizer, Serializable
{
public class MultiObjectiveCMAES implements InterfaceOptimizer, Serializable {
class CounterClass {
public CounterClass(int i) {
value = i;
}
public int value;
public boolean seen = false;
}
private String m_Identifier = "NelderMeadSimplex";
private Population m_Population;
private AbstractOptimizationProblem m_Problem;
@ -55,8 +52,6 @@ public class MultiObjectiveCMAES implements InterfaceOptimizer, Serializable
private int m_lambda = 1;
private int m_lambdamo = 1;
public MultiObjectiveCMAES() {
m_Population = new Population(m_lambdamo);
}
@ -72,57 +67,58 @@ public class MultiObjectiveCMAES implements InterfaceOptimizer, Serializable
return new MultiObjectiveCMAES(this);
}
@Override
// @Override
public void SetIdentifier(String name) {
m_Identifier = name;
}
@Override
// @Override
public void SetProblem(InterfaceOptimizationProblem problem) {
m_Problem = (AbstractOptimizationProblem) problem;
}
/** This method allows you to add the LectureGUI as listener to the Optimizer
/**
* This method allows you to add the LectureGUI as listener to the Optimizer
*
* @param ea
*/
public void addPopulationChangedEventListener(InterfacePopulationChangedEventListener ea) {
public void addPopulationChangedEventListener(
InterfacePopulationChangedEventListener ea) {
this.m_Listener = ea;
}
@Override
// @Override
public void freeWilly() {
}
@Override
// @Override
public InterfaceSolutionSet getAllSolutions() {
Population pop = getPopulation();
return new SolutionSet(pop, pop);
}
@Override
// @Override
public String getIdentifier() {
return m_Identifier;
}
@Override
// @Override
public String getName() {
return "(1+" + m_lambda + ") MO-CMA-ES";
}
@Override
// @Override
public Population getPopulation() {
return m_Population;
}
@Override
// @Override
public InterfaceOptimizationProblem getProblem() {
return m_Problem;
}
@Override
// @Override
public String getStringRepresentation() {
StringBuilder strB = new StringBuilder(200);
strB.append("(1+" + m_lambda + ") MO-CMA-ES:\nOptimization Problem: ");
@ -132,7 +128,7 @@ public class MultiObjectiveCMAES implements InterfaceOptimizer, Serializable
return strB.toString();
}
@Override
// @Override
public void init() {
// initByPopulation(m_Population, true);
this.m_Population.setTargetSize(m_lambdamo);
@ -143,9 +139,7 @@ public class MultiObjectiveCMAES implements InterfaceOptimizer, Serializable
}
@Override
// @Override
public void initByPopulation(Population pop, boolean reset) {
setPopulation(pop);
if (reset) {
@ -155,23 +149,27 @@ public class MultiObjectiveCMAES implements InterfaceOptimizer, Serializable
}
}
/** This method will evaluate the current population using the
* given problem.
* @param population The population that is to be evaluated
/**
* This method will evaluate the current population using the given problem.
*
* @param population
* The population that is to be evaluated
*/
private void evaluatePopulation(Population population) {
this.m_Problem.evaluate(population);
}
@Override
// @Override
public void optimize() {
HashMap<Long, CounterClass> SuccessCounterMap = new HashMap<Long, CounterClass>();
//Eltern markieren und für die hlung vorbereiten
// Eltern markieren und f<EFBFBD>r die Z<EFBFBD>hlung vorbereiten
for (int j = 0; j < m_lambdamo && j < m_Population.size(); j++) {
m_Population.getEAIndividual(j).putData("Parent",m_Population.getEAIndividual(j) );
SuccessCounterMap.put(m_Population.getEAIndividual(j).getIndyID(),new CounterClass(0));
m_Population.getEAIndividual(j).putData("Parent",
m_Population.getEAIndividual(j));
SuccessCounterMap.put(m_Population.getEAIndividual(j).getIndyID(),
new CounterClass(0));
}
// Kinder erzeugen
@ -179,7 +177,8 @@ public class MultiObjectiveCMAES implements InterfaceOptimizer, Serializable
children.setGenerationTo(m_Population.getGeneration());
for (int j = 0; j < children.getTargetSize(); j++) {
AbstractEAIndividual parent=m_Population.getEAIndividual(j%m_lambdamo);
AbstractEAIndividual parent = m_Population.getEAIndividual(j
% m_lambdamo);
AbstractEAIndividual indy = (AbstractEAIndividual) parent.clone();
indy.mutate();
indy.putData("Parent", parent);
@ -187,23 +186,32 @@ public class MultiObjectiveCMAES implements InterfaceOptimizer, Serializable
}
evaluatePopulation(children);
m_Population.addPopulation(children);
// Ranking
ArchivingNSGAII dummyArchive = new ArchivingNSGAIISMeasure();
Population []store=dummyArchive.getNonDomiatedSortedFronts(m_Population);
Population[] store = dummyArchive
.getNonDomiatedSortedFronts(m_Population);
store = dummyArchive.getNonDomiatedSortedFronts(m_Population);
dummyArchive.calculateCrowdingDistance(store);
//Vergleichen und den Successcounter hochzählen wenn wir besser als unser Elter sind
// Vergleichen und den Successcounter hochz<EFBFBD>hlen wenn wir besser als
// unser Elter sind
for (int j = 0; j < m_Population.size(); j++) {
AbstractEAIndividual parent= (AbstractEAIndividual) m_Population.getEAIndividual(j).getData("Parent");
if(m_Population.getEAIndividual(j)!=parent){ //Eltern nicht mit sich selber vergleichen
int parentParetoLevel=((Integer) parent.getData("ParetoLevel")).intValue();
double parentSMeasure=((Double) parent.getData("HyperCube")).doubleValue();
int childParetoLevel=((Integer) m_Population.getEAIndividual(j).getData("ParetoLevel")).intValue();
double childSMeasure=((Double) m_Population.getEAIndividual(j).getData("HyperCube")).doubleValue();
if( childParetoLevel<parentParetoLevel||((childParetoLevel==parentParetoLevel)&&childSMeasure>parentSMeasure ) ){
AbstractEAIndividual parent = (AbstractEAIndividual) m_Population
.getEAIndividual(j).getData("Parent");
if (m_Population.getEAIndividual(j) != parent) { // Eltern nicht mit
// sich selber
// vergleichen
int parentParetoLevel = ((Integer) parent
.getData("ParetoLevel")).intValue();
double parentSMeasure = ((Double) parent.getData("HyperCube"))
.doubleValue();
int childParetoLevel = ((Integer) m_Population.getEAIndividual(
j).getData("ParetoLevel")).intValue();
double childSMeasure = ((Double) m_Population
.getEAIndividual(j).getData("HyperCube")).doubleValue();
if (childParetoLevel < parentParetoLevel
|| ((childParetoLevel == parentParetoLevel) && childSMeasure > parentSMeasure)) {
SuccessCounterMap.get(parent.getIndyID()).value++;
}
} else { // Debug
@ -212,20 +220,28 @@ public class MultiObjectiveCMAES implements InterfaceOptimizer, Serializable
}
}
// Selection
m_Population.clear();
for (int i = 0; i < store.length; i++) {
if(m_Population.size()+store[i].size()<=m_lambdamo){ //Die Front passt noch komplett
if (m_Population.size() + store[i].size() <= m_lambdamo) { // Die
// Front
// passt
// noch
// komplett
m_Population.addPopulation(store[i]);
} else { // die besten aus der aktuellen Front heraussuchen bis voll
while (store[i].size() > 0 && m_Population.size() < m_lambdamo) {
AbstractEAIndividual indy = store[i].getEAIndividual(0);
double bestMeasure=((Double) indy.getData("HyperCube")).doubleValue(); //TODO mal noch effizient machen (sortieren und die besten n herausholen)
double bestMeasure = ((Double) indy.getData("HyperCube"))
.doubleValue(); // TODO mal noch effizient machen
// (sortieren und die besten n
// herausholen)
for (int j = 1; j < store[i].size(); j++) {
if(bestMeasure<((Double) store[i].getEAIndividual(j).getData("HyperCube")).doubleValue()){
bestMeasure=((Double) store[i].getEAIndividual(j).getData("HyperCube")).doubleValue();
if (bestMeasure < ((Double) store[i].getEAIndividual(j)
.getData("HyperCube")).doubleValue()) {
bestMeasure = ((Double) store[i].getEAIndividual(j)
.getData("HyperCube")).doubleValue();
indy = store[i].getEAIndividual(j);
}
}
@ -240,10 +256,23 @@ public class MultiObjectiveCMAES implements InterfaceOptimizer, Serializable
for (int j = 0; j < m_Population.size(); j++) {
AbstractEAIndividual indy = m_Population.getEAIndividual(j);
if(indy.getMutationOperator() instanceof MutateESCovarianceMatrixAdaptionPlus ){ //Das geht nur wenn wir auch die richtige Mutation haben
AbstractEAIndividual parent=(AbstractEAIndividual)indy.getData("Parent");
MutateESCovarianceMatrixAdaptionPlus muta=(MutateESCovarianceMatrixAdaptionPlus) indy.getMutationOperator();
double rate=((double) SuccessCounterMap.get(parent.getIndyID()).value)/((double) m_lambda);
if (indy.getMutationOperator() instanceof MutateESCovarianceMatrixAdaptionPlus) { // Das
// geht
// nur
// wenn
// wir
// auch
// die
// richtige
// Mutation
// haben
AbstractEAIndividual parent = (AbstractEAIndividual) indy
.getData("Parent");
MutateESCovarianceMatrixAdaptionPlus muta = (MutateESCovarianceMatrixAdaptionPlus) indy
.getMutationOperator();
double rate = ((double) SuccessCounterMap.get(parent
.getIndyID()).value)
/ ((double) m_lambda);
if (indy != parent) {
muta.updateCovariance();
@ -262,13 +291,13 @@ public class MultiObjectiveCMAES implements InterfaceOptimizer, Serializable
}
@Override
// @Override
public boolean removePopulationChangedEventListener(
InterfacePopulationChangedEventListener ea) {
return false;
}
@Override
// @Override
public void setPopulation(Population pop) {
m_Population = pop;
m_Population.setNotifyEvalInterval(1);
@ -276,11 +305,14 @@ public class MultiObjectiveCMAES implements InterfaceOptimizer, Serializable
}
/** Something has changed
/**
* Something has changed
*
* @param name
*/
protected void firePropertyChangedEvent(String name) {
if (this.m_Listener != null) this.m_Listener.registerPopulationStateChanged(this, name);
if (this.m_Listener != null)
this.m_Listener.registerPopulationStateChanged(this, name);
}
public int getLambda() {
@ -291,13 +323,10 @@ public class MultiObjectiveCMAES implements InterfaceOptimizer, Serializable
m_lambda = mLambda;
}
/*public int getLambdaMo() {
return m_lambdamo;
}
public void setLambdaMo(int mLambda) {
m_lambdamo = mLambda;
}*/
/*
* public int getLambdaMo() { return m_lambdamo; }
*
* public void setLambdaMo(int mLambda) { m_lambdamo = mLambda; }
*/
}