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.equalDistribution;
011:
012: import java.util.*;
013: import org.jgap.*;
014: import org.jgap.impl.*;
015:
016: /**
017: * Given 64 vents with different weights. Try to make 8 group with each
018: * groups having (nearly) the same weight as the other groups.<p>
019: * Here, each vent has a similar weight with a small deviation. But the
020: * deviation could also be significant (then, good solutions are harder to
021: * find).<p>
022: * The proposed way of solving this problem is quite easy in its structure.
023: * There is potential for optimzations!
024: *
025: * @author Klaus Meffert
026: * @since 3.2
027: */
028: public class MainClass {
029: /** String containing the CVS revision. Read out via reflection!*/
030: private final static String CVS_REVISION = "$Revision: 1.1 $";
031:
032: /**
033: * Holds the available vents, each with a specific weight
034: */
035: private Vent[] m_vents;
036:
037: private int m_numEvolutions;
038:
039: /**
040: * Constructor.
041: *
042: * @throws Exception
043: *
044: * @author Klaus Meffert
045: * @since 3.2
046: */
047: public MainClass() throws Exception {
048: makeVents();
049: Genotype genotype = configureJGAP();
050: doEvolution(genotype);
051: }
052:
053: /**
054: * Sets up the configuration for the problem.
055: *
056: * @throws Exception
057: *
058: * @author Klaus Meffert
059: * @since 3.2
060: */
061: protected Genotype configureJGAP() throws Exception {
062: m_numEvolutions = 50;
063: Configuration gaConf = new DefaultConfiguration();
064: gaConf.resetProperty(Configuration.PROPERTY_FITEVAL_INST);
065: gaConf.setFitnessEvaluator(new DeltaFitnessEvaluator());
066: // Just use a swapping operator instead of mutation and others.
067: // ------------------------------------------------------------
068: gaConf.getGeneticOperators().clear();
069: SwappingMutationOperator swapper = new SwappingMutationOperator(
070: gaConf);
071: gaConf.addGeneticOperator(swapper);
072: // Setup some other parameters.
073: // ----------------------------
074: gaConf.setPreservFittestIndividual(true);
075: gaConf.setKeepPopulationSizeConstant(false);
076: // Set number of individuals (=tries) per generation.
077: // --------------------------------------------------
078: gaConf.setPopulationSize(50);
079: int chromeSize = m_vents.length;
080: Genotype genotype = null;
081: try {
082: // Setup the structure with which to evolve the
083: // solution of the problem.
084: // --------------------------------------------
085: IChromosome sampleChromosome = new Chromosome(gaConf,
086: new IntegerGene(gaConf), chromeSize);
087: gaConf.setSampleChromosome(sampleChromosome);
088: // Setup the important fitness function!
089: // -------------------------------------
090: gaConf
091: .setFitnessFunction(new SampleFitnessFunction(
092: m_vents));
093: //
094: genotype = Genotype.randomInitialGenotype(gaConf);
095: // Now ensure that each number from 1..64 (representing the
096: // indices of the vents) is represented by exactly one gene.
097: // --> Suboptimal here, as randomized initialization becomes
098: // obsolete (other solution would be more complicated).
099: // ---------------------------------------------------------
100: List chromosomes = genotype.getPopulation()
101: .getChromosomes();
102: for (int i = 0; i < chromosomes.size(); i++) {
103: IChromosome chrom = (IChromosome) chromosomes.get(i);
104: for (int j = 0; j < chrom.size(); j++) {
105: Gene gene = (Gene) chrom.getGene(j);
106: gene.setAllele(new Integer(j));
107: }
108: }
109: } catch (InvalidConfigurationException e) {
110: e.printStackTrace();
111: System.exit(-2);
112: }
113: return genotype;
114: }
115:
116: /**
117: * Does the evolution until finished.
118: *
119: * @author Klaus Meffert
120: * @since 3.2
121: */
122: public void doEvolution(Genotype genotype) {
123: int progress = 0;
124: int percentEvolution = m_numEvolutions / 100;
125: for (int i = 0; i < m_numEvolutions; i++) {
126: genotype.evolve();
127: // Print progress.
128: // ---------------
129: if (percentEvolution > 0 && i % percentEvolution == 0) {
130: progress++;
131: IChromosome fittest = genotype.getFittestChromosome();
132: double fitness = fittest.getFitnessValue();
133: System.out
134: .println("Currently best solution has fitness "
135: + fitness);
136: printSolution(fittest);
137: }
138: }
139: // Print summary.
140: // --------------
141: IChromosome fittest = genotype.getFittestChromosome();
142: System.out.println("Best solution has fitness "
143: + fittest.getFitnessValue());
144: printSolution(fittest);
145: }
146:
147: /**
148: * @param a_solution a solution to print to the console
149: *
150: * @author Klaus Meffert
151: * @since 3.2
152: */
153: public void printSolution(IChromosome a_solution) {
154: double groupWeights = 0.0d;
155: for (int i = 0; i < 8; i++) {
156: System.out.println("\nGroup " + i);
157: System.out.println("-------");
158: double groupWeight = 0.0d;
159: for (int j = 0; j < 8; j++) {
160: IntegerGene ventIndex = (IntegerGene) a_solution
161: .getGene((i * 8 + j));
162: Vent vent = (Vent) m_vents[ventIndex.intValue()];
163: double weight = vent.getWeight();
164: groupWeight += weight;
165: System.out.println(" Vent at index "
166: + ventIndex.intValue() + " with weight "
167: + weight);
168: }
169: groupWeights += groupWeight;
170: System.out.println(" --> Group weight: " + groupWeight);
171: }
172: System.out.println("\n Average group weight: " + groupWeights
173: / 8);
174: }
175:
176: /**
177: * Create vents with different weights.
178: *
179: * @author Klaus Meffert
180: * @since 3.2
181: */
182: public void makeVents() {
183: m_vents = new Vent[64];
184: for (int i = 0; i < m_vents.length; i++) {
185: // Set a weight between 290 and 310
186: double weight = 290 + Math.random() * 20;
187: Vent vent = new Vent(weight);
188: m_vents[i] = vent;
189: }
190: }
191:
192: /**
193: * Start the example
194: * @param args ignored
195: *
196: * @author Klaus Meffert
197: * @since 3.2
198: */
199: public static void main(String[] args) {
200: try {
201: new MainClass();
202: } catch (Throwable t) {
203: t.printStackTrace();
204: System.exit(1);
205: }
206: }
207: }
|