KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > supergene > WithoutSupergeneSample


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  * 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 class WithoutSupergeneSample
26     extends SupergeneSample {
27   /** String containing the CVS revision. Read out via reflection!*/
28   private final static String JavaDoc CVS_REVISION = "0.0.0 alpha explosive";
29
30   /**
31    * Executes the genetic algorithm to determine the minimum number of
32    * coins necessary to make up the given target amount of change. The
33    * solution will then be written to System.out.
34    *
35    * @param a_targetChangeAmount The target amount of change for which this
36    * method is attempting to produce the minimum number of coins
37    * @return absolute difference between the required and computed change
38    * @throws Exception
39    */

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