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

📄 tournamentselectortest.java

📁 java实现的遗传算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    selector.add(bestChrom);
    // add third chromosome
    // ---------------------
    gene = new IntegerGene();
    gene.setAllele(new Integer(444));
    Chromosome secondBestChrom = new Chromosome(gene, 3);
    secondBestChrom.setFitnessValue(11);
    selector.add(secondBestChrom);
    // receive top 1 (= best) chromosome
    // ---------------------------------
    Population pop = new Population();
    selector.select(1, null, pop);
    Chromosome[] bestChroms = pop.toChromosomes();
    assertEquals(1, bestChroms.length);
    assertEquals(bestChrom, bestChroms[0]);
    // receive top 3 chromosomes
    // -------------------------
    pop.getChromosomes().clear();
    selector.select(3, null, pop);
    bestChroms = pop.toChromosomes();
    assertEquals(3, bestChroms.length);
    assertEquals(bestChrom, bestChroms[0]);
    assertEquals(bestChrom, bestChroms[1]);
    assertEquals(bestChrom, bestChroms[2]);
  }

  /**
   * Always select best chromosome for granted if prob is 1.0 and index for
   * selected chromosomes in tournament is equal to index of best chromosome.
   *
   * @throws Exception
   * @author Klaus Meffert
   * @since 2.1
   */
  public void testSelect_2()
      throws Exception {
    Configuration conf = new DefaultConfiguration();
    // random generator always returning 1 (index of best chromosome below)
    RandomGeneratorForTest rn = new RandomGeneratorForTest(1);
    conf.setRandomGenerator(rn);
    Genotype.setConfiguration(conf);
    TournamentSelector selector = new TournamentSelector(4, 1.0d);
    // add first chromosome
    // --------------------
    Gene gene = new BooleanGene();
    gene.setAllele(new Boolean(true));
    Chromosome thirdBestChrom = new Chromosome(gene, 7);
    thirdBestChrom.setFitnessValue(10);
    selector.add(thirdBestChrom);
    // add second chromosome
    // ---------------------
    gene = new BooleanGene();
    gene.setAllele(new Boolean(false));
    Chromosome bestChrom = new Chromosome(gene, 3);
    bestChrom.setFitnessValue(12);
    selector.add(bestChrom);
    // receive top 1 (= best) chromosome
    // ---------------------------------
    Population pop = new Population();
    selector.select(1, null, pop);
    Chromosome[] bestChroms = pop.toChromosomes();
    assertEquals(1, bestChroms.length);
    assertEquals(bestChrom, bestChroms[0]);
    // receive top 30 chromosomes.
    // ---------------------------
    pop.getChromosomes().clear();
    selector.select(30, null, pop);
    bestChroms = pop.toChromosomes();
    assertEquals(30, bestChroms.length);
    assertEquals(bestChrom, bestChroms[0]);
    assertEquals(bestChrom, bestChroms[1]);
    assertTrue(bestChrom == bestChroms[0]);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.1
   */
  public void testSelect_3()
      throws Exception {
    Configuration conf = new DefaultConfiguration();
    //Set index of chromosome to be selected by ThresholdSelector to 1.
    //1 because the best chromosome will be index 0 and the other one has
    // index 1.
    RandomGeneratorForTest rn = new RandomGeneratorForTest(0);
    rn.setNextDouble(0.0d);
    conf.setRandomGenerator(rn);
    Genotype.setConfiguration(conf);
    TournamentSelector selector = new TournamentSelector(2, 1.0d);
    // add first chromosome
    // --------------------
    Gene gene = new BooleanGene();
    gene.setAllele(new Boolean(true));
    Chromosome thirdBestChrom = new Chromosome(gene, 7);
    thirdBestChrom.setFitnessValue(10);
    selector.add(thirdBestChrom);
    // add second chromosome
    // ---------------------
    gene = new BooleanGene();
    gene.setAllele(new Boolean(false));
    Chromosome bestChrom = new Chromosome(gene, 3);
    bestChrom.setFitnessValue(12);
    selector.add(bestChrom);
    // receive top 1 (= best) chromosome
    // ---------------------------------
    Population pop = new Population();
    selector.select(1, null, pop);
    Chromosome[] bestChroms = pop.toChromosomes();
    assertFalse(bestChroms[0].equals(bestChrom));
  }

  /**
   * Ensure that selected Chromosome's are not equal to added Chromosome's.
   *
   * @throws Exception
   * @author Klaus Meffert
   * @since 2.1
   */
  public void testSelect_4()
      throws Exception {
    Configuration conf = new DefaultConfiguration();
    Genotype.setConfiguration(conf);
    TournamentSelector selector = new TournamentSelector(1, 0.2d);
    // add first chromosome
    // --------------------
    Gene gene = new BooleanGene();
    gene.setAllele(new Boolean(true));
    Chromosome thirdBestChrom = new Chromosome(gene, 7);
    thirdBestChrom.setFitnessValue(10);
    selector.add(thirdBestChrom);
    // add second chromosome
    // ---------------------
    gene = new BooleanGene();
    gene.setAllele(new Boolean(false));
    Chromosome bestChrom = new Chromosome(gene, 3);
    bestChrom.setFitnessValue(12);
    selector.add(bestChrom);
    // receive top 30 chromosomes.
    // ---------------------------
    Population pop = new Population();
    selector.select(30, null, pop);
    Population bestChroms = pop;
    List chromosomes = (Vector) privateAccessor.getField(selector,
        "m_chromosomes");
    assertFalse(bestChroms.equals(chromosomes));
  }

  /**
   * Never select best chromosome if prob is 0.0d. it is not allowed to select
   * probability to 0.0d therefor we set it via reflection.
   *
   * @throws Exception
   * @author Klaus Meffert
   * @since 2.1
   */
  public void testSelect_5()
      throws Exception {
    Configuration conf = new DefaultConfiguration();
    RandomGeneratorForTest rn = new RandomGeneratorForTest(0);
    conf.setRandomGenerator(rn);
    Genotype.setConfiguration(conf);
    TournamentSelector selector = new TournamentSelector(4, 0.00001d);
    privateAccessor.setField(selector, "m_probability", new Double(0.0d));
    // add first chromosome
    // --------------------
    Gene gene = new BooleanGene();
    gene.setAllele(new Boolean(true));
    Chromosome thirdBestChrom = new Chromosome(gene, 7);
    thirdBestChrom.setFitnessValue(10);
    selector.add(thirdBestChrom);
    // add second chromosome
    // ---------------------
    gene = new BooleanGene();
    gene.setAllele(new Boolean(false));
    Chromosome bestChrom = new Chromosome(gene, 3);
    bestChrom.setFitnessValue(12);
    selector.add(bestChrom);
    // receive top 30 chromosomes.
    // ---------------------------
    Population pop = new Population();
    Chromosome[] bestChroms;
    selector.select(5, null, pop);
    bestChroms = pop.toChromosomes();
    assertEquals(thirdBestChrom, bestChroms[0]);
    assertEquals(thirdBestChrom, bestChroms[1]);
  }

  /**
   * @throws Exception
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testSelect_6()
      throws Exception {
    Configuration conf = new DefaultConfiguration();
    // random generator always returning 1 (index of best chromosome below)
    RandomGeneratorForTest rn = new RandomGeneratorForTest(1);
    conf.setRandomGenerator(rn);
    Genotype.setConfiguration(conf);

    TournamentSelector selector = new TournamentSelector(4, 1.0d);

    Population toAddFrom = new Population();

    // add first chromosome
    // --------------------
    Gene gene = new BooleanGene();
    gene.setAllele(new Boolean(true));
    Chromosome thirdBestChrom = new Chromosome(gene, 7);
    thirdBestChrom.setFitnessValue(10);
    toAddFrom.addChromosome(thirdBestChrom);
    // add second chromosome
    // ---------------------
    gene = new BooleanGene();
    gene.setAllele(new Boolean(false));
    Chromosome bestChrom = new Chromosome(gene, 3);
    bestChrom.setFitnessValue(12);
    toAddFrom.addChromosome(bestChrom);
    // add third chromosome
    // ---------------------
    gene = new IntegerGene();
    gene.setAllele(new Integer(444));
    Chromosome secondBestChrom = new Chromosome(gene, 3);
    secondBestChrom.setFitnessValue(11);
    toAddFrom.addChromosome(secondBestChrom);
    // receive top 1 (= best) chromosome
    // ---------------------------------
    Population pop = new Population();
    selector.select(1, null, pop);
    Chromosome[] bestChroms = pop.toChromosomes();
    // nothing selected (from nothing!)
    assertEquals(0, bestChroms.length);
    selector.select(1, toAddFrom, pop);
    bestChroms = pop.toChromosomes();
    assertEquals(1, bestChroms.length);
    assertEquals(bestChrom, bestChroms[0]);
    // receive top 3 chromosomes
    // -------------------------
    pop.getChromosomes().clear();
    selector.select(3, toAddFrom, pop);
    bestChroms = pop.toChromosomes();
    assertEquals(3, bestChroms.length);
    assertEquals(bestChrom, bestChroms[0]);
    assertEquals(bestChrom, bestChroms[1]);
    assertEquals(bestChrom, bestChroms[2]);
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testReturnsUniqueChromosomes_0() {
    TournamentSelector selector = new TournamentSelector(2, 0.5d);
    assertFalse(selector.returnsUniqueChromosomes());
  }
}

⌨️ 快捷键说明

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