📄 crossoveroperatortest.java
字号:
Chromosome[] population = new Chromosome[] {
chrom1, chrom2};
List chroms = new Vector();
// add some genes to chroms that should not be overridden
Gene gene1 = new IntegerGene(conf, 1, 10);
gene1.setAllele(new Integer(5));
chroms.add(gene1);
Gene gene2 = new IntegerGene(conf, 1, 10);
gene2.setAllele(new Integer(7));
chroms.add(gene2);
Chromosome[] population2 = (Chromosome[]) population.clone();
GeneticOperator op = new CrossoverOperator(conf);
op.operate(new Population(conf, population), chroms);
op.operate(new Population(conf, population2), chroms);
assertTrue(isChromosomesEqual(population, population2));
// check that original genes have not been modified
assertSame(gene1, chroms.get(0));
assertSame(gene2, chroms.get(1));
}
/**
* Test with CompositeGene.
*
* @throws Exception
*
* @author Klaus Meffert
* @since 2.1
*/
public void testOperate_2()
throws Exception {
DefaultConfiguration conf = new DefaultConfiguration();
RandomGeneratorForTest rand = new RandomGeneratorForTest();
rand.setNextIntSequence(new int[] {
0, 1, 0, 1, 2});
conf.setRandomGenerator(rand);
conf.setFitnessFunction(new TestFitnessFunction());
Gene sampleGene = new IntegerGene(conf, 1, 10);
Chromosome chrom = new Chromosome(conf, sampleGene, 3);
conf.setSampleChromosome(chrom);
conf.setPopulationSize(6);
Gene cgene1 = new IntegerGene(conf, 1, 10);
cgene1.setAllele(new Integer(6));
CompositeGene compGene = new CompositeGene(conf);
compGene.addGene(cgene1);
Gene[] genes1 = new Gene[] {
compGene};
Chromosome chrom1 = new Chromosome(conf, genes1);
Gene cgene2 = new IntegerGene(conf, 1, 10);
cgene2.setAllele(new Integer(8));
Gene[] genes2 = new Gene[] {
cgene2};
Chromosome chrom2 = new Chromosome(conf, genes2);
// Age increase necessary to make x-over work.
// -------------------------------------------
chrom2.increaseAge();
Chromosome[] population = new Chromosome[] {
chrom1, chrom2};
List chroms = new Vector();
Gene gene1 = new IntegerGene(conf, 1, 10);
gene1.setAllele(new Integer(5));
chroms.add(gene1);
Gene gene2 = new IntegerGene(conf, 1, 10);
gene2.setAllele(new Integer(7));
chroms.add(gene2);
CrossoverOperator op = new CrossoverOperator(conf);
// Do the crossing over.
// ---------------------
op.operate(new Population(conf, population), chroms);
// new size of chroms = 2 (original chromosomes) + 2 (from "population")
assertEquals(2 + 2, chroms.size());
Chromosome target = (Chromosome) chroms.get(2);
CompositeGene result = (CompositeGene) target.getGene(0);
assertEquals(8, ( (Integer) ( (Vector) result.getAllele())
.get(0)).intValue());
target = (Chromosome) chroms.get(3);
assertEquals(6, ( (Integer) target.getGene(0).getAllele()).intValue());
}
/**
* Considers IGeneticOperatorConstraint. Here, the crossing over of a
* BooleanGene is forbidden by that constraint.
*
* @throws Exception
*
* @author Klaus Meffert
* @since 2.6
*/
public void testOperate_3()
throws Exception {
DefaultConfiguration conf = new DefaultConfiguration();
IGeneticOperatorConstraint constraint = new
GeneticOperatorConstraintForTest();
conf.getJGAPFactory().setGeneticOperatorConstraint(constraint);
RandomGeneratorForTest rand = new RandomGeneratorForTest();
rand.setNextIntSequence(new int[] {
0, 2, 0, 1, 2});
conf.setRandomGenerator(rand);
conf.setFitnessFunction(new TestFitnessFunction());
Gene sampleGene = new IntegerGene(conf, 1, 10);
Chromosome chrom = new Chromosome(conf, sampleGene, 3);
conf.setSampleChromosome(chrom);
conf.setPopulationSize(6);
Gene cgene0 = new IntegerGene(conf, 1, 10);
cgene0.setAllele(new Integer(8));
Gene[] genes0 = new Gene[] {
cgene0};
Chromosome chrom0 = new Chromosome(conf, genes0);
Gene cgene1 = new IntegerGene(conf, 1, 10);
cgene1.setAllele(new Integer(5));
Gene[] genes1 = new Gene[] {
cgene1};
ChromosomeForTest chrom1 = new ChromosomeForTest(conf, genes1);
Gene cgene2 = new IntegerGene(conf, 1, 10);
cgene2.setAllele(new Integer(6));
Gene[] genes2 = new Gene[] {
cgene2};
Chromosome chrom2 = new Chromosome(conf, genes2);
// Age increase necessary to make x-over work.
// -------------------------------------------
chrom2.increaseAge();
Chromosome[] population = new Chromosome[] {
chrom0, chrom1, chrom2};
// Add some nonsense objects to results list (to see if they are kept).
List chroms = new Vector();
Gene gene1 = new IntegerGene(conf, 1, 10);
gene1.setAllele(new Integer(5));
chroms.add(gene1);
Gene gene2 = new IntegerGene(conf, 1, 10);
gene2.setAllele(new Integer(7));
chroms.add(gene2);
CrossoverOperator op = new CrossoverOperator(conf);
// Do the crossing over.
// ---------------------
op.operate(new Population(conf, population), chroms);
assertEquals(2 + 2, chroms.size());
assertSame(gene1, chroms.get(0));
assertSame(gene2, chroms.get(1));
Chromosome target = (Chromosome) chroms.get(2);
assertEquals(6, ( (Integer) target.getGene(0).getAllele()).intValue());
target = (Chromosome) chroms.get(3);
assertEquals(8, ( (Integer) target.getGene(0).getAllele()).intValue());
}
/**
* Ensures the operator is implementing Serializable.
* @throws Exception
*
* @author Klaus Meffert
* @since 2.6
*/
public void testIsSerializable_0()
throws Exception {
CrossoverOperator op = new CrossoverOperator(conf);
assertTrue(isSerializable(op));
}
/**
* Ensures that the operator and all objects contained implement Serializable.
* Here, we use a null configuration.
* @throws Exception
*
* @author Klaus Meffert
* @since 2.6
*/
public void testDoSerialize_0()
throws Exception {
// construct object to be serialized
CrossoverOperator op = new CrossoverOperator(conf,
new
DefaultCrossoverRateCalculator(
conf));
CrossoverOperator o = (CrossoverOperator) doSerialize(op);
assertEquals(o, op);
}
/**
* Ensures that the operator and all objects contained implement Serializable.
* Here, we set a configuration.
* @throws Exception
*
* @author Klaus Meffert
* @since 2.6
*/
public void testDoSerialize_1()
throws Exception {
Configuration conf = new DefaultConfiguration();
// construct object to be serialized
CrossoverOperator op = new CrossoverOperator(conf,
new
DefaultCrossoverRateCalculator(
conf));
CrossoverOperator o = (CrossoverOperator) doSerialize(op);
assertEquals(o, op);
}
public class GeneticOperatorConstraintForTest
implements IGeneticOperatorConstraint {
public boolean isValid(Population a_pop, List a_chromosomes,
GeneticOperator a_caller) {
Iterator it = a_chromosomes.iterator();
while (it.hasNext()) {
Chromosome chrom = (Chromosome) it.next();
if (ChromosomeForTest.class == chrom.getClass()) {
return false;
}
}
return true;
}
}
/**
* Test equals with classcast object.
*
* @throws Exception
* @author Klaus Meffert
* @since 2.6
*/
public void testEquals_0()
throws Exception {
GeneticOperator op = new CrossoverOperator(conf);
assertFalse(op.equals(new Chromosome(conf)));
}
/**
* @throws Exception
*
* @author Klaus Meffert
* @since 2.6
*/
public void testCompareTo_0()
throws Exception {
CrossoverOperator op = new CrossoverOperator(conf);
assertEquals(1, op.compareTo(null));
CrossoverOperator op2 = new CrossoverOperator(conf);
assertEquals(0, op.compareTo(op2));
op = new CrossoverOperator(conf, 3);
assertEquals(1, op.compareTo(op2));
assertEquals( -1, op2.compareTo(op));
op = new CrossoverOperator(conf, new DefaultCrossoverRateCalculator(conf));
assertEquals(1, op.compareTo(op2));
assertEquals( -1, op2.compareTo(op));
op2 = new CrossoverOperator(conf, new DefaultCrossoverRateCalculator(conf));
assertEquals(0, op.compareTo(op2));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -