The CrossoverEAMixer now annotates indies: which operator mutated which individual.
This commit is contained in:
@@ -16,39 +16,53 @@ import eva2.tools.math.RNG;
|
|||||||
* Time: 11:36:38
|
* Time: 11:36:38
|
||||||
* To change this template use File | Settings | File Templates.
|
* To change this template use File | Settings | File Templates.
|
||||||
*/
|
*/
|
||||||
public class CrossoverEAMixer implements InterfaceCrossover, java.io.Serializable {
|
public class CrossoverEAMixer implements InterfaceCrossover, InterfaceEvaluatingCrossoverOperator, java.io.Serializable {
|
||||||
|
public static final String CROSSOVER_EA_MIXER_OPERATOR_KEY = "CrossoverEAMixerOperatorKey";
|
||||||
|
|
||||||
private PropertyCrossoverMixer m_Crossers;
|
protected PropertyCrossoverMixer m_Crossers;
|
||||||
private boolean m_UseSelfAdaption = false;
|
protected boolean m_UseSelfAdaption = false;
|
||||||
protected double m_Tau1 = 0.15;
|
protected double m_Tau1 = 0.15;
|
||||||
protected double m_LowerLimitChance = 0.05;
|
protected double m_LowerLimitChance = 0.05;
|
||||||
|
protected int lastOperatorIndex = -1;
|
||||||
|
|
||||||
public CrossoverEAMixer() {
|
public CrossoverEAMixer() {
|
||||||
InterfaceCrossover[] tmpList;
|
InterfaceCrossover[] tmpList;
|
||||||
ArrayList<String> crossers = GenericObjectEditor.getClassesFromProperties(InterfaceCrossover.class.getCanonicalName(), null);
|
ArrayList<String> crossers = GenericObjectEditor.getClassesFromProperties(InterfaceCrossover.class.getCanonicalName(), null);
|
||||||
tmpList = new InterfaceCrossover[crossers.size()];
|
tmpList = new InterfaceCrossover[crossers.size()];
|
||||||
for (int i = 0; i < crossers.size(); i++) {
|
for (int i = 0; i < crossers.size(); i++) {
|
||||||
if (((String)crossers.get(i)).equals(this.getClass().getName())) continue;
|
Class clz=null;
|
||||||
try {
|
try {
|
||||||
tmpList[i] = (InterfaceCrossover)Class.forName((String)crossers.get(i)).newInstance();
|
clz = (Class)Class.forName((String)crossers.get(i));
|
||||||
} catch (java.lang.ClassNotFoundException e) {
|
} catch (ClassNotFoundException e1) {
|
||||||
System.out.println("Could not find class for " +(String)crossers.get(i) );
|
continue;
|
||||||
} catch (java.lang.InstantiationException k) {
|
}
|
||||||
System.out.println("Instantiation exception for " +(String)crossers.get(i) );
|
if (clz.isAssignableFrom(this.getClass())) {
|
||||||
} catch (java.lang.IllegalAccessException a) {
|
// Do not instanciate this class or its subclasses or die of an infinite loop
|
||||||
System.out.println("Illegal access exception for " +(String)crossers.get(i) );
|
// System.out.println("Skipping " + clz.getClass().getName());
|
||||||
}
|
continue;
|
||||||
}
|
} else {
|
||||||
this.m_Crossers = new PropertyCrossoverMixer(tmpList);
|
// System.out.println("Taking " + clz.getClass().getName());
|
||||||
tmpList = new InterfaceCrossover[2];
|
}
|
||||||
tmpList[0] = new CrossoverESArithmetical();
|
try {
|
||||||
tmpList[1] = new CrossoverESSBX();
|
tmpList[i] = (InterfaceCrossover)Class.forName((String)crossers.get(i)).newInstance();
|
||||||
this.m_Crossers.setSelectedCrossers(tmpList);
|
} catch (java.lang.ClassNotFoundException e) {
|
||||||
this.m_Crossers.normalizeWeights();
|
System.out.println("Could not find class for " +(String)crossers.get(i) );
|
||||||
this.m_Crossers.setDescriptiveString("Combining alternative mutation operators, please norm the weights!");
|
} catch (java.lang.InstantiationException k) {
|
||||||
this.m_Crossers.setWeightsLabel("Weigths");
|
System.out.println("Instantiation exception for " +(String)crossers.get(i) );
|
||||||
|
} catch (java.lang.IllegalAccessException a) {
|
||||||
|
System.out.println("Illegal access exception for " +(String)crossers.get(i) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.m_Crossers = new PropertyCrossoverMixer(tmpList);
|
||||||
|
tmpList = new InterfaceCrossover[2];
|
||||||
|
tmpList[0] = new CrossoverESArithmetical();
|
||||||
|
tmpList[1] = new CrossoverESSBX();
|
||||||
|
this.m_Crossers.setSelectedCrossers(tmpList);
|
||||||
|
this.m_Crossers.normalizeWeights();
|
||||||
|
this.m_Crossers.setDescriptiveString("Combining alternative mutation operators, please norm the weights!");
|
||||||
|
this.m_Crossers.setWeightsLabel("Weigths");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
|
||||||
public CrossoverEAMixer(CrossoverEAMixer mutator) {
|
public CrossoverEAMixer(CrossoverEAMixer mutator) {
|
||||||
this.m_Crossers = (PropertyCrossoverMixer)mutator.m_Crossers.clone();
|
this.m_Crossers = (PropertyCrossoverMixer)mutator.m_Crossers.clone();
|
||||||
this.m_UseSelfAdaption = mutator.m_UseSelfAdaption;
|
this.m_UseSelfAdaption = mutator.m_UseSelfAdaption;
|
||||||
@@ -63,7 +77,7 @@ public class CrossoverEAMixer implements InterfaceCrossover, java.io.Serializabl
|
|||||||
return new CrossoverEAMixer(this);
|
return new CrossoverEAMixer(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** This method allows you to evaluate wether two mutation operators
|
/** This method allows you to evaluate whether two mutation operators
|
||||||
* are actually the same.
|
* are actually the same.
|
||||||
* @param mutator The other mutation operator
|
* @param mutator The other mutation operator
|
||||||
*/
|
*/
|
||||||
@@ -75,13 +89,13 @@ public class CrossoverEAMixer implements InterfaceCrossover, java.io.Serializabl
|
|||||||
} else return false;
|
} else return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** This method allows you to init the mutation operator
|
/** This method allows you to init the crossover operator
|
||||||
* @param individual The individual that will be mutated.
|
* @param individual The individual that will be mutated.
|
||||||
* @param opt The optimization problem.
|
* @param opt The optimization problem.
|
||||||
*/
|
*/
|
||||||
public void init(AbstractEAIndividual individual, InterfaceOptimizationProblem opt){
|
public void init(AbstractEAIndividual individual, InterfaceOptimizationProblem opt){
|
||||||
InterfaceCrossover[] mutators = this.m_Crossers.getSelectedCrossers();
|
InterfaceCrossover[] crossers = this.m_Crossers.getSelectedCrossers();
|
||||||
for (int i = 0; i < mutators.length; i++) mutators[i].init(individual, opt);
|
for (int i = 0; i < crossers.length; i++) crossers[i].init(individual, opt);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** This method performs crossover on two individuals. If the individuals do
|
/** This method performs crossover on two individuals. If the individuals do
|
||||||
@@ -104,20 +118,34 @@ public class CrossoverEAMixer implements InterfaceCrossover, java.io.Serializabl
|
|||||||
InterfaceCrossover[] crossover = this.m_Crossers.getSelectedCrossers();
|
InterfaceCrossover[] crossover = this.m_Crossers.getSelectedCrossers();
|
||||||
double pointer = RNG.randomFloat(0, 1);
|
double pointer = RNG.randomFloat(0, 1);
|
||||||
double dum = probs[0];
|
double dum = probs[0];
|
||||||
int index = 0;
|
lastOperatorIndex = 0;
|
||||||
while ((pointer > dum) && (index < probs.length-1)) {
|
while ((pointer > dum) && (lastOperatorIndex < probs.length-1)) {
|
||||||
index++;
|
lastOperatorIndex++;
|
||||||
dum += probs[index];
|
dum += probs[lastOperatorIndex];
|
||||||
}
|
}
|
||||||
if (index == probs.length) index = RNG.randomInt(0, probs.length-1);
|
if (lastOperatorIndex == probs.length) lastOperatorIndex = RNG.randomInt(0, probs.length-1);
|
||||||
// System.out.println("Using : " + mutators[index].getStringRepresentation());
|
// System.out.println("Using : " + mutators[index].getStringRepresentation());
|
||||||
// for (int i = 0; i < probs.length; i++) {
|
// for (int i = 0; i < probs.length; i++) {
|
||||||
// System.out.println(""+mutators[i].getStringRepresentation()+" : "+ probs[i]);
|
// System.out.println(""+mutators[i].getStringRepresentation()+" : "+ probs[i]);
|
||||||
// }
|
// }
|
||||||
// System.out.println("");
|
// System.out.println("");
|
||||||
return crossover[index].mate(indy1, partners);
|
|
||||||
|
indy1.putData(CROSSOVER_EA_MIXER_OPERATOR_KEY, lastOperatorIndex);
|
||||||
|
for (int i=0; i<partners.size(); i++) {
|
||||||
|
partners.getEAIndividual(i).putData(CROSSOVER_EA_MIXER_OPERATOR_KEY, lastOperatorIndex);
|
||||||
|
}
|
||||||
|
AbstractEAIndividual[] indies = crossover[lastOperatorIndex].mate(indy1, partners);
|
||||||
|
|
||||||
|
maybeAdaptWeights(indies);
|
||||||
|
return indies;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void maybeAdaptWeights(AbstractEAIndividual[] indies) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getLastOperatorIndex() {
|
||||||
|
return lastOperatorIndex;
|
||||||
|
}
|
||||||
|
|
||||||
/** This method allows you to get a string representation of the mutation
|
/** This method allows you to get a string representation of the mutation
|
||||||
* operator
|
* operator
|
||||||
@@ -197,4 +225,24 @@ public class CrossoverEAMixer implements InterfaceCrossover, java.io.Serializabl
|
|||||||
public String tau1TipText() {
|
public String tau1TipText() {
|
||||||
return "Set the value for tau1.";
|
return "Set the value for tau1.";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getEvaluations() {
|
||||||
|
int numEvals=0;
|
||||||
|
InterfaceCrossover[] crossers = this.m_Crossers.getSelectedCrossers();
|
||||||
|
for (int i = 0; i < crossers.length; i++) {
|
||||||
|
if (crossers[i] instanceof InterfaceEvaluatingCrossoverOperator) {
|
||||||
|
numEvals += ((InterfaceEvaluatingCrossoverOperator)crossers[i]).getEvaluations();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return numEvals;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void resetEvaluations() {
|
||||||
|
InterfaceCrossover[] crossers = this.m_Crossers.getSelectedCrossers();
|
||||||
|
for (int i = 0; i < crossers.length; i++) {
|
||||||
|
if (crossers[i] instanceof InterfaceEvaluatingCrossoverOperator) {
|
||||||
|
((InterfaceEvaluatingCrossoverOperator)crossers[i]).resetEvaluations();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user