In order to get a feel for the JGAP package I took a look at the
minimizing change code and wrote my own little test program.
I now have a question about evolve() and the fitness function
evaluate() I wrote.
If I have a population of 10, and call evolve(), shouldn't my fitness
function be called a constant time? I would think 10 times.
However, when I run my basic GA my evaluation function gets called
between 10 and 13 times .. I am not quite sure why this would happen.
Likewise if I set the number of evolutions to say 3, I'd expect my
fitness function evaluate() to be called 3 times as much as before, but
that doesn't happen either.
I suspect I do not understand the linkage between evolve and the
fitness function I defined.
Eventually I want to define my own crossover & mutation operators, but
I haven't gotten to the point.
Thanks,
Esmail
--------- sup****ting/additional information ----------------
Some sample output showing the variability in the number evaluate() is
called.
(eval 1) x = 8 fitness = 56.00
(eval 2) x = 9 fitness = 54.00
(eval 3) x = 6 fitness = 54.00
(eval 4) x = 0 fitness = 0.00
(eval 5) x = 15 fitness = 0.00
(eval 6) x = 13 fitness = 26.00
(eval 7) x = 8 fitness = 56.00
(eval 8) x = 8 fitness = 56.00
(eval 9) x = 13 fitness = 26.00
(eval 10) x = 11 fitness = 44.00
Best solution found:8
(eval 1) x = 9 fitness = 54.00
(eval 2) x = 0 fitness = 0.00
(eval 3) x = 12 fitness = 36.00
(eval 4) x = 2 fitness = 26.00
(eval 5) x = 1 fitness = 14.00
(eval 6) x = 5 fitness = 50.00
(eval 7) x = 9 fitness = 54.00
(eval 8) x = 6 fitness = 54.00
(eval 9) x = 5 fitness = 50.00
(eval 10) x = 10 fitness = 50.00
(eval 11) x = 5 fitness = 50.00
(eval 12) x = 6 fitness = 54.00
(eval 13) x = 6 fitness = 54.00
Best solution found: 6
(eval 1) x = 14 fitness = 14.00
(eval 2) x = 6 fitness = 54.00
(eval 3) x = 1 fitness = 14.00
(eval 4) x = 6 fitness = 54.00
(eval 5) x = 14 fitness = 14.00
(eval 6) x = 1 fitness = 14.00
(eval 7) x = 8 fitness = 56.00
(eval 8) x = 13 fitness = 26.00
(eval 9) x = 14 fitness = 14.00
(eval 10) x = 6 fitness = 54.00
(eval 11) x = 14 fitness = 14.00
Best solution found: 6
my short test source code to maximinze function 15x - x**2 over 0 to 15.
------------------------
im****t org.jgap.*;
im****t org.jgap.impl.*;
public class MyGA
{
public static void main(String []args) throws Exception
{
final int MAX_EVOLUTIONS = 1;
final int POP_SIZE = 10;
final int LOW_RANGE = 0;
final int HIGH_RANGE = 15;
Configuration conf = new DefaultConfiguration();
conf.setPreservFittestIndividual(true); // elitism?
// conf.setKeepPopulationSizeConstant(true); already done in
default
config
FitnessFunction myFunc = new MyFitness();
conf.setFitnessFunction(myFunc);
Gene sampleGene = new IntegerGene(conf, LOW_RANGE, HIGH_RANGE);
IChromosome sampleChromosome = new Chromosome(conf, sampleGene,
1);
conf.setSampleChromosome(sampleChromosome);
conf.setPopulationSize(POP_SIZE);
Genotype population = Genotype.randomInitialGenotype(conf);
population.evolve(MAX_EVOLUTIONS);
IChromosome bestSolutionSoFar =
population.getFittestChromosome();
System.out.println("\nBest solution found: " +
bestSolutionSoFar.getGene(0).getAllele());
}
}
------------------------
im****t org.jgap.FitnessFunction;
im****t org.jgap.IChromosome;
public class MyFitness extends FitnessFunction
{
MyFitness()
{
}
public double evaluate(IChromosome a_subject)
{
int x = (Integer) a_subject.getGene(0).getAllele();
double ret_val = 15.0 * x - (x * x);
System.out.printf("\t(eval %3d) x = %3d fitness = %5.2f\n",
count++, x, ret_val);
return ret_val;
}
private int count = 1;
}


|