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 examples.config;
011:
012: import org.jgap.*;
013: import org.jgap.event.*;
014: import org.jgap.impl.*;
015:
016: /**
017: * This class is to test the working of JGAP with a config file provided for
018: * configuring JGAP.
019: * The problem statement is to maximize the value of the function
020: * f(a,b,c) = (a - b + c). This is trivial but works fine for the purpose of
021: * demonstration of the working of JGAP with a config file. Reasonable bounds
022: * have been set up for the values of a, b and c.
023: *
024: * @author Siddhartha Azad
025: * @since 2.3
026: * */
027: public class MaximizingFunction {
028: /** String containing the CVS revision. Read out via reflection!*/
029: private final static String CVS_REVISION = "$Revision: 1.8 $";
030:
031: /**
032: * Default Constructor
033: * @author Siddhartha Azad
034: * @since 2.3
035: * */
036: public MaximizingFunction() {
037: }
038:
039: /**
040: * Starting the example
041: * @param args not used
042: * @throws Exception
043: *
044: * @author Siddhartha Azad
045: * @since 2.3
046: */
047: public static void main(String args[]) throws Exception {
048: Configuration conf;
049: try {
050: conf = new Configuration("jgapTest.con", false);
051: } catch (Exception ex) {
052: ex.printStackTrace();
053: return;
054: }
055: // set up a sample chromosome
056: Gene[] sampleGenes = new Gene[3];
057: sampleGenes[0] = new IntegerGene(conf, 60, 100);
058: sampleGenes[1] = new IntegerGene(conf, 1, 50);
059: sampleGenes[2] = new IntegerGene(conf, 100, 150);
060: IChromosome sampleChromosome = new Chromosome(conf, sampleGenes);
061: Genotype population;
062: FitnessFunction fitFunc = new MaximizingFunctionFitnessFunction();
063: try {
064: conf.setFitnessFunction(fitFunc);
065: // The higher the value, the better
066: conf.setFitnessEvaluator(new DefaultFitnessEvaluator());
067: conf.setSampleChromosome(sampleChromosome);
068: BestChromosomesSelector bestChromsSelector = new BestChromosomesSelector(
069: conf, 1.0d);
070: bestChromsSelector.setDoubletteChromosomesAllowed(false);
071: conf.addNaturalSelector(bestChromsSelector, true);
072: conf.setRandomGenerator(new StockRandomGenerator());
073: conf.setEventManager(new EventManager());
074: conf.addGeneticOperator(new CrossoverOperator(conf));
075: conf.addGeneticOperator(new MutationOperator(conf, 15));
076: population = Genotype.randomInitialGenotype(conf);
077: } catch (InvalidConfigurationException icEx) {
078: icEx.printStackTrace();
079: return;
080: }
081: // We expect the rest of the config parameter, for example the population
082: // size, to be set via the config file
083:
084: // Evolve the population
085: for (int i = 0; i < 10; i++) {
086: population.evolve();
087: }
088: IChromosome bestSolutionSoFar = population
089: .getFittestChromosome();
090: System.out.println("The best solution has a fitness value of "
091: + bestSolutionSoFar.getFitnessValue());
092: Integer aVal = (Integer) bestSolutionSoFar.getGene(0)
093: .getAllele();
094: Integer bVal = (Integer) bestSolutionSoFar.getGene(1)
095: .getAllele();
096: Integer cVal = (Integer) bestSolutionSoFar.getGene(2)
097: .getAllele();
098: System.out.println("a = " + aVal.intValue());
099: System.out.println("b = " + bVal.intValue());
100: System.out.println("c = " + cVal.intValue());
101: }
102: }
|