01: /*
02: * This file is part of JGAP.
03: *
04: * JGAP offers a dual license model containing the LGPL as well as the MPL.
05: *
06: * For licensing information please see the file license.txt included with JGAP
07: * or have a look at the top of class org.jgap.Chromosome which representatively
08: * includes the JGAP license policy applicable for any file delivered with JGAP.
09: */
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
25: * @since 2.0
26: */
27: public class SupergeneSample extends AbstractSupergeneTest {
28: /** String containing the CVS revision. Read out via reflection!*/
29: private final static String CVS_REVISION = "$Revision: 1.3 $";
30:
31: /**
32: * Executes the genetic algorithm to determine the minimum number of
33: * coins necessary to make up the given target amount of change. The
34: * solution will then be written to System.out.
35: *
36: * @param a_targetChangeAmount the target amount of change for which this
37: * method is attempting to produce the minimum number of coins
38: *
39: * @return absolute difference between the required and computed change
40: * amount
41: * @throws Exception
42: */
43: public int makeChangeForAmount(int a_targetChangeAmount)
44: throws Exception {
45: // Start with a DefaultConfiguration, which comes setup with the
46: // most common settings.
47: // -------------------------------------------------------------
48: Configuration conf = new DefaultConfiguration();
49: // Set the fitness function we want to use, which is our
50: // MinimizingMakeChangeFitnessFunction. We construct it with
51: // the target amount of change passed in to this method.
52: // ---------------------------------------------------------
53: SupergeneChangeFitnessFunction fitnessFunction = new SupergeneChangeFitnessFunction(
54: a_targetChangeAmount);
55: conf.setFitnessFunction(fitnessFunction);
56: conf.setKeepPopulationSizeConstant(false);
57: // Now we need to tell the Configuration object how we want our
58: // Chromosomes to be setup. We do that by actually creating a
59: // sample Chromosome and then setting it on the Configuration
60: // object. As mentioned earlier, we want our Chromosomes to each
61: // have four genes, one for each of the coin types. We want the
62: // values (alleles) of those genes to be integers, which represent
63: // how many coins of that type we have. We therefore use the
64: // IntegerGene class to represent each of the genes. That class
65: // also lets us specify a lower and upper bound, which we set
66: // to sensible values for each coin type.
67: // --------------------------------------------------------------
68: Gene[] sampleGenes = new Gene[3];
69: sampleGenes[DIMES] = getDimesGene(conf);
70: sampleGenes[QUARTERS] = getQuartersGene(conf);
71: sampleGenes[2] = new NickelsPenniesSupergene(conf, new Gene[] {
72: getNickelsGene(conf), getPenniesGene(conf), });
73: int s = solve(conf, a_targetChangeAmount, fitnessFunction,
74: sampleGenes);
75: return s;
76: }
77:
78: public static void main(String[] args) {
79: SupergeneSample test = new SupergeneSample();
80: test.test();
81: }
82: }
|