New GP node and help-file for GenericConstraint.

This commit is contained in:
Marcel Kronfeld
2009-10-19 10:04:05 +00:00
parent e56af4fb60
commit ca490cf482
3 changed files with 165 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
package eva2.server.go.individuals.codings.gp;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Vector;
@@ -10,6 +11,7 @@ import eva2.server.go.problems.GPFunctionProblem;
import eva2.server.go.problems.InterfaceProgramProblem;
import eva2.tools.Mathematics;
import eva2.tools.Pair;
import eva2.tools.ReflectPackage;
/** This gives an abstract node, with default functionality for get and set methods.
@@ -271,10 +273,37 @@ public abstract class AbstractGPNode implements InterfaceProgram, java.io.Serial
test("-(*(x1,x2),*(5,*(x3,x4)))", solG13);
test("+(pow3(x0),+(pow3(x1),1))", solG13);
System.out.println("" + Math.exp(Mathematics.product(solG13)));
test("+(sum(x),abs(sin(*(x0,x3))))", solG5);
test("-(abs(sum(x)),*(abs(-7.5),n))", solG5);
test("-(sum(x),*(7.5,n))", solG5);
System.out.println(createNodeList());
}
/**
* Print all operator identifiers with arities.
*
* @return
*/
public static String createNodeList() {
String ret = new String();
Class<?> cls = AbstractGPNode.class;
Class<?>[] nodes = ReflectPackage.getAssignableClassesInPackage(cls.getPackage().getName(), AbstractGPNode.class, true, false);
for (Class<?> c : nodes) {
if (Modifier.isAbstract(c.getModifiers()) || c.isInterface()) continue;
AbstractGPNode node;
try {
node = (AbstractGPNode)c.newInstance();
ret = ret + " (" + node.getOpIdentifier() + "," + node.getArity() + ")";
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return ret;
}
public static void test(String constr, double[] pos) {
AbstractGPNode node = AbstractGPNode.parseFromString(constr);
GPFunctionProblem func = new GPFunctionProblem(node, null, pos.length, 0., 0.);

View File

@@ -0,0 +1,79 @@
package eva2.server.go.individuals.codings.gp;
import eva2.server.go.problems.InterfaceProgramProblem;
/**
* A node for retrieving the absolute value
*
*/
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 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 GPNodeAbs) {
GPNodeAbs node = (GPNodeAbs)obj;
if (this.m_Nodes.length != node.m_Nodes.length) return false;
for (int i = 0; i < this.m_Nodes.length; i++) {
if (!this.m_Nodes[i].equals(node.m_Nodes[i])) return false;
}
return true;
} else {
return false;
}
}
/** This method will be used to identify the node in the GPAreaEditor
* @return The name.
*/
public String getName() {
return "Abs";
}
/** This method allows you to clone the Nodes
* @return the clone
*/
public Object clone() {
return (Object) new GPNodeAbs(this);
}
/** This method will return the current arity
* @return Arity.
*/
public int getArity() {
return 1;
}
/** This method will evaluate a given node
* @param environment
*/
public Object evaluate(InterfaceProgramProblem environment) {
Object tmpObj;
double result = 0;
tmpObj = this.m_Nodes[0].evaluate(environment);
if (tmpObj instanceof Double) result += ((Double)tmpObj).doubleValue();
Double ret = new Double(result);
if (ret<0) return -ret;
else return ret;
}
@Override
public String getOpIdentifier() {
return "abs";
}
}