1 10 package examples.multiobjective; 11 12 import java.util.*; 13 import org.jgap.*; 14 import org.jgap.impl.*; 15 16 22 public class MOFitnessEvaluator 23 implements FitnessEvaluator { 24 25 private final static String CVS_REVISION = "$Revision: 1.1 $"; 26 27 36 public boolean isFitter(final double a_fitness_value1, 37 final double a_fitness_value2) { 38 throw new RuntimeException ("Not supported for multi-objectives!"); 39 } 40 41 public boolean isFitter(IChromosome a_chrom1, IChromosome a_chrom2) { 42 DoubleGene g1 = (DoubleGene)a_chrom1.getGene(0); 44 double d = g1.doubleValue(); 45 double y1 = formula(1, d); 46 List l = new Vector(); 47 l.add(new Double (y1)); 48 double y2 = formula(2, d); 49 l.add(new Double (y2)); 50 ((Chromosome)a_chrom1).setMultiObjectives(l); 51 52 l.clear(); 53 g1 = (DoubleGene)a_chrom2.getGene(0); 54 d = g1.doubleValue(); 55 y1 = formula(1, d); 56 l.add(new Double (y1)); 57 y2 = formula(2, d); 58 l.add(new Double (y2)); 59 ((Chromosome)a_chrom2).setMultiObjectives(l); 60 61 List v1 = ( (Chromosome) a_chrom1).getMultiObjectives(); 62 List v2 = ( (Chromosome) a_chrom2).getMultiObjectives(); 63 int size = v1.size(); 64 if (size != v2.size()) { 65 throw new RuntimeException ("Size of objectives inconsistent!"); 66 } 67 boolean better = false; 68 for (int i = 0; i < size; i++) { 69 double d1 = ( (Double ) v1.get(i)).doubleValue(); 70 double d2 = ( (Double ) v2.get(i)).doubleValue(); 71 if (d1 > d2) { 72 better = true; 73 } 74 else if (d1 < d2) { 75 return false; 76 } 77 } 78 return better; 79 } 80 81 private double formula(int a_index, double a_x) { 82 if (a_index == 1) { 83 return a_x * a_x; 84 } 85 else { 86 return (a_x - 2) * (a_x - 2); 87 } 88 } 89 } 90 | Popular Tags |