Updating GP - mk branch merge 287:292

This commit is contained in:
Marcel Kronfeld 2009-05-14 07:39:34 +00:00
parent b948bb9da2
commit 947ee034bd
18 changed files with 438 additions and 109 deletions

View File

@ -2,8 +2,12 @@ package eva2.server.go.individuals.codings.gp;
import java.util.ArrayList;
import java.util.Vector;
import eva2.gui.BeanInspector;
import eva2.gui.GenericObjectEditor;
import eva2.server.go.problems.InterfaceProgramProblem;
import eva2.tools.Pair;
/** This gives an abstract node, with default functionality for get and set methods.
@ -41,8 +45,175 @@ public abstract class AbstractGPNode implements InterfaceProgram, java.io.Serial
/** This method returns a string representation
* @return string
*/
public abstract String getStringRepresentation();
public String getStringRepresentation() {
StringBuffer sb = new StringBuffer();
AbstractGPNode.appendStringRepresentation(this, sb);
return sb.toString();
}
private static void appendStringRepresentation(AbstractGPNode node, StringBuffer sbuf) {
String op = node.getOpIdentifier();
sbuf.append(op);
if (node.getArity()>0) {
sbuf.append("(");
for (int i = 0; i < node.m_Nodes.length; i++) {
sbuf.append(node.m_Nodes[i].getStringRepresentation());
if (i<node.m_Nodes.length-1) sbuf.append(", ");
}
sbuf.append(")");
}
}
/**
* This method returns a string identifier of the operator name - it should be unique.
*
* @return string
*/
public abstract String getOpIdentifier();
/**
* Small parser for GP nodes from a String. Format must be (nearly) equivalent to what makeStringRepresentation produces.
* This mainly means prefix notation with braces and commata, such as in:
* AbstractGPNode node = AbstractGPNode.parseFromString("+(2.0,cos(*(pi,pi)))");
* System.out.println("Parsed GPNode: " + node.getStringRepresentation());
* node = AbstractGPNode.parseFromString(node.getStringRepresentation());
* @param str
* @param nodeTypes
* @return
*/
public static Pair<AbstractGPNode,String> parseFromString(String str, Vector<AbstractGPNode> nodeTypes) {
if (nodeTypes == null) {
ArrayList<String>cls = GenericObjectEditor.getClassesFromClassPath(AbstractGPNode.class.getCanonicalName());
nodeTypes = new Vector<AbstractGPNode>(cls.size());
for (int i=0; i<cls.size(); i++) {
try {
AbstractGPNode node = (AbstractGPNode)Class.forName((String)cls.get(i)).newInstance();
nodeTypes.add(node);
} catch(Exception e) {}
}
nodeTypes.add(new GPNodeInput("X"));
}
if (nodeTypes.size()>0) {
Vector<AbstractGPNode> matchSet=AbstractGPNode.match(nodeTypes, str, true, true);
if (matchSet.size()==0) {
// try to read constant
Pair<Double, String> nextState=readDouble(str, true);
if (nextState != null) {
return new Pair<AbstractGPNode,String>(new GPNodeConst(nextState.head().doubleValue()), nextState.tail());
} else {
System.err.println("String has unknown prefix: " + str);
}
}
else if (matchSet.size()>1) System.err.println("String has ambiguous prefix: " + str + " -- " + BeanInspector.toString(matchSet));
else { // exactly one match:
AbstractGPNode currentNode = (AbstractGPNode)matchSet.get(0).clone();
// System.out.println("Found match: " + currentNode.getOpIdentifier() + "/" + currentNode.getArity());
int cutFront=currentNode.getOpIdentifier().length();
String restStr;
if (currentNode.getArity()==0) {
restStr = str.substring(cutFront).trim();
if (currentNode instanceof GPNodeInput) {
Pair<Double, String> nextState=readDouble(restStr, false);
if (nextState!=null) {
((GPNodeInput)currentNode).setIdentifier("X"+((int)nextState.head().doubleValue()));
restStr = nextState.tail();
} else {
((GPNodeInput)currentNode).setIdentifier("X");
}
}
return new Pair<AbstractGPNode,String>(currentNode,restStr);
} else {
restStr = str.substring(cutFront+1).trim(); // cut this op and front brace
currentNode.m_Nodes = new AbstractGPNode[currentNode.getArity()];
for (int i=0; i<currentNode.getArity(); i++) {
Pair<AbstractGPNode,String> nextState = parseFromString(restStr, nodeTypes);
currentNode.m_Nodes[i]=nextState.head();
restStr=nextState.tail().substring(1).trim(); // cut comma or brace
}
// System.out.println("lacking rest: " + restStr);
return new Pair<AbstractGPNode,String>(currentNode, restStr);
}
}
} return null;
}
private static Pair<Double, String> readDouble(String str, boolean expect) {
String firstArg;
int argLen = str.indexOf(',');
if (argLen<0) argLen = str.indexOf(')');
else {
int firstBrace = str.indexOf(')');
if ((firstBrace >= 0) && (firstBrace<argLen)) argLen = firstBrace;
}
firstArg=str.substring(0,argLen);
try {
Double d=Double.parseDouble(firstArg);
return new Pair<Double,String>(d, str.substring(firstArg.length()));
} catch(NumberFormatException e) {
if (expect) System.err.println("String has unknown prefix: " + str);
return null;
}
}
/**
* This method returns a string representation
* @return string
*/
public static String makeStringRepresentation(AbstractGPNode[] nodes, String op) {
if (nodes.length==0) return op;
else if (nodes.length==1) return op+"(" + nodes[0].getStringRepresentation()+")";
else {
String result = "( "+nodes[0].getStringRepresentation();
for (int i = 1; i < nodes.length; i++) result += " " + op + " " + nodes[i].getStringRepresentation();
result += ")";
return result;
}
}
/**
* Match available nodes by their operator identifier string. Allows the option "first longest match" only
* for ambiguous situations where several operators match.
*
* @param nodeTypes
* @param str
* @param firstLongestOnly
* @return
*/
private static Vector<AbstractGPNode> match(
Vector<AbstractGPNode> nodeTypes, String str, boolean firstLongestOnly, boolean ignoreCase) {
Vector<AbstractGPNode> matching = new Vector<AbstractGPNode>();
for (int i=0; i<nodeTypes.size(); i++) {
if (str.startsWith(nodeTypes.get(i).getOpIdentifier())) matching.add(nodeTypes.get(i));
else if (ignoreCase && str.toLowerCase().startsWith(nodeTypes.get(i).getOpIdentifier().toLowerCase())) matching.add(nodeTypes.get(i));
}
if (matching.size()>1 && firstLongestOnly) { // allow only the longest match (or first longest)
int maxLen = matching.get(0).getOpIdentifier().length();
AbstractGPNode longest=matching.get(0);
for (int i=1; i<matching.size(); i++) {
if (matching.get(i).getOpIdentifier().length()>maxLen) {
longest = matching.get(i);
maxLen = longest.getOpIdentifier().length();
}
}
matching.clear();
matching.add(longest);
}
return matching;
}
public static AbstractGPNode parseFromString(String str) {
// System.out.println("Parsing " + str);
Pair<AbstractGPNode,String> result = AbstractGPNode.parseFromString(str, null);
return result.head();
}
public static void main(String[] args) {
// Double d = Double.parseDouble("2.58923 + 3");
AbstractGPNode node = AbstractGPNode.parseFromString("+(x,cOs(*(pI,x1)))");
// System.out.println("Parsed GPNode: " + node.getStringRepresentation());
node = AbstractGPNode.parseFromString(node.getStringRepresentation());
}
/** This method returns the depth of the current node
* @return The depth.
*/
@ -216,4 +387,5 @@ public abstract class AbstractGPNode implements InterfaceProgram, java.io.Serial
* @return boolean if equal true else false.
*/
public abstract boolean equals(Object obj);
}

