gpconfiguration.java
来自「jgap3.2 遗传算法工具包,嘿嘿,笨鸟先飞哦」· Java 代码 · 共 702 行 · 第 1/2 页
JAVA
702 行
/*
* 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 java.util.*;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.jgap.*;
import org.jgap.impl.*;
import org.jgap.distr.*;
import org.jgap.event.*;
import org.jgap.gp.*;
/**
* Configuration for a GP.
*
* @author Klaus Meffert
* @since 3.0
*/
public class GPConfiguration
extends Configuration {
/** String containing the CVS revision. Read out via reflection!*/
private final static String CVS_REVISION = "$Revision: 1.25 $";
/**
* References the current fitness function that will be used to evaluate
* chromosomes during the natural selection process.
*/
private GPFitnessFunction m_objectiveFunction;
/**
* Internal stack, see PushCommand for example.
*/
private Stack m_stack = new Stack();
/**
* Internal memory, see StoreTerminalCommand for example.
*/
private Culture m_memory = new Culture(50); /**@todo make 50 configurable*/
/**
* The probability that a crossover operation is chosen during evolution. Must
* be between 0.0d and 1.0d, inclusive.
*/
private double m_crossoverProb = 0.9d;
/**
* The probability that a reproduction operation is chosen during evolution.
* Must be between 0.0d and 1.0d. crossoverProb + reproductionProb must equal
* 1.0d.
*/
private double m_reproductionProb = 0.1d;
/**
* Percentage of the population that will be filled with new individuals
* during evolution. Must be between 0.0d and 1.0d.
*/
private double m_newChromsPercent = 0.3d;
/**
* In crossover: If random number (0..1) < this value, then choose a function
* otherwise a terminal
*/
private double m_functionProb = 0.9d;
/**
* The maximum depth of an individual resulting from crossover.
*/
private int m_maxCrossoverDepth = 17;
/**
* The maximum depth of an individual when the world is created.
*/
private int m_maxInitDepth = 7;
/**
* The minimum depth of an individual when the world is created.
*/
private int m_minInitDepth = 3;
/**
* The method of choosing an individual to perform an evolution operation on.
*/
private INaturalGPSelector m_selectionMethod;
/**
* The method of crossing over two individuals during evolution.
*/
private CrossMethod m_crossMethod;
/**
* True: Set of available functions must contain any "type of function" that
* may be needed during construction of a new program. A "type of function"
* is, for instance, a terminal with return type
* <code>CommandGene.IntegerClass</code>.
*/
private boolean m_strictProgramCreation;
/**
* If m_strictProgramCreation is false: Maximum number of tries to construct
* a valid program.
*/
private int m_programCreationMaxTries = 5;
/**
* The fitness evaluator. See interface IGPFitnessEvaluator for details.
*/
private IGPFitnessEvaluator m_fitnessEvaluator;
private INodeValidator m_nodeValidator;
/**
* Internal flag to display a warning only once, in case a program could not
* be evolved with the allowed maximum number of nodes.
*
* @since 3.2
*/
private boolean m_warningPrinted;
/**
* Prototype of a valid program. May be cloned if needed (do not reference
* it!)
*
* @since 3.2
*/
private IGPProgram m_prototypeProgram;
private transient Map m_programCache;
private boolean m_useProgramCache = false;
/**
* Constructor utilizing the FitnessProportionateSelection.
*
* @throws InvalidConfigurationException
*
* @author Klaus Meffert
* @since 3.0
*/
public GPConfiguration()
throws InvalidConfigurationException {
this("", null);
}
public GPConfiguration(String a_id, String a_name)
throws InvalidConfigurationException {
super(a_id, a_name);
init();
m_selectionMethod = new FitnessProportionateSelection();
}
/**
* Sets a GP fitness evaluator, such as
* org.jgap.gp.impl.DefaultGPFitnessEvaluator.
*
* @param a_evaluator the fitness evaluator to set
*
* @author Klaus Meffert
* @since 3.1
*/
public void setGPFitnessEvaluator(IGPFitnessEvaluator a_evaluator) {
m_fitnessEvaluator = a_evaluator;
}
/**
* Helper for construction.
*
* @throws InvalidConfigurationException
*
* @author Klaus Meffert
* @since 3.1
*/
protected void init()
throws InvalidConfigurationException {
m_crossMethod = new BranchTypingCross(this);
setEventManager(new EventManager());
setRandomGenerator(new StockRandomGenerator());
setGPFitnessEvaluator(new DefaultGPFitnessEvaluator());
m_programCache = new HashMap(50);
}
/**
* Constructor utilizing the FitnessProportionateSelection.
*
* @param a_selectionMethod the selection method to use
* @throws InvalidConfigurationException
*
* @author Klaus Meffert
* @since 3.1
*/
public GPConfiguration(INaturalGPSelector a_selectionMethod)
throws InvalidConfigurationException {
super();
init();
m_selectionMethod = a_selectionMethod;
}
/**
* Sets the selection method to use.
* @param a_method the selection method to use
*
* @author Klaus Meffert
* @since 3.1
*/
public void setSelectionMethod(INaturalGPSelector a_method) {
if (a_method == null) {
throw new IllegalArgumentException("Selection method must not be null");
}
m_selectionMethod = a_method;
}
/**
* Sets the crossover method to use.
* @param a_method the crossover method to use
*
* @author Klaus Meffert
* @since 3.1
*/
public void setCrossoverMethod(CrossMethod a_method) {
if (a_method == null) {
throw new IllegalArgumentException("Crossover method must not be null");
}
m_crossMethod = a_method;
}
public synchronized void verifyStateIsValid()
throws InvalidConfigurationException {
// Do nothing in here.
// -------------------
}
public synchronized void addGeneticOperator(GeneticOperator a_operatorToAdd)
throws InvalidConfigurationException {
throw new UnsupportedOperationException(
"Use addGeneticOperator(GPGeneticOperator) instead!");
}
// /**@todo implement something like that*/
// public synchronized void addGeneticOperator(IGPGeneticOperator a_operatorToAdd)
// throws InvalidConfigurationException {
// }
public double getCrossoverProb() {
return m_crossoverProb;
}
public void setCrossoverProb(float a_crossoverProb) {
m_crossoverProb = a_crossoverProb;
}
public double getReproductionProb() {
return m_reproductionProb;
}
public void setReproductionProb(float a_reproductionProb) {
m_reproductionProb = a_reproductionProb;
}
/**
* @param a_functionProb probability that a function is choosen in crossing
* over (between 0 and 1)
*
* @author Klaus Meffert
* @since 3.2
*/
public void setFunctionProb(double a_functionProb) {
m_functionProb = a_functionProb;
}
/**
* @return probability that a function is choosen in crossing over
*
* @author Klaus Meffert
* @since 3.2
*/
public double getFunctionProb() {
return m_functionProb;
}
public void setNewChromsPercent(double a_newChromsPercent) {
m_newChromsPercent = a_newChromsPercent;
}
public double getNewChromsPercent() {
return m_newChromsPercent;
}
public int getMaxCrossoverDepth() {
return m_maxCrossoverDepth;
}
public void setMaxCrossoverDepth(int a_maxCrossoverDepth) {
m_maxCrossoverDepth = a_maxCrossoverDepth;
}
public INaturalGPSelector getSelectionMethod() {
return m_selectionMethod;
}
public CrossMethod getCrossMethod() {
return m_crossMethod;
}
public int getMaxInitDepth() {
return m_maxInitDepth;
}
public void setMaxInitDepth(int a_maxDepth) {
m_maxInitDepth = a_maxDepth;
}
public int getMinInitDepth() {
return m_minInitDepth;
}
public void setMinInitDepth(int a_minDepth) {
m_minInitDepth = a_minDepth;
}
public void pushToStack(Object a_value) {
m_stack.push(a_value);
}
public Object popFromStack() {
return m_stack.pop();
}
public Object peekStack() {
return m_stack.peek();
}
public int stackSize() {
return m_stack.size();
}
public void clearStack() {
m_stack.clear();
}
/**
* Stores a value in the internal memory.
*
* @param a_name named index of the memory cell
* @param a_value the value to store
*
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?