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

📄 twowaymutationoperatortest.java

📁 一个开源的用java开发的遗传算法的封装好的工程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    assertEquals(new Integer(0), c1.getGene(0).getAllele());
    assertEquals(new Integer(1), c1.getGene(1).getAllele());
    assertEquals(new Integer(2 + 4), c1.getGene(2).getAllele());
    // test chromosome 2 - 2nd gene should be different
    Chromosome c2 = (Chromosome) chroms.get(1);
    assertEquals(new Integer(3), c2.getGene(0).getAllele());
    assertEquals(new Integer(4), c2.getGene(1).getAllele());
    assertEquals(new Integer(5 + 4), c2.getGene(2).getAllele());
    op.operate(pop, chroms);
    assertEquals(3, chroms.size());
  }

  /**
   * Ensure that nothing is done.
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.1
   */
  public void testOperate_4()
      throws Exception {
    DefaultConfiguration conf = new DefaultConfiguration();
    TwoWayMutationOperator mutOp = new TwoWayMutationOperator(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 3.1
   */
  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};
    TwoWayMutationOperator mutOp = new TwoWayMutationOperator(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 3.1
   */
  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};
    TwoWayMutationOperator mutOp = new TwoWayMutationOperator(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 3.1
   */
  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 3.1
   */
  public void testOperate_6_2()
      throws Exception {
    Configuration conf = new DefaultConfiguration();
    TwoWayMutationOperator mutOp = new TwoWayMutationOperator(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 3.1
   */
  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};
    TwoWayMutationOperator mutOp = new TwoWayMutationOperator(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 3.1
   */
  public void testIsSerializable_0()
      throws Exception {
    TwoWayMutationOperator op = new TwoWayMutationOperator(conf);
    assertTrue(isSerializable(op));
  }

  /**
   * Ensures that operator and all objects contained implement Serializable.
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.1
   */
  public void testDoSerialize_0()
      throws Exception {
    // construct object to be serialized
    IUniversalRateCalculator calc = new DefaultCrossoverRateCalculator(conf);
    TwoWayMutationOperator op = new TwoWayMutationOperator(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 3.1
   */
  public void testEquals_0()
      throws Exception {
    GeneticOperator op = new TwoWayMutationOperator(conf);
    assertFalse(op.equals(new Chromosome(conf)));
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.1
   */
  public void testCompareTo_0()
      throws Exception {
    TwoWayMutationOperator op = new TwoWayMutationOperator(conf);
    assertEquals(1, op.compareTo(null));
    TwoWayMutationOperator op2 = new TwoWayMutationOperator(conf);
    assertEquals(0, op.compareTo(op2));
    op = new TwoWayMutationOperator(conf, 3);
    assertEquals( -1, op.compareTo(op2));
    assertEquals(1, op2.compareTo(op));
    op = new TwoWayMutationOperator(conf,
                                    new DefaultMutationRateCalculator(conf));
    assertEquals(0, op.compareTo(op2));
    op = new TwoWayMutationOperator(conf, 3);
    op2 = new TwoWayMutationOperator(conf, 4);
    assertEquals( -1, op.compareTo(op2));
    assertEquals(1, op2.compareTo(op));
  }
}

⌨️ 快捷键说明

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