Minor fixes, part 2 - some GP adaptions.

This commit is contained in:
Marcel Kronfeld 2010-06-01 16:24:52 +00:00
parent d9f059fe6c
commit 513bf3df7d
7 changed files with 201 additions and 61 deletions

View File

@ -10,7 +10,7 @@ import eva2.tools.BasicResourceLoader;
* *
* --- Changelog * --- Changelog
* 2.043: Added proper Population equality predicate. * 2.043: Added proper Population equality predicate.
* 2.042: Some bugfixes. Removing dependecy on sun.beans.editors, replaced non-free jpeg-codec. There should * 2.042: Some bugfixes. Removing dependency on sun.beans.editors, replaced non-free jpeg-codec. There should
* be no more problems on OpenJDK. Added a simple initialization range, especially for use from Matlab. * be no more problems on OpenJDK. Added a simple initialization range, especially for use from Matlab.
* Some restructurings (RNG and Mathematics is now in eva2.tools.math). Some cleanup. * Some restructurings (RNG and Mathematics is now in eva2.tools.math). Some cleanup.
* 2.040: Several updates: The clustering interface has been changed for easier implementation of adaptive clustering * 2.040: Several updates: The clustering interface has been changed for easier implementation of adaptive clustering

View File

@ -367,18 +367,19 @@ public class PropertySheetPanel extends JPanel implements PropertyChangeListener
* @return * @return
*/ */
private PropertyDescriptor[] reorderProperties(Method meth) { private PropertyDescriptor[] reorderProperties(Method meth) {
PropertyDescriptor[] oldProps = m_Properties.clone();
PropertyDescriptor[] newProps = new PropertyDescriptor[oldProps.length];
// Mathematics.revertArray(oldProps, newProps); // Mathematics.revertArray(oldProps, newProps);
Object[] args = { }; Object[] args = { };
Object retV=null; Object retV=null;
PropertyDescriptor[] newProps = null;
try { try {
retV = meth.invoke(m_Target, args); retV = meth.invoke(m_Target, args); // should return String[] to be interpreted as a list of ordered properties
} catch(Exception ex) {} } catch(Exception ex) {}
if (retV!=null) { if (retV!=null) {
try { try {
if (retV.getClass().isArray()) { if (retV.getClass().isArray()) { // reorder the properties
String[] swProps=(String[])retV; String[] swProps=(String[])retV;
PropertyDescriptor[] oldProps = m_Properties.clone();
newProps = new PropertyDescriptor[oldProps.length];
//int findFirst=findFirstProp(props[0], oldProps); //int findFirst=findFirstProp(props[0], oldProps);
int firstNonNull=0; int firstNonNull=0;
for (int i=0; i<oldProps.length; i++) { for (int i=0; i<oldProps.length; i++) {

View File

@ -27,8 +27,8 @@ public class GPIndividualProgramData extends AbstractEAIndividual implements Int
protected GPArea[] m_Area; protected GPArea[] m_Area;
protected double m_InitFullGrowRatio = 0.5; protected double m_InitFullGrowRatio = 0.5;
protected int m_InitDepth = 4; protected int m_InitDepth = 4;
protected int m_TargetDepth = 8; protected int m_maxDepth = 8;
protected boolean m_CheckTargetDepth = true; protected boolean m_CheckMaxDepth = true;
public GPIndividualProgramData() { public GPIndividualProgramData() {
this.m_Area = new GPArea[1]; this.m_Area = new GPArea[1];
@ -58,8 +58,8 @@ public class GPIndividualProgramData extends AbstractEAIndividual implements Int
} }
this.m_InitFullGrowRatio = individual.m_InitFullGrowRatio; this.m_InitFullGrowRatio = individual.m_InitFullGrowRatio;
this.m_InitDepth = individual.m_InitDepth; this.m_InitDepth = individual.m_InitDepth;
this.m_TargetDepth = individual.m_TargetDepth; this.m_maxDepth = individual.m_maxDepth;
this.m_CheckTargetDepth = individual.m_CheckTargetDepth; this.m_CheckMaxDepth = individual.m_CheckMaxDepth;
// cloning the members of AbstractEAIndividual // cloning the members of AbstractEAIndividual
this.m_Age = individual.m_Age; this.m_Age = individual.m_Age;
@ -90,7 +90,7 @@ public class GPIndividualProgramData extends AbstractEAIndividual implements Int
if (individual instanceof GPIndividualProgramData) { if (individual instanceof GPIndividualProgramData) {
GPIndividualProgramData indy = (GPIndividualProgramData) individual; GPIndividualProgramData indy = (GPIndividualProgramData) individual;
//@todo Eigendlich k<EFBFBD>nnte ich noch die Areas vergleichen //@todo Eigendlich k<EFBFBD>nnte ich noch die Areas vergleichen
if (this.m_TargetDepth != indy.m_TargetDepth) if (this.m_maxDepth != indy.m_maxDepth)
return false; return false;
if ((this.m_Genotype == null) || (indy.m_Genotype == null)) if ((this.m_Genotype == null) || (indy.m_Genotype == null))
return false; return false;
@ -134,9 +134,9 @@ public class GPIndividualProgramData extends AbstractEAIndividual implements Int
this.m_Phenotype = new AbstractGPNode[this.m_Genotype.length]; this.m_Phenotype = new AbstractGPNode[this.m_Genotype.length];
for (int i = 0; i < this.m_Genotype.length; i++) { for (int i = 0; i < this.m_Genotype.length; i++) {
this.m_Phenotype[i] = (AbstractGPNode)this.m_Genotype[i].clone(); this.m_Phenotype[i] = (AbstractGPNode)this.m_Genotype[i].clone();
if ((this.m_CheckTargetDepth) && (this.m_Phenotype[i].isMaxDepthViolated(this.m_TargetDepth))) { if ((this.m_CheckMaxDepth) && (this.m_Phenotype[i].isMaxDepthViolated(this.m_maxDepth))) {
//System.out.println("Trying to meet the Target Depth!"); //System.out.println("Trying to meet the Target Depth!");
this.m_Phenotype[i].repairMaxDepth(this.m_Area[i], this.m_TargetDepth); this.m_Phenotype[i].repairMaxDepth(this.m_Area[i], this.m_maxDepth);
//System.out.println("TragetDepth: " + this.m_TargetDepth + " : " + this.m_Program.getMaxDepth()); //System.out.println("TragetDepth: " + this.m_TargetDepth + " : " + this.m_Program.getMaxDepth());
} }
} }
@ -267,7 +267,7 @@ public class GPIndividualProgramData extends AbstractEAIndividual implements Int
AbstractGPNode parent = nodeToMutate.getParent(); AbstractGPNode parent = nodeToMutate.getParent();
AbstractGPNode newNode = (AbstractGPNode)(((AbstractGPNode)this.m_Area[i].getRandomNode().clone())); AbstractGPNode newNode = (AbstractGPNode)(((AbstractGPNode)this.m_Area[i].getRandomNode().clone()));
newNode.setDepth(nodeToMutate.getDepth()); newNode.setDepth(nodeToMutate.getDepth());
newNode.initGrow(this.m_Area[i], this.m_TargetDepth); newNode.initGrow(this.m_Area[i], this.m_maxDepth);
parent.setNode(newNode, nodeToMutate); parent.setNode(newNode, nodeToMutate);
} }
} }
@ -310,14 +310,14 @@ public class GPIndividualProgramData extends AbstractEAIndividual implements Int
/** This method will toggle between checking for max depth or not. /** This method will toggle between checking for max depth or not.
* @param b the Switch. * @param b the Switch.
*/ */
public void setCheckTargetDepth(boolean b) { public void setCheckMaxDepth(boolean b) {
this.m_CheckTargetDepth = b; this.m_CheckMaxDepth = b;
} }
public boolean getCheckTargetDepth() { public boolean getCheckMaxDepth() {
return this.m_CheckTargetDepth; return this.m_CheckMaxDepth;
} }
public String checkTargetDepthTipText() { public String checkMaxDepthTipText() {
return "If activated the program will be checked for MaxDepth."; return "If activated the maximum depth of the program tree will be enforced.";
} }
/** This method set/get the init Full Grow Ratio. /** This method set/get the init Full Grow Ratio.
@ -339,9 +339,9 @@ public class GPIndividualProgramData extends AbstractEAIndividual implements Int
* @param b The new init Depth of the GP Tree. * @param b The new init Depth of the GP Tree.
*/ */
public void setInitDepth(int b) { public void setInitDepth(int b) {
if (b > this.m_TargetDepth) { if (b > this.m_maxDepth) {
System.out.println("Waring Init Depth will be set to Target Depth!"); System.out.println("Waring Init Depth will be set to Target Depth!");
b = this.m_TargetDepth; b = this.m_maxDepth;
} }
this.m_InitDepth = b; this.m_InitDepth = b;
} }
@ -355,13 +355,17 @@ public class GPIndividualProgramData extends AbstractEAIndividual implements Int
/** This method set/get the target depth. /** This method set/get the target depth.
* @param b The new target Depth of the GP Tree. * @param b The new target Depth of the GP Tree.
*/ */
public void setTargetDepth(int b) { public void setMaxDepth(int b) {
this.m_TargetDepth = b; this.m_maxDepth = b;
} }
public int getTargetDepth() { public int getMaxDepth() {
return this.m_TargetDepth; return this.m_maxDepth;
} }
public String targetDepthTipText() { public String maxDepthTipText() {
return "The target depth of the GP Tree."; return "The maximum depth of the GP tree.";
}
public String[] customPropertyOrder() {
return new String[] {"initDepth", "checkMaxDepth", "maxDepth"};
} }
} }

View File

@ -468,8 +468,8 @@ public abstract class AbstractGPNode implements InterfaceProgram, java.io.Serial
*/ */
public void repairMaxDepth(GPArea area, int depth) { public void repairMaxDepth(GPArea area, int depth) {
if (this.m_Depth == depth-1) { if (this.m_Depth == depth-1) {
// in this case i need to check wether or not my // in this case i need to check whether or not my
// followup nodes are terminals // follow-up nodes are terminals
for (int i = 0; i < this.m_Nodes.length; i++) { for (int i = 0; i < this.m_Nodes.length; i++) {
if (this.m_Nodes[i].getArity() != 0) { if (this.m_Nodes[i].getArity() != 0) {
// replace this node with a new node // replace this node with a new node
@ -490,12 +490,6 @@ public abstract class AbstractGPNode implements InterfaceProgram, java.io.Serial
* @param obj The other subtree. * @param obj The other subtree.
* @return boolean if equal true else false. * @return boolean if equal true else false.
*/ */
// public abstract boolean equals(Object obj);
/** This method allows you to determine wehter or not two subtrees
* are actually the same.
* @param obj The other subtree.
* @return boolean if equal true else false.
*/
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj.getClass().equals(this.getClass())) { if (obj.getClass().equals(this.getClass())) {
AbstractGPNode node = (AbstractGPNode)obj; AbstractGPNode node = (AbstractGPNode)obj;
@ -509,5 +503,4 @@ public abstract class AbstractGPNode implements InterfaceProgram, java.io.Serial
return false; return false;
} }
} }
} }

View File

@ -1,18 +1,25 @@
package eva2.server.go.problems; package eva2.server.go.problems;
import java.io.Serializable;
import eva2.gui.Plot; import eva2.gui.Plot;
import eva2.server.go.PopulationInterface;
import eva2.server.go.individuals.AbstractEAIndividual; import eva2.server.go.individuals.AbstractEAIndividual;
import eva2.server.go.individuals.GAPIndividualProgramData; import eva2.server.go.individuals.GAPIndividualProgramData;
import eva2.server.go.individuals.GPIndividualProgramData; import eva2.server.go.individuals.GPIndividualProgramData;
import eva2.server.go.individuals.InterfaceDataTypeDouble; import eva2.server.go.individuals.InterfaceDataTypeDouble;
import eva2.server.go.individuals.InterfaceDataTypeProgram; import eva2.server.go.individuals.InterfaceDataTypeProgram;
import eva2.server.go.individuals.codings.gp.AbstractGPNode;
import eva2.server.go.individuals.codings.gp.GPArea; import eva2.server.go.individuals.codings.gp.GPArea;
import eva2.server.go.individuals.codings.gp.GPNodeAbs;
import eva2.server.go.individuals.codings.gp.GPNodeAdd; import eva2.server.go.individuals.codings.gp.GPNodeAdd;
import eva2.server.go.individuals.codings.gp.GPNodeCos; import eva2.server.go.individuals.codings.gp.GPNodeCos;
import eva2.server.go.individuals.codings.gp.GPNodeDiv; import eva2.server.go.individuals.codings.gp.GPNodeDiv;
import eva2.server.go.individuals.codings.gp.GPNodeExp; import eva2.server.go.individuals.codings.gp.GPNodeExp;
import eva2.server.go.individuals.codings.gp.GPNodeInput; import eva2.server.go.individuals.codings.gp.GPNodeInput;
import eva2.server.go.individuals.codings.gp.GPNodeMult; import eva2.server.go.individuals.codings.gp.GPNodeMult;
import eva2.server.go.individuals.codings.gp.GPNodeOne;
import eva2.server.go.individuals.codings.gp.GPNodePi;
import eva2.server.go.individuals.codings.gp.GPNodePow2; import eva2.server.go.individuals.codings.gp.GPNodePow2;
import eva2.server.go.individuals.codings.gp.GPNodePow3; import eva2.server.go.individuals.codings.gp.GPNodePow3;
import eva2.server.go.individuals.codings.gp.GPNodeSin; import eva2.server.go.individuals.codings.gp.GPNodeSin;
@ -21,9 +28,10 @@ import eva2.server.go.individuals.codings.gp.GPNodeSub;
import eva2.server.go.individuals.codings.gp.InterfaceProgram; import eva2.server.go.individuals.codings.gp.InterfaceProgram;
import eva2.server.go.populations.Population; import eva2.server.go.populations.Population;
import eva2.server.go.problems.regression.InterfaceRegressionFunction; import eva2.server.go.problems.regression.InterfaceRegressionFunction;
import eva2.server.go.problems.regression.RFKoza_GPI_10_2; import eva2.server.go.problems.regression.RFKoza_GPI_7_3;
import eva2.server.go.strategies.InterfaceOptimizer; import eva2.server.go.strategies.InterfaceOptimizer;
import eva2.tools.EVAERROR; import eva2.tools.EVAERROR;
import eva2.tools.ToolBox;
import eva2.tools.math.RNG; import eva2.tools.math.RNG;
/** /**
@ -33,14 +41,14 @@ import eva2.tools.math.RNG;
* Time: 16:44:25 * Time: 16:44:25
* To change this template use Options | File Templates. * To change this template use Options | File Templates.
*/ */
public class PSymbolicRegression extends AbstractOptimizationProblem implements InterfaceProgramProblem, java.io.Serializable { public class PSymbolicRegression extends AbstractOptimizationProblem implements InterfaceProgramProblem, InterfaceAdditionalPopulationInformer, Serializable {
private double[] m_X = new double[1]; private double[] m_X = new double[1];
private int m_NumberOfConstants = 10; private int m_NumberOfConstants = 3;
private double m_LowerBound = 0; private double m_LowerBound = -1;
private double m_UpperBound = 10; private double m_UpperBound = 1;
private int m_NumberOfCheckPoints = 10; private int m_NumberOfCheckPoints = 20;
transient private InterfaceRegressionFunction m_TargetFunction = new RFKoza_GPI_10_2(); transient private InterfaceRegressionFunction m_TargetFunction = new RFKoza_GPI_7_3();
private double[] m_C = new double[m_NumberOfConstants]; private double[] m_C = new double[m_NumberOfConstants];
private boolean m_UseInnerConst = false; private boolean m_UseInnerConst = false;
private boolean m_UseLocalHillClimbing = false; private boolean m_UseLocalHillClimbing = false;
@ -97,7 +105,7 @@ public class PSymbolicRegression extends AbstractOptimizationProblem implements
/** This method inits the Problem to log multiruns /** This method inits the Problem to log multiruns
*/ */
public void initProblem() { public void initProblem() {
if (m_TargetFunction == null) m_TargetFunction = new RFKoza_GPI_10_2(); if (m_TargetFunction == null) m_TargetFunction = new RFKoza_GPI_7_3();
this.m_OverallBest = null; this.m_OverallBest = null;
this.m_C = new double[this.m_NumberOfConstants]; this.m_C = new double[this.m_NumberOfConstants];
for (int i = 0; i < this.m_C.length; i++) this.m_C[i] = RNG.randomDouble(-10, 10); for (int i = 0; i < this.m_C.length; i++) this.m_C[i] = RNG.randomDouble(-10, 10);
@ -113,18 +121,21 @@ public class PSymbolicRegression extends AbstractOptimizationProblem implements
m_GPArea = new GPArea(); m_GPArea = new GPArea();
if (m_GPArea.isEmpty()) { if (m_GPArea.isEmpty()) {
this.m_GPArea.add2CompleteList(new GPNodeOne());
this.m_GPArea.add2CompleteList(new GPNodePi(), false);
this.m_GPArea.add2CompleteList(new GPNodeAdd()); this.m_GPArea.add2CompleteList(new GPNodeAdd());
this.m_GPArea.add2CompleteList(new GPNodeSub()); this.m_GPArea.add2CompleteList(new GPNodeSub());
this.m_GPArea.add2CompleteList(new GPNodeDiv()); this.m_GPArea.add2CompleteList(new GPNodeDiv());
this.m_GPArea.add2CompleteList(new GPNodeMult()); this.m_GPArea.add2CompleteList(new GPNodeMult());
this.m_GPArea.add2CompleteList(new GPNodeAbs(), false);
this.m_GPArea.add2CompleteList(new GPNodeSin(), false); this.m_GPArea.add2CompleteList(new GPNodeSin(), false);
this.m_GPArea.add2CompleteList(new GPNodeCos(), false); this.m_GPArea.add2CompleteList(new GPNodeCos(), false);
this.m_GPArea.add2CompleteList(new GPNodeExp(), false); this.m_GPArea.add2CompleteList(new GPNodeExp(), false);
this.m_GPArea.add2CompleteList(new GPNodeSqrt(), false);
this.m_GPArea.add2CompleteList(new GPNodePow2(), false); this.m_GPArea.add2CompleteList(new GPNodePow2(), false);
this.m_GPArea.add2CompleteList(new GPNodePow3(), false); this.m_GPArea.add2CompleteList(new GPNodePow3(), false);
this.m_GPArea.add2CompleteList(new GPNodeSqrt(), false);
for (int i = 0; i < this.m_X.length; i++) this.m_GPArea.add2CompleteList(new GPNodeInput("X"+i)); for (int i = 0; i < this.m_X.length; i++) this.m_GPArea.add2CompleteList(new GPNodeInput("X"+i));
for (int i = 0; i < this.m_C.length; i++) this.m_GPArea.add2CompleteList(new GPNodeInput("C"+i)); for (int i = 0; i < this.m_C.length; i++) this.m_GPArea.add2CompleteList(new GPNodeInput("C"+i), false);
} }
if ((oldArea!=null) && (oldArea.getBlackList()!=null) && (oldArea.getBlackList().size()==m_GPArea.getBlackList().size())) { if ((oldArea!=null) && (oldArea.getBlackList()!=null) && (oldArea.getBlackList().size()==m_GPArea.getBlackList().size())) {
m_GPArea.SetBlackList(oldArea.getBlackList()); m_GPArea.SetBlackList(oldArea.getBlackList());
@ -200,7 +211,9 @@ public class PSymbolicRegression extends AbstractOptimizationProblem implements
} }
/** /**
* This method evaluates a single individual and sets the fitness values * This method evaluates a single individual and sets the fitness values. In this case,
* the averaged square error between represented function and target function per check point is calculated.
*
* @param individual The individual that is to be evaluated * @param individual The individual that is to be evaluated
*/ */
public void evaluate(AbstractEAIndividual individual) { public void evaluate(AbstractEAIndividual individual) {
@ -214,9 +227,8 @@ public class PSymbolicRegression extends AbstractOptimizationProblem implements
this.m_C = ((GAPIndividualProgramData)tmpIndy).getDoubleData(); this.m_C = ((GAPIndividualProgramData)tmpIndy).getDoubleData();
fitness = 0; fitness = 0;
for (int j = 0; j < this.m_NumberOfCheckPoints; j++) for (int j = 0; j < this.m_NumberOfCheckPoints; j++) {
for (int i = 0; i < this.m_X.length; i++) { setCheckPoint(m_X, j);
this.m_X[i] = this.m_LowerBound +(j*(this.m_UpperBound-this.m_LowerBound)/this.m_NumberOfCheckPoints);
tmpValue = ((Double)program.evaluate(this)).doubleValue(); tmpValue = ((Double)program.evaluate(this)).doubleValue();
fitness += Math.pow((this.m_TargetFunction.evaluateFunction(this.m_X) - ((Double)program.evaluate(this)).doubleValue()), 2); fitness += Math.pow((this.m_TargetFunction.evaluateFunction(this.m_X) - ((Double)program.evaluate(this)).doubleValue()), 2);
} }
@ -233,8 +245,8 @@ public class PSymbolicRegression extends AbstractOptimizationProblem implements
if (m_Plot==null) this.initEnvironmentPanel(); if (m_Plot==null) this.initEnvironmentPanel();
this.m_Plot.clearAll(); this.m_Plot.clearAll();
program = ((InterfaceDataTypeProgram)this.m_OverallBest).getProgramData()[0]; program = ((InterfaceDataTypeProgram)this.m_OverallBest).getProgramData()[0];
for (double i = 0; i < this.m_NumberOfCheckPoints; i++) { for (int i = 0; i < this.m_NumberOfCheckPoints; i++) {
for (int j = 0; j < this.m_X.length; j++) this.m_X[j] = i ; setCheckPoint(m_X, i);
tmpValue = ((Double)program.evaluate(this)).doubleValue(); tmpValue = ((Double)program.evaluate(this)).doubleValue();
this.m_Plot.setConnectedPoint(this.m_X[0], tmpValue, 0); this.m_Plot.setConnectedPoint(this.m_X[0], tmpValue, 0);
tmpValue = this.m_TargetFunction.evaluateFunction(this.m_X); tmpValue = this.m_TargetFunction.evaluateFunction(this.m_X);
@ -245,6 +257,17 @@ public class PSymbolicRegression extends AbstractOptimizationProblem implements
} }
} }
/**
* Select a test point - TODO this btw only makes much sense in 1D.
* @param x
* @param j
*/
private void setCheckPoint(double[] x, int j) {
for (int i = 0; i < x.length; i++) {
x[i] = this.m_LowerBound +(j*(this.m_UpperBound-this.m_LowerBound)/(this.m_NumberOfCheckPoints-1));
}
}
/** This method returns a string describing the optimization problem. /** This method returns a string describing the optimization problem.
* @param opt The Optimizer that is used or had been used. * @param opt The Optimizer that is used or had been used.
* @return The description. * @return The description.
@ -458,4 +481,122 @@ public class PSymbolicRegression extends AbstractOptimizationProblem implements
public InterfaceDataTypeProgram getGPIndividual() { public InterfaceDataTypeProgram getGPIndividual() {
return (InterfaceDataTypeProgram)this.m_Template; return (InterfaceDataTypeProgram)this.m_Template;
} }
public String GPIndividualTipText() {
return "Modify the properties of the template GP individual such as maximum tree depth etc.";
}
public double getLowerBound() {
return m_LowerBound;
}
public void setLowerBound(double mLowerBound) {
m_LowerBound = mLowerBound;
}
public String lowerBoundTipText() {
return "The lower bound of the 1D double interval where the target function is sampled.";
}
public double getUpperBound() {
return m_UpperBound;
}
public void setUpperBound(double mUpperBound) {
m_UpperBound = mUpperBound;
}
public String upperBoundTipText() {
return "The upper bound of the 1D double interval where the target function is sampled.";
}
public String[] customPropertyOrder() {
return new String[] {"lowerBound", "upperBound"};
}
@Override
public String[] getAdditionalFileStringHeader(PopulationInterface pop) {
String[] superHd = super.getAdditionalFileStringHeader(pop);
return ToolBox.appendArrays(new String[]{"bestIndySize","avgIndySize","avgIndyDepth"}, superHd);
}
@Override
public Object[] getAdditionalFileStringValue(PopulationInterface pop) {
Object[] superDat = super.getAdditionalFileStringValue(pop);
return ToolBox.appendArrays(new Object[]{getBestIndySize(pop), getAvgIndySize(pop), getAvgIndyDepth(pop)}, superDat);
}
/**
* Get the average tree depth of the given population (for program individuals).
*
* @see #getIndyDepth(AbstractEAIndividual)
* @param pop
* @return
*/
public static double getAvgIndyDepth(PopulationInterface pop) {
Population p = (Population)pop;
double sum=0;
for (int i=0; i<p.size(); i++) {
sum+=getIndyDepth(p.getEAIndividual(i));
}
return sum/p.size();
}
/**
* Return the average number of nodes of program individuals.
*
* @see #getIndySize(AbstractEAIndividual)
* @param pop
* @return
*/
public static double getAvgIndySize(PopulationInterface pop) {
Population p = (Population)pop;
double sum=0;
for (int i=0; i<p.size(); i++) {
sum+=getIndySize(p.getEAIndividual(i));
}
return sum/p.size();
}
/**
* Return the number of nodes of the best current individual if it represents a program,
* otherwise zero.
*
* @param pop
* @return
*/
public static int getBestIndySize(PopulationInterface pop) {
Population p = (Population)pop;
AbstractEAIndividual indy = p.getBestEAIndividual();
return getIndySize(indy);
}
/**
* Return the number of nodes in an individual representing a program or null
* if it is of an incompatible type.
*
* @param indy
* @return
*/
public static int getIndySize(AbstractEAIndividual indy) {
if (indy instanceof InterfaceDataTypeProgram) {
InterfaceProgram prog = ((InterfaceDataTypeProgram)indy).getProgramData()[0];
if (prog instanceof AbstractGPNode) {
AbstractGPNode gpNode = (AbstractGPNode)prog;
return gpNode.getNumberOfNodes();
} else return 0;
} else return 0;
}
/**
* Return the depth of an individual representing a program or null
* if it is of an incompatible type.
*
* @param indy
* @return
*/
public static int getIndyDepth(AbstractEAIndividual indy) {
if (indy instanceof InterfaceDataTypeProgram) {
InterfaceProgram prog = ((InterfaceDataTypeProgram)indy).getProgramData()[0];
if (prog instanceof AbstractGPNode) {
AbstractGPNode gpNode = (AbstractGPNode)prog;
return gpNode.getMaxDepth();
} else return 0;
} else return 0;
}
} }

View File

@ -27,7 +27,9 @@ public class RFKoza_GPI_7_3 implements InterfaceRegressionFunction, java.io.Seri
*/ */
public double evaluateFunction(double[] x) { public double evaluateFunction(double[] x) {
double result = 0; double result = 0;
for (int i = 0; i < x.length; i++) result += Math.pow(x[i], 4) + Math.pow(x[i], 3) + Math.pow(x[i], 2) + Math.pow(x[i], 1); for (int i = 0; i < x.length; i++) {
result += Math.pow(x[i], 4) + Math.pow(x[i], 3) + Math.pow(x[i], 2) + x[i];
}
return result; return result;
} }
@ -46,6 +48,6 @@ public class RFKoza_GPI_7_3 implements InterfaceRegressionFunction, java.io.Seri
* @return description * @return description
*/ */
public static String globalInfo() { public static String globalInfo() {
return "This target function is given in Koza GP I chapter 7.3."; return "This target function f(x)=x^4+x^3+x^2+x as given in Koza GP I chapter 7.3.";
} }
} }

View File

@ -1,16 +1,15 @@
package eva2.server.go.strategies; package eva2.server.go.strategies;
import eva2.gui.BeanInspector;
import eva2.server.go.InterfacePopulationChangedEventListener; import eva2.server.go.InterfacePopulationChangedEventListener;
import eva2.server.go.individuals.AbstractEAIndividual; import eva2.server.go.individuals.AbstractEAIndividual;
import eva2.server.go.individuals.GAIndividualBinaryData;
import eva2.server.go.operators.mutation.InterfaceMutationGenerational; import eva2.server.go.operators.mutation.InterfaceMutationGenerational;
import eva2.server.go.operators.selection.InterfaceSelection; import eva2.server.go.operators.selection.InterfaceSelection;
import eva2.server.go.operators.selection.SelectTournament; import eva2.server.go.operators.selection.SelectRandom;
import eva2.server.go.operators.selection.SelectXProbRouletteWheel;
import eva2.server.go.populations.InterfaceSolutionSet; import eva2.server.go.populations.InterfaceSolutionSet;
import eva2.server.go.populations.Population; import eva2.server.go.populations.Population;
import eva2.server.go.populations.SolutionSet; import eva2.server.go.populations.SolutionSet;
import eva2.server.go.problems.B1Problem; import eva2.server.go.problems.F1Problem;
import eva2.server.go.problems.InterfaceOptimizationProblem; import eva2.server.go.problems.InterfaceOptimizationProblem;
/** The traditional genetic algorithms as devised by Holland. To only special here /** The traditional genetic algorithms as devised by Holland. To only special here
@ -29,9 +28,9 @@ import eva2.server.go.problems.InterfaceOptimizationProblem;
public class GeneticAlgorithm implements InterfaceOptimizer, java.io.Serializable { public class GeneticAlgorithm implements InterfaceOptimizer, java.io.Serializable {
private Population m_Population = new Population(); private Population m_Population = new Population();
private InterfaceOptimizationProblem m_Problem = new B1Problem(); private InterfaceOptimizationProblem m_Problem = new F1Problem();
private InterfaceSelection m_ParentSelection = new SelectTournament(); private InterfaceSelection m_ParentSelection = new SelectXProbRouletteWheel();
private InterfaceSelection m_PartnerSelection = new SelectTournament(); private InterfaceSelection m_PartnerSelection = new SelectRandom();
private boolean m_UseElitism = true; private boolean m_UseElitism = true;
private int m_Plague = 0; private int m_Plague = 0;
private int m_NumberOfPartners = 1; private int m_NumberOfPartners = 1;