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

📄 integergenetest.java

📁 java实现的遗传算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                                                 "a");
      fail();
    } catch (UnsupportedRepresentationException uex) {
      ;//this is OK
    }
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testCompareToNative_0() {
    Gene gene1 = new IntegerGene(13, 65);
    gene1.setAllele(new Integer(58));
    Gene gene2 = new IntegerGene(53, 67);
    gene2.setAllele(new Integer(59));
    assertEquals( ( (Integer) gene1.getAllele()).compareTo(gene2.
        getAllele()), gene1.compareTo(gene2));
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testCompareToNative_1() {
    Gene gene1 = new IntegerGene(13, 65);
    gene1.setAllele(new Integer(58));
    Gene gene2 = new IntegerGene(53, 67);
    gene2.setAllele(new Integer(58));
    assertEquals( ( (Integer) gene1.getAllele()).compareTo(gene2.
        getAllele()), gene1.compareTo(gene2));
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testCompareToNative_2() {
    Gene gene1 = new IntegerGene(13, 65);
    gene1.setAllele(new Integer(59));
    Gene gene2 = new IntegerGene(53, 67);
    gene2.setAllele(new Integer(58));
    assertEquals( ( (Integer) gene1.getAllele()).compareTo(gene2.
        getAllele()), gene1.compareTo(gene2));
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testCompareToNative_3() {
    Gene gene1 = new IntegerGene(13, 65);
    gene1.setAllele(new Integer(59));
    Gene gene2 = new IntegerGene(53, 67);
    gene2.setAllele(new Integer( -59));
    assertEquals( ( (Integer) gene1.getAllele()).compareTo(gene2.
        getAllele()), gene1.compareTo(gene2));
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testCompareToNative_4() {
    Gene gene1 = new IntegerGene(13, 65);
    gene1.setAllele(new Integer(0));
    Gene gene2 = new IntegerGene(53, 67);
    gene2.setAllele(new Integer( -0));
    assertEquals( ( (Integer) gene1.getAllele()).compareTo(gene2.
        getAllele()), gene1.compareTo(gene2));
  }

  /**
   * @author Klaus Meffert
   * @since 2.4
   */
  public void testCompareTo_0() {
    Gene gene1 = new IntegerGene(13, 65);
    gene1.setAllele(new Integer(0));
    Gene gene2 = new IntegerGene2(53, 67);
    gene2.setAllele(new Integer( -0));
    try {
      gene1.compareTo(gene2);
      fail();
    }
    catch (ClassCastException cex) {
      ; //this is OK
    }
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testCompareTo_1() {
    Gene gene1 = new IntegerGene(13, 65);
    gene1.setAllele(new Integer(58));
    Gene gene2 = new IntegerGene(53, 67);
    gene2.setAllele(new Integer(59));
    assertEquals( -1, gene1.compareTo(gene2));
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testCompareTo_2() {
    Gene gene1 = new IntegerGene(13, 65);
    gene1.setAllele(new Integer(58));
    Gene gene2 = new IntegerGene(53, 67);
    gene2.setAllele(new Integer(58));
    assertEquals(0, gene1.compareTo(gene2));
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testCompareTo_3() {
    Gene gene1 = new IntegerGene(13, 65);
    gene1.setAllele(new Integer(59));
    Gene gene2 = new IntegerGene(53, 67);
    gene2.setAllele(new Integer(58));
    assertEquals(1, gene1.compareTo(gene2));
    assertEquals( -1, gene2.compareTo(gene1));
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testCompareTo_4() {
    Gene gene1 = new IntegerGene(13, 65);
    Gene gene2 = new IntegerGene(53, 67);
    assertEquals(0, gene1.compareTo(gene2));
    assertEquals(0, gene2.compareTo(gene1));
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testApplyMutation_0() {
    IntegerGene gene = new IntegerGene(0, 100);
    gene.setAllele(new Integer(50));
    gene.applyMutation(0, 0.0d);
    assertEquals(50, gene.intValue());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testApplyMutation_1()
      throws Exception {
    DefaultConfiguration config = new DefaultConfiguration();
    config.setRandomGenerator(new RandomGeneratorForTest(15));
    Genotype.setConfiguration(config);
    IntegerGene gene = new IntegerGene(0, 100);
    gene.setAllele(new Integer(50));
    gene.applyMutation(0, 0.5d);
    assertEquals(Math.round(50 + (100 - 0) * 0.5d), gene.intValue());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testApplyMutation_2()
      throws Exception {
    DefaultConfiguration config = new DefaultConfiguration();
    config.setRandomGenerator(new RandomGeneratorForTest(15));
    Genotype.setConfiguration(config);
    IntegerGene gene = new IntegerGene(44, 100);
    gene.setAllele(new Integer(50));
    gene.applyMutation(0, 0.3d);
    assertEquals(Math.round(50 + (100 - 44) * 0.3d), gene.intValue());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testApplyMutation_3()
      throws Exception {
    DefaultConfiguration config = new DefaultConfiguration();
    config.setRandomGenerator(new RandomGeneratorForTest(15));
    Genotype.setConfiguration(config);
    IntegerGene gene = new IntegerGene(33, 100);
    gene.setAllele(new Integer(50));
    gene.applyMutation(0, 1.9d);
    assertEquals(Math.round(33 + 15), gene.intValue());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testApplyMutation_4()
      throws Exception {
    DefaultConfiguration config = new DefaultConfiguration();
    config.setRandomGenerator(new RandomGeneratorForTest(15));
    Genotype.setConfiguration(config);
    IntegerGene gene = new IntegerGene(2, 100);
    gene.setAllele(new Integer(60));
    gene.applyMutation(0, 1.9d);
    assertEquals(Math.round(2 + 15), gene.intValue());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testApplyMutation_5()
      throws Exception {
    DefaultConfiguration config = new DefaultConfiguration();
    config.setRandomGenerator(new RandomGeneratorForTest(15));
    Genotype.setConfiguration(config);
    IntegerGene gene = new IntegerGene(0, 100);
    gene.setAllele(new Integer(60));
    gene.applyMutation(0, -1.0d);
    assertEquals(Math.round(0 + 15), gene.intValue());
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testApplyMutation_6() {
    IntegerGene gene = new IntegerGene(0, 100);
    gene.setAllele(new Integer(60));
    gene.applyMutation(0, -0.4d);
    assertEquals(Math.round(60 + (100 * ( -0.4d))), gene.intValue());
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testSetToRandomValue_0() {
    Gene gene = new IntegerGene(1, 6);
    gene.setAllele(new Integer(5));

    gene.setToRandomValue(new RandomGeneratorForTest(0.2d));
    assertEquals(new Integer((int)(0.2d * (6 - 1) + 1)), gene.getAllele());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testSetToRandomValue_1()
      throws Exception {
    Gene gene = new IntegerGene( -1, 7);
    gene.setAllele(new Integer(4));

    Configuration conf = new DefaultConfiguration();
    Genotype.setConfiguration(conf);

    gene.setToRandomValue(new RandomGeneratorForTest(0.3d));
    assertEquals(new Integer((int)(0.3d * (7 + 1) - 1)), gene.getAllele());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testSetToRandomValue_2()
      throws Exception {
     Gene gene = new IntegerGene( -2, -1);
     gene.setAllele(new Integer(4));

     Configuration conf = new DefaultConfiguration();
     Genotype.setConfiguration(conf);

     gene.setToRandomValue(new RandomGeneratorForTest(0.8d));
     assertEquals(new Integer((int)(0.8d * ( -1 + 2) - 2)), gene.getAllele());
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testSetToRandomValue_3(){
    IntegerGene gene = new IntegerGene(0, 8);
    gene.setAllele(new Integer(5));
    gene.setToRandomValue(new RandomGeneratorForTest(4));

    if (gene.intValue() < 0 ||
        gene.intValue() > 8) {
      fail();
    }
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testSetToRandomValue_4(){
    IntegerGene gene = new IntegerGene(1, 6);
    gene.setAllele(new Integer(2));
    gene.setToRandomValue(new RandomGeneratorForTest(3));

    if (gene.intValue() < 1 ||
        gene.intValue() > 6) {
      fail();
    }
  }

  /**
   * Exposing bug #1357474
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.5
   */
  public void testSetToRandomValue_5()
      throws Exception {
     Gene gene = new IntegerGene( 1, 3);
     gene.setAllele(new Integer(4));

     Configuration conf = new DefaultConfiguration();
     Genotype.setConfiguration(conf);

     gene.setToRandomValue(new RandomGeneratorForTest(0.95d));
     assertEquals(new Integer(3), gene.getAllele());
     gene.setToRandomValue(new RandomGeneratorForTest(0.05d));
     assertEquals(new Integer(1), gene.getAllele());
  }

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

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

  /**
   * Descendent of IntegerGene being virtually the same but of a different
   * class so that equals and compareTo should signal a difference.
   *
   * @author Klaus Meffert
   * @since 2.4
   */
  class IntegerGene2
      extends IntegerGene {
    public IntegerGene2() {
      super();
    }

    public IntegerGene2(int a_lowerBounds, int a_upperBounds) {
      super(a_lowerBounds, a_upperBounds);
    }
  }

  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 + -