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

📄 fixedbinarygenetest.java

📁 一个开源的用java开发的遗传算法的封装好的工程
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
   * @since 2.0
   */
  public void testGetPersistentRepresentation_2()
      throws Exception {
    FixedBinaryGene gene = new FixedBinaryGene(conf, 3);
    String s = gene.getPersistentRepresentation();
    assertEquals("FixedBinaryGene[0,0,0]", s);
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testClone_0()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 1);
    FixedBinaryGene gene2 = (FixedBinaryGene) gene1.clone();
    assertEquals(gene1, gene2);
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testClone_1()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
    FixedBinaryGene gene2 = (FixedBinaryGene) gene1.clone();
    assertEquals(gene1, gene2);
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testSetBit_0()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
    gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
    assertTrue(gene1.getBit(0));
    gene1.setBit(0, false);
    assertFalse(gene1.getBit(0));
    gene1.setBit(1, true);
    assertTrue(gene1.getBit(1));
    gene1.setBit(4, false);
    assertFalse(gene1.getBit(0));
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testSetBit_1()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
    gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
    gene1.setBit(2, 4, true);
    assertTrue(gene1.getBit(0));
    assertTrue(gene1.getBit(1));
    assertTrue(gene1.getBit(2));
    assertTrue(gene1.getBit(3));
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testSetBit_2()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
    gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
    try {
      gene1.setBit(2, 2);
      fail();
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testSetBit_3()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
    gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
    try {
      gene1.setBit(2, -1);
      fail();
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testSetBit_4()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
    gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
    try {
      gene1.setBit(2, 1, false);
      fail();
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.6
   * @throws Exception
   */
  public void testSetBit_5()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
    gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
    FixedBinaryGene gene2 = new FixedBinaryGene(conf, 9);
    gene2.setBit(2, 6, gene1);
    assertTrue(gene2.getBit(2));
    assertTrue(gene2.getBit(3));
    assertTrue(gene2.getBit(6));
    assertFalse(gene2.getBit(0));
    assertFalse(gene2.getBit(1));
    assertFalse(gene2.getBit(4));
    assertFalse(gene2.getBit(5));
    assertFalse(gene2.getBit(7));
    assertFalse(gene2.getBit(8));
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.6
   * @throws Exception
   */
  public void testSetBit_6()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 4);
    gene1.setAllele(new int[] {1, 1, 0, 0});
    FixedBinaryGene gene2 = new FixedBinaryGene(conf, 7);
    gene2.setBit(2, 6, gene1);
    assertTrue(gene2.getBit(2));
    assertTrue(gene2.getBit(3));
    assertTrue(gene2.getBit(6));
    assertFalse(gene2.getBit(0));
    assertFalse(gene2.getBit(1));
    assertFalse(gene2.getBit(4));
    assertFalse(gene2.getBit(5));
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testSubbString_0()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
    gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
    FixedBinaryGene gene2 = gene1.substring(0, 4);
    assertEquals(5, gene2.getLength());
    assertEquals(1, gene2.size());
    assertTrue(gene2.getBit(0));
    assertTrue(gene2.getBit(1));
    assertFalse(gene2.getBit(2));
    assertFalse(gene2.getBit(3));
    assertTrue(gene2.getBit(4));
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testSubString_1()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
    gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
    try {
      gene1.substring(0, 7);
      fail();
    }
    catch (IndexOutOfBoundsException iex) {
      ; //this is OK
    }
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testFlip_0()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
    gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
    gene1.flip(0);
    assertFalse(gene1.getBit(0));
    gene1.flip(6);
    assertFalse(gene1.getBit(6));
    gene1.flip(2);
    assertTrue(gene1.getBit(2));
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testFlip_1()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
    gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
    try {
      gene1.flip(7);
      fail();
    }
    catch (IndexOutOfBoundsException iex) {
      ; //this is OK
    }
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testSetToRandomValue_0()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
    try {
      gene1.setToRandomValue(null);
      fail();
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testSetToRandomValue_1()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
    gene1.setToRandomValue(new StockRandomGenerator());
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testSetToRandomValue_2()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
    gene1.setToRandomValue(new RandomGeneratorForTest(false));
    for (int i = 0; i < 7; i++) {
      assertFalse(gene1.getBit(i));
    }
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testSetToRandomValue_3()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
    gene1.setToRandomValue(new RandomGeneratorForTest(true));
    for (int i = 0; i < 7; i++) {
      assertTrue(gene1.getBit(i));
    }
  }

  /**
   *
   * @author Klaus Meffert
   * @throws Exception
   */
  public void testNewGene_0()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
    IGeneConstraintChecker checker = new GeneConstraintChecker();
    gene1.setConstraintChecker(checker);
    FixedBinaryGene gene2 = (FixedBinaryGene) gene1.newGene();
    assertTrue(gene1.equals(gene2));
    assertTrue(gene2.equals(gene1));
    assertEquals(checker, gene2.getConstraintChecker());
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testHashCode_0()
      throws Exception {
    FixedBinaryGene gene = new FixedBinaryGene(conf, 6);
    gene.hashCode();
    gene.setBit(0, 5, false);
    gene.hashCode();
    gene.setBit(0, 5, true);
    gene.hashCode();
    /**@todo implement checks for uniqueness*/
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.5
   * @throws Exception
   */
  public void testHashCode_1()
      throws Exception {
    Gene gene1 = new FixedBinaryGene(conf, 2);
    Gene gene2 = new FixedBinaryGene(conf, 2);
    int[] v1 = (int[]) gene1.getAllele();
    int[] v2 = (int[]) gene1.getAllele();
    int h1 = v1.hashCode();
    int h2 = v2.hashCode();
    assertEquals(gene1.hashCode(), gene2.hashCode());
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.5
   * @throws Exception
   */
  public void testHashCode_2()
      throws Exception {
    Gene gene1 = new FixedBinaryGene(conf, 3);
    gene1.setAllele(new int[] {0, 1, 0});
    Gene gene2 = new FixedBinaryGene(conf, 3);
    gene2.setAllele(new int[] {0, 1, 0});
    assertEquals(gene1.hashCode(), gene2.hashCode());
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.5
   * @throws Exception
   */
  public void testHashCode_3()
      throws Exception {
    Gene gene1 = new FixedBinaryGene(conf, 2);
    gene1.setAllele(new int[] {1, 1});
    Gene gene2 = new FixedBinaryGene(conf, 2);
    gene1.setAllele(new int[] {1, 0});
    assertFalse(gene1.hashCode() == gene2.hashCode());
    assertTrue(gene1.hashCode() == gene1.hashCode());
    assertTrue(gene2.hashCode() == gene2.hashCode());
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.4
   * @throws Exception
   */
  public void testSetEnergy_0()
      throws Exception {
    BaseGene gene = new FixedBinaryGene(conf, 2);
    assertEquals(0.0, gene.getEnergy(), DELTA);
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.4
   * @throws Exception
   */
  public void testSetEnergy_1()
      throws Exception {
    BaseGene gene = new FixedBinaryGene(conf, 3);
    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;
    }
  }

  /**@todo test compareTo with applicationcata*/
}

⌨️ 快捷键说明

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