📄 mutationoperatortest.java
字号:
Chromosome c2 = (Chromosome) chroms.get(1);
assertEquals(new Integer(3), c2.getGene(0).getAllele());
assertEquals(new Integer(4 + 4), c2.getGene(1).getAllele());
assertEquals(new Integer(5), c2.getGene(2).getAllele());
op.operate(pop, chroms);
assertEquals(4, chroms.size());
}
/**
* Ensure that nothing is done.
* @throws Exception
*
* @author Klaus Meffert
* @since 2.4
*/
public void testOperate_4()
throws Exception {
DefaultConfiguration conf = new DefaultConfiguration();
MutationOperator mutOp = new MutationOperator(conf, 0);
mutOp.setMutationRateCalc(null);
List candChroms = new Vector();
BooleanGene gene1 = new BooleanGene(conf);
Chromosome chrom1 = new Chromosome(conf, gene1, 1);
chrom1.getGene(0).setAllele(Boolean.valueOf(false));
IntegerGene gene2 = new IntegerGene(conf, 0, 10);
Chromosome chrom2 = new Chromosome(conf, gene2, 1);
chrom2.getGene(0).setAllele(new Integer(3));
candChroms.add(chrom1);
candChroms.add(chrom2);
mutOp.operate(null, candChroms);
assertEquals(2, candChroms.size());
assertEquals(chrom1, candChroms.get(0));
assertEquals(chrom2, candChroms.get(1));
}
/**
* Mutation, especially tested for an IntegerGene.
*
* @throws Exception
*
* @author Klaus Meffert
* @since 2.2
*/
public void testOperate_5()
throws Exception {
Configuration conf = new Configuration();
conf.setPopulationSize(5);
RandomGeneratorForTest rn = new RandomGeneratorForTest();
rn.setNextInt(0);
rn.setNextDouble(0.8d); //C
conf.setRandomGenerator(rn);
BooleanGene gene1 = new BooleanGene(conf);
Chromosome chrom1 = new Chromosome(conf, gene1, 1);
chrom1.getGene(0).setAllele(Boolean.valueOf(false));
IntegerGene gene2 = new IntegerGene(conf, 0, 10); //B: B1, B2
Chromosome chrom2 = new Chromosome(conf, gene2, 1);
chrom2.getGene(0).setAllele(new Integer(3)); //A
Chromosome[] chroms = new Chromosome[] {
chrom1, chrom2};
MutationOperator mutOp = new MutationOperator(conf,
new
DefaultMutationRateCalculator(
conf));
Population pop = new Population(conf, chroms);
mutOp.operate(pop, pop.getChromosomes());
// now we should have the double number of chromosomes because the target
// list is the same as the source list of chromosomes
assertEquals(2 + 2, pop.getChromosomes().size());
//old gene
assertFalse( ( (BooleanGene) pop.getChromosome(0).getGene(0))
.booleanValue());
//mutated gene
assertTrue( ( (BooleanGene) pop.getChromosome(2).getGene(0)).booleanValue());
//old gene
assertEquals(3, ( (IntegerGene) pop.getChromosome(1).getGene(0)).intValue());
//mutated gene: A + (B2-B1) * (-1 + C * 2) --> see IntegerGene.applyMutation
// A, B1, B2, C: see comments above
// -1 + C * 2: see IntegerGene.applyMutation
assertEquals( (int) Math.round(3 + (10 - 0) * ( -1 + 0.8d * 2)),
( (IntegerGene) pop.getChromosome(3).getGene(0)).intValue());
}
/**
* Mutation, especially tested for an IntegerGene. Uses a CompositeGene.
*
* @throws Exception
*
* @author Klaus Meffert
* @since 2.2
*/
public void testOperate_5_2()
throws Exception {
Configuration conf = new Configuration();
conf.setPopulationSize(5);
BooleanGene gene1 = new BooleanGene(conf);
CompositeGene comp1 = new CompositeGene(conf);
comp1.addGene(gene1);
Chromosome chrom1 = new Chromosome(conf, comp1, 1);
( (CompositeGene) chrom1.getGene(0)).geneAt(0).setAllele(
Boolean.valueOf(false));
IntegerGene gene2 = new IntegerGene(conf, 0, 10);
CompositeGene comp2 = new CompositeGene(conf);
comp2.addGene(gene2);
Chromosome chrom2 = new Chromosome(conf, comp2, 1);
( (CompositeGene) chrom2.getGene(0)).geneAt(0).setAllele(new Integer(3));
Chromosome[] chroms = new Chromosome[] {
chrom1, chrom2};
MutationOperator mutOp = new MutationOperator(conf,
new
DefaultMutationRateCalculator(
conf));
RandomGeneratorForTest rn = new RandomGeneratorForTest();
rn.setNextInt(0);
rn.setNextDouble(0.8d);
conf.setRandomGenerator(rn);
Population pop = new Population(conf, chroms);
mutOp.operate(pop, pop.getChromosomes());
assertEquals(2 + 2, pop.getChromosomes().size());
//old gene
assertFalse( ( (BooleanGene) ( (CompositeGene) pop.getChromosome(0).getGene(
0)).geneAt(0)).booleanValue());
//mutated gene
assertTrue( ( (BooleanGene) ( (CompositeGene) pop.getChromosome(2).getGene(
0)).geneAt(0)).booleanValue());
//old gene
assertEquals(3,
( (IntegerGene) ( (CompositeGene) pop.getChromosome(1).
getGene(0)).geneAt(0)).intValue());
//mutated gene: A + (B2-B1) * (-1 + C * 2) --> see IntegerGene.applyMutation
// A, B1, B2, C: see comments above
// -1 + C * 2: see IntegerGene.applyMutation
assertEquals( (int) Math.round(3 + (10 - 0) * ( -1 + 0.8d * 2)),
( (
IntegerGene) ( (CompositeGene) pop.getChromosome(3).getGene(0)).
geneAt(0)).intValue());
}
/**
* Following should be possible without exception.
* @throws Exception
*
* @author Klaus Meffert
* @since 2.2
*/
public void testOperate_6()
throws Exception {
Configuration conf = new DefaultConfiguration();
MutationOperator mutOp = new MutationOperator(conf, 0);
mutOp.setMutationRateCalc(null);
mutOp.operate(null, null);
}
/**
* @throws Exception
*
* @author Klaus Meffert
* @since 2.6
*/
public void testOperate_6_2()
throws Exception {
Configuration conf = new DefaultConfiguration();
MutationOperator mutOp = new MutationOperator(conf, 0);
mutOp.setMutationRateCalc(new DefaultMutationRateCalculator(conf));
mutOp.operate(null, null);
}
/**
* Considers IGeneticOperatorConstraint. Here, the mutation of a BooleanGene
* is forbidden by that constraint.
*
* @throws Exception
*
* @author Klaus Meffert
* @since 2.6
*/
public void testOperate_8()
throws Exception {
Configuration conf = new Configuration();
conf.setPopulationSize(5);
RandomGeneratorForTest rn = new RandomGeneratorForTest();
rn.setNextInt(0);
rn.setNextInt(0);
rn.setNextDouble(0.8d);
conf.setRandomGenerator(rn);
BooleanGene gene1 = new BooleanGene(conf);
Chromosome chrom1 = new Chromosome(conf, gene1, 1);
chrom1.getGene(0).setAllele(Boolean.valueOf(false));
IntegerGene gene2 = new IntegerGene(conf, 0, 10);
Chromosome chrom2 = new Chromosome(conf, gene2, 1);
chrom2.getGene(0).setAllele(new Integer(3));
Chromosome[] chroms = new Chromosome[] {
chrom1, chrom2};
MutationOperator mutOp = new MutationOperator(conf,
new
DefaultMutationRateCalculator(
conf));
IGeneticOperatorConstraint constraint = new
GeneticOperatorConstraintForTest();
conf.getJGAPFactory().setGeneticOperatorConstraint(
constraint);
Population pop = new Population(conf, chroms);
mutOp.operate(pop, pop.getChromosomes());
// +1 (not +2) because only IntegerGene should have been mutated.
assertEquals(2 + 1, pop.getChromosomes().size());
//old gene
assertFalse( ( (BooleanGene) pop.getChromosome(0).getGene(0)).booleanValue());
//old gene
assertEquals(3, ( (IntegerGene) pop.getChromosome(1).
getGene(0)).intValue());
//mutated gene
assertEquals( (int) Math.round(3 + (10 - 0) * ( -1 + 0.8d * 2)),
( (IntegerGene) pop.getChromosome(2).getGene(0)).intValue());
}
/**
* Ensures operator is implementing Serializable
* @throws Exception
*
* @author Klaus Meffert
* @since 2.6
*/
public void testIsSerializable_0()
throws Exception {
MutationOperator op = new MutationOperator(conf);
assertTrue(isSerializable(op));
}
/**
* Ensures that operator and all objects contained implement Serializable.
* @throws Exception
*
* @author Klaus Meffert
* @since 2.6
*/
public void testDoSerialize_0()
throws Exception {
// construct object to be serialized
IUniversalRateCalculator calc = new DefaultCrossoverRateCalculator(conf);
MutationOperator op = new MutationOperator(conf, calc);
Object o = doSerialize(op);
assertEquals(o, op);
}
public class GeneticOperatorConstraintForTest
implements IGeneticOperatorConstraint {
public boolean isValid(Population a_pop, List a_chromosomes,
GeneticOperator a_caller) {
Chromosome chrom = (Chromosome) a_chromosomes.get(0);
Gene gene = chrom.getGene(0);
return gene.getClass() != BooleanGene.class;
}
}
/**
* Test equals with classcast object.
*
* @throws Exception
* @author Klaus Meffert
* @since 2.6
*/
public void testEquals_0()
throws Exception {
GeneticOperator op = new MutationOperator(conf);
assertFalse(op.equals(new Chromosome(conf)));
}
/**
* @throws Exception
*
* @author Klaus Meffert
* @since 2.6
*/
public void testCompareTo_0()
throws Exception {
MutationOperator op = new MutationOperator(conf);
assertEquals(1, op.compareTo(null));
MutationOperator op2 = new MutationOperator(conf);
assertEquals(0, op.compareTo(op2));
op = new MutationOperator(conf, 3);
assertEquals( -1, op.compareTo(op2));
assertEquals(1, op2.compareTo(op));
op = new MutationOperator(conf, new DefaultMutationRateCalculator(conf));
assertEquals(0, op.compareTo(op2));
op = new MutationOperator(conf, 3);
op2 = new MutationOperator(conf, 4);
assertEquals( -1, op.compareTo(op2));
assertEquals(1, op2.compareTo(op));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -