⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 weightedrouletteselectortest.java

📁 一个开源的用java开发的遗传算法的封装好的工程
💻 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 the WeightedRouletteSelector class.
 *
 * @author Klaus Meffert
 * @since 1.1
 */
public class WeightedRouletteSelectorTest
    extends JGAPTestCase {
  /** String containing the CVS revision. Read out via reflection!*/
  private final static String CVS_REVISION = "$Revision: 1.28 $";

  public static Test suite() {
    TestSuite suite = new TestSuite(WeightedRouletteSelectorTest.class);
    return suite;
  }

  public void setUp() {
    super.setUp();
    Configuration.reset();
  }

  /**
   * Test if construction possible without failure.
   */
  public void testConstruct_0() {
    new WeightedRouletteSelector();
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testAdd_0()
      throws Exception {
    WeightedRouletteSelector selector = new WeightedRouletteSelector(conf);
    Configuration conf = new DefaultConfiguration();
    Gene gene = new BooleanGene(conf);
    Chromosome chrom = new Chromosome(conf, gene, 5);
    conf.setFitnessFunction(new TestFitnessFunction());
    conf.setSampleChromosome(chrom);
    conf.setPopulationSize(5);
    selector.add(chrom);
    Map chromosomes = (Map) privateAccessor.getField(selector,
        "m_wheel");
    assertEquals(1, chromosomes.size());
    Iterator it = chromosomes.keySet().iterator();
    assertEquals(chrom, it.next());
    selector.add(chrom);
    assertEquals(1, chromosomes.size());
    it = chromosomes.keySet().iterator();
    assertEquals(chrom, it.next());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testSelect_0()
      throws Exception {
    WeightedRouletteSelector selector = new WeightedRouletteSelector(conf);
    Gene gene = new BooleanGene(conf);
    gene.setAllele(Boolean.valueOf(true));
    Chromosome bestChrom = new Chromosome(conf, gene, 7);
    bestChrom.setFitnessValue(10);
    selector.add(bestChrom);
    Population p = new Population(conf);
    selector.select(1, null, p);
    assertSame(bestChrom, p.getChromosome(0));
    assertEquals(1, p.size());
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testSelect_1()
      throws Exception {
    DefaultConfiguration conf = new DefaultConfiguration();
    RandomGeneratorForTest randgen = new RandomGeneratorForTest();
    randgen.setNextDouble(0.9999d);
    conf.setRandomGenerator(randgen);
    WeightedRouletteSelector selector = new WeightedRouletteSelector(conf);
    selector.setDoubletteChromosomesAllowed(false);
    // add first chromosome
    // --------------------
    Gene gene = new BooleanGene(conf);
    gene.setAllele(Boolean.valueOf(true));
    Chromosome thirdBestChrom = new Chromosome(conf, gene, 4);
    thirdBestChrom.setFitnessValue(10);
    selector.add(thirdBestChrom);
    // add second chromosome
    // ---------------------
    gene = new DoubleGene(conf);
    gene.setAllele(new Double(2.3d));
    Chromosome bestChrom = new Chromosome(conf, gene, 3);
    bestChrom.setFitnessValue(12);
    selector.add(bestChrom);
    // add third chromosome
    // ---------------------
    gene = new IntegerGene(conf);
    gene.setAllele(new Integer(444));
    Chromosome secondBestChrom = new Chromosome(conf, gene, 2);
    secondBestChrom.setFitnessValue(11);
    selector.add(secondBestChrom);
    // receive top 1 (= best) chromosome
    // ---------------------------------
    Population popNew = new Population(conf);
    selector.select(1, null, popNew);
    IChromosome[] bestChroms = popNew.toChromosomes();
    assertEquals(1, bestChroms.length);
    assertEquals(thirdBestChrom, bestChroms[0]);
    assertSame(thirdBestChrom, bestChroms[0]);
    // now select top 4 chromosomes (should only select 3!)
    // ----------------------------------------------------
    popNew.getChromosomes().clear();
    selector.select(4, null, popNew);
    bestChroms = popNew.toChromosomes();
    assertEquals(3, bestChroms.length);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testSelect_2()
      throws Exception {
    DefaultConfiguration conf = new DefaultConfiguration();
    RandomGeneratorForTest randgen = new RandomGeneratorForTest();
    randgen.setNextDouble(0.9999d);
    conf.setRandomGenerator(randgen);
    WeightedRouletteSelector selector = new WeightedRouletteSelector(conf);
    selector.setDoubletteChromosomesAllowed(false);
    Population toAddFrom = new Population(conf);
    // add first chromosome
    // --------------------
    Gene gene = new BooleanGene(conf);
    gene.setAllele(Boolean.valueOf(true));
    Chromosome thirdBestChrom = new Chromosome(conf, gene, 4);
    thirdBestChrom.setFitnessValue(10);
    toAddFrom.addChromosome(thirdBestChrom);
    // add second chromosome
    // ---------------------
    gene = new DoubleGene(conf);
    gene.setAllele(new Double(2.3d));
    Chromosome bestChrom = new Chromosome(conf, gene, 3);
    bestChrom.setFitnessValue(12);
    toAddFrom.addChromosome(bestChrom);
    // add third chromosome
    // --------------------
    gene = new IntegerGene(conf);
    gene.setAllele(new Integer(444));
    Chromosome secondBestChrom = new Chromosome(conf, gene, 2);
    secondBestChrom.setFitnessValue(11);
    toAddFrom.addChromosome(secondBestChrom);
    // receive top 1 (= best) chromosome
    // ---------------------------------
    Population popNew = new Population(conf);
    selector.select(1, toAddFrom, popNew);
    IChromosome[] bestChroms = popNew.toChromosomes();
    assertEquals(1, bestChroms.length);
    assertEquals(thirdBestChrom, bestChroms[0]);
    // now select top 4 chromosomes (should only select 3!)
    // ----------------------------------------------------
    popNew.getChromosomes().clear();
    selector.select(4, toAddFrom, popNew);
    bestChroms = popNew.toChromosomes();
    assertEquals(3, bestChroms.length);
  }

  /**
   * Ensure the scaling of fitness value works without error (like division
   * by zero)
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testSelect_3()
      throws Exception {
    DefaultConfiguration conf = new DefaultConfiguration();
    conf.addNaturalSelector(new WeightedRouletteSelector(), false);
    RandomGeneratorForTest randgen = new RandomGeneratorForTest();
    randgen.setNextDouble(0.0d);
    conf.setRandomGenerator(randgen);
    WeightedRouletteSelector selector = new WeightedRouletteSelector(conf);
    selector.setDoubletteChromosomesAllowed(false);
    Population toAddFrom = new Population(conf);
    // add first chromosome
    // --------------------
    Gene gene = new BooleanGene(conf);
    gene.setAllele(Boolean.valueOf(true));
    Chromosome thirdBestChrom = new Chromosome(conf, gene, 4);
    thirdBestChrom.setFitnessValue(0);
    toAddFrom.addChromosome(thirdBestChrom);
    // add second chromosome
    // ---------------------
    gene = new DoubleGene(conf);
    gene.setAllele(new Double(2.3d));
    Chromosome bestChrom = new Chromosome(conf, gene, 3);
    bestChrom.setFitnessValue(1.0d);
    toAddFrom.addChromosome(bestChrom);
    // add third chromosome
    // ---------------------
    gene = new IntegerGene(conf);
    gene.setAllele(new Integer(444));
    Chromosome secondBestChrom = new Chromosome(conf, gene, 2);
    secondBestChrom.setFitnessValue( -1.0d);
    toAddFrom.addChromosome(secondBestChrom);
    // receive top 3 chromosomes, no error should occur
    // ------------------------------------------------
    Population popNew = new Population(conf);
    selector.select(3, toAddFrom, popNew);
  }

  /**
   * Use DeltaFitnessEvaluator
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.4
   */
  public void testSelect_4()
      throws Exception {
    conf.setFitnessEvaluator(new DeltaFitnessEvaluator());
    RandomGeneratorForTest randgen = new RandomGeneratorForTest();
    randgen.setNextDouble(0.9999d);
    conf.setRandomGenerator(randgen);
    WeightedRouletteSelector selector = new WeightedRouletteSelector(conf);
    selector.setDoubletteChromosomesAllowed(false);
    Population toAddFrom = new Population(conf);
    // add first chromosome
    // --------------------
    Gene gene = new BooleanGene(conf);
    gene.setAllele(Boolean.valueOf(true));
    Chromosome thirdBestChrom = new Chromosome(conf, gene, 4);
    thirdBestChrom.setFitnessValue(1);
    toAddFrom.addChromosome(thirdBestChrom);
    // add second chromosome
    // ---------------------
    gene = new DoubleGene(conf);
    gene.setAllele(new Double(2.3d));
    Chromosome secondBestChrom = new Chromosome(conf, gene, 3);
    secondBestChrom.setFitnessValue(2);
    toAddFrom.addChromosome(secondBestChrom);
    // add third chromosome
    // --------------------
    gene = new IntegerGene(conf);
    gene.setAllele(new Integer(444));
    Chromosome bestChrom = new Chromosome(conf, gene, 2);
    bestChrom.setFitnessValue(3);
    toAddFrom.addChromosome(bestChrom);
    // receive top 1 (= best) chromosome
    // ---------------------------------
    Population popNew = new Population(conf);
    selector.select(1, toAddFrom, popNew);
    IChromosome[] bestChroms = popNew.toChromosomes();
    assertEquals(1, bestChroms.length);
    assertEquals(bestChrom, bestChroms[0]);
    // now select top 4 chromosomes (should only select 3!)
    // ----------------------------------------------------
    popNew.getChromosomes().clear();
    selector.select(4, toAddFrom, popNew);
    bestChroms = popNew.toChromosomes();
    assertEquals(3, bestChroms.length);
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testEmpty_0()
      throws Exception {
    WeightedRouletteSelector selector = new WeightedRouletteSelector();
    Configuration conf = new DefaultConfiguration();
    conf.setPopulationSize(7);
    conf.setFitnessFunction(new TestFitnessFunction());
    Gene gene = new BooleanGene(conf);
    Chromosome chrom = new Chromosome(conf, gene, 5);
    conf.setSampleChromosome(chrom);
    selector.add(chrom);
    selector.empty();
    Map chromosomes = (Map) privateAccessor.getField(selector, "m_wheel");
    assertEquals(0, chromosomes.size());
  }

  /**
   * Test if clear()-method does not affect original Population.
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testEmpty_1()
      throws Exception {
    Configuration conf = new DefaultConfiguration();
    WeightedRouletteSelector selector = new WeightedRouletteSelector(conf);
    Gene gene = new BooleanGene(conf);
    Chromosome chrom = new Chromosome(conf, gene, 5);
    chrom.setFitnessValue(3);
    Population pop = new Population(conf, 1);
    pop.addChromosome(chrom);
    selector.add(chrom);
    Population popNew = new Population(conf);
    selector.select(1, null, popNew);
    selector.empty();
    assertEquals(1, popNew.size());
  }

  /**
   * Test if clear()-method does not affect return value.
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testEmpty_2()
      throws Exception {
    Configuration conf = new DefaultConfiguration();
    WeightedRouletteSelector selector = new WeightedRouletteSelector(conf);
    Gene gene = new BooleanGene(conf);
    Chromosome chrom = new Chromosome(conf, gene, 5);
    chrom.setFitnessValue(7);
    Population pop = new Population(conf, 1);
    pop.addChromosome(chrom);
    selector.add(chrom);
    Population popNew = new Population(conf);
    selector.select(1, null, popNew);
    selector.empty();
    assertEquals(1, popNew.size());
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testReturnsUniqueChromosomes_0() {
    WeightedRouletteSelector selector = new WeightedRouletteSelector();
    assertFalse(selector.returnsUniqueChromosomes());
  }

  /**
   * Ensures WRS is implementing Serializable.
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public void testIsSerializable_0()
      throws Exception {
    WeightedRouletteSelector selector = new WeightedRouletteSelector();
    assertTrue(isSerializable(selector));
  }

  /**
   * Ensures that WRS implements Serializable
   * correctly.
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public void testDoSerialize_0()
      throws Exception {
    WeightedRouletteSelector selector = new WeightedRouletteSelector(conf);
    Object o = doSerialize(selector);
    assertEquals(o, selector);
  }

  /**@todo add test*/
//  public void test_WeightedSelection_0() {
//    WeightedRouletteSelector ws = new WeightedRouletteSelector();
//    // set a population of size 2
//    int numCrossovers = 100;
//    int crossoversSoFar = 0;
//    while (crossoversSoFar < numCrossovers) {
//      ws.select(2, a_population, parents);
//      Chromosome mother = parents.getChromosome(0);
//      Chromosome father = parents.getChromosome(1);
//      parents.removeChromosome(1);
//      parents.removeChromosome(0);
//      if (father.equals(mother)) {
//        continue;
//      }
//      // I had some crossover function here yet you don't need it for the test case.
//      crossoversSoFar++;
//    }
//  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -