Some updates, mainly GP stuff...

This commit is contained in:
Marcel Kronfeld 2010-06-08 14:11:07 +00:00
parent a370c45029
commit 257c0965c0
30 changed files with 240 additions and 147 deletions

View File

@ -221,6 +221,7 @@ public class GOEPanel extends JPanel implements ItemListener {
// System.out.println("Copying " + BeanInspector.toString(source));
SerializedObject so = new SerializedObject(source);
result = so.getObject();
so=null;
} catch (Exception ex) {
System.err.println("GenericObjectEditor: Problem making backup object");
System.err.println(source.getClass().getName());

View File

@ -114,6 +114,7 @@ implements PropertyEditor {
try {
SerializedObject so = new SerializedObject(addObj);
addObj = so.getObject();
so=null;
if (selected != -1) {
m_ListModel.insertElementAt(addObj, selected);
} else {

View File

@ -949,7 +949,7 @@ public abstract class AbstractEAIndividual implements IndividualInterface, java.
if ((i+1) < b.length) sb.append(separator);
}
} else if (individual instanceof InterfaceDataTypeProgram) {
InterfaceProgram[] b = ((InterfaceDataTypeProgram)individual).getProgramData();
InterfaceProgram[] b = ((InterfaceDataTypeProgram)individual).getProgramDataWithoutUpdate();
for (int i = 0; i < b.length; i++) {
sb.append(b[i].getStringRepresentation());
if ((i+1) < b.length) sb.append(separator);

View File

@ -27,7 +27,7 @@ public class GPIndividualProgramData extends AbstractEAIndividual implements Int
protected GPArea[] m_Area;
protected double m_InitFullGrowRatio = 0.5;
protected int m_InitDepth = 4;
protected int m_maxDepth = 8;
protected int m_maxAllowedDepth = 8;
protected boolean m_CheckMaxDepth = true;
public GPIndividualProgramData() {
@ -58,7 +58,7 @@ public class GPIndividualProgramData extends AbstractEAIndividual implements Int
}
this.m_InitFullGrowRatio = individual.m_InitFullGrowRatio;
this.m_InitDepth = individual.m_InitDepth;
this.m_maxDepth = individual.m_maxDepth;
this.m_maxAllowedDepth = individual.m_maxAllowedDepth;
this.m_CheckMaxDepth = individual.m_CheckMaxDepth;
// cloning the members of AbstractEAIndividual
@ -90,7 +90,7 @@ public class GPIndividualProgramData extends AbstractEAIndividual implements Int
if (individual instanceof GPIndividualProgramData) {
GPIndividualProgramData indy = (GPIndividualProgramData) individual;
//@todo Eigendlich k<EFBFBD>nnte ich noch die Areas vergleichen
if (this.m_maxDepth != indy.m_maxDepth)
if (this.m_maxAllowedDepth != indy.m_maxAllowedDepth)
return false;
if ((this.m_Genotype == null) || (indy.m_Genotype == null))
return false;
@ -134,9 +134,13 @@ public class GPIndividualProgramData extends AbstractEAIndividual implements Int
this.m_Phenotype = new AbstractGPNode[this.m_Genotype.length];
for (int i = 0; i < this.m_Genotype.length; i++) {
this.m_Phenotype[i] = (AbstractGPNode)this.m_Genotype[i].clone();
if ((this.m_CheckMaxDepth) && (this.m_Phenotype[i].isMaxDepthViolated(this.m_maxDepth))) {
//System.out.println("Trying to meet the Target Depth!");
this.m_Phenotype[i].repairMaxDepth(this.m_Area[i], this.m_maxDepth);
// if (!m_Phenotype[0].checkDepth(0)) {
// System.err.println("error... " + m_Genotype[0].checkDepth(0));
// }
if ((this.m_CheckMaxDepth) && (this.m_Phenotype[i].isMaxDepthViolated(this.m_maxAllowedDepth))) {
System.err.println("Trying to meet the Target Depth! " + this.m_Phenotype[i].isMaxDepthViolated(this.m_maxAllowedDepth) + " "+ m_Phenotype[i].getMaxDepth());
this.m_Phenotype[i].repairMaxDepth(this.m_Area[i], this.m_maxAllowedDepth);
//System.out.println("TragetDepth: " + this.m_TargetDepth + " : " + this.m_Program.getMaxDepth());
}
}
@ -148,7 +152,8 @@ public class GPIndividualProgramData extends AbstractEAIndividual implements Int
* @return InterfaceProgram[] representing the Program.
*/
public InterfaceProgram[] getProgramDataWithoutUpdate() {
return this.m_Phenotype;
if (this.m_Phenotype==null) return getProgramData();
else return this.m_Phenotype;
}
/** This method allows you to set the program phenotype.
@ -244,6 +249,7 @@ public class GPIndividualProgramData extends AbstractEAIndividual implements Int
*/
public void SetPGenotype(AbstractGPNode[] b) {
this.m_Genotype = b;
this.m_Phenotype=null;
}
/** This method will allow the user to set the current program 'genotype'.
@ -252,28 +258,41 @@ public class GPIndividualProgramData extends AbstractEAIndividual implements Int
*/
public void SetPGenotype(AbstractGPNode b, int i) {
this.m_Genotype[i] = b;
m_Genotype[i].updateDepth(0);
// System.out.println("Setting pheno of depth " + b.getMaxDepth() + " " + b.getStringRepresentation());
this.m_Phenotype=null;
}
/** This method performs a simple one element mutation on the program
/**
* This method performs a simple one element mutation on the program
*/
public void defaultMutate() {
ArrayList allNodes = new ArrayList();
for (int i = 0; i < this.m_Genotype.length; i++) {
this.m_Genotype[i].addNodesTo(allNodes);
AbstractGPNode nodeToMutate = (AbstractGPNode) allNodes.get(RNG.randomInt(0, allNodes.size()-1));
if (nodeToMutate.getParent() == null) {
AbstractGPNode nodeToMutate = this.m_Genotype[i].getRandomNode();
if (nodeToMutate.getParent() == null) { // mutate at root
this.defaultInit(null);
} else {
AbstractGPNode parent = nodeToMutate.getParent();
AbstractGPNode newNode = (AbstractGPNode)(((AbstractGPNode)this.m_Area[i].getRandomNode().clone()));
newNode.setDepth(nodeToMutate.getDepth());
newNode.initGrow(this.m_Area[i], this.m_maxDepth);
parent.setNode(newNode, nodeToMutate);
if (m_CheckMaxDepth && (nodeToMutate.getDepth()==m_maxAllowedDepth)) { // mutate with a constant
AbstractGPNode newNode = (AbstractGPNode)(((AbstractGPNode)this.m_Area[i].getRandomNodeWithArity(0).clone()));
newNode.setDepth(nodeToMutate.getDepth());
parent.setNode(newNode, nodeToMutate);
} else {
AbstractGPNode newNode = (AbstractGPNode)(((AbstractGPNode)this.m_Area[i].getRandomNode().clone()));
newNode.setDepth(nodeToMutate.getDepth());
newNode.initGrow(this.m_Area[i], this.m_maxAllowedDepth);
parent.setNode(newNode, nodeToMutate);
}
//if (!m_Genotype[i].checkDepth(0) || (m_Genotype[i].isMaxDepthViolated(m_maxAllowedDepth))) {
// System.err.println("Error in GPIndividualProgramData.defaultMutate!");
//}
}
}
m_Phenotype=null; // reset pheno
}
public void defaultInit(InterfaceOptimizationProblem prob) {
m_Phenotype=null; // reset pheno
for (int i = 0; i < this.m_Area.length; i++) {
if (this.m_Area[i] == null) {
EVAERROR.errorMsgOnce("Error in GPIndividualProgramData.defaultInit(): Area["+i+"] == null !!");
@ -339,9 +358,9 @@ public class GPIndividualProgramData extends AbstractEAIndividual implements Int
* @param b The new init Depth of the GP Tree.
*/
public void setInitDepth(int b) {
if (b > this.m_maxDepth) {
if (b > this.m_maxAllowedDepth) {
System.out.println("Waring Init Depth will be set to Target Depth!");
b = this.m_maxDepth;
b = this.m_maxAllowedDepth;
}
this.m_InitDepth = b;
}
@ -355,17 +374,29 @@ public class GPIndividualProgramData extends AbstractEAIndividual implements Int
/** This method set/get the target depth.
* @param b The new target Depth of the GP Tree.
*/
public void setMaxDepth(int b) {
this.m_maxDepth = b;
public void setMaxAllowedDepth(int b) {
this.m_maxAllowedDepth = b;
}
public int getMaxDepth() {
return this.m_maxDepth;
public int getMaxAllowedDepth() {
return this.m_maxAllowedDepth;
}
public String maxDepthTipText() {
return "The maximum depth of the GP tree.";
public String maxAllowedDepthTipText() {
return "The maximum depth allowed for the GP tree.";
}
public String[] customPropertyOrder() {
return new String[] {"initDepth", "checkMaxDepth", "maxDepth"};
return new String[] {"initDepth", "checkMaxDepth", "maxAllowedDepth"};
}
public void updateDepth() {
for (int i=0; i<m_Genotype.length; i++) {
m_Genotype[i].updateDepth(0);
}
}
public void checkDepth() {
for (int i=0; i<m_Genotype.length; i++) {
m_Genotype[i].checkDepth(0);
}
}
}

View File

@ -33,4 +33,9 @@ public interface InterfaceGPIndividual {
*/
public Object[] getFunctionArea();
/**
* Return the maximal allowed depth of a GP tree (or -1 if it does not apply).
* @return
*/
public int getMaxAllowedDepth();
}

View File

@ -12,6 +12,7 @@ import eva2.server.go.problems.InterfaceProgramProblem;
import eva2.tools.Pair;
import eva2.tools.ReflectPackage;
import eva2.tools.math.Mathematics;
import eva2.tools.math.RNG;
/** This gives an abstract node, with default functionality for get and set methods.
@ -56,6 +57,18 @@ public abstract class AbstractGPNode implements InterfaceProgram, java.io.Serial
return sb.toString();
}
/**
* Recursively perform deep cloning of the members of this instance and all sub-nodes.
*
* @param node the node to clone values from
**/
protected void cloneMembers(AbstractGPNode node) {
this.m_Depth = node.m_Depth;
this.m_Parent = node.m_Parent;
this.m_Nodes = new AbstractGPNode[node.m_Nodes.length];
for (int i = 0; i < node.m_Nodes.length; i++) this.m_Nodes[i] = (AbstractGPNode) node.m_Nodes[i].clone();
}
private static void appendStringRepresentation(AbstractGPNode node, StringBuffer sbuf) {
String op = node.getOpIdentifier();
sbuf.append(op);
@ -350,14 +363,20 @@ public abstract class AbstractGPNode implements InterfaceProgram, java.io.Serial
this.m_Nodes[index] = node;
}
/** This method allows you to set a node specified by the index
/**
* This method allows you to set a node specified by the reference.
* @param newnode The new node.
* @param oldnode The old node.
*/
public void setNode(AbstractGPNode newnode, AbstractGPNode oldnode) {
newnode.setParent(this);
newnode.setDepth(this.m_Depth+1);
for (int i = 0; i < this.m_Nodes.length; i++) if (this.m_Nodes[i].equals(oldnode)) this.m_Nodes[i] = newnode;
newnode.updateDepth(this.m_Depth+1);
for (int i = 0; i < this.m_Nodes.length; i++) {
if (this.m_Nodes[i] == oldnode) {
this.m_Nodes[i] = newnode;
// System.out.println("SWITCHED " + i);
}
}
}
/** This method returns all nodes begining with the current node.
* @param ListOfNodes This ArrayList will contain all nodes
@ -367,6 +386,27 @@ public abstract class AbstractGPNode implements InterfaceProgram, java.io.Serial
for (int i = 0; i < this.m_Nodes.length; i++) this.m_Nodes[i].addNodesTo(ListOfNodes);
}
/**
* Return a shallow reference to a random node within this tree.
* @return
*/
public AbstractGPNode getRandomNode() {
ArrayList allNodes = new ArrayList(10);
addNodesTo(allNodes);
return (AbstractGPNode) allNodes.get(RNG.randomInt(allNodes.size()));
}
/**
* Return a shallow reference to a random leaf of this tree.
* @return
*/
public AbstractGPNode getRandomLeaf() {
if (m_Nodes.length>0) {
int k=RNG.randomInt(m_Nodes.length);
return m_Nodes[k].getRandomLeaf();
} else return this;
}
/** This method allows you to set the parent of the node
* @param parent The new parent
*/
@ -435,7 +475,8 @@ public abstract class AbstractGPNode implements InterfaceProgram, java.io.Serial
return result;
}
/** This method will return the max depth of the tree
/**
* Return the maximal depth of the tree starting here, but relating to the whole tree.
* @return The max depth.
*/
public int getMaxDepth() {
@ -446,6 +487,16 @@ public abstract class AbstractGPNode implements InterfaceProgram, java.io.Serial
return result;
}
/**
* Return the depth of the subtree only.
*
* @return
*/
public int getSubtreeDepth() {
int maxDepth = getMaxDepth();
return maxDepth-m_Depth;
}
/** This method will check if maxdepth is violated
* @param maxDepth The max depth.
* @return True if MaxDepth is violated
@ -503,4 +554,32 @@ public abstract class AbstractGPNode implements InterfaceProgram, java.io.Serial
return false;
}
}
/**
* Update the depth of this node tree starting with the given initial depth of the root.
*
* @param myDepth
*/
public void updateDepth(int myDepth) {
m_Depth=myDepth;
for (int i=0; i<m_Nodes.length; i++) {
m_Nodes[i].updateDepth(myDepth+1);
}
}
/**
* Check the depth of this node tree starting with the given initial depth of the root.
*
* @param myDepth
*/
public boolean checkDepth(int myDepth) {
if (m_Depth!=myDepth) {
System.err.println("Depth was wrong at level " + myDepth);
return false;
}
for (int i=0; i<m_Nodes.length; i++) {
if (!m_Nodes[i].checkDepth(myDepth+1)) return false;
}
return true;
}
}

View File

@ -12,10 +12,7 @@ public class GPNodeAbs extends AbstractGPNode implements java.io.Serializable {
public GPNodeAbs() {
}
public GPNodeAbs(GPNodeAbs node) {
this.m_Depth = node.m_Depth;
this.m_Parent = node.m_Parent;
this.m_Nodes = new AbstractGPNode[node.m_Nodes.length];
for (int i = 0; i < node.m_Nodes.length; i++) this.m_Nodes[i] = (AbstractGPNode) node.m_Nodes[i].clone();
this.cloneMembers(node);
}
/** This method will be used to identify the node in the GPAreaEditor

View File

@ -18,10 +18,7 @@ public class GPNodeAdd extends AbstractGPNode implements java.io.Serializable {
}
public GPNodeAdd(GPNodeAdd node) {
this.m_Depth = node.m_Depth;
this.m_Parent = node.m_Parent;
this.m_Nodes = new AbstractGPNode[node.m_Nodes.length];
for (int i = 0; i < node.m_Nodes.length; i++) this.m_Nodes[i] = (AbstractGPNode) node.m_Nodes[i].clone();
this.cloneMembers(node);
}
/** This method will be used to identify the node in the GPAreaEditor

View File

@ -16,10 +16,7 @@ public class GPNodeConst extends AbstractGPNode implements java.io.Serializable
public GPNodeConst(GPNodeConst node) {
value = node.value;
this.m_Depth = node.m_Depth;
this.m_Parent = node.m_Parent;
this.m_Nodes = new AbstractGPNode[node.m_Nodes.length];
for (int i = 0; i < node.m_Nodes.length; i++) this.m_Nodes[i] = (AbstractGPNode) node.m_Nodes[i].clone();
this.cloneMembers(node);
}
@Override

View File

@ -14,10 +14,7 @@ public class GPNodeCos extends AbstractGPNode implements java.io.Serializable {
public GPNodeCos() {
}
public GPNodeCos(GPNodeCos node) {
this.m_Depth = node.m_Depth;
this.m_Parent = node.m_Parent;
this.m_Nodes = new AbstractGPNode[node.m_Nodes.length];
for (int i = 0; i < node.m_Nodes.length; i++) this.m_Nodes[i] = (AbstractGPNode) node.m_Nodes[i].clone();
this.cloneMembers(node);
}
/** This method will be used to identify the node in the GPAreaEditor

View File

@ -22,10 +22,7 @@ public class GPNodeDiv extends AbstractGPNode implements java.io.Serializable {
public GPNodeDiv(GPNodeDiv node) {
this.m_LowerBorderForSec = node.m_LowerBorderForSec;
this.m_Depth = node.m_Depth;
this.m_Parent = node.m_Parent;
this.m_Nodes = new AbstractGPNode[node.m_Nodes.length];
for (int i = 0; i < node.m_Nodes.length; i++) this.m_Nodes[i] = (AbstractGPNode) node.m_Nodes[i].clone();
this.cloneMembers(node);
}
/** This method will be used to identify the node in the GPAreaEditor

View File

@ -14,10 +14,7 @@ public class GPNodeExp extends AbstractGPNode implements java.io.Serializable {
public GPNodeExp() {
}
public GPNodeExp(GPNodeExp node) {
this.m_Depth = node.m_Depth;
this.m_Parent = node.m_Parent;
this.m_Nodes = new AbstractGPNode[node.m_Nodes.length];
for (int i = 0; i < node.m_Nodes.length; i++) this.m_Nodes[i] = (AbstractGPNode) node.m_Nodes[i].clone();
this.cloneMembers(node);
}
/** This method will be used to identify the node in the GPAreaEditor

View File

@ -15,10 +15,7 @@ public class GPNodeFlowExec2 extends AbstractGPNode implements java.io.Serializa
}
public GPNodeFlowExec2(GPNodeFlowExec2 node) {
this.m_Depth = node.m_Depth;
this.m_Parent = node.m_Parent;
this.m_Nodes = new AbstractGPNode[node.m_Nodes.length];
for (int i = 0; i < node.m_Nodes.length; i++) this.m_Nodes[i] = (AbstractGPNode) node.m_Nodes[i].clone();
this.cloneMembers(node);
}
/** This method will be used to identify the node in the GPAreaEditor

View File

@ -15,10 +15,7 @@ public class GPNodeFlowExec3 extends AbstractGPNode implements java.io.Serializa
}
public GPNodeFlowExec3(GPNodeFlowExec3 node) {
this.m_Depth = node.m_Depth;
this.m_Parent = node.m_Parent;
this.m_Nodes = new AbstractGPNode[node.m_Nodes.length];
for (int i = 0; i < node.m_Nodes.length; i++) this.m_Nodes[i] = (AbstractGPNode) node.m_Nodes[i].clone();
this.cloneMembers(node);
}
/** This method will be used to identify the node in the GPAreaEditor

View File

@ -30,10 +30,7 @@ public class GPNodeInput extends AbstractGPNode implements java.io.Serializable
public GPNodeInput(GPNodeInput node) {
this.m_Identifier = node.m_Identifier;
this.m_Depth = node.m_Depth;
this.m_Parent = node.m_Parent;
this.m_Nodes = new AbstractGPNode[node.m_Nodes.length];
for (int i = 0; i < node.m_Nodes.length; i++) this.m_Nodes[i] = (AbstractGPNode) node.m_Nodes[i].clone();
this.cloneMembers(node);
}
public void setIdentifier(String str) {

View File

@ -15,10 +15,7 @@ public class GPNodeMult extends AbstractGPNode implements java.io.Serializable {
public GPNodeMult() {
}
public GPNodeMult(GPNodeMult node) {
this.m_Depth = node.m_Depth;
this.m_Parent = node.m_Parent;
this.m_Nodes = new AbstractGPNode[node.m_Nodes.length];
for (int i = 0; i < node.m_Nodes.length; i++) this.m_Nodes[i] = (AbstractGPNode) node.m_Nodes[i].clone();
this.cloneMembers(node);
}
/** This method will be used to identify the node in the GPAreaEditor

View File

@ -15,10 +15,7 @@ public class GPNodeNeg extends AbstractGPNode implements java.io.Serializable {
public GPNodeNeg() {
}
public GPNodeNeg(GPNodeNeg node) {
this.m_Depth = node.m_Depth;
this.m_Parent = node.m_Parent;
this.m_Nodes = new AbstractGPNode[node.m_Nodes.length];
for (int i = 0; i < node.m_Nodes.length; i++) this.m_Nodes[i] = (AbstractGPNode) node.m_Nodes[i].clone();
this.cloneMembers(node);
}
/** This method will be used to identify the node in the GPAreaEditor

View File

@ -9,10 +9,14 @@ public class GPNodeOne extends GPNodeConst implements java.io.Serializable {
super(1.);
}
public GPNodeOne(GPNodeOne node) {
super(node);
}
/** This method allows you to clone the Nodes
* @return the clone
*/
public Object clone() {
return (Object) new GPNodeOne();
return (Object) new GPNodeOne(this);
}
}

View File

@ -29,10 +29,7 @@ public class GPNodeOutput extends AbstractGPNode implements java.io.Serializable
public GPNodeOutput(GPNodeOutput node) {
this.m_Identifier = node.m_Identifier;
this.m_Depth = node.m_Depth;
this.m_Parent = node.m_Parent;
this.m_Nodes = new AbstractGPNode[node.m_Nodes.length];
for (int i = 0; i < node.m_Nodes.length; i++) this.m_Nodes[i] = (AbstractGPNode) node.m_Nodes[i].clone();
this.cloneMembers(node);
}
@Override

View File

@ -9,11 +9,15 @@ public class GPNodePi extends GPNodeConst implements java.io.Serializable {
super(Math.PI);
}
/** This method allows you to clone the Nodes
public GPNodePi(GPNodePi node) {
super(node);
}
/** This method allows you to clone the Nodes
* @return the clone
*/
public Object clone() {
return (Object) new GPNodePi();
return (Object) new GPNodePi(this);
}
/** This method will be used to identify the node in the GPAreaEditor

View File

@ -14,10 +14,7 @@ public class GPNodePow2 extends AbstractGPNode implements java.io.Serializable {
public GPNodePow2() {
}
public GPNodePow2(GPNodePow2 node) {
this.m_Depth = node.m_Depth;
this.m_Parent = node.m_Parent;
this.m_Nodes = new AbstractGPNode[node.m_Nodes.length];
for (int i = 0; i < node.m_Nodes.length; i++) this.m_Nodes[i] = (AbstractGPNode) node.m_Nodes[i].clone();
this.cloneMembers(node);
}
/** This method will be used to identify the node in the GPAreaEditor

View File

@ -14,10 +14,7 @@ public class GPNodePow3 extends AbstractGPNode implements java.io.Serializable {
public GPNodePow3() {
}
public GPNodePow3(GPNodePow3 node) {
this.m_Depth = node.m_Depth;
this.m_Parent = node.m_Parent;
this.m_Nodes = new AbstractGPNode[node.m_Nodes.length];
for (int i = 0; i < node.m_Nodes.length; i++) this.m_Nodes[i] = (AbstractGPNode) node.m_Nodes[i].clone();
this.cloneMembers(node);
}
/** This method will be used to identify the node in the GPAreaEditor

View File

@ -14,10 +14,7 @@ public class GPNodeProd extends AbstractGPNode implements java.io.Serializable {
}
public GPNodeProd(GPNodeProd node) {
this.m_Depth = node.m_Depth;
this.m_Parent = node.m_Parent;
this.m_Nodes = new AbstractGPNode[node.m_Nodes.length];
for (int i = 0; i < node.m_Nodes.length; i++) this.m_Nodes[i] = (AbstractGPNode) node.m_Nodes[i].clone();
this.cloneMembers(node);
}
/** This method will be used to identify the node in the GPAreaEditor

View File

@ -14,10 +14,7 @@ public class GPNodeSin extends AbstractGPNode implements java.io.Serializable {
public GPNodeSin() {
}
public GPNodeSin(GPNodeSin node) {
this.m_Depth = node.m_Depth;
this.m_Parent = node.m_Parent;
this.m_Nodes = new AbstractGPNode[node.m_Nodes.length];
for (int i = 0; i < node.m_Nodes.length; i++) this.m_Nodes[i] = (AbstractGPNode) node.m_Nodes[i].clone();
this.cloneMembers(node);
}
/** This method will be used to identify the node in the GPAreaEditor

View File

@ -15,10 +15,7 @@ public class GPNodeSqrt extends AbstractGPNode implements java.io.Serializable {
public GPNodeSqrt() {
}
public GPNodeSqrt(GPNodeSqrt node) {
this.m_Depth = node.m_Depth;
this.m_Parent = node.m_Parent;
this.m_Nodes = new AbstractGPNode[node.m_Nodes.length];
for (int i = 0; i < node.m_Nodes.length; i++) this.m_Nodes[i] = (AbstractGPNode) node.m_Nodes[i].clone();
this.cloneMembers(node);
}
/** This method will be used to identify the node in the GPAreaEditor

View File

@ -15,10 +15,7 @@ public class GPNodeSub extends AbstractGPNode implements java.io.Serializable {
public GPNodeSub() {
}
public GPNodeSub(GPNodeSub node) {
this.m_Depth = node.m_Depth;
this.m_Parent = node.m_Parent;
this.m_Nodes = new AbstractGPNode[node.m_Nodes.length];
for (int i = 0; i < node.m_Nodes.length; i++) this.m_Nodes[i] = (AbstractGPNode) node.m_Nodes[i].clone();
this.cloneMembers(node);
}
/** This method will be used to identify the node in the GPAreaEditor

View File

@ -14,10 +14,7 @@ public class GPNodeSum extends AbstractGPNode implements java.io.Serializable {
}
public GPNodeSum(GPNodeSum node) {
this.m_Depth = node.m_Depth;
this.m_Parent = node.m_Parent;
this.m_Nodes = new AbstractGPNode[node.m_Nodes.length];
for (int i = 0; i < node.m_Nodes.length; i++) this.m_Nodes[i] = (AbstractGPNode) node.m_Nodes[i].clone();
this.cloneMembers(node);
}
/** This method will be used to identify the node in the GPAreaEditor

View File

@ -4,6 +4,7 @@ package eva2.server.go.operators.crossover;
import java.util.ArrayList;
import eva2.server.go.individuals.AbstractEAIndividual;
import eva2.server.go.individuals.GPIndividualProgramData;
import eva2.server.go.individuals.InterfaceGPIndividual;
import eva2.server.go.individuals.codings.gp.AbstractGPNode;
import eva2.server.go.populations.Population;
@ -18,12 +19,18 @@ import eva2.tools.math.RNG;
* To change this template use Options | File Templates.
*/
public class CrossoverGPDefault implements InterfaceCrossover, java.io.Serializable {
private InterfaceOptimizationProblem m_OptimizationProblem;
/**
*
*/
private static final long serialVersionUID = 8900427365914281930L;
private InterfaceOptimizationProblem m_OptimizationProblem;
private boolean maintainMaxDepth = true;
private static final boolean TRACE=false;
public CrossoverGPDefault() {
}
public CrossoverGPDefault(CrossoverGPDefault c) {
this.maintainMaxDepth = c.maintainMaxDepth;
this.m_OptimizationProblem = c.m_OptimizationProblem;
}
/** This method will enable you to clone a given mutation operator
@ -33,53 +40,67 @@ public class CrossoverGPDefault implements InterfaceCrossover, java.io.Serializa
return new CrossoverGPDefault(this);
}
/** This method performs crossover on two individuals. If the individuals do
* not implement InterfaceGAIndividual, then nothing will happen.
/**
* Exchange subtrees of two GP nodes. Allows to keep the depth restriction.
*
* @param indy1 The first individual
* @param partners The second individual
*/
public AbstractEAIndividual[] mate(AbstractEAIndividual indy1, Population partners) {
if (partners.size()>1) System.err.println("Warning, crossover may not work on more than one partner! " + this.getClass());
AbstractEAIndividual[] result = null;
result = new AbstractEAIndividual[partners.size()+1];
result[0] = (AbstractEAIndividual) (indy1).clone();
for (int i = 0; i < partners.size(); i++) result[i+1] = (AbstractEAIndividual) ((AbstractEAIndividual)partners.get(i)).clone();
//for (int i = 0; i < result.length; i++) System.out.println("Before Crossover: " +result[i].getSolutionRepresentationFor());
if (TRACE) for (int i = 0; i < result.length; i++) System.out.println("Before Crossover: " +result[i].getStringRepresentation());
if (partners.size() == 0) return result;
if ((indy1 instanceof InterfaceGPIndividual) && (partners.get(0) instanceof InterfaceGPIndividual)) {
//select node from 0 and memorize parent
ArrayList allNodes = new ArrayList();
int allowedDepth = ((InterfaceGPIndividual)indy1).getMaxAllowedDepth();
AbstractGPNode[] nodes = ((InterfaceGPIndividual)result[0]).getPGenotype();
for (int t = 0; t < nodes.length; t++) {
allNodes = new ArrayList();
((InterfaceGPIndividual)result[0]).getPGenotype()[t].addNodesTo(allNodes);
AbstractGPNode oldNode = (AbstractGPNode) allNodes.get(RNG.randomInt(0, allNodes.size()-1));
AbstractGPNode newNode, memorizingNode = oldNode, tmpNode;
AbstractGPNode oldParent, newParent;
oldParent = oldNode.getParent();
for (int i = 1; i < result.length; i++) {
// choose Node from i and add it to i-1
allNodes = new ArrayList();
((InterfaceGPIndividual)result[i]).getPGenotype()[t].addNodesTo(allNodes);
newNode = (AbstractGPNode) allNodes.get(RNG.randomInt(0, allNodes.size()-1));
tmpNode = newNode;
newParent = tmpNode.getParent();
if (oldParent == null) ((InterfaceGPIndividual)result[i-1]).SetPGenotype(newNode, t);
else oldParent.setNode(newNode, oldNode);
oldNode = tmpNode;
oldParent = newParent;
for (int t = 0; t < nodes.length; t++) { // for each of the genotypes (multiploidy??)
((InterfaceGPIndividual)result[0]).getPGenotype()[t].getRandomNode();
AbstractGPNode selNodeThis = ((InterfaceGPIndividual)result[0]).getPGenotype()[t].getRandomNode();
AbstractGPNode selNodeOther = ((InterfaceGPIndividual)result[1]).getPGenotype()[t].getRandomNode();
if (maintainMaxDepth) {//System.err.print(".");
int maxTries=10;
// if the echange would violate the depth restriction, choose new nodes for a few times...
while (maxTries>=0 && ((selNodeOther.getSubtreeDepth()+selNodeThis.getDepth()>allowedDepth) ||
(selNodeThis.getSubtreeDepth()+selNodeOther.getDepth()>allowedDepth))) {
if (RNG.flipCoin(0.5)) selNodeThis = ((InterfaceGPIndividual)result[0]).getPGenotype()[t].getRandomNode();
else selNodeOther = ((InterfaceGPIndividual)result[1]).getPGenotype()[t].getRandomNode();
maxTries--;
}
if (maxTries<0) { // on a failure, at least exchange two leaves, which always works
// System.err.println("Unable to select fitting nodes! Just switch leaves...");
selNodeThis = ((InterfaceGPIndividual)result[0]).getPGenotype()[t].getRandomLeaf();
selNodeOther = ((InterfaceGPIndividual)result[1]).getPGenotype()[t].getRandomLeaf();
}
}
// add node from 0 to result.length-1
if (oldParent == null) {
((InterfaceGPIndividual)result[result.length-1]).SetPGenotype(memorizingNode, t);
} else {
oldParent.setNode(memorizingNode, oldNode);
if (TRACE) {
System.out.println("Selected t " + selNodeThis.getStringRepresentation());
System.out.println("Selected o " + selNodeOther.getStringRepresentation());
}
AbstractGPNode selNodeThisParent, selNodeOtherParent;
selNodeThisParent = selNodeThis.getParent();
selNodeOtherParent = selNodeOther.getParent();
// actually switch individuals!
if (selNodeThisParent == null) ((InterfaceGPIndividual)result[0]).SetPGenotype((AbstractGPNode)selNodeOther.clone(), t);
else selNodeThisParent.setNode((AbstractGPNode)selNodeOther.clone(), selNodeThis);
// for (int i = 0; i < result.length; i++) System.out.println("-- Betw Crossover: " +result[i].getStringRepresentation());
if (selNodeOtherParent == null) ((InterfaceGPIndividual)result[1]).SetPGenotype((AbstractGPNode)selNodeThis.clone(), t);
else selNodeOtherParent.setNode((AbstractGPNode)selNodeThis.clone(), selNodeOther);
}
}
//in case the crossover was successfull lets give the mutation operators a chance to mate the strategy parameters
for (int i = 0; i < result.length; i++) result[i].getMutationOperator().crossoverOnStrategyParameters(indy1, partners);
//for (int i = 0; i < result.length; i++) System.out.println("After Crossover: " +result[i].getSolutionRepresentationFor());
for (int i = 0; i < result.length; i++) {
((GPIndividualProgramData)result[i]).checkDepth();
result[i].getMutationOperator().crossoverOnStrategyParameters(indy1, partners);
}
if (TRACE) for (int i = 0; i < result.length; i++) System.out.println("After Crossover: " +result[i].getStringRepresentation());
return result;
}

View File

@ -575,7 +575,7 @@ public class PSymbolicRegression extends AbstractOptimizationProblem implements
*/
public static int getIndySize(AbstractEAIndividual indy) {
if (indy instanceof InterfaceDataTypeProgram) {
InterfaceProgram prog = ((InterfaceDataTypeProgram)indy).getProgramData()[0];
InterfaceProgram prog = ((InterfaceDataTypeProgram)indy).getProgramDataWithoutUpdate()[0];
if (prog instanceof AbstractGPNode) {
AbstractGPNode gpNode = (AbstractGPNode)prog;
return gpNode.getNumberOfNodes();
@ -584,7 +584,7 @@ public class PSymbolicRegression extends AbstractOptimizationProblem implements
}
/**
* Return the depth of an individual representing a program or null
* Return the maximal depth of an individual representing a program or null
* if it is of an incompatible type.
*
* @param indy
@ -592,10 +592,14 @@ public class PSymbolicRegression extends AbstractOptimizationProblem implements
*/
public static int getIndyDepth(AbstractEAIndividual indy) {
if (indy instanceof InterfaceDataTypeProgram) {
InterfaceProgram prog = ((InterfaceDataTypeProgram)indy).getProgramData()[0];
InterfaceProgram prog = ((InterfaceDataTypeProgram)indy).getProgramDataWithoutUpdate()[0];
if (prog instanceof AbstractGPNode) {
AbstractGPNode gpNode = (AbstractGPNode)prog;
return gpNode.getMaxDepth();
int d = gpNode.getMaxDepth();
//if (!gpNode.checkDepth(0)) {
// System.out.println(d + "\n" + gpNode.getStringRepresentation());
//}
return d;
} else return 0;
} else return 0;
}

View File

@ -242,7 +242,7 @@ public class StatisticsWithGUI extends AbstractStatistics implements Serializabl
// plot the column as indicated by the graph description
if (currentStatDoubleData[colIndex]!=null) plotFitnessPoint(0, subGraph++, functionCalls, currentStatDoubleData[colIndex]);
else {
EVAERROR.errorMsgOnce("Error, data field " + graphDesc.get(i).head + " does not contain primitive data and cannot be plotted.");
// EVAERROR.errorMsgOnce("Error, data field " + graphDesc.get(i).head + " does not contain primitive data and cannot be plotted.");
subGraph++; // increase index anyways or the name assignment gets inconsistent
}
}