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

📄 stringgenetest.java

📁 用java语言写的遗传算法库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      throws Exception {
    Gene gene1 = new StringGene(0, 10);
    gene1.setAllele("");
    String pres1 = gene1.getPersistentRepresentation();
    Gene gene2 = new StringGene();
    gene2.setValueFromPersistentRepresentation(pres1);
    String pres2 = gene2.getPersistentRepresentation();
    assertEquals(pres1, pres2);
  }

  public void testPersistentRepresentation_3()
      throws Exception {
    Gene gene1 = new StringGene();
    gene1.setAllele("");
    String pres1 = gene1.getPersistentRepresentation();
    Gene gene2 = new StringGene();
    gene2.setValueFromPersistentRepresentation(pres1);
    String pres2 = gene2.getPersistentRepresentation();
    assertEquals(pres1, pres2);
  }

  public void testPersistentRepresentation_4()
      throws Exception {
    Gene gene1 = new StringGene();
    gene1.setAllele(null);
    String pres1 = gene1.getPersistentRepresentation();
    Gene gene2 = new StringGene();
    gene2.setValueFromPersistentRepresentation(pres1);
    String pres2 = gene2.getPersistentRepresentation();
    assertEquals(pres1, pres2);
  }

  public void testPersistentRepresentation_5()
      throws Exception {
    StringGene gene1 = new StringGene(2, 10,
                                      "ABCDE" + CompositeGene.GENE_DELIMITER);
    gene1.setAllele(new String("BABE"));
    String pres1 = gene1.getPersistentRepresentation();
    StringGene gene2 = new StringGene();
    gene2.setValueFromPersistentRepresentation(pres1);
    String pres2 = gene2.getPersistentRepresentation();
    assertEquals(pres1, pres2);
    assertEquals(gene1, gene2);
    assertEquals(gene1.getAlphabet(), gene2.getAlphabet());
  }

  public void testPersistentRepresentation_6()
      throws Exception {
    Gene gene1 = new StringGene(2, 10, "ABCDE");
    gene1.setAllele(new String("BABE"));
    gene1.setValueFromPersistentRepresentation(null);
    assertEquals("BABE", gene1.getAllele());
  }

  public void testPersistentRepresentation_7()
      throws Exception {
    StringGene gene1 = new StringGene(2, 10, "ABCDE");
    gene1.setAllele(null);

    assertEquals("null:2:10:ABCDE", gene1.getPersistentRepresentation());
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testPersistentRepresentation_8() {
    StringGene gene1 = new StringGene(2, 10, "ABCDE");
    gene1.setAllele(null);
    try {
      gene1.setValueFromPersistentRepresentation("null:2:ABCDE");
      fail();
    }
    catch (UnsupportedRepresentationException uex) {
      ; //this is OK
    }
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testPersistentRepresentation_9() {
    StringGene gene1 = new StringGene(2, 10, "ABCDE");
    assertEquals("null:2:10:ABCDE", gene1.getPersistentRepresentation());
  }

  public void testApplyMutation_0() {
    Gene gene1 = new StringGene(5, 5);
    gene1.setAllele("12345");
    gene1.applyMutation(0, 0.99d);
    gene1.applyMutation(4, -0.99d);
  }

  public void testApplyMutation_1() {
    Gene gene1 = new StringGene(1, 1);
    gene1.setAllele("1");
    gene1.applyMutation(0, 0.99d);
  }

  /**
   * Invalid index specified
   */
  public void testApplyMutation_2() {
    Gene gene1 = new StringGene(1, 1);
    gene1.setAllele("1");
    try {
      gene1.applyMutation(1, 0.99d);
      fail();
    }
    catch (StringIndexOutOfBoundsException sex) {
      ; //this is OK
    }
  }

  /**
   * No allele set
   */
  public void testApplyMutation_3() {
    Gene gene1 = new StringGene(1, 1);
    gene1.applyMutation(0, 0.99d);
  }

  public void testApplyMutation_4() {
    Gene gene1 = new StringGene(6, 6, StringGene.ALPHABET_CHARACTERS_LOWER);
    gene1.setAllele("ijklmn");
    gene1.applyMutation(0, 0.3d);
    assertFalse(gene1.getAllele().equals("ijklmn"));
    gene1.setAllele("ijklmn");
    gene1.applyMutation(4, -0.3d);
    assertFalse(gene1.getAllele().equals("ijklmn"));
  }

  /**
   * Mutation 0.0 should not change anything
   */
  public void testApplyMutation_5() {
    Gene gene1 = new StringGene(6, 6, StringGene.ALPHABET_CHARACTERS_LOWER);
    gene1.setAllele("ijklmn");
    gene1.applyMutation(0, 0.0d);
    assertEquals(gene1.getAllele(), "ijklmn");
  }

  public void testSetMinMaxLength_0()
      throws Exception {
    StringGene gene = new StringGene();
    gene.setMinLength(4);
    gene.setMaxLength(3);
    assertEquals(4, gene.getMinLength());
    assertEquals(3, gene.getMaxLength());
  }

  public void testSetToRandomValue_0() {
    StringGene gene = new StringGene(1, 6, StringGene.ALPHABET_CHARACTERS_UPPER);

    gene.setToRandomValue(new RandomGeneratorForTest(2));
    assertEquals("CCC", gene.getAllele());
    gene.setToRandomValue(new RandomGeneratorForTest(1));
    assertEquals("BB", gene.getAllele());
    gene.setToRandomValue(new RandomGeneratorForTest(0));
    assertEquals("A", gene.getAllele());
  }

  public void testSetToRandomValue_1() {
    Gene gene = new StringGene(1, 6, StringGene.ALPHABET_CHARACTERS_UPPER);
    gene.setAllele("XYZA"); // should not matter here
    gene.setToRandomValue(new RandomGeneratorForTest(3));
    assertEquals("DDDD", gene.getAllele());
  }

  public void testSetToRandomValue_2() {
    Gene gene = new StringGene(1, 6, "ABC");
    gene.setToRandomValue(new RandomGeneratorForTest(3));
    assertEquals("AAAA", gene.getAllele());
  }

  public void testSetToRandomValue_3()
      throws Exception {
    StringGene gene = new StringGene(1, 7, "DEF");

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

    gene.setToRandomValue(new RandomGeneratorForTest(2));
    assertEquals("FFF", gene.getAllele());
  }

  public void testSetToRandomValue_4()
      throws Exception {
    StringGene gene = new StringGene(1, 7, "DEF");
    gene.setAllele("EEFD");

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

    RandomGeneratorForTest rn = new RandomGeneratorForTest();
    // set random generator to produce
    // 1) length of new allele (-1)
    // 2) first character out of alphabet ("DEF"), starting from 0
    // 3) second character out of alphabet
    // 4) third character out of alphabet
    // 5) fourth character out of alphabet
    rn.setNextIntSequence(new int[] {3, 2, 1, 0, 2});
    gene.setToRandomValue(rn);
    assertEquals("FEDF", gene.getAllele());
  }

  public void testSetToRandomValue_5() {
    StringGene gene = new StringGene(1, 8, StringGene.ALPHABET_CHARACTERS_LOWER);
    gene.setToRandomValue(new StockRandomGenerator());

    for (int i = 0; i < gene.size(); i++) {
      if ( ( (String) gene.getAllele()).charAt(i) < 'a' ||
          ( (String) gene.getAllele()).charAt(i) > 'z') {
        fail();
      }
    }
  }

  public void testSetToRandomValue_6() {
    Gene gene = new StringGene(1, 6, "");
    try {
      gene.setToRandomValue(new StockRandomGenerator());
      fail();
    }
    catch (IllegalStateException iex) {
      ; //this is OK
    }
  }

  public void testSetToRandomValue_7() {
    Gene gene = new StringGene(1, 6, null);
    try {
      gene.setToRandomValue(new StockRandomGenerator());
      fail();
    }
    catch (IllegalStateException iex) {
      ; //this is OK
    }
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testSetToRandomValue_8() {
    StringGene gene = new StringGene(2, 6, "ABC");
    try {
      gene.setMaxLength(1);
      gene.setToRandomValue(new StockRandomGenerator());
      fail();
    }
    catch (IllegalStateException iex) {
      ; //this is OK
    }
  }

  public void testSetConstraintChecker_0() {
    StringGene gene = new StringGene(1, 6, "ABC");
    assertNull(gene.getConstraintChecker());
    gene.setConstraintChecker(new IGeneConstraintChecker() {
      public boolean verify(Gene a_gene, Object a_alleleValue)
          throws RuntimeException {
        return false;
      }
    });
    assertNotNull(gene.getConstraintChecker());
  }

  public void testIsValidAlphabet_0() {
    StringGene gene = new StringGene(1, 6, "");
    try {
      gene.setAllele("HALLO");
      fail();
    }
    catch (IllegalArgumentException ilex) {
      ; //this is OK
    }
  }
}

⌨️ 快捷键说明

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