Hi,
I am new to java and genetic programming and trying to write a GA for xor gate using functions and,or,not.
I am getting an error :-
Incorrect number of children for node ! at pop.subpop.0.species.ind, was expecting 1 but got 2.
i would be thankful for any help.
the definitions of some functions and regression class are as follows-
NOT.java-
public class Not extends GPNode
{
public String toString() { return "!"; }
public int expectedChildren() { return 1; }
public void eval(final EvolutionState state,final int thread, final GPData input,final ADFStack stack,
final GPIndividual individual, final Problem problem)
{
boolean result;
//Boolean_Data rd = ((Boolean_Data)(input));
Boolean_Data rd = ((Boolean_Data)(input));
children[0].eval(state,thread,input,stack,individual,problem);
result = !rd.x;
}
}
And.Java
public class And extends GPNode
{
public String toString() { return "&&"; }
public int expectedChildren() { return 2; }
public void checkConstraints(final EvolutionState state,final int tree,
final GPIndividual typicalIndividual,
final Parameter individualBase)
{
super.checkConstraints(state,tree,typicalIndividual,individualBase);
if (children.length!=2)
state.output.error("Incorrect number of children for node " +
toStringForError() + " at " +
individualBase);
}
public void eval(final EvolutionState state,final int thread,
final GPData input,final ADFStack stack,
final GPIndividual individual, final Problem problem)
{
boolean result;
Boolean_Data rd = ((Boolean_Data)(input));
children[0].eval(state,thread,input,stack,individual,problem);
result = rd.x;
children[1].eval(state,thread,input,stack,individual,problem);
rd.x=result && rd.x;
}
}
REGRESSION.java
public class MultiValuedRegression extends GPProblem implements SimpleProblemForm
{
public static final String P_DATA = "data";
public boolean currentX;
public boolean currentY;
public boolean expected_result;
double sum = 0;
int hits=0;
public Object clone()
{
MultiValuedRegression newobj = (MultiValuedRegression) (super.clone());
newobj.input = (Boolean_Data)(input.clone());
return newobj;
}
public void setup(final EvolutionState state,
final Parameter base)
{
super.setup(state,base);
if (!(input instanceof Boolean_Data))
state.output.fatal("GPData class must subclass from " + Boolean_Data.class,
base.push(P_DATA), null);
}
public void evaluate(final EvolutionState state, final Individual ind, final int subpopulation, final int threadnum)
{
Boolean_Data input = (Boolean_Data)(this.input);
sum = 0;
hits=0;
currentX=false;
currentY=false;
expected_result= true;
((GPIndividual)ind).trees[0].child.eval(
state,threadnum,input,stack,((GPIndividual)ind),this);
if(expected_result == input.x)
{
sum++;
hits++;
}
currentX=true;
currentY=false;
expected_result = false;
((GPIndividual)ind).trees[0].child.eval(
state,threadnum,input,stack,((GPIndividual)ind),this);
if(expected_result == input.x)
{
sum++;
hits++;
}
currentX=false;
currentY=true;
expected_result = false;
((GPIndividual)ind).trees[0].child.eval(
state,threadnum,input,stack,((GPIndividual)ind),this);
if(expected_result == input.x)
{
sum++;
hits++;
}
currentX=false;
currentY=false;
expected_result = true;
((GPIndividual)ind).trees[0].child.eval(
state,threadnum,input,stack,((GPIndividual)ind),this);
if(expected_result == input.x)
{
sum++;
hits++;
}
KozaFitness f = ((KozaFitness)ind.fitness);
f.setStandardizedFitness(state, sum/4);
f.hits = hits;
ind.evaluated = true;
}
}