I am using ECJ (an evolutionary algorithms package) and I want to call certain variables that are in an extended class but I can’t figure out how to get to them from the problem class.
As you can see in the problem class below I want to be able to call the variable x in the NetworkGene class but it doesn’t work because I end up in the VectorGene class in which I can only call variable y.
class Problem {
double fitness = 0;
public void evaluate(final Individual ind){
if(!(ind. instanceOf GeneVectorIndividual)){
state.output.fatal("Not a GeneVectorIndividual",null);
}
fitness = 0;
for(){
fitness += ind.genome[i].x;
}
}
}
public abstrabct class VectorIndividual extends Individual{
}
public class GeneVectorIndividual extends VectorIndividual{
VectorGene[] genome;
}
public abstract class VectorGene implements Prototype {
double y;
}
public class NetworkGene extends VectorGene{
double x;
}
Classic polymorphism problem.
The quick and dirty solution is this: in the
forloop in classProblem, add these linesBut probably there’s a design problem you need to solve:
Why is
ind.genomean array of VectorGenes instead of NetworkGenes?What does
xrepresent? Is there something you could express withinVectorGenelike a methodgetSomeValue()which inNetworkGeneyou would implement to returnx(and in other subclasses you’d implement it to return some other appropriate value)?Do you really need the inheritance relationship between
VectorGeneandNetworkGene– does it relate to some difference that you actually need to exploit in the problem you are currently trying to solve? Couldn’t you just have a single class containing propertiesxandy?