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

📄 mapgenetest.java

📁 一个开源的用java开发的遗传算法的封装好的工程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  }

  /**
   *
   * @throws Exception
   * @author Klaus Meffert
   * @since 2.5
   */
  public void testPersistentRepresentation_0()
      throws Exception {
    Map alleles = new HashMap();
    for (int i = -3; i < 45; i = i + 2) {
      alleles.put(new Integer(i), new Integer(i));
    }
    Gene gene1 = new MapGene(conf, alleles);
    gene1.setAllele(new Integer(17));
    String pres1 = gene1.getPersistentRepresentation();
    Gene gene2 = new MapGene(conf);
    gene2.setValueFromPersistentRepresentation(pres1);
    String pres2 = gene2.getPersistentRepresentation();
    // Compare genes itself, not string representation as the latter does not
    // have a well-defined order of elements.
    Gene gene3 = new MapGene(conf);
    gene3.setValueFromPersistentRepresentation(pres2);
    assertEquals(gene1, gene3);
    assertEquals(gene2, gene3);
  }

  /**
   * Should be possible without exception
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.5
   */
  public void testPersistentRepresentation_1()
      throws Exception {
    Gene gene1 = new MapGene(conf);
    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(conf);
    gene1.setAllele(new Integer(45));
    gene1.setValueFromPersistentRepresentation("6"
                                               +
                                               MapGene.
                                               PERSISTENT_FIELD_DELIMITER
                                               +
        "(java.lang.Integer,0,java.lang.Double,1.0d),"
                                               +
        "(java.lang.Integer,2,java.lang.Double,3.0d),"
                                               +
        "(java.lang.Integer,4,java.lang.Double,5.0d)");
    assertEquals(6, ( (Integer) gene1.getAllele()).intValue());
    assertEquals(3, ( (Map) privateAccessor.getField(gene1,
        "m_geneMap")).size());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testPersistentRepresentation_3()
      throws Exception {
    Gene gene1 = new MapGene(conf);
    gene1.setAllele(new Integer(45));
    gene1.setValueFromPersistentRepresentation("null"
                                               + MapGene.
                                               PERSISTENT_FIELD_DELIMITER
                                               + "(java.lang.Integer,0,java.lang.Double,1.0d),"
                                               + "(java.lang.Double, 2,java.lang.Double,3.0d),"
                                               + "(java.lang.Double,4,java.lang.Double,5.0d)");
    assertNull(gene1.getAllele());
    assertEquals(3, ( (Map) privateAccessor.getField(gene1,
        "m_geneMap")).size());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testPersistentRepresentation_4()
      throws Exception {
    Gene gene1 = new MapGene(conf);
    gene1.setAllele(new Integer(45));
    try {
      gene1.setValueFromPersistentRepresentation("null"
                                                 + MapGene.
                                                 PERSISTENT_FIELD_DELIMITER
                                                 + "(0,1.0d),1");
      fail();
    }
    catch (IllegalStateException uex) {
      ; //this is OK
    }
  }

  /**
   * Possible without exception.
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testPersistentRepresentation_5()
      throws Exception {
    Gene gene1 = new MapGene(conf);
    gene1.setAllele(new Integer(45));
    gene1.setValueFromPersistentRepresentation("null"
                                               +
                                               MapGene.PERSISTENT_FIELD_DELIMITER
                                               +
        "(java.lang.Double,0,java.lang.Double,1.0d),");
  }

  /**
   * @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(conf, alleles);
    gene1.setAllele(new Integer( -23));
    String pres1 = gene1.getPersistentRepresentation();
    Gene gene2 = new MapGene(conf);
    gene2.setValueFromPersistentRepresentation(pres1);
    String pres2 = gene2.getPersistentRepresentation();
    // Compare genes itself, not string representation as the latter does not
    // have a well-defined order of elements.
    Gene gene3 = new MapGene(conf);
    gene3.setValueFromPersistentRepresentation(pres2);
    assertEquals(gene1, gene3);
    assertEquals(gene2, gene3);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testCompareTo_0()
      throws Exception {
    MapGene gene1 = new MapGene(conf);
    gene1.addAllele(new Integer(58), new Integer(1));
    MapGene gene2 = new MapGene(conf);
    gene2.addAllele(new Integer(59), new Integer(2));
    assertEquals( -1, gene1.compareTo(gene2));
    assertEquals(1, gene2.compareTo(gene1));
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testCompareTo_1()
      throws Exception {
    MapGene gene1 = new MapGene(conf);
    gene1.addAllele(new Integer(58), new Integer(1));
    MapGene gene2 = new MapGene(conf);
    gene2.addAllele(new Integer(58), new Integer(1));
    assertEquals(0, gene1.compareTo(gene2));
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testCompareTo_2()
      throws Exception {
    MapGene gene1 = new MapGene(conf);
    gene1.addAllele(new Integer(58), null);
    MapGene gene2 = new MapGene(conf);
    gene2.addAllele(new Integer(58), new Integer(1));
    assertEquals( -1, gene1.compareTo(gene2));
    assertEquals(1, gene2.compareTo(gene1));
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testCompareTo_3()
      throws Exception {
    MapGene gene1 = new MapGene(conf);
    gene1.addAllele(new Integer(58), new Integer(1));
    gene1.addAllele(new Integer(77), new Integer(1));
    gene1.addAllele(new Integer(62), new Integer(1));
    MapGene gene2 = new MapGene(conf);
    gene2.addAllele(new Integer(58), new Integer(1));
    gene2.addAllele(new Integer(62), new Integer(1));
    gene2.addAllele(new Integer(77), new Integer(1));
    assertEquals(0, gene1.compareTo(gene2));
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testRemoveAlleles_0()
      throws Exception {
    MapGene gene1 = new MapGene(conf);
    Object key = new Integer(58);
    gene1.addAllele(key, null);
    assertEquals(1, gene1.getAlleles().size());
    gene1.removeAlleles(key);
    assertEquals(0, gene1.getAlleles().size());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testRemoveAlleles_1()
      throws Exception {
    MapGene gene1 = new MapGene(conf);
    Object key = new Integer(58);
    gene1.addAllele(key, new Double(5));
    assertEquals(1, gene1.getAlleles().size());
    gene1.removeAlleles(new Integer(33));
    assertEquals(1, gene1.getAlleles().size());
  }

  /**@todo add test for applyMutation*/
//  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
   */
  public void testSetToRandomValue_0()
      throws Exception {
    Gene gene = new MapGene(conf);
    gene.setToRandomValue(new RandomGeneratorForTest(3));
    assertEquals(new Integer(3), gene.getAllele());
  }

  public void testSetToRandomValue_1()
      throws Exception {
    MapGene gene = new MapGene(conf);
    gene.addAllele(new Integer(2), new Integer(3));
    gene.setToRandomValue(new StockRandomGenerator());
    assertEquals(new Integer(2), gene.getAlleles().keySet().iterator().next());
    assertEquals(new Integer(3), gene.getAlleles().values().iterator().next());
  }
}

⌨️ 快捷键说明

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