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

📄 doublegenetest.java

📁 java实现的遗传算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    try {
      gene1.setValueFromPersistentRepresentation("2.3" +
                                                 DoubleGene.
                                                 PERSISTENT_FIELD_DELIMITER +
                                                 "b" +
                                                 DoubleGene.
                                                 PERSISTENT_FIELD_DELIMITER +
                                                 "a");
      fail();
    }
    catch (UnsupportedRepresentationException uex) {
      ; //this is OK
    }
  }

  /**
   *
   * @throws Exception
   * @author Klaus Meffert
   */
  public void testPersistentRepresentation_5()
      throws Exception {
    Gene gene1 = new DoubleGene(2.05d, 7.53d);
    try {
      gene1.setValueFromPersistentRepresentation("a" +
                                                 DoubleGene.
                                                 PERSISTENT_FIELD_DELIMITER +
                                                 "b" +
                                                 DoubleGene.
                                                 PERSISTENT_FIELD_DELIMITER +
                                                 "a");
      fail();
    }
    catch (UnsupportedRepresentationException uex) {
      ; //this is OK
    }
  }

  /**
   * @author Klaus Meffert
   */
  public void testCleanup_0() {
    //cleanup should do nothing!
    Gene gene = new DoubleGene(1.3d, 6.5d);
    Gene copy = gene.newGene();
    gene.cleanup();
    assertEquals(copy, gene);
  }

  /**
   * @author Klaus Meffert
   */
  public void testSetToRandomValue_0() {
    Gene gene = new DoubleGene(1.3d, 6.5d);
    gene.setAllele(new Double(5.8d));

    gene.setToRandomValue(new RandomGeneratorForTest(0.789d));
    assertEquals(new Double(0.789d * (6.5d - 1.3d) + 1.3d), gene.getAllele());
  }

  /**
   *
   * @throws Exception
   * @author Klaus Meffert
   */
  public void testSetToRandomValue_1()
      throws Exception {
    Gene gene = new DoubleGene( -1.3d, 6.5d);
    gene.setAllele(new Double(5.8d));

    Configuration conf = new DefaultConfiguration();
    conf.setRandomGenerator(new RandomGeneratorForTest(0.258d));
    Genotype.setConfiguration(conf);

    gene.setToRandomValue(new RandomGeneratorForTest(0.014));
    assertEquals(new Double(0.014d * (6.5d + 1.3d) - 1.3d), gene.getAllele());
  }

  /**
   *
   * @throws Exception
   * @author Klaus Meffert
   */
  public void testSetToRandomValue_2()
      throws Exception {
    Gene gene = new DoubleGene( -1.3d, -0.5d);
    gene.setAllele(new Double(5.8d));

    Configuration conf = new DefaultConfiguration();
    conf.setRandomGenerator(new RandomGeneratorForTest(0.258d));
    Genotype.setConfiguration(conf);

    gene.setToRandomValue(new RandomGeneratorForTest(0.83d));
    assertEquals(new Double(0.83d * ( -0.5d + 1.3d) - 1.3d), gene.getAllele());
  }

  /**
   * @author Klaus Meffert
   */
  public void testSetToRandomValue_3() {
    DoubleGene gene = new DoubleGene(1.3d, 6.5d);
    gene.setAllele(new Double(5.8d));
    gene.setToRandomValue(new RandomGeneratorForTest(0.478d));

    if (gene.doubleValue() < 1.3d ||
        gene.doubleValue() > 6.5d) {
      fail();
    }
  }

  /**
   * @author Klaus Meffert
   */
  public void testSetToRandomValue_4() {
    DoubleGene gene = new DoubleGene(1.3d, 6.5d);
    gene.setAllele(new Double(5.8d));
    gene.setToRandomValue(new RandomGeneratorForTest(8.584d));

    if (gene.doubleValue() < 1.3d ||
        gene.doubleValue() > 6.5d) {
      fail();
    }
  }

  /**
   * @author Klaus Meffert
   */
  public void testCompareToNative_0() {
    Gene gene1 = new DoubleGene(1.3d, 6.5d);
    gene1.setAllele(new Double(5.8d));
    Gene gene2 = new DoubleGene(5.3d, 6.7d);
    gene2.setAllele(new Double(5.9d));
    assertEquals( ( (Double) gene1.getAllele()).compareTo(gene2.getAllele()),
                 gene1.compareTo(gene2));
  }

  /**
   * @author Klaus Meffert
   */
  public void testCompareToNative_1() {
    Gene gene1 = new DoubleGene(1.3d, 6.5d);
    gene1.setAllele(new Double(5.8d));
    Gene gene2 = new DoubleGene(5.3d, 6.7d);
    gene2.setAllele(new Double(5.8d));
    assertEquals( ( (Double) gene1.getAllele()).compareTo(gene2.getAllele()),
                 gene1.compareTo(gene2));
  }

  /**
   * @author Klaus Meffert
   */
  public void testCompareToNative_2() {
    Gene gene1 = new DoubleGene(1.3d, 6.5d);
    gene1.setAllele(new Double(5.9d));
    Gene gene2 = new DoubleGene(5.3d, 6.7d);
    gene2.setAllele(new Double(5.8d));
    assertEquals( ( (Double) gene1.getAllele()).compareTo(gene2.getAllele()),
                 gene1.compareTo(gene2));
  }

  /**
   * @author Klaus Meffert
   */
  public void testCompareToNative_3() {
    Gene gene1 = new DoubleGene(1.3d, 6.5d);
    gene1.setAllele(new Double(5.9d));
    Gene gene2 = new DoubleGene(5.3d, 6.7d);
    gene2.setAllele(new Double(5.4d));
    assertEquals( ( (Double) gene1.getAllele()).compareTo(gene2.getAllele()),
                 gene1.compareTo(gene2));
  }

  /**
   * @author Klaus Meffert
   */
  public void testCompareToNative_4() {
    Gene gene1 = new DoubleGene( -1.3d, 6.5d);
    gene1.setAllele(new Double(0.0d));
    Gene gene2 = new DoubleGene( -5.3d, 6.7d);
    gene2.setAllele(new Double( -0.0d));
    assertEquals( ( (Double) gene1.getAllele()).compareTo(gene2.getAllele()),
                 gene1.compareTo(gene2));
  }

  /**
   * @throws Exception
   * @author Klaus Meffert
   */
  public void testApplyMutation_0() {
    DoubleGene gene = new DoubleGene(0, 100);
    gene.setAllele(new Double(50));
    gene.applyMutation(0, 0.0d);
    assertEquals(50.0d, gene.doubleValue(), DELTA);
  }

  /**
   * @throws Exception
   * @author Klaus Meffert
   */
  public void testApplyMutation_1()
      throws Exception {
    DefaultConfiguration config = new DefaultConfiguration();
    config.setRandomGenerator(new RandomGeneratorForTest(15.0d));
    Genotype.setConfiguration(config);
    DoubleGene gene = new DoubleGene(0, 100);
    gene.setAllele(new Double(50));
    gene.applyMutation(0, 0.5d);
    assertEquals(50 + (100 - 0) * 0.5d, gene.doubleValue(), DELTA);
  }

  /**
   * @throws Exception
   * @author Klaus Meffert
   */
  public void testApplyMutation_2()
      throws Exception {
    DefaultConfiguration config = new DefaultConfiguration();
    config.setRandomGenerator(new RandomGeneratorForTest(15.0d));
    Genotype.setConfiguration(config);
    DoubleGene gene = new DoubleGene(44, 100);
    gene.setAllele(new Double(50));
    gene.applyMutation(0, 0.3d);
    assertEquals(50 + (100 - 44) * 0.3d, gene.doubleValue(), DELTA);
  }

  /**
   * @throws Exception
   * @author Klaus Meffert
   */
  public void testApplyMutation_3()
      throws Exception {
    DefaultConfiguration config = new DefaultConfiguration();
    config.setRandomGenerator(new RandomGeneratorForTest(0.5d));
    Genotype.setConfiguration(config);
    DoubleGene gene = new DoubleGene(33, 100);
    gene.setAllele(new Double(50));
    gene.applyMutation(0, 1.9d);
    assertEquals(33 + 0.5d * (100 - 33), gene.doubleValue(), DELTA);
  }

  /**
   * @throws Exception
   * @author Klaus Meffert
   */
  public void testApplyMutation_4()
      throws Exception {
    DefaultConfiguration config = new DefaultConfiguration();
    config.setRandomGenerator(new RandomGeneratorForTest(0.4d));
    Genotype.setConfiguration(config);
    DoubleGene gene = new DoubleGene(2, 100);
    gene.setAllele(new Double(60));
    gene.applyMutation(0, 1.9d);
    assertEquals(2 + 0.4d * (100 - 2), gene.doubleValue(), DELTA);
  }

  /**
   * Exceed bounds for applyMutation to force randomized setting of allele
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testApplyMutation_5()
      throws Exception {
    DefaultConfiguration config = new DefaultConfiguration();
    config.setRandomGenerator(new RandomGeneratorForTest(0.8d));
    Genotype.setConfiguration(config);
    DoubleGene gene = new DoubleGene(0, 100);
    gene.setAllele(new Double(60));
    gene.applyMutation(1, -1.0d);
    assertEquals(0 + 0.8d * (100 - 0), gene.doubleValue(), DELTA);
  }

  /**
   *
   * @author Klaus Meffert
   */
  public void testApplyMutation_6() {
    DoubleGene gene = new DoubleGene(0, 100);
    gene.setAllele(new Double(60));
    gene.applyMutation(77, -0.4d);
    assertEquals(60 + (100 * ( -0.4d)), gene.doubleValue(), DELTA);
  }

  /**
   *
   * @author Klaus Meffert
   */
  public void testApplyMutation_7() {
    DoubleGene gene = new DoubleGene(0, 100);
    try {
      gene.applyMutation(0, -0.4d);
      fail();
    }
    catch (NullPointerException nex) {
      ; //this is OK
    }
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testSetConstraintChecker_0() {
    DoubleGene gene = new DoubleGene(0, 100);
    assertNull(gene.getConstraintChecker());
    gene.setConstraintChecker(new IGeneConstraintChecker() {
      public boolean verify(Gene a_gene, Object a_alleleValue)
          throws RuntimeException {
        return false;
      }
    });
    assertNotNull(gene.getConstraintChecker());
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testHashCode_0() {
    DoubleGene gene = new DoubleGene(0, 100);
    assertEquals(-3, gene.hashCode());
  }

  /**
   * @author Klaus Meffert
   * @since 2.4
   */
  public void testSetEnergy_0() {
    BaseGene gene = new DoubleGene();
    assertEquals(0.0, gene.getEnergy(), DELTA);
  }

  /**
   * @author Klaus Meffert
   * @since 2.4
   */
  public void testSetEnergy_1() {
    BaseGene gene = new DoubleGene();
    gene.setEnergy(2.3);
    assertEquals(2.3, gene.getEnergy(), DELTA);
    gene.setEnergy(-55.8);
    assertEquals(-55.8, gene.getEnergy(), DELTA);
    gene.setEnergy(0.5);
    gene.setEnergy(0.8);
    assertEquals(0.8, gene.getEnergy(), DELTA);
  }

  class GeneConstraintChecker implements IGeneConstraintChecker {
    public boolean verify(Gene a_gene, Object a_alleleValue)
        throws RuntimeException {
      return true;
    }
  }
}

⌨️ 快捷键说明

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