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

📄 gppopulationtest.java

📁 JGAP(发音"jay-gap")是一款用Java编写的遗传算法包。提供了基本的遗传算法.你可以使用它来解决一些适用于遗传算法解决的问题.
💻 JAVA
字号:
/*
 * This file is part of JGAP.
 *
 * JGAP offers a dual license model containing the LGPL as well as the MPL.
 *
 * For licencing information please see the file license.txt included with JGAP
 * or have a look at the top of class org.jgap.Chromosome which representatively
 * includes the JGAP license policy applicable for any file delivered with JGAP.
 */
package org.jgap.gp.impl;

import junit.framework.*;
import org.jgap.*;
import org.jgap.impl.*;
import org.jgap.gp.terminal.*;
import org.jgap.gp.function.*;

import org.jgap.gp.*;

/**
 * Tests the GPPopulation class.
 *
 * @author Klaus Meffert
 * @since 3.0
 */
public class GPPopulationTest
    extends GPTestCase {
  /** String containing the CVS revision. Read out via reflection!*/
  private final static String CVS_REVISION = "$Revision: 1.1 $";

  public static Test suite() {
    TestSuite suite = new TestSuite(GPPopulationTest.class);
    return suite;
  }

  public void setUp() {
    super.setUp();
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testConstruct_0()
      throws Exception {
    GPPopulation gppop = new GPPopulation(m_gpconf, 10);
    assertEquals(10, gppop.getPopSize());
    assertEquals(10, gppop.size());
  }

  /**
   * Create with no nodes available.
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testCreate_0()
      throws Exception {
    GPPopulation gppop = new GPPopulation(m_gpconf, 10);
    Class[] types = {
        CommandGene.IntegerClass};
    Class[][] argTypes = { {}
    };
    CommandGene[][] nodeSets = { {
    }
    };
    try {
      gppop.create(types, argTypes, nodeSets, new int[] {1}, new int[] {1}, 1,
                   true, 10, new boolean[] {true});
      fail();
    } catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testCreate_1()
      throws Exception {
    GPPopulation gppop = new GPPopulation(m_gpconf, 10);
    Class[] types = {
        CommandGene.IntegerClass};
    Class[][] argTypes = { {}
    };
    CommandGene[][] nodeSets = { {
        new Add(m_gpconf, CommandGene.IntegerClass),
        Variable.create(m_gpconf, "X", CommandGene.IntegerClass),
        new Constant(m_gpconf, CommandGene.IntegerClass, new Integer(1)),
    }
    };
    rn.setNextInt(2); //Constant
    /**@todo adapt*/
//    ProgramChromosome pc = gppop.create(types, argTypes, nodeSets, 1, true);
//    assertEquals(0, pc.getDepth(0));
//    assertEquals(1, pc.size());
//    assertEquals(Constant.class, pc.getGene(0).getClass());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testCreate_2()
      throws Exception {
    GPPopulation gppop = new GPPopulation(m_gpconf, 10);
    Class[] types = {
        CommandGene.IntegerClass};
    Class[][] argTypes = { {}
    };
    CommandGene[][] nodeSets = { {
        new Add(m_gpconf, CommandGene.IntegerClass),
        Variable.create(m_gpconf, "X", CommandGene.IntegerClass),
        new Constant(m_gpconf, CommandGene.IntegerClass, new Integer(1)),
    }
    };
    rn.setNextIntSequence(new int[] {0, 2, 1}); //AddCommand, Constant, Variable
    /**@todo adapt*/

//    ProgramChromosome pc = gppop.create(types, argTypes, nodeSets, 3, true);
//    assertEquals(1, pc.getDepth(0)); //AddCommand
//    assertEquals(0, pc.getDepth(1)); //Constant
//    assertEquals(0, pc.getDepth(2)); //Variable
//    assertEquals(3, pc.size());
//    assertEquals(AddCommand.class, pc.getGene(0).getClass());
//    assertEquals(Constant.class, pc.getGene(1).getClass());
//    assertEquals(Variable.class, pc.getGene(2).getClass());
  }

  /**
   * Given nodeset doesn't allow to build a valid program.
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testCreate_3()
      throws Exception {
    GPPopulation gppop = new GPPopulation(m_gpconf, 10);
    Class[] types = {
        CommandGene.IntegerClass};
    Class[][] argTypes = { {}
    };
    CommandGene[][] nodeSets = { {
        new Add(m_gpconf, CommandGene.IntegerClass),
        new Subtract(m_gpconf, CommandGene.IntegerClass),
        new Push(m_gpconf, CommandGene.IntegerClass),
    }
    };
    m_gpconf.setRandomGenerator(new StockRandomGenerator());
    try {
      gppop.create(types, argTypes, nodeSets, new int[] {1}, new int[] {1}, 3,
                   true, 10, new boolean[] {true});
      fail();
    } catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void testSerialize_0()
      throws Exception {
    GPPopulation pop = new GPPopulation(m_gpconf, 2);
    GPProgram prog = new GPProgram(m_gpconf, 2);
    ProgramChromosome chrom = new ProgramChromosome(m_gpconf, 2, prog);
    prog.setChromosome(0, chrom);
    prog.setChromosome(1, chrom);
    pop.setGPProgram(0, prog);
    pop.setGPProgram(1, prog);
    // Serialize population to a file.
    // -------------------------------
    GPPopulation pop2 = (GPPopulation)doSerialize(pop);
    assertEquals(pop, pop2);
  }
}

⌨️ 快捷键说明

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