programchromosometest.java

来自「jgap3.2 遗传算法工具包,嘿嘿,笨鸟先飞哦」· Java 代码 · 共 490 行 · 第 1/2 页

JAVA
490
字号
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testToStringNorm_4()
      throws Exception {
    ProgramChromosome pc = new ProgramChromosome(m_gpconf);
    pc.setGene(0, new Modulo(m_gpconf, CommandGene.IntegerClass));
    pc.setGene(1, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
    pc.setGene(2, new Variable(m_gpconf, "Y", CommandGene.IntegerClass));
    pc.redepth();
    String s = pc.toStringNorm(1);
    assertEquals("X", s);
    s = pc.toStringNorm(2);
    assertEquals("Y", s);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testToStringNorm_5()
      throws Exception {
    ProgramChromosome pc = new ProgramChromosome(m_gpconf);
    pc.setGene(0, new Modulo(m_gpconf, CommandGene.IntegerClass));
    pc.setGene(1, new Subtract(m_gpconf, CommandGene.IntegerClass));
    pc.setGene(2, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
    pc.setGene(3, new Variable(m_gpconf, "Y", CommandGene.IntegerClass));
    pc.setGene(4, new Variable(m_gpconf, "Z", CommandGene.IntegerClass));
    pc.redepth();
    String s = pc.toStringNorm(0);
    assertEquals("(X - Y) % Z", s);
    s = pc.toStringNorm(1);
    assertEquals("(X - Y)", s);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testToStringNorm_6()
      throws Exception {
    ProgramChromosome pc = new ProgramChromosome(m_gpconf);
    pc.setGene(0, new Multiply(m_gpconf, CommandGene.IntegerClass));
    pc.setGene(1, new Push(m_gpconf, CommandGene.IntegerClass));
    pc.setGene(2, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
    pc.setGene(3, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
    pc.redepth();
    String s = pc.toStringNorm(1);
    assertEquals("(push X)", s);
    s = pc.toStringNorm(0);
    assertEquals("(push X) * X", s);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testToStringNorm_7()
      throws Exception {
    ProgramChromosome pc = new ProgramChromosome(m_gpconf);
    pc.setGene(0,
               new SubProgram(m_gpconf,
                              new Class[] {CommandGene.IntegerClass,
                              CommandGene.IntegerClass,
                              CommandGene.IntegerClass}));
    pc.setGene(1, new Multiply(m_gpconf, CommandGene.IntegerClass));
    pc.setGene(2, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
    pc.setGene(3, new Variable(m_gpconf, "Y", CommandGene.IntegerClass));
    pc.setGene(4, new Push(m_gpconf, CommandGene.IntegerClass));
    pc.setGene(5, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
    pc.setGene(6,
               new Constant(m_gpconf, CommandGene.IntegerClass, new Integer(7)));
    pc.redepth();
    String s = pc.toStringNorm(0);
    assertEquals("sub[(X * Y) --> (push X) --> 7]", s);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testToStringNorm_8()
      throws Exception {
    ProgramChromosome pc = new ProgramChromosome(m_gpconf);
    pc.setGene(0,
               new SubProgram(m_gpconf,
                              new Class[] {CommandGene.IntegerClass,
                              CommandGene.IntegerClass}));
    pc.setGene(1, new Multiply(m_gpconf, CommandGene.IntegerClass));
    pc.setGene(2, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
    pc.setGene(3, new Variable(m_gpconf, "Y", CommandGene.IntegerClass));
    pc.setGene(4, new Push(m_gpconf, CommandGene.IntegerClass));
    pc.setGene(5,
               new Constant(m_gpconf, CommandGene.IntegerClass, new Integer(9)));
    pc.redepth();
    String s = pc.toStringNorm(0);
    assertEquals("sub[(X * Y) --> (push 9)]", s);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testToString_0()
      throws Exception {
    ProgramChromosome pc = new ProgramChromosome(m_gpconf);
    pc.setGene(0, new Add(m_gpconf, CommandGene.IntegerClass));
    pc.setGene(1, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
    pc.setGene(2, new Variable(m_gpconf, "Y", CommandGene.IntegerClass));
    pc.redepth();
    String s = pc.toString(0);
    assertEquals("+ ( X Y )", s);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testToString_1()
      throws Exception {
    ProgramChromosome pc = new ProgramChromosome(m_gpconf);
    pc.setGene(0, new Subtract(m_gpconf, CommandGene.IntegerClass));
    pc.setGene(1, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
    pc.setGene(2, new Variable(m_gpconf, "Y", CommandGene.IntegerClass));
    pc.redepth();
    String s = pc.toString(1);
    assertEquals("X ", s);
    s = pc.toString(2);
    assertEquals("Y ", s);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testRedepth_0()
      throws Exception {
    ProgramChromosome pc = new ProgramChromosome(m_gpconf);
    pc.setGene(0, new Add(m_gpconf, CommandGene.IntegerClass));
    pc.setGene(1, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
    try {
      pc.redepth();
      fail();
    } catch (IllegalStateException ise) {
      ; //this i expected
    }
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testGetDepth_0()
      throws Exception {
    ProgramChromosome pc = new ProgramChromosome(m_gpconf);
    pc.setGene(0, new Add(m_gpconf, CommandGene.IntegerClass)); //Node 0
    pc.setGene(1, new Variable(m_gpconf, "Y", CommandGene.IntegerClass)); //Node 1
    pc.setGene(2, new Variable(m_gpconf, "Z", CommandGene.IntegerClass)); // Node 2
    pc.redepth();
    assertEquals(1, pc.getDepth(0)); //1 = one level below node 0
    assertEquals(0, pc.getDepth(1));
    assertEquals(0, pc.getDepth(2));
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testGetDepth_1()
      throws Exception {
    ProgramChromosome pc = new ProgramChromosome(m_gpconf);
    pc.setGene(0, new IfElse(m_gpconf, CommandGene.IntegerClass)); //Node 0
    pc.setGene(1, new Variable(m_gpconf, "Y", CommandGene.IntegerClass));
    pc.setGene(2, new Variable(m_gpconf, "Z", CommandGene.IntegerClass));
    pc.setGene(3, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
    pc.redepth();
    assertEquals(1, pc.getDepth(0)); //1 = one level below node 0
    assertEquals(0, pc.getDepth(1));
    assertEquals(0, pc.getDepth(2));
    assertEquals(0, pc.getDepth(3));
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testGetDepth_2()
      throws Exception {
    ProgramChromosome pc = new ProgramChromosome(m_gpconf);
    pc.setGene(0, new IfElse(m_gpconf, CommandGene.IntegerClass)); //Node 0
    pc.setGene(1, new Variable(m_gpconf, "Y", CommandGene.IntegerClass));
    pc.setGene(2, new Add(m_gpconf, CommandGene.IntegerClass)); // Node 2
    pc.setGene(3, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
    pc.setGene(4,
               new Constant(m_gpconf, CommandGene.IntegerClass, new Integer(3)));
    pc.setGene(5, new Variable(m_gpconf, "Z", CommandGene.IntegerClass));
    pc.redepth();
    assertEquals(2, pc.getDepth(0)); //2 = one level below node 0
    assertEquals(0, pc.getDepth(1));
    assertEquals(1, pc.getDepth(2)); //1 = one level below node 2
    assertEquals(0, pc.getDepth(3));
    assertEquals(0, pc.getDepth(4));
    assertEquals(0, pc.getDepth(5));
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testSerialize_0()
      throws Exception {
    ProgramChromosome pc = new ProgramChromosome(m_gpconf);
    pc.setGene(0, new Add(m_gpconf, CommandGene.IntegerClass));
    pc.setGene(1, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
    pc.setGene(2, new Variable(m_gpconf, "Y", CommandGene.IntegerClass));
    pc.redepth();
    ProgramChromosome pc2 = (ProgramChromosome) doSerialize(pc);
    assertEquals(pc, pc2);
  }
}

⌨️ 快捷键说明

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