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

📄 crossoveroperatortest.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 CrossoverOperator class.
 *
 * @author Klaus Meffert
 * @since 1.1
 */
public class CrossoverOperatorTest
    extends JGAPTestCase {
  /** String containing the CVS revision. Read out via reflection!*/
  private static final String CVS_REVISION = "$Revision: 1.27 $";

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

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

  /**
   * Following should be possible without error.
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testConstruct_0()
      throws Exception {
    new CrossoverOperator(conf, null);
    new CrossoverOperator(conf, new DefaultMutationRateCalculator(conf));
    new CrossoverOperator(conf, 2);
    new CrossoverOperator(new DefaultConfiguration(), 1);
    new CrossoverOperator(conf, 50);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testConstruct_1()
      throws Exception {
    try {
      new CrossoverOperator(conf, 0);
      fail();
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testConstruct_2()
      throws Exception {
    try {
      new CrossoverOperator(new DefaultConfiguration(), -3);
      fail();
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   * Use flat crossover rate and just exchange two alleles via crossover.
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 1.1
   */
  public void testOperate_0()
      throws Exception {
    DefaultConfiguration conf = new DefaultConfiguration();
    // preset "random" values: index first chromosome, index second chromosome,
    // locus (index of gene on chromosome)
    RandomGeneratorForTest rand = new RandomGeneratorForTest();
    rand.setNextIntSequence(new int[] {
                            0, 1, 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, 100);
    cgene1.setAllele(new Integer(66));
    Gene[] genes1 = new Gene[] {
        cgene1};
    Chromosome chrom1 = new Chromosome(conf, genes1);
    Gene cgene2 = new IntegerGene(conf, 1, 100);
    cgene2.setAllele(new Integer(88));
    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);
    Gene gene3 = new IntegerGene(conf, 1, 10);
    gene3.setAllele(new Integer(4));
    chroms.add(gene3);
    CrossoverOperator op = new CrossoverOperator(conf);
    // following crossover-operation should exchange alleles of cgene1 and
    // cgene2.
    op.operate(new Population(conf, population), chroms);
    // chroms size = 3 + 2 genes
    //               3 = number of already existent genes
    //               2 = number of genes added from "population" (which contains
    //                   2 genes)
    assertEquals(5, chroms.size());
    // get Gene 3 = first new gene (0..2 = old genes, 3..4 = new genes)
    Chromosome target = (Chromosome) chroms.get(3);
    // 88 = allele of cgene2
    assertEquals(88, ( (Integer) target.getGene(0).getAllele()).intValue());
    // get Gene 4 = second new gene (0..2 = old genes, 3..4 = new genes)
    target = (Chromosome) chroms.get(4);
    // 66 = allele of cgene1
    assertEquals(66, ( (Integer) target.getGene(0).getAllele()).intValue());
  }

  /**
   * Consider crossover rate calculator.
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testOperate_0_2()
      throws Exception {
    DefaultConfiguration conf = new DefaultConfiguration();
//    conf.addGeneticOperator(op);
    // preset "random" values: index first chromosome, index second chromosome,
    // locus (index of gene on chromosome)
    RandomGeneratorForTest rand = new RandomGeneratorForTest();
    rand.setNextIntSequence(new int[] {
                            0, 1, 0});
    conf.setRandomGenerator(rand);
    conf.setFitnessFunction(new TestFitnessFunction());
    Gene sampleGene = new IntegerGene(conf, 1, 10);
    Chromosome chrom = new Chromosome(conf, sampleGene, 2);
    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(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);
    CrossoverOperator op = new CrossoverOperator(conf,
                                                 new
                                                 DefaultCrossoverRateCalculator(
        conf));
    op.operate(new Population(conf, population), chroms);
    // chroms size = 1 + 2 genes
    //               1 = number of already existent genes
    //               2 = number of genes added from "population" (which contains
    //                   2 genes)
    assertEquals(1 + 2, chroms.size());
    // get Gene 1 = first new gene (0..0 = old genes, 1..2 = new genes)
    Chromosome target = (Chromosome) chroms.get(1);
    assertEquals(8, ( (Integer) target.getGene(0).getAllele()).intValue());
    // get Gene 2 = second new gene (0..0 = old genes, 1..2 = new genes)
    target = (Chromosome) chroms.get(2);
    assertEquals(6, ( (Integer) target.getGene(0).getAllele()).intValue());
    // chroms size = 1 + 2 + 2 genes
    //               1 + 2 = number of already existent genes
    //               2     = number of genes added from "population" (which
    //                       contains 2 genes)
    op.operate(new Population(conf, population), chroms);
    assertEquals(1 + 2 + 2, chroms.size());
  }

  /**
   * Tests if crossing over produces same results for two operate-runs.
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.0
   */
  public void testOperate_1()
      throws Exception {
    DefaultConfiguration conf = new DefaultConfiguration();
//    conf.addGeneticOperator(op);
    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));
    Gene[] genes1 = new Gene[] {
        cgene1};
    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);

⌨️ 快捷键说明

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