1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 import junit.framework.*; 15 16 22 public class GreedyCrossoverTest 23 extends JGAPTestCase { 24 25 private static final String CVS_REVISION = "$Revision: 1.20 $"; 26 27 public static Test suite() { 28 TestSuite suite = new TestSuite(GreedyCrossoverTest.class); 29 return suite; 30 } 31 32 public void setUp() { 33 super.setUp(); 34 Configuration.reset(); 35 } 36 37 43 public void testOperate_0() 44 throws Exception { 45 DefaultConfiguration conf = new DefaultConfiguration(); 46 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 47 rand.setNextIntSequence(new int[] { 48 0, 1, 0, 1, 2}); 49 conf.setRandomGenerator(rand); 50 conf.setFitnessFunction(new TestFitnessFunction()); 51 Gene sampleGene = new IntegerGene(conf, 1, 10); 52 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 53 conf.setSampleChromosome(chrom); 54 conf.setPopulationSize(6); 55 GreedyCrossover op = new GreedyCrossover(conf); 56 op.ASSERTIONS = true; 57 op.setStartOffset(0); 58 Gene cgene1 = new IntegerGene(conf, 1, 10); 59 cgene1.setAllele(new Integer (6)); 60 Gene cgene2 = new IntegerGene(conf, 1, 10); 61 cgene2.setAllele(new Integer (8)); 62 Gene[] genes1 = new Gene[] { 63 cgene1, cgene2}; 64 Chromosome chrom1 = new Chromosome(conf, genes1); 65 Gene[] genes2 = new Gene[] { 66 cgene2, cgene1}; 67 Chromosome chrom2 = new Chromosome(conf, genes2); 68 Chromosome[] population = new Chromosome[] { 69 chrom1, chrom2}; 70 List chroms = new Vector(); 71 Gene gene1 = new IntegerGene(conf, 1, 10); 72 gene1.setAllele(new Integer (5)); 73 chroms.add(gene1); 74 Gene gene2 = new IntegerGene(conf, 1, 10); 75 gene2.setAllele(new Integer (7)); 76 chroms.add(gene2); 77 Gene gene3 = new IntegerGene(conf, 1, 10); 78 gene3.setAllele(new Integer (4)); 79 chroms.add(gene3); 80 op.operate(new Population(conf, population), chroms); 81 assertEquals(5, chroms.size()); 82 Chromosome target = (Chromosome) chroms.get(4); 83 assertEquals(8, ( (Integer ) target.getGene(0).getAllele()).intValue()); 84 target = (Chromosome) chroms.get(3); 85 assertEquals(6, ( (Integer ) target.getGene(0).getAllele()).intValue()); 86 } 87 88 95 public void testOperate_1() 96 throws Exception { 97 DefaultConfiguration conf = new DefaultConfiguration(); 98 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 99 rand.setNextIntSequence(new int[] { 100 0, 1, 0, 1, 2}); 101 conf.setRandomGenerator(rand); 102 conf.setFitnessFunction(new TestFitnessFunction()); 103 Gene sampleGene = new IntegerGene(conf, 1, 10); 104 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 105 conf.setSampleChromosome(chrom); 106 conf.setPopulationSize(6); 107 GreedyCrossover op = new GreedyCrossover(conf); 108 op.ASSERTIONS = true; 109 op.setStartOffset(1); 110 Gene cgene1 = new IntegerGene(conf, 1, 10); 111 cgene1.setAllele(new Integer (6)); 112 Gene cgene2 = new IntegerGene(conf, 1, 10); 113 cgene2.setAllele(new Integer (8)); 114 Gene[] genes1 = new Gene[] { 115 cgene1, cgene2}; 116 Chromosome chrom1 = new Chromosome(conf, genes1); 117 Gene[] genes2 = new Gene[] { 118 cgene2, cgene1}; 119 Chromosome chrom2 = new Chromosome(conf, genes2); 120 Chromosome[] population = new Chromosome[] { 121 chrom1, chrom2}; 122 List chroms = new Vector(); 123 Gene gene1 = new IntegerGene(conf, 1, 10); 124 gene1.setAllele(new Integer (5)); 125 chroms.add(gene1); 126 Gene gene2 = new IntegerGene(conf, 1, 10); 127 gene2.setAllele(new Integer (7)); 128 chroms.add(gene2); 129 try { 130 op.operate(new Population(conf, population), chroms); 131 fail(); 132 } 133 catch (Error e) { 134 ; } 136 } 137 138 145 public void testOperate_2() 146 throws Exception { 147 DefaultConfiguration conf = new DefaultConfiguration(); 148 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 149 rand.setNextIntSequence(new int[] { 150 0, 1, 0, 1, 2}); 151 conf.setRandomGenerator(rand); 152 conf.setFitnessFunction(new TestFitnessFunction()); 153 GreedyCrossover op = new GreedyCrossover(conf); 154 op.ASSERTIONS = true; 155 Gene sampleGene = new IntegerGene(conf, 1, 10); 156 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 157 conf.setSampleChromosome(chrom); 158 conf.setPopulationSize(6); 159 Gene cgene1 = new IntegerGene(conf, 1, 10); 160 cgene1.setAllele(new Integer (6)); 161 CompositeGene compGene = new CompositeGene(conf); 162 compGene.addGene(cgene1); 163 Gene cgene2 = new IntegerGene(conf, 1, 10); 164 cgene2.setAllele(new Integer (8)); 165 Gene[] genes1 = new Gene[] { 166 cgene1, cgene2}; 167 Chromosome chrom1 = new Chromosome(conf, genes1); 168 Gene[] genes2 = new Gene[] { 169 cgene1, cgene2, cgene1}; 170 Chromosome chrom2 = new Chromosome(conf, genes2); 171 Chromosome[] population = new Chromosome[] { 172 chrom1, chrom2}; 173 List chroms = new Vector(); 174 Gene gene1 = new IntegerGene(conf, 1, 10); 175 gene1.setAllele(new Integer (5)); 176 chroms.add(gene1); 177 try { 178 op.operate(new Population(conf, population), chroms); 179 fail(); 180 } 181 catch (Error e) { 182 ; } 184 } 185 186 193 public void testOperate_3() 194 throws Exception { 195 DefaultConfiguration conf = new DefaultConfiguration(); 196 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 197 rand.setNextIntSequence(new int[] { 198 0, 1, 0, 1, 2}); 199 conf.setRandomGenerator(rand); 200 conf.setFitnessFunction(new TestFitnessFunction()); 201 GreedyCrossover op = new GreedyCrossover(conf); 202 op.ASSERTIONS = true; 203 op.setStartOffset(0); 204 Gene sampleGene = new IntegerGene(conf, 1, 10); 205 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 206 conf.setSampleChromosome(chrom); 207 conf.setPopulationSize(6); 208 Gene cgene1 = new IntegerGene(conf, 1, 10); 209 cgene1.setAllele(new Integer (6)); 210 CompositeGene compGene = new CompositeGene(conf); 211 compGene.addGene(cgene1); 212 Gene cgene2 = new IntegerGene(conf, 1, 10); 213 cgene2.setAllele(new Integer (8)); 214 Gene[] genes1 = new Gene[] { 215 compGene, cgene1, cgene1}; 216 Chromosome chrom1 = new Chromosome(conf, genes1); 217 Gene[] genes2 = new Gene[] { 218 compGene, cgene1, cgene1}; 219 Chromosome chrom2 = new Chromosome(conf, genes2); 220 Chromosome[] population = new Chromosome[] { 221 chrom1, chrom2}; 222 List chroms = new Vector(); 223 Gene gene1 = new IntegerGene(conf, 1, 10); 224 gene1.setAllele(new Integer (5)); 225 chroms.add(gene1); 226 Gene gene2 = new IntegerGene(conf, 1, 10); 227 gene2.setAllele(new Integer (7)); 228 chroms.add(gene2); 229 Gene gene3 = new IntegerGene(conf, 1, 10); 230 gene3.setAllele(new Integer (4)); 231 chroms.add(gene3); 232 try { 233 op.operate(new Population(conf, population), chroms); 234 fail(); 235 } 236 catch (Error e) { 237 ; } 239 } 240 241 249 public void testOperate_4() 250 throws Exception { 251 Configuration conf = new DefaultConfiguration(); 252 conf.setFitnessFunction(new TestFitnessFunction()); 253 GreedyCrossover cross = new GreedyCrossover(conf) { 254 256 public double distance(Object a_from, Object a_to) { 257 IntegerGene from = (IntegerGene) a_from; 258 IntegerGene to = (IntegerGene) a_to; 259 int a = from.intValue(); 260 int b = to.intValue(); 261 if (a > b) { 262 int t = a; 263 a = b; 264 b = t; 265 } 266 ; 267 if (a == 1 && b == 4) { 269 return 1; 270 } 271 if (a == 4 && b == 5) { 272 return 2; 273 } 274 if (a == 1 && b == 2) { 276 return 10; 277 } 278 if (a == 1 && b == 3) { 279 return 20; 280 } 281 if (a == 0 && b == 2) { 283 return 100; 284 } 285 if (a == 2 && b == 3) { 286 return 200; 287 } 288 throw new Error ("These two should not be compared: " + a + " and " + b); 289 } 290 }; 291 cross.ASSERTIONS = true; 292 cross.setStartOffset(0); 293 Chromosome a = chromosome(new int[] {1, 2, 3, 4, 5, 0}); 294 Chromosome b = chromosome(new int[] {4, 1, 3, 2, 0, 5}); 295 Chromosome must_a = chromosome(new int[] {1, 2, 0, 5, 3, 4}); 301 Chromosome must_b = chromosome(new int[] {4, 1, 2, 0, 5, 3}); 304 cross.operate(b, a); 305 assertEquals(a, must_a); 306 assertEquals(b, must_b); 307 } 308 309 316 public void testOperate_5() 317 throws Exception { 318 DefaultConfiguration conf = new DefaultConfiguration(); 319 GreedyCrossover op = new GreedyCrossover(conf); 320 op.ASSERTIONS = true; 321 conf.addGeneticOperator(op); 322 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 323 rand.setNextDouble(0.45d); 324 rand.setNextInt(0); 325 op.setStartOffset(0); 326 conf.setRandomGenerator(rand); 327 conf.setFitnessFunction(new TestFitnessFunction()); 328 Gene sampleGene = new IntegerGene(conf, 1, 10); 329 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 330 conf.setSampleChromosome(chrom); 331 conf.setPopulationSize(6); 332 Gene cgene1 = new IntegerGene(conf, 1, 10); 333 cgene1.setAllele(new Integer (6)); 334 Gene cgene1_2 = new IntegerGene(conf, 1, 150); 335 cgene1.setAllele(new Integer (99)); 336 Gene[] genes1 = new Gene[] { 337 cgene1, cgene1_2}; 338 Chromosome chrom1 = new Chromosome(conf, genes1); 339 Gene cgene2 = new IntegerGene(conf, 1, 10); 340 cgene2.setAllele(new Integer (9)); 341 Gene cgene2_2 = new IntegerGene(conf, 1, 10); 342 cgene2.setAllele(new Integer (1)); 343 Gene[] genes2 = new Gene[] { 344 cgene2, cgene2_2}; 345 Chromosome chrom2 = new Chromosome(conf, genes2); 346 Chromosome[] population = new Chromosome[] { 347 chrom1, chrom2}; 348 List chroms = new Vector(); 349 Gene gene1 = new IntegerGene(conf, 1, 10); 350 gene1.setAllele(new Integer (5)); 351 chroms.add(gene1); 352 Gene gene2 = new IntegerGene(conf, 1, 10); 353 gene2.setAllele(new Integer (7)); 354 chroms.add(gene2); 355 Gene gene3 = new IntegerGene(conf, 1, 10); 356 gene3.setAllele(new Integer (4)); 357 chroms.add(gene3); 358 assertEquals(3, chroms.size()); 359 Population pop = new Population(conf, population); 360 op.operate(pop, chroms); 361 assertEquals(2, pop.size()); 362 assertEquals(3 + 2, chroms.size()); 363 op.operate(pop, chroms); 364 assertEquals(2, pop.size()); 365 assertEquals(3 + 2 + 2, chroms.size()); 366 } 367 368 375 public void testOperate_6() 376 throws Exception { 377 DefaultConfiguration conf = new DefaultConfiguration(); 378 GreedyCrossover op = new GreedyCrossover(conf); 379 op.ASSERTIONS = true; 380 conf.addGeneticOperator(op); 381 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 382 rand.setNextDouble(0.45d); 383 rand.setNextInt(0); 384 op.setStartOffset(0); 385 conf.setRandomGenerator(rand); 386 conf.setFitnessFunction(new TestFitnessFunction()); 387 Gene sampleGene = new IntegerGene(conf, 1, 10); 388 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 389 conf.setSampleChromosome(chrom); 390 conf.setPopulationSize(6); 391 Gene cgene1 = new IntegerGene(conf, 1, 10); 392 cgene1.setAllele(new Integer (6)); 393 Gene[] genes1 = new Gene[] { 394 cgene1}; 395 Chromosome chrom1 = new Chromosome(conf, genes1); 396 Gene cgene2 = new IntegerGene(conf, 1, 10); 397 cgene2.setAllele(new Integer (9)); 398 Gene[] genes2 = new Gene[] { 399 cgene2}; 400 Chromosome chrom2 = new Chromosome(conf, genes2); 401 Chromosome[] population = new Chromosome[] { 402 chrom1, chrom2}; 403 List chroms = new Vector(); 404 Gene gene1 = new IntegerGene(conf, 1, 10); 405 gene1.setAllele(new Integer (5)); 406 chroms.add(gene1); 407 Gene gene2 = new IntegerGene(conf, 1, 10); 408 gene2.setAllele(new Integer (7)); 409 chroms.add(gene2); 410 Gene gene3 = new IntegerGene(conf, 1, 10); 411 gene3.setAllele(new Integer (4)); 412 chroms.add(gene3); 413 assertEquals(3, chroms.size()); 414 Population pop = new Population(conf, population); 415 try { 416 op.operate(pop, chroms); 417 fail(); 418 } 419 catch (Error e) { 420 ; } 422 } 423 424 433 private Chromosome chromosome(int[] a_genes) 434 throws Exception { 435 IntegerGene[] ig = new IntegerGene[a_genes.length]; 436 for (int i = 0; i < ig.length; i++) { 437 ig[i] = new IntegerGene(conf, 0, 5); 438 ig[i].setAllele(new Integer (a_genes[i])); 439 } 440 return new Chromosome(conf, ig); 441 } 442 443 449 public void testStartoffset_0() 450 throws Exception { 451 GreedyCrossover op = new GreedyCrossover(conf); 452 assertEquals(1, op.getStartOffset()); 453 op.setStartOffset(2); 454 assertEquals(2, op.getStartOffset()); 455 op.setStartOffset(1); 456 assertEquals(1, op.getStartOffset()); 457 op.setStartOffset(0); 458 assertEquals(0, op.getStartOffset()); 459 } 460 461 468 public void testIsSerializable_0() 469 throws Exception { 470 GreedyCrossover op = new GreedyCrossover(conf); 471 assertTrue(isSerializable(op)); 472 } 473 474 481 public void testDoSerialize_0() 482 throws Exception { 483 GreedyCrossover op = new GreedyCrossover(conf); 485 GreedyCrossover o = (GreedyCrossover) doSerialize(op); 486 assertEquals(o, op); 487 } 488 489 496 public void testEquals_0() 497 throws Exception { 498 GeneticOperator op = new GreedyCrossover(conf); 499 assertFalse(op.equals(new Chromosome(conf))); 500 } 501 502 508 public void testCompareTo_0() 509 throws Exception { 510 GreedyCrossover op = new GreedyCrossover(conf); 511 assertEquals(1, op.compareTo(null)); 512 GreedyCrossover op2 = new GreedyCrossover(conf); 513 assertEquals(0, op.compareTo(op2)); 514 op.setStartOffset(2); 515 assertEquals( -1, op.compareTo(op2)); 516 assertEquals(1, op2.compareTo(op)); 517 } 518 } 519 | Popular Tags |