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

📄 evaluatortest.java

📁 JGAP(发音"jay-gap")是一款用Java编写的遗传算法包。提供了基本的遗传算法.你可以使用它来解决一些适用于遗传算法解决的问题.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    pop10.addChromosome(chrom);
    chrom = new Chromosome(conf, genes);
    chrom.setFitnessValue(19);
    pop10.addChromosome(chrom);
    genotype = new Genotype(conf, pop10);
    eval.storeGenotype(0, 1, genotype);
    // run 0, permutation 1
    Population pop01 = new Population(conf);
    genes = new Gene[1];
    gene = new BooleanGene();
    genes[0] = gene;
    chrom = new Chromosome(conf, genes);
    chrom.setFitnessValue(4);
    pop01.addChromosome(chrom);
    chrom = new Chromosome(conf, genes);
    chrom.setFitnessValue(9);
    pop01.addChromosome(chrom);
    chrom = new Chromosome(conf, genes);
    chrom.setFitnessValue(8);
    pop01.addChromosome(chrom);
    genotype = new Genotype(conf, pop01);
    eval.storeGenotype(1, 0, genotype);
    // run 1, permutation 1
    Population pop11 = new Population(conf);
    genes = new Gene[1];
    gene = new BooleanGene();
    genes[0] = gene;
    chrom = new Chromosome(conf, genes);
    chrom.setFitnessValue(14);
    pop11.addChromosome(chrom);
    chrom = new Chromosome(conf, genes);
    chrom.setFitnessValue(11);
    pop11.addChromosome(chrom);
    chrom = new Chromosome(conf, genes);
    chrom.setFitnessValue(28);
    pop11.addChromosome(chrom);
    genotype = new Genotype(conf, pop11);
    eval.storeGenotype(1, 1, genotype);
    Evaluator.GenotypeDataAvg avg = eval.calcPerformance(0);
    assertEquals(Math.max(pop00.determineFittestChromosome().getFitnessValue(),
                          pop10.determineFittestChromosome().getFitnessValue()),
                 avg.bestFitnessValue, DELTA);
    assertEquals(0, avg.bestFitnessValueGeneration);
    assertEquals( ( (7.3 + 4.8 + 11.4) / 3) / 2
                 + ( (7.0d + 17.0d + 19) / 3) / 2, avg.avgFitnessValue, DELTA);
    assertEquals( (Math.abs(4.8 - 7.3) / 2 + Math.abs(11.4 - 4.8) / 2) / 2
                 + (Math.abs(17.0 - 7) / 2 + Math.abs(19.0 - 17) / 2) / 2,
                 avg.avgDiversityFitnessValue, DELTA);
    assertEquals( (Math.abs(19 - 11.4d)) / 1, avg.avgBestDeltaFitnessValue,
                 DELTA);
    assertEquals( (double) 3 / 2 + (double) 3 / 2, avg.sizeAvg, DELTA);
    avg = eval.calcPerformance(1);
    assertEquals(Math.max(pop01.determineFittestChromosome().getFitnessValue(),
                          pop11.determineFittestChromosome().getFitnessValue()),
                 avg.bestFitnessValue, DELTA);
    assertEquals(0, avg.bestFitnessValueGeneration);
    assertEquals( ( (4 + 9 + 8.0d) / 3) / 2
                 + ( (14 + 11.0d + 28) / 3) / 2, avg.avgFitnessValue, DELTA);
    assertEquals( (Math.abs(9.0d - 4) / 2 + Math.abs(8.0d - 9) / 2) / 2
                 + (Math.abs(11.0d - 14) / 2 + Math.abs(28.0d - 11) / 2) / 2,
                 avg.avgDiversityFitnessValue, DELTA);
    assertEquals( (Math.abs(28 - 9)) / (double) 1, avg.avgBestDeltaFitnessValue,
                 DELTA);
    assertEquals( (double) 3 / 2 + (double) 3 / 2, avg.sizeAvg, DELTA);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testConstruct_0()
      throws Exception {
    try {
      Evaluator eval = new Evaluator(null);
      fail();
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testConstruct_1()
      throws Exception {
    Configuration conf = new ConfigurationForTest();
    PermutingConfiguration pconf = new PermutingConfiguration(conf);
    Evaluator eval = new Evaluator(pconf);
    assertEquals(0, eval.getData().getRowCount());
    assertEquals(0, eval.getData().getColumnCount());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testHasNext_0()
      throws Exception {
    Configuration conf = new ConfigurationForTest();
    PermutingConfiguration pconf = new PermutingConfiguration(conf);
    Evaluator eval = new Evaluator(pconf);
    assertEquals(pconf.hasNext(), eval.hasNext());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testNext_0()
      throws Exception {
    Configuration conf = new ConfigurationForTest();
    PermutingConfiguration pconf = new PermutingConfiguration(conf);
    pconf.addGeneticOperatorSlot(new MutationOperator(conf));
    pconf.addRandomGeneratorSlot(new StockRandomGenerator());
    pconf.addFitnessFunctionSlot(new TestFitnessFunction());
    pconf.addNaturalSelectorSlot(new BestChromosomesSelector(conf));
    Evaluator eval = new Evaluator(pconf);
    assertTrue(eval.hasNext());
    Configuration config = eval.next();
    assertNotNull(config);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testSetValue_0()
      throws Exception {
    Configuration conf = new ConfigurationForTest();
    PermutingConfiguration pconf = new PermutingConfiguration(conf);
    Evaluator eval = new Evaluator(pconf);
    Comparable rowKey = new Integer(4);
    Comparable colKey = new Integer(6);
    double value = 2.3d;
    eval.setValue(value, rowKey, colKey);
    assertEquals(value, eval.getValue(rowKey, colKey).doubleValue(), DELTA);
    assertNull(eval.getValue(rowKey, rowKey));
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testSetValue_1()
      throws Exception {
    Configuration conf = new ConfigurationForTest();
    PermutingConfiguration pconf = new PermutingConfiguration(conf);
    Evaluator eval = new Evaluator(pconf);
    Comparable rowKey = new Integer(4);
    Comparable colKey = new Integer(6);
    double value = 2.3d;
    eval.setValue(1, 2, value, rowKey, colKey);
    assertEquals(value, eval.getValue(1, 2, rowKey, colKey).doubleValue(), DELTA);
    assertNull(eval.getValue(1,1, rowKey, colKey));
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testSetValue_2()
      throws Exception {
    Configuration conf = new ConfigurationForTest();
    PermutingConfiguration pconf = new PermutingConfiguration(conf);
    Evaluator eval = new Evaluator(pconf);
    Comparable rowKey = new Integer(4);
    Comparable colKey = new Integer(6);
    double value = 2.3d;
    eval.setValue(1, 2, value, rowKey, colKey);
    double value2 = 4.8d;
    eval.setValue(2, 2, value2, rowKey, colKey);
    assertEquals(value, eval.getValue(1, 2, rowKey, colKey).doubleValue(), DELTA);
    assertEquals(value2, eval.getValue(2, 2, rowKey, colKey).doubleValue(), DELTA);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testSetValue_3()
      throws Exception {
    Configuration conf = new ConfigurationForTest();
    PermutingConfiguration pconf = new PermutingConfiguration(conf);
    Evaluator eval = new Evaluator(pconf);
    Comparable rowKey = new Integer(4);
    Comparable colKey = new Integer(6);
    double value = 2.3d;
    eval.setValue(1, 2, value, rowKey, colKey);
    double value2 = 4.8d;
    eval.setValue(1, 2, value2, rowKey, colKey);
    assertEquals(value2, eval.getValue(1, 2, rowKey, colKey).doubleValue(), DELTA);
  }
}

⌨️ 快捷键说明

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