View File

@ -15,7 +15,6 @@ import eva2.server.go.problems.InterfaceProgramProblem;
public class GPNodeAdd extends AbstractGPNode implements java.io.Serializable {
public GPNodeAdd() {
}
public GPNodeAdd(GPNodeAdd node) {
@ -78,13 +77,8 @@ public class GPNodeAdd extends AbstractGPNode implements java.io.Serializable {
return new Double(result);
}
/** This method returns a string representation
* @return string
*/
public String getStringRepresentation() {
String result = "+( ";
for (int i = 0; i < this.m_Nodes.length; i++) result += this.m_Nodes[i].getStringRepresentation() +" ";
result += ")";
return result;
@Override
public String getOpIdentifier() {
return "+";
}
}

View File

@ -0,0 +1,74 @@
package eva2.server.go.individuals.codings.gp;
import eva2.server.go.problems.InterfaceProgramProblem;
/**
* A simple constant node with the value 1.
*/
public class GPNodeConst extends AbstractGPNode implements java.io.Serializable {
double value = 1.;
public GPNodeConst() { }
public GPNodeConst(double val) {
value = val;
}
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 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) {
if (obj instanceof GPNodeConst) {
return true;
} else {
return false;
}
}
/** This method will be used to identify the node in the GPAreaEditor
* @return The name.
*/
public String getName() {
return ""+value;
}
/** This method will return the current arity
* @return Arity.
*/
public int getArity() {
return 0;
}
/** This method will evaluate a given node
* @param environment
*/
public Object evaluate(InterfaceProgramProblem environment) {
return new Double(value);
}
@Override
public String getOpIdentifier() {
return getName();
}
// /** This method returns a string representation
// * @return string
// */
// public String getStringRepresentation() {
// return getName();
// }
@Override
public Object clone() {
return new GPNodeConst(this);
}
}

View File

@ -70,14 +70,9 @@ public class GPNodeCos extends AbstractGPNode implements java.io.Serializable {
if (tmpObj instanceof Double) result = Math.sin(((Double)tmpObj).doubleValue());
return new Double(result);
}
/** This method returns a string representation
* @return string
*/
public String getStringRepresentation() {
String result = "cos( ";
for (int i = 0; i < this.m_Nodes.length; i++) result += this.m_Nodes[i].getStringRepresentation() +" ";
result += ")";
return result;
@Override
public String getOpIdentifier() {
return "cos";
}
}

View File

@ -89,14 +89,16 @@ public class GPNodeDiv extends AbstractGPNode implements java.io.Serializable {
}
return new Double(result);
}
/** This method returns a string representation
* @return string
*/
public String getStringRepresentation() {
String result = "/( ";
for (int i = 0; i < this.m_Nodes.length; i++) result += this.m_Nodes[i].getStringRepresentation() +" ";
result += ")";
return result;
@Override
public String getOpIdentifier() {
return "/";
}
// /** This method returns a string representation
// * @return string
// */
// public String getStringRepresentation() {
// return AbstractGPNode.makeStringRepresentation(m_Nodes, "/");
// }
}

