Problems using the bulk fitness function facility

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Problems using the bulk fitness function facility

David Kemp-4
For my particular problem, I need access to the whole population before
I can determine the fitness of an individual, and so I thought that the
easiest approach would be to implement a BulkFitnessFunction.  I had
trouble getting it working, did some debugging, and decided that either
I have misunderstood the intention behind BulkFitnessFunction, or there
is a bug in GABreeder.

After a quick search of this users mailing list, I saw that I am not the
only one (See: Mike Lechner <mike.lechner@gm...>, July 22, 2009,
BulkFitFn doesn't mutate or breed?).  This thread did not seem to come
to a resolution.

Attached is the simpest example that I could think of.  So simple that
it does not actually need  to use a bulk fitness function, but it shows
the problem.  The population consists of two individuals, with one
integer gene each, and a bulk fitness function that simply uses the
value of the gene as its fitness value. If you run it as it is, the two
individuals at the end of the 100 iterations are the same as the two you
start with.

If you uncomment the line where I set the breeder to be my breeder then,
unless you are really unlucky, at least one of the individuals should
have been replaced with a "fitter" one at some stage during the 100
iterations.

Cheers,
David Kemp


package kemp.jgapexample;

import java.util.List;

import org.jgap.BulkFitnessFunction;
import org.jgap.Chromosome;
import org.jgap.Configuration;
import org.jgap.Gene;
import org.jgap.Genotype;
import org.jgap.IChromosome;
import org.jgap.Population;
import org.jgap.impl.DefaultConfiguration;
import org.jgap.impl.GABreeder;
import org.jgap.impl.IntegerGene;

public class ExampleWithBulkFitnessFunction {

    public static void main(String[] args) throws Exception {
        Configuration conf = new DefaultConfiguration();
//        conf.setBreeder(new MyBreeder());
        conf.setBulkFitnessFunction(new MyFitnessFunction());
        conf.setPopulationSize(2);
        conf.setSampleChromosome(new Chromosome(conf, new Gene[] {new IntegerGene(conf, 0, 1000)}));
        Genotype population = Genotype.randomInitialGenotype(conf);
        evolve(population);
    }

    private static void evolve(Genotype population) {
        for (int i = 0; i < 100; i++) {
            population.evolve();
            List<IChromosome> chromosomes = population.getPopulation().getChromosomes();
            System.out.println(String.format("%s, %s",
                    chromosomes.get(0).getGenes()[0].getAllele(),
                    chromosomes.get(1).getGenes()[0].getAllele()));
        }
    }


    private static class MyFitnessFunction extends BulkFitnessFunction {
        @Override
        public void evaluate(Population population) {
            List<IChromosome> chromosomes = population.getChromosomes();
            for (IChromosome chromosome : chromosomes) {
                chromosome.setFitnessValue((Integer) ((IntegerGene) chromosome.getGenes()[0]).getAllele());
            }
        }
    }
   
    private static class MyBreeder extends GABreeder {
        @Override
        protected void updateChromosomes(Population a_pop, Configuration a_conf) {
            int currentPopSize = a_pop.size();
            // Ensure all chromosomes are updated.
            // -----------------------------------
            BulkFitnessFunction bulkFunction = a_conf.getBulkFitnessFunction();
            boolean bulkFitFunc = (bulkFunction != null);
            if (bulkFitFunc) {
                bulkFunction.evaluate(a_pop);
            } else {
              for (int i = 0; i < currentPopSize; i++) {
                IChromosome chrom = a_pop.getChromosome(i);
                chrom.getFitnessValue();
              }
            }
        }
    }
}

------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
jgap-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgap-users