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

📄 doublegenetest.java

📁 JGAP(发音"jay-gap")是一款用Java编写的遗传算法包。提供了基本的遗传算法.你可以使用它来解决一些适用于遗传算法解决的问题.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   *
   * @throws Exception
   * @author Klaus Meffert
   */
  public void testPersistentRepresentation_4()
      throws Exception {
    Gene gene1 = new DoubleGene(conf, 2.05d, 7.53d);
    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(conf, 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
    }
  }

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

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testSetToRandomValue_0()
      throws Exception {
    Gene gene = new DoubleGene(conf, 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(conf, -1.3d, 6.5d);
    gene.setAllele(new Double(5.8d));
    conf.setRandomGenerator(new RandomGeneratorForTest(0.258d));
    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 {
    /**@todo test needed any longer?*/
    Configuration conf = new ConfigurationForTest();
    Gene gene = new DoubleGene(conf, -1.3d, -0.5d);
    gene.setAllele(new Double(5.8d));
    conf.setRandomGenerator(new RandomGeneratorForTest(0.258d));
    gene.setToRandomValue(new RandomGeneratorForTest(0.83d));
    assertEquals(new Double(0.83d * ( -0.5d + 1.3d) - 1.3d), gene.getAllele());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testSetToRandomValue_3()
      throws Exception {
    DoubleGene gene = new DoubleGene(conf, 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();
    }
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testSetToRandomValue_4()
      throws Exception {
    DoubleGene gene = new DoubleGene(conf, 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();
    }
  }

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

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

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

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

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

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testApplyMutation_0()
      throws Exception {
    DoubleGene gene = new DoubleGene(conf, 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 {
    conf.setRandomGenerator(new RandomGeneratorForTest(15.0d));
    DoubleGene gene = new DoubleGene(conf, 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 {
    conf.setRandomGenerator(new RandomGeneratorForTest(15.0d));
    DoubleGene gene = new DoubleGene(conf, 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 {
    conf.setRandomGenerator(new RandomGeneratorForTest(0.5d));
    DoubleGene gene = new DoubleGene(conf, 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 {
    conf.setRandomGenerator(new RandomGeneratorForTest(0.4d));
    DoubleGene gene = new DoubleGene(conf, 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 {
    conf.setRandomGenerator(new RandomGeneratorForTest(0.8d));
    DoubleGene gene = new DoubleGene(conf, 0, 100);
    gene.setAllele(new Double(60));
    gene.applyMutation(1, -1.0d);
    assertEquals(0 + 0.8d * (100 - 0), gene.doubleValue(), DELTA);
  }

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

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

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

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

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testHashCode_1()
      throws Exception {
    Configuration conf = new ConfigurationForTest();
    DoubleGene c1 = new DoubleGene(conf);
    DoubleGene c2 = new DoubleGene(conf);
    assertEquals(c1.hashCode(), c2.hashCode());
    c1.setAllele(new Double(2));
    assertFalse(c1.hashCode() == c2.hashCode());
    assertEquals(c1.hashCode(), c1.hashCode());
    c2.setAllele(new Double(2));
    assertTrue(c1.hashCode() == c2.hashCode());
  }

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

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.4
   */
  public void testSetEnergy_1()
      throws Exception {
    BaseGene gene = new DoubleGene(conf);
    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);
  }


  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testBounds_0()
      throws Exception {
    DoubleGene gene = new DoubleGene(conf, -3.4d, +5.7d);
    assertEquals( -3.4d, gene.getLowerBound(), DELTA);
    assertEquals( 5.7d, gene.getUpperBound(), DELTA);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testBounds_1()
      throws Exception {
    DoubleGene gene = new DoubleGene(conf);
    assertEquals( - (Double.MAX_VALUE / 2), gene.getLowerBound(), DELTA);
    assertEquals(Double.MAX_VALUE / 2, gene.getUpperBound(), DELTA);
  }

  class GeneConstraintChecker
      implements IGeneConstraintChecker {
    public boolean verify(Gene a_gene, Object a_alleleValue,
                          IChromosome a_chrom, int a_index) {
      return true;
    }
  }
}

⌨️ 快捷键说明

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