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

📄 mapgenetest.java

📁 java实现的遗传算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  public void testPersistentRepresentation_1()
      throws Exception {
    Gene gene1 = new MapGene();
    gene1.setAllele(new Integer(45));
    gene1.setValueFromPersistentRepresentation(null);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testPersistentRepresentation_2()
      throws Exception {
    MapGene gene1 = new MapGene();
    gene1.setAllele(new Integer(45));
    gene1.setValueFromPersistentRepresentation("6"
                                               +
                                               MapGene.
                                               PERSISTENT_FIELD_DELIMITER
                                               + "(0,1.0d),(2,3.0d),(4,5.0d)");
    assertEquals(6, ( (Integer) gene1.getAllele()).intValue());
    assertEquals(3, ( (Map) privateAccessor.getField(gene1,
        "m_geneMap")).size());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testPersistentRepresentation_3()
      throws Exception {
    Gene gene1 = new IntegerGene(2, 753);
    gene1.setAllele(new Integer(45));
    gene1.setValueFromPersistentRepresentation("null" +
                                               IntegerGene.
                                               PERSISTENT_FIELD_DELIMITER +
                                               "3" +
                                               IntegerGene.
                                               PERSISTENT_FIELD_DELIMITER +
                                               "4");
    assertNull(gene1.getAllele());
    assertEquals(3, ( (Integer) privateAccessor.getField(gene1,
        "m_lowerBounds")).intValue());
    assertEquals(4, ( (Integer) privateAccessor.getField(gene1,
        "m_upperBounds")).intValue());
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testPersistentRepresentation_4() {
    Gene gene1 = new IntegerGene(2, 753);
    gene1.setAllele(new Integer(45));
    try {
      gene1.setValueFromPersistentRepresentation("null" +
                                                 IntegerGene.
                                                 PERSISTENT_FIELD_DELIMITER +
                                                 "3.5" +
                                                 IntegerGene.
                                                 PERSISTENT_FIELD_DELIMITER +
                                                 "4");
      fail();
    }
    catch (UnsupportedRepresentationException uex) {
      ; //this is OK
    }
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testPersistentRepresentation_5() {
    Gene gene1 = new IntegerGene(2, 753);
    gene1.setAllele(new Integer(45));
    try {
      gene1.setValueFromPersistentRepresentation("null" +
                                                 IntegerGene.
                                                 PERSISTENT_FIELD_DELIMITER +
                                                 "3" +
                                                 IntegerGene.
                                                 PERSISTENT_FIELD_DELIMITER +
                                                 "a");
      fail();
    }
    catch (UnsupportedRepresentationException uex) {
      ; //this is OK
    }
  }

  /**
   *
   * @throws Exception
   * @author Klaus Meffert
   * @since 2.5
   */
  public void testPersistentRepresentation_6()
      throws Exception {
    Map alleles = new HashMap();
    for (int i = -49; i < -3; i++) {
      alleles.put(new Integer(i), new Integer(i+1));
    }
    Gene gene1 = new MapGene(alleles);
    gene1.setAllele(new Integer(-23));
    String pres1 = gene1.getPersistentRepresentation();
    Gene gene2 = new MapGene();
    gene2.setValueFromPersistentRepresentation(pres1);
    String pres2 = gene2.getPersistentRepresentation();
    assertEquals(pres1, pres2);
  }

  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));
  }

  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));
  }

  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));
  }

  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));
  }

  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));
  }

  public void testApplyMutation_0() {
    IntegerGene gene = new IntegerGene(0, 100);
    gene.setAllele(new Integer(50));
    gene.applyMutation(0, 0.0d);
    assertEquals(50, gene.intValue());
  }

  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());
  }

  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());
  }

  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());
  }

  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());
  }

  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());
  }

  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());
  }

  public void testSetToRandomValue_0() {
    Gene gene = new MapGene();
    gene.setToRandomValue(new RandomGeneratorForTest(3));
    assertEquals(new Integer(3), gene.getAllele());
  }

  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());
  }

  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());
  }

  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();
    }
  }

  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();
    }
  }
}

⌨️ 快捷键说明

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