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: * Computes the optimal change with the same condition as
17: * SupergeneTest, but without using supergenes. Implemented
18: * to compare the performance.
19: * To test the Supergene, we created the "makechange" version with
20: * additional condition: the number of nickels and pennies must be
21: * both even or both odd. The supergene encloses two genes
22: * (nickels and pennies) and is valid if the condition above is
23: * satisfied.
24: *
25: * @author Audrius Meskauskas
26: * @author Klaus Meffert
27: */
28: class WithoutSupergeneSample extends SupergeneSample {
29: /** String containing the CVS revision. Read out via reflection!*/
30: private final static String CVS_REVISION = "0.0.0 alpha explosive";
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: * @return absolute difference between the required and computed change
40: * @throws Exception
41: */
42: public int makeChangeForAmount(int a_targetChangeAmount)
43: throws Exception {
44: // Start with a DefaultConfiguration, which comes setup with the
45: // most common settings.
46: // -------------------------------------------------------------
47: Configuration conf = new DefaultConfiguration();
48: // Set the fitness function we want to use. We construct it with
49: // the target amount of change passed in to this method.
50: // ---------------------------------------------------------
51: WithoutSupergeneChangeFitFForTesting fitnessFunction = new WithoutSupergeneChangeFitFForTesting(
52: a_targetChangeAmount);
53: conf.setFitnessFunction(fitnessFunction);
54: // Now we need to tell the Configuration object how we want our
55: // Chromosomes to be setup. We do that by actually creating a
56: // sample Chromosome and then setting it on the Configuration
57: // object. As mentioned earlier, we want our Chromosomes to each
58: // have four genes, one for each of the coin types. We want the
59: // values (alleles) of those genes to be integers, which represent
60: // how many coins of that type we have. We therefore use the
61: // IntegerGene class to represent each of the genes. That class
62: // also lets us specify a lower and upper bound, which we set
63: // to sensible values for each coin type.
64: // --------------------------------------------------------------
65: Gene[] sampleGenes = new Gene[4];
66: sampleGenes[DIMES] = getDimesGene(conf); // Dimes
67: sampleGenes[NICKELS] = getNickelsGene(conf); // Nickels
68: sampleGenes[QUARTERS] = getQuartersGene(conf); // Quarters
69: sampleGenes[PENNIES] = getPenniesGene(conf); // Pennies
70: int s = solve(conf, a_targetChangeAmount, fitnessFunction,
71: sampleGenes);
72: return s;
73: }
74:
75: public static void main(String[] args) {
76: WithoutSupergeneSample test = new WithoutSupergeneSample();
77: test.test();
78: System.exit(0);
79: }
80: }
|