KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > config > MaximizingFunction


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package examples.config;
11
12 import org.jgap.*;
13 import org.jgap.data.config.*;
14 import org.jgap.event.*;
15 import org.jgap.impl.*;
16
17 /**
18  * This class is to test the working of JGAP with a config file provided for
19  * configuring JGAP.
20  * The problem statement is to maximize the value of the function
21  * f(a,b,c) = (a - b + c). This is trivial but works fine for the purpose of
22  * demonstration of the working of JGAP with a config file. Reasonable bounds
23  * have been set up for the values of a, b and c.
24  *
25  * @author Siddhartha Azad
26  * @since 2.3
27  * */

28 public class MaximizingFunction {
29   /** String containing the CVS revision. Read out via reflection!*/
30   private final static String JavaDoc CVS_REVISION = "$Revision: 1.7 $";
31
32   /**
33    * Default Constructor
34    * @author Siddhartha Azad
35    * @since 2.3
36    * */

37   public MaximizingFunction() {
38   }
39
40   /**
41    * Starting the example
42    * @param args not used
43    * @throws Exception
44    *
45    * @author Siddhartha Azad
46    * @since 2.3
47    */

48   public static void main(String JavaDoc args[]) throws Exception JavaDoc{
49     Configuration conf;
50     try {
51       conf = new Configuration("jgapTest.con", false);
52     }
53     catch (Exception JavaDoc ex) {
54       ex.printStackTrace();
55       return;
56     }
57     // set up a sample chromosome
58
Gene[] sampleGenes = new Gene[3];
59     sampleGenes[0] = new IntegerGene(conf, 60, 100);
60     sampleGenes[1] = new IntegerGene(conf, 1, 50);
61     sampleGenes[2] = new IntegerGene(conf, 100, 150);
62     IChromosome sampleChromosome = new Chromosome(conf, sampleGenes);
63     Genotype population;
64     FitnessFunction fitFunc = new MaximizingFunctionFitnessFunction();
65     try {
66       conf.setFitnessFunction(fitFunc);
67       // The higher the value, the better
68
conf.setFitnessEvaluator(new DefaultFitnessEvaluator());
69       conf.setSampleChromosome(sampleChromosome);
70       BestChromosomesSelector bestChromsSelector = new
71           BestChromosomesSelector(conf, 1.0d);
72       bestChromsSelector.setDoubletteChromosomesAllowed(false);
73       conf.addNaturalSelector(bestChromsSelector, true);
74       conf.setRandomGenerator(new StockRandomGenerator());
75       conf.setEventManager(new EventManager());
76       conf.addGeneticOperator(new CrossoverOperator(conf));
77       conf.addGeneticOperator(new MutationOperator(conf, 15));
78       population = Genotype.randomInitialGenotype(conf);
79     }
80     catch (InvalidConfigurationException icEx) {
81       icEx.printStackTrace();
82       return;
83     }
84     // We expect the rest of the config parameter, for example the population
85
// size, to be set via the config file
86

87     // Evolve the population
88
for (int i = 0; i < 10; i++) {
89       population.evolve();
90     }
91     IChromosome bestSolutionSoFar = population.getFittestChromosome();
92     System.out.println("The best solution has a fitness value of " +
93                        bestSolutionSoFar.getFitnessValue());
94     Integer JavaDoc aVal = (Integer JavaDoc) bestSolutionSoFar.getGene(0).getAllele();
95     Integer JavaDoc bVal = (Integer JavaDoc) bestSolutionSoFar.getGene(1).getAllele();
96     Integer JavaDoc cVal = (Integer JavaDoc) bestSolutionSoFar.getGene(2).getAllele();
97     System.out.println("a = " + aVal.intValue());
98     System.out.println("b = " + bVal.intValue());
99     System.out.println("c = " + cVal.intValue());
100   }
101 }
102
Popular Tags