001: /*
002: * This file is part of JGAP.
003: *
004: * JGAP offers a dual license model containing the LGPL as well as the MPL.
005: *
006: * For licensing information please see the file license.txt included with JGAP
007: * or have a look at the top of class org.jgap.Chromosome which representatively
008: * includes the JGAP license policy applicable for any file delivered with JGAP.
009: */
010: package examples.chromInit;
011:
012: import org.jgap.*;
013: import org.jgap.impl.*;
014:
015: /**
016: * Simple test class that demonstrates how to initialize chromosomes with
017: * different numbers of Genes.
018: *
019: * @author Klaus Meffert
020: * @since 2.4
021: */
022: public class ChromosomeInit {
023: /** String containing the CVS revision. Read out via reflection!*/
024: private static final String CVS_REVISION = "$Revision: 1.7 $";
025:
026: public static void main(String[] args) {
027: int numEvolutions = 500;
028: // Create configuration by using DefaultConfiguration and removing
029: // CrossoverOperator. Removal necessary because that operator does not
030: // work with chromosomes of different gene size!
031: Configuration gaConf = new DefaultConfiguration();
032: // The next statement is dirty and to be avoided outside this example!
033: gaConf.getGeneticOperators().remove(0);
034: gaConf.setPreservFittestIndividual(true);
035: gaConf.setKeepPopulationSizeConstant(false);
036: try {
037: int chromeSize;
038: if (args.length == 0) {
039: chromeSize = 7;
040: } else {
041: chromeSize = Integer.parseInt(args[0]);
042: }
043: if (chromeSize > 15) {
044: System.err
045: .println("This example does not handle "
046: + "Chromosomes greater than 15 bits in length.");
047: System.exit(-1);
048: }
049: IChromosome sampleChromosome = new Chromosome(gaConf,
050: new BooleanGene(gaConf), chromeSize);
051: gaConf.setSampleChromosome(sampleChromosome);
052: gaConf.setPopulationSize(20);
053: gaConf.setFitnessFunction(new MaxFunction());
054: // Completely initialize the population with custom code.
055: // Notice that we assign the double number of Genes to
056: // each other Chromosome.
057: // ------------------------------------------------------
058: int populationSize = gaConf.getPopulationSize();
059: Population pop = new Population(gaConf, populationSize);
060: for (int i = 0; i < populationSize; i++) {
061: int mult;
062: // Every second Chromosome has double the number of Genes.
063: // -------------------------------------------------------
064: if (i % 2 == 0) {
065: mult = 1;
066: } else {
067: mult = 2;
068: }
069: Gene[] sampleGenes = sampleChromosome.getGenes();
070: Gene[] newGenes = new Gene[sampleGenes.length * mult];
071: RandomGenerator generator = gaConf.getRandomGenerator();
072: for (int j = 0; j < newGenes.length; j = j + mult) {
073: // We use the newGene() method on each of the genes in the
074: // sample Chromosome to generate our new Gene instances for
075: // the Chromosome we're returning. This guarantees that the
076: // new Genes are setup with all of the correct internal state
077: // for the respective gene position they're going to inhabit.
078: // ----------------------------------------------------------
079: newGenes[j] = sampleGenes[j / mult].newGene();
080: // Set the gene's value (allele) to a random value.
081: // ------------------------------------------------
082: newGenes[j].setToRandomValue(generator);
083: if (mult > 1) {
084: newGenes[j + 1] = sampleGenes[j / 2].newGene();
085: // Set the gene's value (allele) to a random value.
086: // ------------------------------------------------
087: newGenes[j + 1].setToRandomValue(generator);
088: }
089: }
090: IChromosome chrom = Chromosome
091: .randomInitialChromosome(gaConf);
092: chrom.setGenes(newGenes);
093: pop.addChromosome(chrom);
094: }
095: // Now we need to construct the Genotype. This could otherwise be
096: // accomplished more easily by writing
097: // "Genotype genotype = Genotype.randomInitialGenotype(...)"
098: Genotype genotype = new Genotype(gaConf, pop);
099: int progress = 0;
100: int percentEvolution = numEvolutions / 100;
101: for (int i = 0; i < numEvolutions; i++) {
102: genotype.evolve();
103: // Print progress.
104: // ---------------
105: if (percentEvolution > 0 && i % percentEvolution == 0) {
106: progress++;
107: IChromosome fittest = genotype
108: .getFittestChromosome();
109: double fitness = fittest.getFitnessValue();
110: System.out.println("Fittest Chromosome has value "
111: + fitness);
112: }
113: }
114: IChromosome fittest = genotype.getFittestChromosome();
115: System.out.println("Fittest Chromosome has value "
116: + fittest.getFitnessValue());
117: } catch (InvalidConfigurationException e) {
118: e.printStackTrace();
119: System.exit(-2);
120: }
121: }
122: }
|