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

📄 integergenetest.java

📁 一个开源的用java开发的遗传算法的封装好的工程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  /**
   * Constructed IntegerGene without user initialization.
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testPersistentRepresentation_6()
      throws Exception {
    IntegerGene gene1 = new IntegerGene(conf);
    assertEquals("null" + IntegerGene.PERSISTENT_FIELD_DELIMITER
                 + Integer.MIN_VALUE + IntegerGene.PERSISTENT_FIELD_DELIMITER
                 + Integer.MAX_VALUE,
                 gene1.getPersistentRepresentation());
  }

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

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

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

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

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

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

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

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

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

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

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testApplyMutation_0()
      throws Exception {
    IntegerGene gene = new IntegerGene(conf, 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 {
    conf.setRandomGenerator(new RandomGeneratorForTest(15));
    IntegerGene gene = new IntegerGene(conf, 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 {
    conf.setRandomGenerator(new RandomGeneratorForTest(15));
    IntegerGene gene = new IntegerGene(conf, 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 {
    conf.setRandomGenerator(new RandomGeneratorForTest(15));
    IntegerGene gene = new IntegerGene(conf, 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 {
    conf.setRandomGenerator(new RandomGeneratorForTest(15));
    IntegerGene gene = new IntegerGene(conf, 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 {
    conf.setRandomGenerator(new RandomGeneratorForTest(15));
    IntegerGene gene = new IntegerGene(conf, 0, 100);
    gene.setAllele(new Integer(60));
    gene.applyMutation(0, -1.0d);
    assertEquals(Math.round(0 + 15), gene.intValue());
  }

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

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testApplyMutation_7()
      throws Exception {
    IntegerGene gene = new IntegerGene(conf, 0, 100);
    gene.setAllele(null);
    gene.applyMutation(0, 0.4d);
    assertEquals(Math.round( (100 * (0.4d))), gene.intValue());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testApplyMutation_8()
      throws Exception {
    IntegerGene gene = new IntegerGene(conf, 5, 100);
    gene.setAllele(null);
    conf.setRandomGenerator(new RandomGeneratorForTest(10));
    gene.applyMutation(0, -0.4d);
    assertEquals(Math.round(10 + 5), gene.intValue());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testSetToRandomValue_0()
      throws Exception {
    Gene gene = new IntegerGene(conf, 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(conf, -1, 7);
    gene.setAllele(new Integer(4));
    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 {
    Configuration conf = new ConfigurationForTest();
    Gene gene = new IntegerGene(conf, -2, -1);
    gene.setAllele(new Integer(4));
    gene.setToRandomValue(new RandomGeneratorForTest(0.8d));
    assertEquals(new Integer( (int) (0.8d * ( -1 + 2) - 2)), gene.getAllele());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testSetToRandomValue_3()
      throws Exception {
    IntegerGene gene = new IntegerGene(conf, 0, 8);
    gene.setAllele(new Integer(5));
    gene.setToRandomValue(new RandomGeneratorForTest(4));
    if (gene.intValue() < 0
        || gene.intValue() > 8) {
      fail();
    }
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testSetToRandomValue_4()
      throws Exception {
    IntegerGene gene = new IntegerGene(conf, 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 {
    Configuration conf = new ConfigurationForTest();
    Gene gene = new IntegerGene(conf, 1, 3);
    gene.setAllele(new Integer(4));
    gene.setToRandomValue(new RandomGeneratorForTest(0.95d));
    assertEquals(new Integer(3), gene.getAllele());
    gene.setToRandomValue(new RandomGeneratorForTest(0.05d));
    assertEquals(new Integer(1), gene.getAllele());
  }

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

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

  /**
   * 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()
        throws InvalidConfigurationException {
      super(Genotype.getStaticConfiguration());
    }

    public IntegerGene2(final Configuration a_conf, int a_lowerBounds,
                        int a_upperBounds)
        throws InvalidConfigurationException {
      super(a_conf, a_lowerBounds, a_upperBounds);
    }
  }
  class GeneConstraintChecker
      implements IGeneConstraintChecker {
    public boolean verify(Gene a_gene, Object a_alleleValue,
                          IChromosome a_chrom, int a_index) {
      return true;
    }
  }
  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testGetBounds_0()
      throws Exception {
    IntegerGene gene = new IntegerGene(conf, 2, 5);
    assertEquals(2, gene.getLowerBounds());
    assertEquals(5, gene.getUpperBounds());
  }
}

⌨️ 快捷键说明

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