KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > supergene > SupergeneSample


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.supergene;
11
12 import org.jgap.*;
13 import org.jgap.impl.*;
14
15 /**
16  * To test the Supergene, we created the "make change" version with
17  * additional condition: the number of nickels and pennies must be
18  * both even or both odd. The supergene encloses two genes
19  * (nickels and pennies) and is valid if the condition above is
20  * satisfied.
21  *
22  * @author Neil Rotstan
23  * @author Klaus Meffert
24  * @author Audrius Meskauskas (subsequent adaptation)
25  * @since 2.0
26  */

27 public class SupergeneSample
28     extends AbstractSupergeneTest {
29   /** String containing the CVS revision. Read out via reflection!*/
30   private final static String JavaDoc CVS_REVISION = "$Revision: 1.2 $";
31
32   /**
33    * Executes the genetic algorithm to determine the minimum number of
34    * coins necessary to make up the given target amount of change. The
35    * solution will then be written to System.out.
36    *
37    * @param a_targetChangeAmount the target amount of change for which this
38    * method is attempting to produce the minimum number of coins
39    *
40    * @throws Exception
41    * @return absolute difference between the required and computed change
42    * amount
43    */

44   public int makeChangeForAmount(int a_targetChangeAmount)
45       throws Exception JavaDoc {
46     // Start with a DefaultConfiguration, which comes setup with the
47
// most common settings.
48
// -------------------------------------------------------------
49
Configuration conf = new DefaultConfiguration();
50     // Set the fitness function we want to use, which is our
51
// MinimizingMakeChangeFitnessFunction. We construct it with
52
// the target amount of change passed in to this method.
53
// ---------------------------------------------------------
54
SupergeneChangeFitnessFunction fitnessFunction =
55         new SupergeneChangeFitnessFunction(a_targetChangeAmount);
56     conf.setFitnessFunction(fitnessFunction);
57     conf.setKeepPopulationSizeConstant(false);
58     // Now we need to tell the Configuration object how we want our
59
// Chromosomes to be setup. We do that by actually creating a
60
// sample Chromosome and then setting it on the Configuration
61
// object. As mentioned earlier, we want our Chromosomes to each
62
// have four genes, one for each of the coin types. We want the
63
// values (alleles) of those genes to be integers, which represent
64
// how many coins of that type we have. We therefore use the
65
// IntegerGene class to represent each of the genes. That class
66
// also lets us specify a lower and upper bound, which we set
67
// to sensible values for each coin type.
68
// --------------------------------------------------------------
69
Gene[] sampleGenes = new Gene[3];
70     sampleGenes[DIMES] = getDimesGene(conf);
71     sampleGenes[QUARTERS] = getQuartersGene(conf);
72     sampleGenes[2] = new NickelsPenniesSupergene(conf, new Gene[] {
73         getNickelsGene(conf),
74         getPenniesGene(conf),
75     });
76     int s = solve(conf, a_targetChangeAmount, fitnessFunction, sampleGenes);
77     return s;
78   }
79
80   public static void main(String JavaDoc[] args) {
81     SupergeneSample test = new SupergeneSample();
82     test.test();
83   }
84 }
85
Popular Tags