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

📄 compositegenetest.java

📁 一个开源的用java开发的遗传算法的封装好的工程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    assertEquals(newGene1.getAllele(), v.elementAt(0));
    assertEquals(newGene2.getAllele(), v.elementAt(1));
    assertEquals(2, v.size());
  }

  public void testEquals_0()
      throws Exception {
    Gene gene1 = new CompositeGene(conf);
    Gene gene2 = new CompositeGene(conf);
    assertTrue(gene1.equals(gene2));
    assertTrue(gene2.equals(gene1));
  }

  public void testEquals_1()
      throws Exception {
    Gene gene1 = new CompositeGene(conf);
    assertFalse(gene1.equals(null));
  }

  public void testEquals_2()
      throws Exception {
    Gene gene1 = new CompositeGene(conf);
    assertFalse(gene1.equals(new BooleanGene(conf)));
  }

  public void testEquals_3()
      throws Exception {
    Gene gene1 = new CompositeGene(conf);
    assertFalse(gene1.equals(new Vector()));
  }

  /**
   * Tests that no NullPointerException arises.
   *
   * @author Klaus Meffert
   * @since 2.6
   * @throws Exception
   */
  public void testEquals_4_2()
      throws Exception {
    CompositeGene gene1 = new CompositeGene(conf, new BooleanGene(conf));
    try {
      gene1.addGene(null, false);
      fail();
    }
    catch (IllegalArgumentException iex) {
      ; // this is expected
    }
  }

  public void testEquals_5()
      throws Exception {
    CompositeGene gene1 = new CompositeGene(conf);
    Gene newGene1 = new IntegerGene(conf, 3, 5);
    gene1.addGene(newGene1, false);
    CompositeGene gene2 = new CompositeGene(conf);
    gene2.addGene(newGene1, false);
    assertTrue(gene1.equals(gene2));
    assertTrue(gene2.equals(gene1));
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.4
   * @throws Exception
   */
  public void testEquals_5_2()
      throws Exception {
    CompositeGene gene1 = new CompositeGene(conf);
    Gene newGene1 = new IntegerGene(conf, 3, 5);
    Gene newGene2 = new IntegerGene(conf, 3, 7);
    gene1.addGene(newGene1, false);
    gene1.addGene(newGene2, false);
    CompositeGene gene2 = new CompositeGene(conf);
    gene2.addGene(newGene2, false);
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
  }

  public void testEquals_6()
      throws Exception {
    CompositeGene gene1 = new CompositeGene(conf);
    Gene newGene1 = new IntegerGene(conf, 3, 5);
    gene1.addGene(newGene1, false);
    Gene newGene2 = new IntegerGene(conf, 7, 9);
    CompositeGene gene2 = new CompositeGene(conf);
    gene2.addGene(newGene2, false);
    assertEquals(newGene1.equals(newGene2), gene1.equals(gene2));
    assertEquals(newGene2.equals(newGene1), gene2.equals(gene1));
  }

  public void testEquals_7()
      throws Exception {
    CompositeGene gene1 = new CompositeGene(conf);
    Gene newGene1 = new IntegerGene(conf, 3, 5);
    gene1.addGene(newGene1, false);
    Gene newGene2 = new DoubleGene(conf, 7.2d, 59.4d);
    CompositeGene gene2 = new CompositeGene(conf);
    gene2.addGene(newGene2, false);
    assertEquals(newGene1.equals(newGene2), gene1.equals(gene2));
    assertEquals(newGene2.equals(newGene1), gene2.equals(gene1));
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.4
   * @throws Exception
   */
  public void testEquals_8()
      throws Exception {
    Gene newGene1 = new IntegerGene(conf, 3, 5);
    newGene1.setAllele(new Integer(3));
    Gene newGene2 = new IntegerGene(conf, 3, 15);
    newGene2.setAllele(new Integer(9));
    CompositeGene gene1 = new CompositeGene(conf);
    gene1.addGene(newGene1, false);
    CompositeGene gene2 = new CompositeGene(conf);
    gene2.addGene(newGene2, false);
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
  }

  public void testSetAllele_0()
      throws Exception {
    Gene gene = new CompositeGene(conf);
    try {
      gene.setAllele(null);
      fail();
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  public void testSetAllele_1()
      throws Exception {
    Gene gene = new CompositeGene(conf);
    try {
      gene.setAllele(new Double(2.3d));
      fail();
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   * Set Allele to empty Vector, no exception should occur.
   *
   * @throws Exception
   */
  public void testSetAllele_2()
      throws Exception {
    Gene gene1 = new CompositeGene(conf);
    gene1.setAllele(new Vector());
  }

  public void testSetAllele_3()
      throws Exception {
    Gene gene1 = new CompositeGene(conf);
    try {
      gene1.setAllele("22");
      fail();
    }
    catch (IllegalArgumentException classex) {
      ; //this is OK
    }
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testSetAllele_4()
      throws Exception {
    CompositeGene gene = new CompositeGene(conf);
    DoubleGene gene2 = new DoubleGene(conf, 1.0d, 3.0d);
    gene2.setAllele(new Double(1.0d));
    gene.addGene(gene2);
    gene.setConstraintChecker(new IGeneConstraintChecker() {
      public boolean verify(Gene a_gene, Object a_alleleValue,
                            IChromosome a_chrom, int a_index) {
        return false;
      }
    });
    List l = new Vector();
    l.add(new Double(2.0d));
    gene.setAllele(l);
    assertEquals(1.0d, ( (Double) gene2.getAllele()).doubleValue(), DELTA);
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testSetAllele_5()
      throws Exception {
    CompositeGene gene = new CompositeGene(conf);
    DoubleGene gene2 = new DoubleGene(conf, 1.0d, 3.0d);
    gene2.setAllele(new Double(1.0d));
    gene.addGene(gene2);
    gene.setConstraintChecker(new IGeneConstraintChecker() {
      public boolean verify(Gene a_gene, Object a_alleleValue,
                            IChromosome a_chrom, int a_index) {
        return true;
      }
    });
    List l = new Vector();
    l.add(new Double(2.0d));
    gene.setAllele(l);
    assertEquals(2.0d, ( (Double) gene2.getAllele()).doubleValue(), DELTA);
  }

  public void testNewGene_0()
      throws Exception {
    CompositeGene gene1 = new CompositeGene(conf);
    IGeneConstraintChecker checker = new GeneConstraintChecker();
    gene1.setConstraintChecker(checker);
    gene1.addGene(new DoubleGene(conf, 2.05d, 7.53d), false);
    gene1.addGene(new DoubleGene(conf, 128.35d, 155.90d), false);
    gene1.addGene(new IntegerGene(conf, 3, 8), false);
    gene1.addGene(new BooleanGene(conf), false);
    gene1.addGene(new StringGene(conf), false);
    gene1.addGene(new StringGene(conf, 2, 5), false);
    gene1.addGene(new StringGene(conf, 6, 11, "ABC"), false);
    CompositeGene gene2 = (CompositeGene) gene1.newGene();
    assertTrue(gene1.equals(gene2));
    assertEquals(checker, gene2.getConstraintChecker());
    //Remove all genes from gene2 that are contained in gene1.
    //Because they should be equal, gene2 should then be empty.
    //---------------------------------------------------------
    for (int i = 0; i < gene1.size(); i++) {
      gene2.removeGene(gene1.geneAt(i));
    }
    assertEquals(0, gene2.size());
  }

  public void testPersistentPresentation_0()
      throws Exception {
    CompositeGene gene1 = new CompositeGene(conf);
    Gene gene0 = new DoubleGene(conf, 2.05d, 7.53d);
    gene0.setAllele(new Double(7.52d));
    gene1.addGene(gene0, false);
    gene1.addGene(new DoubleGene(conf, 128.35d, 155.90d), false);
    gene0 = new IntegerGene(conf, 3, 8);
    gene0.setAllele(new Integer(5));
    gene1.addGene(gene0, false);
    gene0 = new BooleanGene(conf);
    gene0.setAllele(Boolean.valueOf(true));
    gene1.addGene(gene0, false);
    gene1.addGene(new StringGene(conf), false);
    gene1.addGene(new StringGene(conf, 2, 5), false);
    gene0 = new StringGene(conf, 6, 11, ":ABC"); // using ':'
    gene0.setAllele("B:BCBCCA");
    gene1.addGene(gene0, false);
    String pres1 = gene1.getPersistentRepresentation();
    CompositeGene gene2 = new CompositeGene(conf);
    gene2.setValueFromPersistentRepresentation(pres1);
    String pres2 = gene2.getPersistentRepresentation();
    assertEquals(pres1, pres2);
  }

  /**
   * Use special characters in alphabet.
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public void testPersistentPresentation_0_2()
      throws Exception {
    CompositeGene gene1 = new CompositeGene(conf);
    Gene gene0 = new StringGene(conf, 6, 31, "##*~?運\/%$

⌨️ 快捷键说明

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