1 10 package examples.distinctGenes; 11 12 import org.jgap.*; 13 import org.jgap.impl.*; 14 15 22 public class Main { 23 24 private static final String CVS_REVISION = "$Revision: 1.2 $"; 25 26 30 public static void main(String [] args) { 31 int numEvolutions = 500; 32 Configuration gaConf = new DefaultConfiguration(); 33 gaConf.reset(); 34 gaConf.setFitnessEvaluator(new DeltaFitnessEvaluator()); 35 gaConf.setPreservFittestIndividual(true); 36 gaConf.setKeepPopulationSizeConstant(false); 37 Genotype genotype = null; 38 final int chromeSize = 41; try { 40 IChromosome sampleChromosome = new MyChromosome(gaConf, 41 new BooleanGene(gaConf), chromeSize); 42 gaConf.setSampleChromosome(sampleChromosome); 43 gaConf.setPopulationSize(20); 44 gaConf.setFitnessFunction(new SampleFitnessFunction()); 45 genotype = Genotype.randomInitialGenotype(gaConf); 46 } 47 catch (InvalidConfigurationException e) { 48 e.printStackTrace(); 49 System.exit( -2); 50 } 51 int progress = 0; 52 int percentEvolution = numEvolutions / 10; 53 for (int i = 0; i < numEvolutions; i++) { 54 genotype.evolve(); 55 if (percentEvolution > 0 && i % percentEvolution == 0) { 58 progress++; 59 IChromosome fittest = genotype.getFittestChromosome(); 60 double fitness = fittest.getFitnessValue(); 61 System.out.println("Generation " + i + 62 ": Currently fittest Chromosome has fitness " + 63 fitness); 64 if (fitness < 0.0001) { 65 break; 66 } 67 } 68 } 69 IChromosome fittest = genotype.getFittestChromosome(); 72 System.out.println("Fittest Chromosome has fitness " + 73 fittest.getFitnessValue()); 74 } 75 } 76 | Popular Tags |