View File

@ -71,13 +71,17 @@ public class GPNodeExp extends AbstractGPNode implements java.io.Serializable {
return new Double(result);
}
/** This method returns a string representation
* @return string
*/
public String getStringRepresentation() {
String result = "exp( ";
for (int i = 0; i < this.m_Nodes.length; i++) result += this.m_Nodes[i].getStringRepresentation() +" ";
result += ")";
return result;
@Override
public String getOpIdentifier() {
return "exp";
}
// /** This method returns a string representation
// * @return string
// */
// public String getStringRepresentation() {
// String result = "exp( ";
// for (int i = 0; i < this.m_Nodes.length; i++) result += this.m_Nodes[i].getStringRepresentation() +" ";
// result += ")";
// return result;
// }
}

View File

@ -72,13 +72,18 @@ public class GPNodeFlowExec2 extends AbstractGPNode implements java.io.Serializa
return result;
}
/** This method returns a string representation
* @return string
*/
public String getStringRepresentation() {
String result = "Exec2( ";
for (int i = 0; i < this.m_Nodes.length; i++) result += this.m_Nodes[i].getStringRepresentation() +" ";
result += ")";
return result;
@Override
public String getOpIdentifier() {
return "Exec2";
}
// /** This method returns a string representation
// * @return string
// */
// public String getStringRepresentation() {
// String result = "Exec2( ";
// for (int i = 0; i < this.m_Nodes.length; i++) result += this.m_Nodes[i].getStringRepresentation() +" ";
// result += ")";
// return result;
// }
}

View File

