001: /*
002: * This file is part of JGAP.
003: *
004: * JGAP offers a dual license model containing the LGPL as well as the MPL.
005: *
006: * For licencing information please see the file license.txt included with JGAP
007: * or have a look at the top of class org.jgap.Chromosome which representatively
008: * includes the JGAP license policy applicable for any file delivered with JGAP.
009: */
010: package org.jgap.perf;
011:
012: import java.util.*;
013: import org.jgap.*;
014: import org.jgap.impl.*;
015:
016: /**
017: * This class provides an implementation of an overall performance test.
018: * To obtain this, the provided example has been modified slightly, regarding
019: * the random number generator. We use a static number generator here which does
020: * not deserve the name "random generator". With that we have a determined
021: * calculation path that results in reproducable results.
022: * By executing the example several times we get a performance measurement.
023: * The measured time has to be compared to other results manually as with
024: * different hardware equipment the numbers vary a lot.
025: *
026: * @author Klaus Meffert
027: * @since 2.0
028: */
029: public class TestOverallPerformance {
030: /** String containing the CVS revision. Read out via reflection!*/
031: private final static String CVS_REVISION = "$Revision: 1.6 $";
032:
033: /**
034: * The total number of times we'll let the population evolve.
035: */
036: private static final int MAX_ALLOWED_EVOLUTIONS = 1000;
037:
038: /**
039: * Executes the genetic algorithm to determine the minimum number of
040: * coins necessary to make up the given target amount of change. The
041: * solution will then be written to System.out.
042: *
043: * @param a_targetChangeAmount the target amount of change for which this
044: * method is attempting to produce the minimum number of coins
045: *
046: * @throws Exception
047: *
048: * @author Klaus Meffert
049: * @since 2.0
050: */
051: public void makeChangeForAmount(int a_targetChangeAmount)
052: throws Exception {
053: // Start with a DefaultConfiguration, which comes setup with the
054: // most common settings.
055: // -------------------------------------------------------------
056: Configuration.reset();
057: Configuration conf = new DefaultConfiguration();
058: RandomGeneratorForTesting gen = new RandomGeneratorForTesting();
059: gen.setNextDouble(0.5d);
060: gen.setNextBoolean(true);
061: gen.setNextInt(3);
062: gen.setNextFloat(0.7f);
063: gen.setNextLong(6);
064: conf.setRandomGenerator(gen);
065: // Set the fitness function we want to use, which is our
066: // MinimizingMakeChangeFitnessFunction. We construct it with
067: // the target amount of change passed in to this method.
068: // ---------------------------------------------------------
069: FitnessFunction myFunc = new TestOverallPerformanceFitnessFunc(
070: a_targetChangeAmount);
071: conf.setFitnessFunction(myFunc);
072: // Now we need to tell the Configuration object how we want our
073: // Chromosomes to be setup. We do that by actually creating a
074: // sample Chromosome and then setting it on the Configuration
075: // object. As mentioned earlier, we want our Chromosomes to each
076: // have four genes, one for each of the coin types. We want the
077: // values (alleles) of those genes to be integers, which represent
078: // how many coins of that type we have. We therefore use the
079: // IntegerGene class to represent each of the genes. That class
080: // also lets us specify a lower and upper bound, which we set
081: // to sensible values for each coin type.
082: // Here we use "fantasy" coins just to have more genes and bloat the time
083: // consumed for test performance test!
084: // --------------------------------------------------------------
085: Gene[] sampleGenes = new Gene[10];
086: sampleGenes[0] = new IntegerGene(conf, 0, 3); // Quarters
087: sampleGenes[1] = new IntegerGene(conf, 0, 2); // Dimes
088: sampleGenes[2] = new IntegerGene(conf, 0, 1); // Nickels
089: sampleGenes[3] = new IntegerGene(conf, 0, 4); // Pennies
090: sampleGenes[4] = new IntegerGene(conf, 0, 3); // A
091: sampleGenes[5] = new IntegerGene(conf, 0, 1); // B
092: sampleGenes[6] = new IntegerGene(conf, 0, 1); // C
093: sampleGenes[7] = new IntegerGene(conf, 0, 2); // D
094: sampleGenes[8] = new IntegerGene(conf, 0, 3); // E
095: sampleGenes[9] = new IntegerGene(conf, 0, 1); // F
096: Chromosome sampleChromosome = new Chromosome(conf, sampleGenes);
097: conf.setSampleChromosome(sampleChromosome);
098: // Finally, we need to tell the Configuration object how many
099: // 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[] args) throws Exception {
131: final int amount = 287;
132: final int numRuns = 20;
133: long starttime, timeMillis;
134: System.out.println("Test started.");
135: // get current time
136: starttime = getCurrentMilliseconds();
137: for (int i = 0; i < numRuns; i++) {
138: TestOverallPerformance runner = new TestOverallPerformance();
139: runner.makeChangeForAmount(amount);
140: }
141: // calculate time of run
142: timeMillis = getCurrentMilliseconds() - starttime;
143: System.out
144: .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: }
|