KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > perf > TestOverallPerformance


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 org.jgap.perf;
11
12 import java.util.*;
13 import org.jgap.*;
14 import org.jgap.impl.*;
15
16 /**
17  * This class provides an implementation of an overall performance test.
18  * To obtain this, the provided example has been modified slightly, regarding
19  * the random number generator. We use a static number generator here which does
20  * not deserve the name "random generator". With that we have a determined
21  * calculation path that results in reproducable results.
22  * By executing the example several times we get a performance measurement.
23  * The measured time has to be compared to other results manually as with
24  * different hardware equipment the numbers vary a lot.
25  *
26  * @author Klaus Meffert
27  * @since 2.0
28  */

29 public class TestOverallPerformance {
30   /** String containing the CVS revision. Read out via reflection!*/
31   private final static String JavaDoc CVS_REVISION = "$Revision: 1.5 $";
32
33   /**
34    * The total number of times we'll let the population evolve.
35    */

36   private static final int MAX_ALLOWED_EVOLUTIONS = 1000;
37
38   /**
39    * Executes the genetic algorithm to determine the minimum number of
40    * coins necessary to make up the given target amount of change. The
41    * solution will then be written to System.out.
42    *
43    * @param a_targetChangeAmount the target amount of change for which this
44    * method is attempting to produce the minimum number of coins
45    *
46    * @throws Exception
47    *
48    * @author Klaus Meffert
49    * @since 2.0
50    */

51   public void makeChangeForAmount(int a_targetChangeAmount)
52       throws Exception JavaDoc {
53     // Start with a DefaultConfiguration, which comes setup with the
54
// most common settings.
55
// -------------------------------------------------------------
56
Configuration.reset();
57     Configuration conf = new DefaultConfiguration();
58     RandomGeneratorForTest gen = new RandomGeneratorForTest();
59     gen.setNextDouble(0.5d);
60     gen.setNextBoolean(true);
61     gen.setNextInt(3);
62     gen.setNextFloat(0.7f);
63     gen.setNextLong(6);
64     conf.setRandomGenerator(gen);
65     // Set the fitness function we want to use, which is our
66
// MinimizingMakeChangeFitnessFunction. We construct it with
67
// the target amount of change passed in to this method.
68
// ---------------------------------------------------------
69
FitnessFunction myFunc =
70         new TestOverallPerformanceFitnessFunc(a_targetChangeAmount);
71     conf.setFitnessFunction(myFunc);
72     // Now we need to tell the Configuration object how we want our
73
// Chromosomes to be setup. We do that by actually creating a
74
// sample Chromosome and then setting it on the Configuration
75
// object. As mentioned earlier, we want our Chromosomes to each
76
// have four genes, one for each of the coin types. We want the
77
// values (alleles) of those genes to be integers, which represent
78
// how many coins of that type we have. We therefore use the
79
// IntegerGene class to represent each of the genes. That class
80
// also lets us specify a lower and upper bound, which we set
81
// to sensible values for each coin type.
82
// Here we use "fantasy" coins just to have more genes and bloat the time
83
// consumed for test performance test!
84
// --------------------------------------------------------------
85
Gene[] sampleGenes = new Gene[10];
86     sampleGenes[0] = new IntegerGene(conf, 0, 3); // Quarters
87
sampleGenes[1] = new IntegerGene(conf, 0, 2); // Dimes
88
sampleGenes[2] = new IntegerGene(conf, 0, 1); // Nickels
89
sampleGenes[3] = new IntegerGene(conf, 0, 4); // Pennies
90
sampleGenes[4] = new IntegerGene(conf, 0, 3); // A
91
sampleGenes[5] = new IntegerGene(conf, 0, 1); // B
92
sampleGenes[6] = new IntegerGene(conf, 0, 1); // C
93
sampleGenes[7] = new IntegerGene(conf, 0, 2); // D
94
sampleGenes[8] = new IntegerGene(conf, 0, 3); // E
95
sampleGenes[9] = new IntegerGene(conf, 0, 1); // F
96
Chromosome sampleChromosome = new Chromosome(conf, sampleGenes);
97     conf.setSampleChromosome(sampleChromosome);
98     // Finally, we need to tell the Configuration object how many
99
// Chromosomes we want in our population. The more Chromosomes,
100
// the larger number of potential solutions (which is good for
101
// finding the answer), but the longer it will take to evolve
102
// the population (which could be seen as bad). We'll just set
103
// the population size to 10000 here. It is that big because of performance
104
// test issues!
105
// ------------------------------------------------------------
106
conf.setPopulationSize(10000);
107     // Create random initial population of Chromosomes.
108
// ------------------------------------------------
109
Genotype population = Genotype.randomInitialGenotype(conf);
110     // Evolve the population. Since we don't know what the best answer
111
// is going to be, we just evolve the max number of times.
112
// ---------------------------------------------------------------
113
for (int i = 0; i < MAX_ALLOWED_EVOLUTIONS; i++) {
114       population.evolve();
115     }
116     // Determine the best solution we found.
117
// -------------------------------------
118
population.getFittestChromosome();
119   }
120
121   /**
122    * Execute the performance test.
123    *
124    * @param args ignored
125    * @throws Exception
126    *
127    * @author Klaus Meffert
128    * @since 2.0
129    */

130   public static void main(String JavaDoc[] args)
131       throws Exception JavaDoc {
132     final int amount = 287;
133     final int numRuns = 20;
134     long starttime, timeMillis;
135     System.out.println("Test started.");
136     // get current time
137
starttime = getCurrentMilliseconds();
138     for (int i = 0; i < numRuns; i++) {
139       TestOverallPerformance runner = new TestOverallPerformance();
140       runner.makeChangeForAmount(amount);
141     }
142     // calculate time of run
143
timeMillis = getCurrentMilliseconds() - starttime;
144     System.out.println("Overall time needed for executing performance test: "
145                        + timeMillis + " [millisecs]");
146   }
147
148   /**
149    * @return current time in milliseconds
150    */

151   private static long getCurrentMilliseconds() {
152     Calendar cal = Calendar.getInstance(TimeZone.getDefault());
153     return cal.getTimeInMillis();
154   }
155 }
156
Popular Tags