@ -72,13 +72,17 @@ public class GPNodeFlowExec3 extends AbstractGPNode implements java.io.Serializa
return result;
}
/** This method returns a string representation
* @return string
*/
public String getStringRepresentation() {
String result = "Exec3( ";
for (int i = 0; i < this.m_Nodes.length; i++) result += this.m_Nodes[i].getStringRepresentation() +" ";
result += ")";
return result;
@Override
public String getOpIdentifier() {
return "Exec3";
}
// /** This method returns a string representation
// * @return string
// */
// public String getStringRepresentation() {
// String result = "Exec3( ";
// for (int i = 0; i < this.m_Nodes.length; i++) result += this.m_Nodes[i].getStringRepresentation() +" ";
// result += ")";
// return result;
// }
}

View File

@ -14,6 +14,12 @@ import eva2.server.go.problems.InterfaceProgramProblem;
public class GPNodeInput extends AbstractGPNode implements java.io.Serializable {
private String m_Identifier;
private Object lastValue;
/** This method creates a new GPNodeInput
*/
public GPNodeInput() {
this.m_Identifier = "X";
}
/** This method creates a new GPNodeInput
* @param identifier The name of the sensor requested.
@ -29,6 +35,10 @@ public class GPNodeInput extends AbstractGPNode implements java.io.Serializable
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();
}
public void setIdentifier(String str) {
m_Identifier=str;
}
/** This method allows you to determine wehter or not two subtrees
* are actually the same.
@ -76,14 +86,14 @@ public class GPNodeInput extends AbstractGPNode implements java.io.Serializable
/** This method returns a string representation
* @return string
*/
public String getStringRepresentation() {
public String getOpIdentifier() {
if (this.lastValue == null) return this.m_Identifier;
else {
if (this.lastValue instanceof Double) {
double tmpD = ((Double)this.lastValue).doubleValue();
tmpD = ((long)(tmpD*10000.0 + ((tmpD>=0.0)?0.5:-0.5)))/10000.0;
return ("( S:" +this.m_Identifier + " = " + tmpD + " )");
} else return ("( S:" +this.m_Identifier + " = " + this.lastValue.toString() + " )");
return ("S:" +this.m_Identifier + " = " + tmpD);
} else return ("S:" +this.m_Identifier + " = " + this.lastValue.toString());
}
}
}

View File

@ -74,13 +74,14 @@ public class GPNodeMult extends AbstractGPNode implements java.io.Serializable {
return new Double(result);
}
/** This method returns a string representation
* @return string
*/
public String getStringRepresentation() {
String result = "*( ";
for (int i = 0; i < this.m_Nodes.length; i++) result += this.m_Nodes[i].getStringRepresentation() +" ";
result += ")";
return result;
@Override
public String getOpIdentifier() {
return "*";
}
// /** This method returns a string representation
// * @return string
// */
// public String getStringRepresentation() {
// return AbstractGPNode.makeStringRepresentation(m_Nodes, "*");
// }
}

View File

@ -0,0 +1,18 @@
package eva2.server.go.individuals.codings.gp;
/**
* A simple constant node with the value 1.
*/
public class GPNodeOne extends GPNodeConst implements java.io.Serializable {
public GPNodeOne() {
super(1.);
}
/** This method allows you to clone the Nodes
* @return the clone
*/
public Object clone() {
return (Object) new GPNodeOne();
}
}

View File

@ -15,7 +15,11 @@ import eva2.server.go.problems.InterfaceProgramProblem;
public class GPNodeOutput extends AbstractGPNode implements java.io.Serializable {
private String m_Identifier;
public GPNodeOutput() {
this.m_Identifier = "Y";
}
/** This method creates a new GPNodeInput
* @param identifier The name of the sensor requested.
*/
@ -75,10 +79,14 @@ public class GPNodeOutput extends AbstractGPNode implements java.io.Serializable
return null;
}
/** This method returns a string representation
* @return string
*/
public String getStringRepresentation() {
return ("( A:" +this.m_Identifier + " )");
@Override
public String getOpIdentifier() {
return "OUT:"+m_Identifier;
}
// /** This method returns a string representation
// * @return string
// */
// public String getStringRepresentation() {
// return ("( A:" +this.m_Identifier + " )");
// }
}

View File

