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

📄 evaluatortest.java

📁 一个开源的用java开发的遗传算法的封装好的工程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    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);
  }

  /**
   * Test for overwriting an already set value.
   * @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);
  }

  /**
   * Consider one permutation.
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.1
   */
  public void testCalcAvgFitness_0()
      throws Exception {
    Configuration conf = new ConfigurationForTest();
    PermutingConfiguration pconf = new PermutingConfiguration(conf);
    Evaluator eval = new Evaluator(pconf);
    Comparable rowKey1 = new Integer(3);
    Comparable rowKey2 = new Integer(4);
    Comparable colKey = new Integer(6);
    // Run 1
    double value1 = 2.3d;
    eval.setValue(1, 1, value1, rowKey1, colKey);
    // Run 2
    double value2 = 4.8d;
    eval.setValue(1, 2, value2, rowKey2, colKey);
    KeyedValues2D fitnessvals = eval.calcAvgFitness(1);
    assertEquals(2, fitnessvals.getRowCount());
    assertEquals(1, fitnessvals.getColumnCount());
    Number val = fitnessvals.getValue(rowKey1, colKey);
    assertEquals(value1 / 2, val.doubleValue(), DELTA);
    val = fitnessvals.getValue(rowKey2, colKey);
    assertEquals(value2 / 2, val.doubleValue(), DELTA);
  }

  /**
   * Consider all permutations.
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.1
   */
  public void testCalcAvgFitness_1()
      throws Exception {
    Configuration conf = new ConfigurationForTest();
    PermutingConfiguration pconf = new PermutingConfiguration(conf);
    Evaluator eval = new Evaluator(pconf);
    Comparable rowKey1 = new Integer(3);
    Comparable rowKey2 = new Integer(4);
    Comparable colKey = new Integer(6);
    // Perm. 1, Run 1
    double value1 = 2.3d;
    eval.setValue(1, 1, value1, rowKey1, colKey);
    // Perm. 1, Run 2
    double value2 = 4.8d;
    eval.setValue(1, 2, value2, rowKey2, colKey);
    // Perm. 2, Run 1
    double value3 = 11.5d;
    eval.setValue(2, 1, value3, rowKey1, colKey);
    // Perm. 2, Run 2
    double value4 = 8.0d;
    eval.setValue(2, 2, value4, rowKey2, colKey);
    KeyedValues2D fitnessvals = eval.calcAvgFitness( -1);
    assertEquals(2, fitnessvals.getRowCount());
    assertEquals(1, fitnessvals.getColumnCount());
    Number val = fitnessvals.getValue(rowKey1, colKey);
    assertEquals(value3 / 2 + value1 / 2, val.doubleValue(), DELTA);
    val = fitnessvals.getValue(rowKey2, colKey);
    assertEquals(value2 / 2 + value4 / 2, val.doubleValue(), DELTA);
  }
}

⌨️ 快捷键说明

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