📄 tournamentselectortest.java
字号:
/*
* This file is part of JGAP.
*
* JGAP offers a dual license model containing the LGPL as well as the MPL.
*
* For licencing information please see the file license.txt included with JGAP
* or have a look at the top of class org.jgap.Chromosome which representatively
* includes the JGAP license policy applicable for any file delivered with JGAP.
*/
package org.jgap.impl;
import java.util.*;
import org.jgap.*;
import junit.framework.*;
/**
* Tests for TournamentSelector class
*
* @author Klaus Meffert
* @since 2.0
*/
public class TournamentSelectorTest
extends JGAPTestCase {
/** String containing the CVS revision. Read out via reflection!*/
private final static String CVS_REVISION = "$Revision: 1.6 $";
public static Test suite() {
TestSuite suite = new TestSuite(TournamentSelectorTest.class);
return suite;
}
/**
* Valid construction
* @throws Exception
*
* @author Klaus Meffert
* @since 2.1
*/
public void testConstruct_0()
throws Exception {
new TournamentSelector(1, 1.0d);
new TournamentSelector(1, 0.5d);
new TournamentSelector(10, 0.00001d);
new TournamentSelector(50, 0.4d);
}
/**
* Invalid construction
* @throws Exception
*
* @author Klaus Meffert
* @since 2.1
*/
public void testConstruct_1()
throws Exception {
try {
new TournamentSelector(0, 0.5d);
fail();
}
catch (IllegalArgumentException iex) {
; //this is OK
}
}
/**
* Invalid construction
* @throws Exception
*
* @author Klaus Meffert
* @since 2.1
*/
public void testConstruct_2()
throws Exception {
try {
new TournamentSelector( -1, 0.5d);
fail();
}
catch (IllegalArgumentException iex) {
; //this is OK
}
}
/**
* Invalid construction
* @throws Exception
*
* @author Klaus Meffert
* @since 2.1
*/
public void testConstruct_3()
throws Exception {
try {
new TournamentSelector(4, 0.0d);
fail();
}
catch (IllegalArgumentException iex) {
; //this is OK
}
}
/**
* Invalid construction
* @throws Exception
*
* @author Klaus Meffert
* @since 2.1
*/
public void testConstruct_4()
throws Exception {
try {
new TournamentSelector(4, 1.0001d);
fail();
}
catch (IllegalArgumentException iex) {
; //this is OK
}
}
public void testAdd_0()
throws Exception {
TournamentSelector selector = new TournamentSelector(5, 0.5d);
Configuration conf = new DefaultConfiguration();
Genotype.setConfiguration(conf);
Gene gene = new BooleanGene();
Chromosome chrom = new Chromosome(gene, 5);
selector.add(chrom);
List chromosomes = ( (Vector) privateAccessor.getField(selector,
"m_chromosomes"));
assertEquals(1, chromosomes.size());
assertEquals(chrom, chromosomes.get(0));
selector.add(chrom);
assertEquals(chrom, chromosomes.get(0));
assertEquals(2, chromosomes.size());
selector.add(chrom);
assertEquals(3, chromosomes.size());
}
public void testEmpty_0()
throws Exception {
TournamentSelector selector = new TournamentSelector(4, 0.1d);
Configuration conf = new DefaultConfiguration();
Genotype.setConfiguration(conf);
Gene gene = new BooleanGene();
Chromosome chrom = new Chromosome(gene, 5);
selector.add(chrom);
selector.empty();
List chromosomes = ( (Vector) privateAccessor.getField(selector,
"m_chromosomes"));
assertEquals(0, chromosomes.size());
}
/**
* Test if clear()-method does not affect original Population.
*
* @throws Exception
* @author Klaus Meffert
* @since 2.1
*/
public void testEmpty_1()
throws Exception {
TournamentSelector selector = new TournamentSelector(4, 0.1d);
Configuration conf = new DefaultConfiguration();
Genotype.setConfiguration(conf);
Gene gene = new BooleanGene();
Chromosome chrom = new Chromosome(gene, 5);
Population pop = new Population(1);
pop.addChromosome(chrom);
selector.add(chrom);
Population popNew = new Population();
selector.select(1, null, popNew);
selector.empty();
assertEquals(1, popNew.size());
assertNotNull(popNew.getChromosome(0));
}
/**
* Test if clear()-method does not affect return value.
*
* @throws Exception
* @author Klaus Meffert
* @since 2.1
*/
public void testEmpty_2()
throws Exception {
TournamentSelector selector = new TournamentSelector(10, 1.0d);
Configuration conf = new DefaultConfiguration();
Genotype.setConfiguration(conf);
Gene gene = new BooleanGene();
Chromosome chrom = new Chromosome(gene, 5);
Population pop = new Population(1);
pop.addChromosome(chrom);
selector.add(chrom);
Population popNew = new Population();
selector.select(1, null, popNew);
pop = popNew;
selector.empty();
assertEquals(1, pop.size());
assertNotNull(pop.getChromosome(0));
}
/**
* Test if below functionality working without error
*
* @throws Exception
* @author Klaus Meffert
* @since 2.1
*/
public void testSelect_0()
throws Exception {
Configuration conf = new DefaultConfiguration();
Genotype.setConfiguration(conf);
TournamentSelector selector = new TournamentSelector(4, 0.3d);
Gene gene = new IntegerGene();
gene.setAllele(new Integer(444));
Chromosome secondBestChrom = new Chromosome(gene, 3);
secondBestChrom.setFitnessValue(11);
selector.add(secondBestChrom);
gene = new BooleanGene();
gene.setAllele(new Boolean(false));
Chromosome bestChrom = new Chromosome(gene, 3);
bestChrom.setFitnessValue(12);
selector.add(bestChrom);
selector.select(1, null, new Population());
}
/**
* Always select best chromosome for granted if prob is 1.0 and index for
* selected chromosomes in tournament is equal to index of best chromosome.
*
* @throws Exception
* @author Klaus Meffert
* @since 2.1
*/
public void testSelect_1()
throws Exception {
Configuration conf = new DefaultConfiguration();
// random generator always returning 1 (index of best chromosome below)
RandomGeneratorForTest rn = new RandomGeneratorForTest(1);
conf.setRandomGenerator(rn);
Genotype.setConfiguration(conf);
TournamentSelector selector = new TournamentSelector(4, 1.0d);
// add first chromosome
// --------------------
Gene gene = new BooleanGene();
gene.setAllele(new Boolean(true));
Chromosome thirdBestChrom = new Chromosome(gene, 7);
thirdBestChrom.setFitnessValue(10);
selector.add(thirdBestChrom);
// add second chromosome
// ---------------------
gene = new BooleanGene();
gene.setAllele(new Boolean(false));
Chromosome bestChrom = new Chromosome(gene, 3);
bestChrom.setFitnessValue(12);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -