1 10 package examples.multiobjective; 11 12 import java.util.*; 13 import org.jgap.*; 14 import org.jgap.impl.*; 15 16 25 public class MultiObjectiveExample { 26 27 private final static String CVS_REVISION = "$Revision: 1.3 $"; 28 29 32 private static final int MAX_ALLOWED_EVOLUTIONS = 200; 33 34 42 public void execute() 43 throws Exception { 44 Configuration conf = new DefaultConfiguration(); 48 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 BulkFitnessFunction myFunc = 64 new MultiObjectiveFitnessFunction(); 65 conf.setBulkFitnessFunction(myFunc); 66 67 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 conf.setPopulationSize(500); 82 Genotype population = Genotype.randomInitialGenotype(conf); 85 population = Genotype.randomInitialGenotype(conf); 88 for (int i = 0; i < MAX_ALLOWED_EVOLUTIONS; i++) { 92 population.evolve(); 93 } 94 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 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 149 public static void main(String [] args) 150 throws Exception { 151 MultiObjectiveExample instance = new MultiObjectiveExample(); 152 instance.execute(); 153 } 154 155 159 public class MOFitnessComparator 160 implements java.util.Comparator { 161 162 public int compare(final Object a_chrom1, final Object 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 ("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 ) v1.get(i)).doubleValue(); 173 double d2 = ( (Double ) 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 |