1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 import junit.framework.*; 15 16 22 public class CrossoverOperatorTest 23 extends JGAPTestCase { 24 25 private static final String CVS_REVISION = "$Revision: 1.26 $"; 26 27 public static Test suite() { 28 TestSuite suite = new TestSuite(CrossoverOperatorTest.class); 29 return suite; 30 } 31 32 public void setUp() { 33 super.setUp(); 34 Configuration.reset(); 35 } 36 37 43 public void testConstruct_0() 44 throws Exception { 45 new CrossoverOperator(conf, null); 46 new CrossoverOperator(conf, new DefaultMutationRateCalculator(conf)); 47 new CrossoverOperator(conf, 2); 48 new CrossoverOperator(new DefaultConfiguration(), 1); 49 new CrossoverOperator(conf, 50); 50 } 51 52 57 public void testConstruct_1() 58 throws Exception { 59 try { 60 new CrossoverOperator(conf, 0); 61 fail(); 62 } 63 catch (IllegalArgumentException iex) { 64 ; } 66 } 67 68 73 public void testConstruct_2() 74 throws Exception { 75 try { 76 new CrossoverOperator(new DefaultConfiguration(), -3); 77 fail(); 78 } 79 catch (IllegalArgumentException iex) { 80 ; } 82 } 83 84 91 public void testOperate_0() 92 throws Exception { 93 DefaultConfiguration conf = new DefaultConfiguration(); 94 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 98 rand.setNextIntSequence(new int[] { 99 0, 1, 0}); 100 conf.setRandomGenerator(rand); 101 conf.setFitnessFunction(new TestFitnessFunction()); 102 Gene sampleGene = new IntegerGene(conf, 1, 10); 103 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 104 conf.setSampleChromosome(chrom); 105 conf.setPopulationSize(6); 106 Gene cgene1 = new IntegerGene(conf, 1, 100); 107 cgene1.setAllele(new Integer (66)); 108 Gene[] genes1 = new Gene[] { 109 cgene1}; 110 Chromosome chrom1 = new Chromosome(conf, genes1); 111 Gene cgene2 = new IntegerGene(conf, 1, 100); 112 cgene2.setAllele(new Integer (88)); 113 Gene[] genes2 = new Gene[] { 114 cgene2}; 115 Chromosome chrom2 = new Chromosome(conf, genes2); 116 Chromosome[] population = new Chromosome[] { 117 chrom1, chrom2}; 118 List chroms = new Vector(); 119 Gene gene1 = new IntegerGene(conf, 1, 10); 120 gene1.setAllele(new Integer (5)); 121 chroms.add(gene1); 122 Gene gene2 = new IntegerGene(conf, 1, 10); 123 gene2.setAllele(new Integer (7)); 124 chroms.add(gene2); 125 Gene gene3 = new IntegerGene(conf, 1, 10); 126 gene3.setAllele(new Integer (4)); 127 chroms.add(gene3); 128 CrossoverOperator op = new CrossoverOperator(conf); 129 op.operate(new Population(conf, population), chroms); 132 assertEquals(5, chroms.size()); 137 Chromosome target = (Chromosome) chroms.get(3); 139 assertEquals(88, ( (Integer ) target.getGene(0).getAllele()).intValue()); 141 target = (Chromosome) chroms.get(4); 143 assertEquals(66, ( (Integer ) target.getGene(0).getAllele()).intValue()); 145 } 146 147 155 public void testOperate_0_2() 156 throws Exception { 157 DefaultConfiguration conf = new DefaultConfiguration(); 158 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 162 rand.setNextIntSequence(new int[] { 163 0, 1, 0}); 164 conf.setRandomGenerator(rand); 165 conf.setFitnessFunction(new TestFitnessFunction()); 166 Gene sampleGene = new IntegerGene(conf, 1, 10); 167 Chromosome chrom = new Chromosome(conf, sampleGene, 2); 168 conf.setSampleChromosome(chrom); 169 conf.setPopulationSize(6); 170 Gene cgene1 = new IntegerGene(conf, 1, 10); 171 cgene1.setAllele(new Integer (6)); 172 Gene[] genes1 = new Gene[] { 173 cgene1}; 174 Chromosome chrom1 = new Chromosome(conf, genes1); 175 Gene cgene2 = new IntegerGene(conf, 1, 10); 176 cgene2.setAllele(new Integer (8)); 177 Gene[] genes2 = new Gene[] { 178 cgene2}; 179 Chromosome chrom2 = new Chromosome(conf, genes2); 180 Chromosome[] population = new Chromosome[] { 181 chrom1, chrom2}; 182 List chroms = new Vector(); 183 Gene gene1 = new IntegerGene(conf, 1, 10); 184 gene1.setAllele(new Integer (5)); 185 chroms.add(gene1); 186 CrossoverOperator op = new CrossoverOperator(conf, 187 new 188 DefaultCrossoverRateCalculator( 189 conf)); 190 op.operate(new Population(conf, population), chroms); 191 assertEquals(1 + 2, chroms.size()); 196 Chromosome target = (Chromosome) chroms.get(1); 198 assertEquals(8, ( (Integer ) target.getGene(0).getAllele()).intValue()); 199 target = (Chromosome) chroms.get(2); 201 assertEquals(6, ( (Integer ) target.getGene(0).getAllele()).intValue()); 202 op.operate(new Population(conf, population), chroms); 207 assertEquals(1 + 2 + 2, chroms.size()); 208 } 209 210 218 public void testOperate_1() 219 throws Exception { 220 DefaultConfiguration conf = new DefaultConfiguration(); 221 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 223 rand.setNextIntSequence(new int[] { 224 0, 1, 0, 1, 2}); 225 conf.setRandomGenerator(rand); 226 conf.setFitnessFunction(new TestFitnessFunction()); 227 Gene sampleGene = new IntegerGene(conf, 1, 10); 228 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 229 conf.setSampleChromosome(chrom); 230 conf.setPopulationSize(6); 231 Gene cgene1 = new IntegerGene(conf, 1, 10); 232 cgene1.setAllele(new Integer (6)); 233 Gene[] genes1 = new Gene[] { 234 cgene1}; 235 Chromosome chrom1 = new Chromosome(conf, genes1); 236 Gene cgene2 = new IntegerGene(conf, 1, 10); 237 cgene2.setAllele(new Integer (8)); 238 Gene[] genes2 = new Gene[] { 239 cgene2}; 240 Chromosome chrom2 = new Chromosome(conf, genes2); 241 Chromosome[] population = new Chromosome[] { 242 chrom1, chrom2}; 243 List chroms = new Vector(); 244 Gene gene1 = new IntegerGene(conf, 1, 10); 246 gene1.setAllele(new Integer (5)); 247 chroms.add(gene1); 248 Gene gene2 = new IntegerGene(conf, 1, 10); 249 gene2.setAllele(new Integer (7)); 250 chroms.add(gene2); 251 Chromosome[] population2 = (Chromosome[]) population.clone(); 252 GeneticOperator op = new CrossoverOperator(conf); 253 op.operate(new Population(conf, population), chroms); 254 op.operate(new Population(conf, population2), chroms); 255 assertTrue(isChromosomesEqual(population, population2)); 256 assertSame(gene1, chroms.get(0)); 258 assertSame(gene2, chroms.get(1)); 259 } 260 261 269 public void testOperate_2() 270 throws Exception { 271 DefaultConfiguration conf = new DefaultConfiguration(); 272 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 273 rand.setNextIntSequence(new int[] { 274 0, 1, 0, 1, 2}); 275 conf.setRandomGenerator(rand); 276 conf.setFitnessFunction(new TestFitnessFunction()); 277 Gene sampleGene = new IntegerGene(conf, 1, 10); 278 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 279 conf.setSampleChromosome(chrom); 280 conf.setPopulationSize(6); 281 Gene cgene1 = new IntegerGene(conf, 1, 10); 282 cgene1.setAllele(new Integer (6)); 283 CompositeGene compGene = new CompositeGene(conf); 284 compGene.addGene(cgene1); 285 Gene[] genes1 = new Gene[] { 286 compGene}; 287 Chromosome chrom1 = new Chromosome(conf, genes1); 288 Gene cgene2 = new IntegerGene(conf, 1, 10); 289 cgene2.setAllele(new Integer (8)); 290 Gene[] genes2 = new Gene[] { 291 cgene2}; 292 Chromosome chrom2 = new Chromosome(conf, genes2); 293 Chromosome[] population = new Chromosome[] { 294 chrom1, chrom2}; 295 List chroms = new Vector(); 296 Gene gene1 = new IntegerGene(conf, 1, 10); 297 gene1.setAllele(new Integer (5)); 298 chroms.add(gene1); 299 Gene gene2 = new IntegerGene(conf, 1, 10); 300 gene2.setAllele(new Integer (7)); 301 chroms.add(gene2); 302 CrossoverOperator op = new CrossoverOperator(conf); 303 op.operate(new Population(conf, population), chroms); 306 assertEquals(2 + 2, chroms.size()); 308 Chromosome target = (Chromosome) chroms.get(2); 309 CompositeGene result = (CompositeGene) target.getGene(0); 310 assertEquals(8, ( (Integer ) ( (Vector) result.getAllele()) 311 .get(0)).intValue()); 312 target = (Chromosome) chroms.get(3); 313 assertEquals(6, ( (Integer ) target.getGene(0).getAllele()).intValue()); 314 } 315 316 325 public void testOperate_3() 326 throws Exception { 327 DefaultConfiguration conf = new DefaultConfiguration(); 328 IGeneticOperatorConstraint constraint = new 329 GeneticOperatorConstraintForTest(); 330 conf.getJGAPFactory().setGeneticOperatorConstraint(constraint); 331 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 332 rand.setNextIntSequence(new int[] { 333 0, 2, 0, 1, 2}); 334 conf.setRandomGenerator(rand); 335 conf.setFitnessFunction(new TestFitnessFunction()); 336 Gene sampleGene = new IntegerGene(conf, 1, 10); 337 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 338 conf.setSampleChromosome(chrom); 339 conf.setPopulationSize(6); 340 Gene cgene0 = new IntegerGene(conf, 1, 10); 341 cgene0.setAllele(new Integer (8)); 342 Gene[] genes0 = new Gene[] { 343 cgene0}; 344 Chromosome chrom0 = new Chromosome(conf, genes0); 345 Gene cgene1 = new IntegerGene(conf, 1, 10); 346 cgene1.setAllele(new Integer (5)); 347 Gene[] genes1 = new Gene[] { 348 cgene1}; 349 ChromosomeForTest chrom1 = new ChromosomeForTest(conf, genes1); 350 Gene cgene2 = new IntegerGene(conf, 1, 10); 351 cgene2.setAllele(new Integer (6)); 352 Gene[] genes2 = new Gene[] { 353 cgene2}; 354 Chromosome chrom2 = new Chromosome(conf, genes2); 355 Chromosome[] population = new Chromosome[] { 356 chrom0, chrom1, chrom2}; 357 List chroms = new Vector(); 359 Gene gene1 = new IntegerGene(conf, 1, 10); 360 gene1.setAllele(new Integer (5)); 361 chroms.add(gene1); 362 Gene gene2 = new IntegerGene(conf, 1, 10); 363 gene2.setAllele(new Integer (7)); 364 chroms.add(gene2); 365 CrossoverOperator op = new CrossoverOperator(conf); 366 op.operate(new Population(conf, population), chroms); 369 assertEquals(2 + 2, chroms.size()); 370 assertSame(gene1, chroms.get(0)); 371 assertSame(gene2, chroms.get(1)); 372 Chromosome target = (Chromosome) chroms.get(2); 373 assertEquals(6, ( (Integer ) target.getGene(0).getAllele()).intValue()); 374 target = (Chromosome) chroms.get(3); 375 assertEquals(8, ( (Integer ) target.getGene(0).getAllele()).intValue()); 376 } 377 378 385 public void testIsSerializable_0() 386 throws Exception { 387 CrossoverOperator op = new CrossoverOperator(conf); 388 assertTrue(isSerializable(op)); 389 } 390 391 399 public void testDoSerialize_0() 400 throws Exception { 401 CrossoverOperator op = new CrossoverOperator(conf, 403 new 404 DefaultCrossoverRateCalculator( 405 conf)); 406 CrossoverOperator o = (CrossoverOperator) doSerialize(op); 407 assertEquals(o, op); 408 } 409 410 418 public void testDoSerialize_1() 419 throws Exception { 420 Configuration conf = new DefaultConfiguration(); 421 CrossoverOperator op = new CrossoverOperator(conf, 423 new 424 DefaultCrossoverRateCalculator( 425 conf)); 426 CrossoverOperator o = (CrossoverOperator) doSerialize(op); 427 assertEquals(o, op); 428 } 429 430 public class GeneticOperatorConstraintForTest 431 implements IGeneticOperatorConstraint { 432 public boolean isValid(Population a_pop, List a_chromosomes, 433 GeneticOperator a_caller) { 434 Iterator it = a_chromosomes.iterator(); 435 while (it.hasNext()) { 436 Chromosome chrom = (Chromosome) it.next(); 437 if (ChromosomeForTest.class == chrom.getClass()) { 438 return false; 439 } 440 } 441 return true; 442 } 443 } 444 451 public void testEquals_0() 452 throws Exception { 453 GeneticOperator op = new CrossoverOperator(conf); 454 assertFalse(op.equals(new Chromosome(conf))); 455 } 456 457 463 public void testCompareTo_0() 464 throws Exception { 465 CrossoverOperator op = new CrossoverOperator(conf); 466 assertEquals(1, op.compareTo(null)); 467 CrossoverOperator op2 = new CrossoverOperator(conf); 468 assertEquals(0, op.compareTo(op2)); 469 op = new CrossoverOperator(conf, 3); 470 assertEquals(1, op.compareTo(op2)); 471 assertEquals( -1, op2.compareTo(op)); 472 op = new CrossoverOperator(conf, new DefaultCrossoverRateCalculator(conf)); 473 assertEquals(1, op.compareTo(op2)); 474 assertEquals( -1, op2.compareTo(op)); 475 op2 = new CrossoverOperator(conf, new DefaultCrossoverRateCalculator(conf)); 476 assertEquals(0, op.compareTo(op2)); 477 } 478 } 479 | Popular Tags |