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

📄 bestchromosomesselectortest.java

📁 一个开源的用java开发的遗传算法的封装好的工程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    Chromosome thirdBestChrom = new Chromosome(conf, gene, 7);
    thirdBestChrom.setFitnessValue(10);
    selector.add(thirdBestChrom);
    // add second chromosome
    // ---------------------
    gene = new BooleanGene(conf);
    gene.setAllele(Boolean.valueOf(false));
    Chromosome bestChrom = new Chromosome(conf, gene, 3);
    bestChrom.setFitnessValue(12);
    selector.add(bestChrom);
    selector.setOriginalRate(1.0d);
    // receive top 30 chromosomes (select-method should take into account only
    // 2 chroms!)
    // -----------------------------------------------------------------------
    Population pop = new Population(conf);
    selector.select(30, null, pop);
    Population bestChroms = pop;
    Population chromosomes = (Population) privateAccessor.getField(selector,
        "m_chromosomes");
    assertTrue(bestChroms.equals(chromosomes));
  }

  /**
   * Test selection algorithm with allowed doublettes.
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 1.1
   */
  public void testSelect_4()
      throws Exception {
    BestChromosomesSelector selector = new BestChromosomesSelector(conf);
    selector.setDoubletteChromosomesAllowed(true);
    // the following original rate controls that only 30% of the chromosomes
    // will be considered for selection as given with BestChromosomesSelector.
    // The last 70% will be added as doublettes in this case.
    selector.setOriginalRate(0.3d);
    // add first chromosome
    // --------------------
    Gene gene = new BooleanGene(conf);
    gene.setAllele(Boolean.valueOf(true));
    Chromosome thirdBestChrom = new Chromosome(conf, gene, 7);
    thirdBestChrom.setFitnessValue(10);
    selector.add(thirdBestChrom);
    // add second chromosome
    // ---------------------
    gene = new BooleanGene(conf);
    gene.setAllele(Boolean.valueOf(false));
    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, 3);
    secondBestChrom.setFitnessValue(11);
    selector.add(secondBestChrom);
    // receive top 1 (= best) chromosome
    // ---------------------------------
    Population pop = new Population(conf);
    selector.select(1, null, pop);
    IChromosome[] bestChroms = pop.toChromosomes();
    assertEquals(1, bestChroms.length);
    assertEquals(bestChrom, bestChroms[0]);
    // receive top 4 chromosomes with original rate = 0.3
    // --------------------------------------------------
    pop.getChromosomes().clear();
    selector.select(4, null, pop);
    bestChroms = pop.toChromosomes();
    assertEquals(4, bestChroms.length);
    assertEquals(bestChrom, bestChroms[0]);
    assertEquals(bestChrom, bestChroms[1]); //because of originalRate = 0.3
    assertEquals(secondBestChrom, bestChroms[2]);
    assertEquals(thirdBestChrom, bestChroms[3]);
    // non-unique chromosomes should have been returned
    assertSame(bestChroms[0], bestChroms[1]);
    // receive top 4 chromosomes with original rate = 1
    // ------------------------------------------------
    pop.getChromosomes().clear();
    selector.setOriginalRate(1.0d);
    selector.select(4, null, pop);
    bestChroms = pop.toChromosomes();
    assertEquals(4, bestChroms.length);
    assertEquals(bestChrom, bestChroms[0]);
    assertEquals(secondBestChrom, bestChroms[1]);
    assertEquals(thirdBestChrom, bestChroms[2]);
    assertEquals(bestChrom, bestChroms[3]);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 1.1
   */
  public void testEmpty_0()
      throws Exception {
    BestChromosomesSelector selector = new BestChromosomesSelector(conf);
    Gene gene = new BooleanGene(conf);
    Chromosome chrom = new Chromosome(conf, gene, 5);
    selector.add(chrom);
    selector.empty();
    Boolean needsSorting = (Boolean) privateAccessor.getField(selector,
        "m_needsSorting");
    assertEquals(Boolean.FALSE, needsSorting);
    List chromosomes = ( (Population) privateAccessor.getField(selector,
        "m_chromosomes")).getChromosomes();
    assertEquals(0, chromosomes.size());
  }

  /**
   * Test if clear()-method does not affect original Population.
   *
   * @throws Exception
   * @author Klaus Meffert
   * @since 1.1
   */
  public void testEmpty_1()
      throws Exception {
    BestChromosomesSelector selector = new BestChromosomesSelector(conf);
    Gene gene = new BooleanGene(conf);
    Chromosome chrom = new Chromosome(conf, gene, 5);
    selector.add(chrom);
    Population popNew = new Population(conf);
    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 1.1
   */
  public void testEmpty_2()
      throws Exception {
    BestChromosomesSelector selector = new BestChromosomesSelector(conf);
    Gene gene = new BooleanGene(conf);
    Chromosome chrom = new Chromosome(conf, gene, 5);
    Population pop = new Population(conf, 1);
    pop.addChromosome(chrom);
    Population popNew = new Population(conf);
    selector.select(1, pop, popNew);
    selector.empty();
    assertEquals(1, popNew.size());
    assertNotNull(popNew.getChromosome(0));
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testSetOriginalRate_0()
      throws Exception {
    BestChromosomesSelector selector = new BestChromosomesSelector(conf);
    try {
      selector.setOriginalRate(1.01d);
      fail();
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testSetOriginalRate_0_1()
      throws Exception {
    BestChromosomesSelector selector = new BestChromosomesSelector(conf);
    try {
      selector.setOriginalRate( -0.1d);
      fail();
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testSetOriginalRate_1()
      throws Exception {
    BestChromosomesSelector selector = new BestChromosomesSelector(conf);
    selector.setOriginalRate(0.3d);
    assertEquals(0.3d, selector.getOriginalRate(), DELTA);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testReturnsUnique_0()
      throws Exception {
    BestChromosomesSelector selector = new BestChromosomesSelector(conf);
    assertTrue(selector.returnsUniqueChromosomes());
  }

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

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

⌨️ 快捷键说明

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