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

📄 mutationoperatortest.java

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

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

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

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testConstruct_0()
      throws Exception {
    MutationOperator mutOp = new MutationOperator(conf, 234);
    assertEquals(234, mutOp.getMutationRate());
    assertNull(mutOp.getMutationRateCalc());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testConstruct_1()
      throws Exception {
    MutationOperator mutOp = new MutationOperator(conf);
    assertEquals(0, mutOp.getMutationRate());
    assertNotNull(mutOp.getMutationRateCalc());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testConstruct_2()
      throws Exception {
    MutationOperator mutOp = new MutationOperator(conf, null);
    assertEquals(0, mutOp.getMutationRate());
    assertNull(mutOp.getMutationRateCalc());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testConstruct_3()
      throws Exception {
    IUniversalRateCalculator calc = new DefaultMutationRateCalculator(conf);
    MutationOperator mutOp = new MutationOperator(conf, calc);
    assertEquals(0, mutOp.getMutationRate());
    assertEquals(calc, mutOp.getMutationRateCalc());
  }

  /**
   * Ensure that size of mutated set of chromosomes equals the size of original
   * chromosomes.
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testOperate_0()
      throws Exception {
    Configuration conf = new DefaultConfiguration();
    conf.setFitnessFunction(new TestFitnessFunction());
    MutationOperator mutOp = new MutationOperator(conf);
    List candChroms = new Vector();
    Chromosome[] population = new Chromosome[] {};
    mutOp.operate(new Population(conf, population), candChroms);
    assertEquals(candChroms.size(), population.length);
  }

  /**
   * Ensure that size of mutated set of chromosomes equals the size of original
   * chromosomes.
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testOperate_0_2()
      throws Exception {
    Configuration conf = new DefaultConfiguration();
    conf.setFitnessFunction(new TestFitnessFunction());
    MutationOperator mutOp = new MutationOperator(conf,
                                                  new
                                                  DefaultMutationRateCalculator(
        conf));
    List candChroms = new Vector();
    RandomGeneratorForTest gen = new RandomGeneratorForTest();
    gen.setNextInt(9);
    conf.setRandomGenerator(gen);
    Chromosome c1 = new Chromosome(conf, new BooleanGene(conf), 9);
    conf.setSampleChromosome(c1);
    conf.addNaturalSelector(new BestChromosomesSelector(conf), true);
    conf.setPopulationSize(5);
    for (int i = 0; i < c1.getGenes().length; i++) {
      c1.getGene(i).setAllele(Boolean.TRUE);
    }
    Chromosome c2 = new Chromosome(conf, new IntegerGene(conf), 4);
    for (int i = 0; i < c2.getGenes().length; i++) {
      c2.getGene(i).setAllele(new Integer(27));
    }
    Chromosome[] population = new Chromosome[] {
        c1, c2};
    mutOp.operate(new Population(conf, population), candChroms);
    assertEquals(candChroms.size(), population.length);
  }

  /**
   * Mutating with different types of genes contained in the population should
   * be possible without exception.
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testOperate_1()
      throws Exception {
    List candChroms = new Vector();
    Configuration conf = new Configuration();
    conf.setPopulationSize(3);
    conf.setRandomGenerator(new StockRandomGenerator());
    MutationOperator mutOp = new MutationOperator(conf,
                                                  new
                                                  DefaultMutationRateCalculator(
        conf));
    Chromosome[] population = new Chromosome[] {
        new Chromosome(conf, new BooleanGene(conf), 9),
        (new Chromosome(conf, new IntegerGene(conf), 4))};
    mutOp.operate(new Population(conf, population), candChroms);
  }

  /**
   * NullpointerException because of null Configuration.
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testOperate_2()
      throws Exception {
    MutationOperator mutOp = new MutationOperator(conf);
    List candChroms = new Vector();
    Chromosome[] population = new Chromosome[] {
        new Chromosome(conf, new BooleanGene(conf), 9),
        (new Chromosome(conf, new IntegerGene(conf), 4))};
    try {
      mutOp.operate(new Population(null, population), candChroms);
      fail();
    }
    catch (InvalidConfigurationException nex) {
      ; //this is OK
    }
  }

  /**
   * Tests if population size grows expectedly after two consecutive calls.
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.1
   */
  public void testOperate_3()
      throws Exception {
    DefaultConfiguration conf = new DefaultConfiguration();
    MutationOperator op = new MutationOperator(conf,
                                               new
                                               DefaultMutationRateCalculator(
        conf));
    conf.addGeneticOperator(op);
    RandomGeneratorForTest rand = new RandomGeneratorForTest();
    rand.setNextInt(0);
    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));
    Gene[] genes1 = new Gene[] {
        cgene1};
    Chromosome chrom1 = new Chromosome(conf, genes1);
    Gene cgene2 = new IntegerGene(conf, 1, 10);
    cgene2.setAllele(new Integer(9));
    Gene[] genes2 = new Gene[] {
        cgene2};
    Chromosome chrom2 = new Chromosome(conf, genes2);
    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);
    Gene gene3 = new IntegerGene(conf, 1, 10);
    gene3.setAllele(new Integer(4));
    chroms.add(gene3);
    assertEquals(3, chroms.size());
    Population pop = new Population(conf, population);
    op.operate(pop, chroms);
    assertEquals(2, pop.size());
    assertEquals(3 + 2, chroms.size());
    op.operate(pop, chroms);
    assertEquals(2, pop.size());
    assertEquals(3 + 2 + 2, chroms.size());
  }

  /**
   * @throws Exception
   *
   * @author Dan Clark
   * @since 2.6
   */
  public void testOperate_3_1()
      throws Exception {
    DefaultConfiguration conf = new DefaultConfiguration();
    GeneticOperator op = new MutationOperator(conf, 10);
    conf.addGeneticOperator(op);
    RandomGeneratorForTest rand = new RandomGeneratorForTest();
    // 0 in this sequence represents a gene to be mutated
    // thus, the middle gene of each chromosome should be mutated
    rand.setNextIntSequence(new int[] {1, 0, 1});
    rand.setNextDouble(0.7d);
    conf.setRandomGenerator(rand);
    conf.setFitnessFunction(new TestFitnessFunction());
    Gene sampleGene = new IntegerGene(conf, 0, 9);
    Chromosome chrom = new Chromosome(conf, sampleGene, 3);
    conf.setSampleChromosome(chrom);
    conf.setPopulationSize(6);
    Gene[] genes1 = new Gene[3];
    for (int i = 0; i < genes1.length; i++) {
      genes1[i] = new IntegerGene(conf, 0, 9);
      genes1[i].setAllele(new Integer(i));
    }
    Chromosome chrom1 = new Chromosome(conf, genes1);
    Gene[] genes2 = new Gene[3];
    for (int i = 0; i < genes2.length; i++) {
      genes2[i] = new IntegerGene(conf, 0, 9);
      genes2[i].setAllele(new Integer(i + 3));
    }
    Chromosome chrom2 = new Chromosome(conf, genes2);
    Chromosome[] population = new Chromosome[] {
        chrom1, chrom2};
    // this will cause an increase of 4 in mutated values
    // original + (0.7*2 - 1) * 10 (range of allele values)
    List chroms = new Vector();
    Population pop = new Population(conf, population);
    op.operate(pop, chroms);
    assertEquals(2, chroms.size());
    // test chromosome 1 - 2nd gene should be different
    Chromosome c1 = (Chromosome) chroms.get(0);
    assertEquals(new Integer(0), c1.getGene(0).getAllele());
    assertEquals(new Integer(1 + 4), c1.getGene(1).getAllele());
    assertEquals(new Integer(2), c1.getGene(2).getAllele());
    // test chromosome 2 - 2nd gene should be different

⌨️ 快捷键说明

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