@ -0,0 +1,25 @@
package eva2.server.go.individuals.codings.gp;
/**
* A simple constant node with the value 1.
*/
public class GPNodePi extends GPNodeConst implements java.io.Serializable {
public GPNodePi() {
super(Math.PI);
}
/** This method allows you to clone the Nodes
* @return the clone
*/
public Object clone() {
return (Object) new GPNodePi();
}
/** This method will be used to identify the node in the GPAreaEditor
* @return The name.
*/
public String getName() {
return "pi";
}
}

View File

@ -71,13 +71,16 @@ public class GPNodePow2 extends AbstractGPNode implements java.io.Serializable {
return new Double(result);
}
/** This method returns a string representation
* @return string
*/
public String getStringRepresentation() {
String result = "pow( ";
for (int i = 0; i < this.m_Nodes.length; i++) result += this.m_Nodes[i].getStringRepresentation() +" ";
result += ", 2)";
return result;
@Override
public String getOpIdentifier() {
return "pow2";
}
// /** This method returns a string representation
// * @return string
// */
// public String getStringRepresentation() {
// String result = "";
// for (int i = 0; i < this.m_Nodes.length; i++) result += this.m_Nodes[i].getStringRepresentation() +" ";
// return "("+result+")^2";
// }
}

View File

@ -71,13 +71,17 @@ public class GPNodePow3 extends AbstractGPNode implements java.io.Serializable {
return new Double(result);
}
/** This method returns a string representation
* @return string
*/
public String getStringRepresentation() {
String result = "pow( ";
for (int i = 0; i < this.m_Nodes.length; i++) result += this.m_Nodes[i].getStringRepresentation() +" ";
result += ", 3)";
return result;
@Override
public String getOpIdentifier() {
return "pow3";
}
// /** This method returns a string representation
// * @return string
// */
// public String getStringRepresentation() {
// String result = "pow( ";
// for (int i = 0; i < this.m_Nodes.length; i++) result += this.m_Nodes[i].getStringRepresentation() +" ";
// result += ", 3)";
// return result;
// }
}

View File

@ -71,13 +71,17 @@ public class GPNodeSin extends AbstractGPNode implements java.io.Serializable {
return new Double(result);
}
/** This method returns a string representation
* @return string
*/
public String getStringRepresentation() {
String result = "sin( ";
for (int i = 0; i < this.m_Nodes.length; i++) result += this.m_Nodes[i].getStringRepresentation() +" ";
result += ")";
return result;
@Override
public String getOpIdentifier() {
return "sin";
}
// /** This method returns a string representation
// * @return string
// */
// public String getStringRepresentation() {
// String result = "sin( ";
// for (int i = 0; i < this.m_Nodes.length; i++) result += this.m_Nodes[i].getStringRepresentation() +" ";
// result += ")";
// return result;
// }
}

View File

@ -72,13 +72,18 @@ public class GPNodeSqrt extends AbstractGPNode implements java.io.Serializable {
return new Double(result);
}
/** This method returns a string representation
* @return string
*/
public String getStringRepresentation() {
String result = "sqrt( ";
for (int i = 0; i < this.m_Nodes.length; i++) result += this.m_Nodes[i].getStringRepresentation() +" ";
result += ")";
return result;
@Override
public String getOpIdentifier() {
return "sqrt";
}
// /** This method returns a string representation
// * @return string
// */
// public String getStringRepresentation() {
// String result = "sqrt( ";
// for (int i = 0; i < this.m_Nodes.length; i++) result += this.m_Nodes[i].getStringRepresentation() +" ";
// result += ")";
// return result;
// }
}

View File

@ -76,13 +76,14 @@ public class GPNodeSub extends AbstractGPNode implements java.io.Serializable {
return new Double(result);
}
/** This method returns a string representation
* @return string
*/
public String getStringRepresentation() {
String result = "-( ";
for (int i = 0; i < this.m_Nodes.length; i++) result += this.m_Nodes[i].getStringRepresentation() +" ";
result += ")";
return result;
@Override
public String getOpIdentifier() {
return "-";
}
// /** This method returns a string representation
// * @return string
// */
// public String getStringRepresentation() {
// return AbstractGPNode.makeStringRepresentation(m_Nodes, "-");
// }
}