KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > multiobjective > MultiObjectiveExample


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.multiobjective;
11
12 import java.util.*;
13 import org.jgap.*;
14 import org.jgap.impl.*;
15
16 /**
17  * Example for a multiobjective problem. Here, we have a function F with one
18  * input parameter t and two output values F1 and F2, with F1 = t²
19  * and F2 = (t - 2)². This example is from Goldberg (pp. 199), who adapted it
20  * from Schaffer (1984).
21  *
22  * @author Klaus Meffert
23  * @since 2.6
24  */

25 public class MultiObjectiveExample {
26   /** String containing the CVS revision. Read out via reflection!*/
27   private final static String JavaDoc CVS_REVISION = "$Revision: 1.3 $";
28
29   /**
30    * The total number of times we'll let the population evolve.
31    */

32   private static final int MAX_ALLOWED_EVOLUTIONS = 200;
33
34   /**
35    * Executes the genetic algorithm.
36    *
37    * @throws Exception
38    *
39    * @author Klaus Meffert
40    * @since 2.6
41    */

42   public void execute()
43       throws Exception JavaDoc {
44     // Start with a DefaultConfiguration, which comes setup with the
45
// most common settings.
46
// -------------------------------------------------------------
47
Configuration conf = new DefaultConfiguration();
48     // Add BestChromosomesSelector with doublettes allowed.
49
// ----------------------------------------------------
50
conf.getNaturalSelectors(true).clear();
51     BestChromosomesSelector bestChromsSelector = new BestChromosomesSelector(
52         conf, 0.95d);
53     bestChromsSelector.setDoubletteChromosomesAllowed(true);
54     conf.addNaturalSelector(bestChromsSelector, true);
55
56     conf.setFitnessEvaluator(new MOFitnessEvaluator());
57     conf.setPreservFittestIndividual(false);
58     conf.setKeepPopulationSizeConstant(false);
59     // Set the fitness function we want to use, which is our
60
// MinimizingMakeChangeFitnessFunction. We construct it with
61
// the target amount of change passed in to this method.
62
// ---------------------------------------------------------
63
BulkFitnessFunction myFunc =
64         new MultiObjectiveFitnessFunction();
65     conf.setBulkFitnessFunction(myFunc);
66
67     // Set sample chromosome.
68
// ----------------------
69
Gene[] sampleGenes = new Gene[1];
70     sampleGenes[0] = new DoubleGene(conf, MultiObjectiveFitnessFunction.MIN_X,
71                                     MultiObjectiveFitnessFunction.MAX_X);
72     IChromosome sampleChromosome = new Chromosome(null, sampleGenes);
73     conf.setSampleChromosome(sampleChromosome);
74
75     // Finally, we need to tell the Configuration object how many
76
// Chromosomes we want in our population. The more Chromosomes,
77
// the larger number of potential solutions (which is good for
78
// finding the answer), but the longer it will take to evolve
79
// the population (which could be seen as bad).
80
// ------------------------------------------------------------
81
conf.setPopulationSize(500);
82     // Create random initial population of Chromosomes.
83
// ------------------------------------------------
84
Genotype population = Genotype.randomInitialGenotype(conf);
85     // Now we initialize the population randomly, anyway!
86
// If you want to load previous results from file, remove the next line!
87
population = Genotype.randomInitialGenotype(conf);
88     // Evolve the population. Since we don't know what the best answer
89
// is going to be, we just evolve the max number of times.
90
// ---------------------------------------------------------------
91
for (int i = 0; i < MAX_ALLOWED_EVOLUTIONS; i++) {
92       population.evolve();
93     }
94     // Remove solutions that are not Pareto-optimal.
95
// ---------------------------------------------
96
List chroms = population.getPopulation().getChromosomes();
97     int size = population.getPopulation().getChromosomes().size();
98     int i = 0;
99     boolean removed = false;
100     MOFitnessComparator comp = new MOFitnessComparator();
101     while (i<size-1) {
102       IChromosome chrom1 = population.getPopulation().getChromosome(i);
103       int j = i + 1;
104       while (j < size) {
105         IChromosome chrom2 = population.getPopulation().getChromosome(j);
106         int res = comp.compare(chrom1, chrom2);
107         if (res != 0) {
108           if (res == -1) {
109             population.getPopulation().getChromosomes().remove(i);
110             size--;
111             removed = true;
112             break;
113           }
114           else {
115             population.getPopulation().getChromosomes().remove(j);
116             size--;
117           }
118         }
119         else {
120           j++;
121         }
122       }
123       if (removed) {
124         removed = false;
125       }
126       else {
127         i++;
128       }
129     }
130     // Print all Pareto-optimal solutions.
131
// -----------------------------------
132
Collections.sort(chroms, comp);
133     for (int k=0;k<chroms.size();k++) {
134       Chromosome bestSolutionSoFar = (Chromosome) chroms.get(k);
135       System.out.println(MultiObjectiveFitnessFunction.
136                          getVector(bestSolutionSoFar));
137     }
138   }
139
140   /**
141    * Main method to run the example.
142    *
143    * @param args ignored
144    * @throws Exception
145    *
146    * @author Klaus Meffert
147    * @since 2.6
148    */

149   public static void main(String JavaDoc[] args)
150       throws Exception JavaDoc {
151     MultiObjectiveExample instance = new MultiObjectiveExample();
152     instance.execute();
153   }
154
155   /**
156    * @author Klaus Meffert
157    * @since 2.6
158    */

159   public class MOFitnessComparator
160       implements java.util.Comparator JavaDoc {
161
162     public int compare(final Object JavaDoc a_chrom1, final Object JavaDoc a_chrom2) {
163       List v1 = ( (Chromosome) a_chrom1).getMultiObjectives();
164       List v2 = ( (Chromosome) a_chrom2).getMultiObjectives();
165       int size = v1.size();
166       if (size != v2.size()) {
167         throw new RuntimeException JavaDoc("Size of objectives inconsistent!");
168       }
169       boolean better1 = false;
170       boolean better2 = false;
171       for (int i = 0; i < size; i++) {
172         double d1 = ( (Double JavaDoc) v1.get(i)).doubleValue();
173         double d2 = ( (Double JavaDoc) v2.get(i)).doubleValue();
174         if (d1 < d2) {
175           better1 = true;
176         }
177         else if (d2 < d1) {
178           better2 = true;
179         }
180       }
181       if (better1) {
182         if (better2) {
183           return 0;
184         }
185         else {
186           return 1;
187         }
188       }
189       else {
190         if (better2) {
191           return -1;
192         }
193         else {
194           return 0;
195         }
196       }
197     }
198   }
199 }
200
Popular Tags