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

📄 stringgenetest.java

📁 JGAP(发音"jay-gap")是一款用Java编写的遗传算法包。提供了基本的遗传算法.你可以使用它来解决一些适用于遗传算法解决的问题.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    String pres1 = gene1.getPersistentRepresentation();
    Gene gene2 = new StringGene(conf);
    gene2.setValueFromPersistentRepresentation(pres1);
    String pres2 = gene2.getPersistentRepresentation();
    assertEquals(pres1, pres2);
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testPersistentRepresentation_3()
      throws Exception {
    Gene gene1 = new StringGene(conf);
    gene1.setAllele("");
    String pres1 = gene1.getPersistentRepresentation();
    Gene gene2 = new StringGene(conf);
    gene2.setValueFromPersistentRepresentation(pres1);
    String pres2 = gene2.getPersistentRepresentation();
    assertEquals(pres1, pres2);
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testPersistentRepresentation_4()
      throws Exception {
    Gene gene1 = new StringGene(conf);
    gene1.setAllele(null);
    String pres1 = gene1.getPersistentRepresentation();
    Gene gene2 = new StringGene(conf);
    gene2.setValueFromPersistentRepresentation(pres1);
    String pres2 = gene2.getPersistentRepresentation();
    assertEquals(pres1, pres2);
  }

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

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testPersistentRepresentation_6()
      throws Exception {
    Gene gene1 = new StringGene(conf, 2, 10, "ABCDE");
    gene1.setAllele("BABE");
    gene1.setValueFromPersistentRepresentation(null);
    assertEquals("BABE", gene1.getAllele());
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testPersistentRepresentation_7()
      throws Exception {
    StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE");
    gene1.setAllele(null);
    assertEquals("null:2:10:ABCDE", gene1.getPersistentRepresentation());
  }

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

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

  /**
   * Invalid number in second argument.
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testPersistentRepresentation_10()
      throws Exception {
    StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE");
    gene1.setAllele(null);
    try {
      gene1.setValueFromPersistentRepresentation("null:a:10:ABCDE");
      fail();
    }
    catch (UnsupportedRepresentationException uex) {
      ; //this is OK
    }
  }

  /**
   * Invalid number in third argument.
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testPersistentRepresentation_11()
      throws Exception {
    StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE");
    gene1.setAllele(null);
    try {
      gene1.setValueFromPersistentRepresentation("null:2:3b:ABCDE");
      fail();
    }
    catch (UnsupportedRepresentationException uex) {
      ; //this is OK
    }
  }

  /**
   * Minlen to great
   * @throws Exception
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testPersistentRepresentation_12()
      throws Exception {
    StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE");
    gene1.setAllele(null);
    try {
      gene1.setValueFromPersistentRepresentation("nada:7:6:ABCDE");
      fail();
    }
    catch (UnsupportedRepresentationException uex) {
      ; //this is OK
    }
  }

  /**
   * Maxlen to small
   * @throws Exception
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testPersistentRepresentation_14()
      throws Exception {
    StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE");
    gene1.setAllele(null);
    try {
      gene1.setValueFromPersistentRepresentation("nada:1:3:ABCDE");
      fail();
    }
    catch (UnsupportedRepresentationException uex) {
      ; //this is OK
    }
  }

  /**
   * Illegal character.
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testPersistentRepresentation_15()
      throws Exception {
    StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE");
    gene1.setAllele(null);
    try {
      gene1.setValueFromPersistentRepresentation("ABHJ:4:7:ABCDE");
      fail();
    }
    catch (UnsupportedRepresentationException uex) {
      ; //this is OK
    }
  }

  /**
   * Following should be possible without exception.
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testApplyMutation_0()
      throws Exception {
    Configuration conf = new ConfigurationForTest();
    Gene gene1 = new StringGene(conf, 5, 5);
    gene1.setAllele("12345");
    gene1.applyMutation(0, 0.99d);
    gene1.applyMutation(4, -0.99d);
  }

  /**
   * Following should be possible without exception.
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testApplyMutation_1()
      throws Exception {
    Configuration conf = new ConfigurationForTest();
    Gene gene1 = new StringGene(conf, 1, 1);
    gene1.setAllele("1");
    gene1.applyMutation(0, 0.99d);
  }

  /**
   * Invalid index specified.
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testApplyMutation_2()
      throws Exception {
    Configuration conf = new ConfigurationForTest();
    Gene gene1 = new StringGene(conf, 1, 1);
    gene1.setAllele("1");
    try {
      gene1.applyMutation(1, 0.99d);
      fail();
    }
    catch (StringIndexOutOfBoundsException sex) {
      ; //this is OK
    }
  }

  /**
   * No allele set.
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testApplyMutation_3()
      throws Exception {
    Configuration conf = new ConfigurationForTest();
    Gene gene1 = new StringGene(conf, 1, 1);
    gene1.applyMutation(0, 0.99d);
  }

  /**
   * @throws Exception
   * @author Klaus Meffert
   */
  public void testApplyMutation_4()
      throws Exception {
    Configuration conf = new ConfigurationForTest();
    Gene gene1 = new StringGene(conf, 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.
   *
   * @throws Exception
   * @author Klaus Meffert
   */
  public void testApplyMutation_5()
      throws Exception {
    Configuration conf = new ConfigurationForTest();
    Gene gene1 = new StringGene(conf, 6, 6,
                                StringGene.ALPHABET_CHARACTERS_LOWER);
    gene1.setAllele("ijklmn");
    gene1.applyMutation(0, 0.0d);
    assertEquals(gene1.getAllele(), "ijklmn");
  }

  /**
   * applyMutation with empty alphabet.
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testApplyMutation_6()
      throws Exception {
    Configuration conf = new ConfigurationForTest();
    StringGene gene1 = new StringGene(conf, 6, 6);
    gene1.setAllele("ijklmn");
    gene1.setAlphabet("");
    try {
      gene1.applyMutation(0, 0.0d);
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   * null random generator should lead to NullPointerException.
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testApplyMutation_7()
      throws Exception {
    Configuration conf = new ConfigurationForTest();
    conf.setRandomGenerator(null);
    Gene gene1 = new StringGene(conf, 5, 5);
    gene1.setAllele("12345");
    try {
      gene1.applyMutation(0, 0.99d);
      fail();
    }
    catch (NullPointerException nex) {
      ; //this is OK
    }
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testSetMinMaxLength_0()
      throws Exception {
    StringGene gene = new StringGene(conf);
    gene.setMinLength(4);
    gene.setMaxLength(3);
    assertEquals(4, gene.getMinLength());
    assertEquals(3, gene.getMaxLength());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testSetToRandomValue_0()
      throws Exception {
    StringGene gene = new StringGene(conf, 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());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testSetToRandomValue_1()
      throws Exception {
    Gene gene = new StringGene(conf, 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()
      throws Exception {
    Gene gene = new StringGene(conf, 1, 6, "ABC");
    gene.setToRandomValue(new RandomGeneratorForTest(3));
    assertEquals("AAAA", gene.getAllele());
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testSetToRandomValue_3()
      throws Exception {
    StringGene gene = new StringGene(conf, 1, 7, "DEF");
    gene.setToRandomValue(new RandomGeneratorForTest(2));
    assertEquals("FFF", gene.getAllele());
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testSetToRandomValue_4()
      throws Exception {
    StringGene gene = new StringGene(conf, 1, 7, "DEF");
    gene.setAllele("EEFD");
    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());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testSetToRandomValue_5()
      throws Exception {
    StringGene gene = new StringGene(conf, 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();
      }
    }
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testSetToRandomValue_6()
      throws Exception {
    Gene gene = new StringGene(conf, 1, 6, "");
    try {
      gene.setToRandomValue(new StockRandomGenerator());
      fail();
    }
    catch (IllegalStateException iex) {
      ; //this is OK
    }
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testSetToRandomValue_7()
      throws Exception {
    Gene gene = new StringGene(conf, 1, 6, null);
    try {
      gene.setToRandomValue(new StockRandomGenerator());
      fail();
    }
    catch (IllegalStateException iex) {
      ; //this is OK
    }
  }

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

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testSetConstraintChecker_0()
      throws Exception {
    StringGene gene = new StringGene(conf, 1, 6, "ABC");
    assertNull(gene.getConstraintChecker());
    gene.setConstraintChecker(new IGeneConstraintChecker() {
      public boolean verify(Gene a_gene, Object a_alleleValue,
                            IChromosome a_chrom, int a_index) {
        return false;
      }
    });
    assertNotNull(gene.getConstraintChecker());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testIsValidAlphabet_0()
      throws Exception {
    StringGene gene = new StringGene(conf, 1, 6, "");
    try {
      gene.setAllele("HALLO");
      fail();
    }
    catch (IllegalArgumentException ilex) {
      ; //this is OK
    }
  }

  /**
   * @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);
  }

  class GeneConstraintChecker
      implements IGeneConstraintChecker {
    public boolean verify(Gene a_gene, Object a_alleleValue,
                          IChromosome a_chrom,
                          int a_index) {
      return true;
    }
  }
}

⌨️ 快捷键说明

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