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

📄 fixedbinarygenetest.java

📁 一个开源的用java开发的遗传算法的封装好的工程
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
  public void testSetAllele_4()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 4);
    try {
      gene1.setAllele(new int[] {0, 3, 1, 4});
      fail();
    }
    catch (Exception e) {
      ; //this is OK
    }
  }

  /**
   * Allele is of wrong length.
   *
   * @author vamsi
   * @throws Exception
   */
  public void testSetAllele_5()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 4);
    try {
      gene1.setAllele(new int[] {0, 0});
      fail();
    }
    catch (Exception e) {
      ; //this is OK
    }
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testSetAllele_6()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
    gene1.setConstraintChecker(new IGeneConstraintChecker() {
      public boolean verify(Gene a_gene, Object a_alleleValue,
                            IChromosome a_chrom, int a_index) {
        return false;
      }
    });
    gene1.setAllele(new int[] {0, 0, 1});
    assertFalse(gene1.getBit(0));
    assertFalse(gene1.getBit(1));
    assertFalse(gene1.getBit(2));
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testSetAllele_7()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
    gene1.setConstraintChecker(new IGeneConstraintChecker() {
      public boolean verify(Gene a_gene, Object a_alleleValue,
                            IChromosome a_chrom, int a_index) {
        return true;
      }
    });
    gene1.setAllele(new int[] {0, 0, 1});
    assertFalse(gene1.getBit(0));
    assertFalse(gene1.getBit(1));
    assertTrue(gene1.getBit(2));
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testSetConstraintChecker_0()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
    assertNull(gene1.getConstraintChecker());
    gene1.setConstraintChecker(new IGeneConstraintChecker() {
      public boolean verify(Gene a_gene, Object a_alleleValue,
                            IChromosome a_chrom, int a_index) {
        return false;
      }
    });
    assertNotNull(gene1.getConstraintChecker());
  }

  /**
   * Comparison should return 0 if same, -1 if less 1 if more.
   *
   * @author vamsi
   * @throws Exception
   */
  public void testCompareTo_0()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 4);
    FixedBinaryGene gene2 = new FixedBinaryGene(conf, 4);
    gene1.setAllele(new int[] {1, 0, 1, 0});
    gene2.setAllele(new int[] {1, 1, 0, 1});
    assertEquals(1, gene1.compareTo(null));
    assertEquals( -1, gene1.compareTo(gene2));
    assertEquals(1, gene2.compareTo(gene1));
  }

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testCompareTo_1()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
    FixedBinaryGene gene2 = new FixedBinaryGene(conf, 3);
    assertEquals(0, gene1.compareTo(gene2));
    assertEquals(0, gene2.compareTo(gene1));
  }

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testCompareTo_2()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
    FixedBinaryGene gene2 = new FixedBinaryGene(conf, 3);
    gene1.setAllele(new int[] {1, 1, 1});
    gene2.setAllele(new int[] {1, 1, 1});
    assertEquals(0, gene1.compareTo(gene2));
    assertEquals(0, gene2.compareTo(gene1));
    gene1.setAllele(new int[] {0, 0, 0});
    gene2.setAllele(new int[] {0, 0, 0});
    assertEquals(0, gene1.compareTo(gene2));
    assertEquals(0, gene2.compareTo(gene1));
  }

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testCompareTo_3_1()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
    BooleanGene gene2 = new BooleanGene(conf);
    try {
      gene1.compareTo(gene2);
      fail();
    }
    catch (Exception e) {
      ; //this is OK (should compare only FixedBinaryGene's)
    }
  }

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testCompareTo_3_2()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
    try {
      gene1.compareTo(new Integer(3));
      fail();
    }
    catch (Exception e) {
      ; //this is OK (should compare only FixedBinaryGene's)
    }
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testCompareTo_4()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
    FixedBinaryGene gene2 = new FixedBinaryGene(conf, 4);
    assertEquals( -1, gene1.compareTo(gene2));
    assertEquals(1, gene2.compareTo(gene1));
  }

  /**
   * Apply Mutation (index,percentage). if >0 make 1(0) if <0 make 0(1)
   *
   * @author vamsi
   * @throws Exception
   */
  public void testApplyMutation_0()
      throws Exception {
    FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
    gene.setAllele(new int[] {0, 0, 1, 1});
    gene.applyMutation(0, 0.0d);
    assertEquals("FixedBinaryGene[0,0,1,1]", gene.toString());
  }

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testApplyMutation_1()
      throws Exception {
    FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
    gene.setAllele(new int[] {0, 0, 1, 0});
    gene.applyMutation(1, 0.000001d);
    assertEquals("FixedBinaryGene[0,1,1,0]", gene.toString());
  }

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testApplyMutation_2()
      throws Exception {
    FixedBinaryGene gene = new FixedBinaryGene(conf, 5);
    gene.setAllele(new int[] {1, 0, 1, 0, 1});
    try {
      //index size is greater
      gene.applyMutation(333, -0.000001d);
      fail();
    }
    catch (Exception e) {
      ; //this is OK
    }
  }

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testApplyMutation_3()
      throws Exception {
    FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
    gene.setAllele(new int[] {1, 1, 0, 1});
    gene.applyMutation(0, -1.0d);
    assertEquals("FixedBinaryGene[0,1,0,1]", gene.toString());
  }

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testApplyMutation_4()
      throws Exception {
    FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
    gene.setAllele(new int[] {0, 1, 0, 1});
    gene.applyMutation(0, -2.0d);
    gene.applyMutation(3, 2.0d);
    gene.applyMutation(1, -4.0d);
    assertEquals("FixedBinaryGene[0,0,0,1]", gene.toString());
  }

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testApplyMutation_5()
      throws Exception {
    FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
    gene.setAllele(new int[] {1, 1, 1, 1});
    gene.applyMutation(0, 2.0d);
    gene.applyMutation(1, 2.0d);
    gene.applyMutation(2, 2.0d);
    gene.applyMutation(3, 2.0d);
    assertEquals("FixedBinaryGene[1,1,1,1]", gene.toString());
  }

  /**
   *
   * @author vamsi
   * @since 2.0
   * @throws Exception
   */
  public void testSetValueFromPersistentRepresentation_0()
      throws Exception {
    FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
    try {
      gene.setValueFromPersistentRepresentation(null);
      fail();
    }
    catch (UnsupportedRepresentationException uex) {
      ; //this is OK
    }
  }

  /**
   * @since 2.0
   * @throws Exception
   */
  public void testSetValueFromPersistentRepresentation_1()
      throws Exception {
    FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
    try {
      gene.setValueFromPersistentRepresentation("null");
      fail();
    }
    catch (UnsupportedRepresentationException uex) {
      ; //this is OK
    }
  }

  /**
   * @throws Exception
   *
   * @author vamsi
   * @since 2.0
   */
  public void testSetValueFromPersistentRepresentation_2()
      throws Exception {
    FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
    gene.setValueFromPersistentRepresentation("[1,1,1,1]");
    assertTrue(gene.getBit(0));
    assertTrue(gene.getBit(1));
    assertTrue(gene.getBit(2));
    assertTrue(gene.getBit(3));
  }

  /**
   * @throws Exception
   *
   * @author vamsi
   * @since 2.0
   */
  public void testSetValueFromPersistentRepresentation_3()
      throws Exception {
    FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
    gene.setValueFromPersistentRepresentation("[0,0,0,0]");
    assertFalse(gene.getBit(0));
    assertFalse(gene.getBit(1));
    assertFalse(gene.getBit(2));
    assertFalse(gene.getBit(3));
  }

  /**
   * @throws Exception
   *
   * @author vamsi
   * @since 2.0
   */
  public void testSetValueFromPersistentRepresentation_4()
      throws Exception {
    FixedBinaryGene gene = new FixedBinaryGene(conf, 5);
    gene.setValueFromPersistentRepresentation("[0,1,1,0,0]");
    assertFalse(gene.getBit(0));
    assertTrue(gene.getBit(1));
    assertTrue(gene.getBit(2));
    assertFalse(gene.getBit(3));
    assertFalse(gene.getBit(4));
  }

  /**
   * @throws Exception
   *
   * @since 2.0
   * @author Klaus Meffert
   */
  public void testSetValueFromPersistentRepresentation_5()
      throws Exception {
    FixedBinaryGene gene = new FixedBinaryGene(conf, 5);
    try {
      gene.setValueFromPersistentRepresentation("[0,1,1,0]");
      fail();
    }
    catch (UnsupportedRepresentationException uex) {
      ; //this is OK
    }
  }

  /**
   *
   * @author vamsi
   * @since 2.0
   * @throws Exception
   */
  public void testSetValueFromPersistentRepresentation_6()
      throws Exception {
    FixedBinaryGene gene = new FixedBinaryGene(conf, 1);
    try {
      gene.setValueFromPersistentRepresentation("X");
      fail();
    }
    catch (UnsupportedRepresentationException uex) {
      ; //this is OK
    }
  }

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testGetPersistentRepresentation_0()
      throws Exception {
    FixedBinaryGene gene = new FixedBinaryGene(conf, 3);
    gene.setAllele(new int[] {1, 0, 1});
    String s = gene.getPersistentRepresentation();
    assertEquals("FixedBinaryGene[1,0,1]", s);
  }

  /**
   * @throws Exception
   *
   * @author vamsi
   * @since 2.0
   */
  public void testGetPersistentRepresentation_1()
      throws Exception {
    FixedBinaryGene gene = new FixedBinaryGene(conf, 3);
    try {
      gene.setValueFromPersistentRepresentation(null);
      fail();
    }
    catch (UnsupportedRepresentationException uex) {
      ; //this is OK
    }
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert

⌨️ 快捷键说明

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