📄 gpgenotype.java
字号:
/*
* This file is part of JGAP.
*
* JGAP offers a dual license model containing the LGPL as well as the MPL.
*
* For licensing 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.io.*;
import java.util.*;
import org.apache.log4j.*;
import org.jgap.*;
import org.jgap.event.*;
import org.jgap.gp.*;
import org.jgap.gp.terminal.*;
import org.jgap.util.*;
/**
* Genotype for GP Programs.
*
* @author Klaus Meffert
* @since 3.0
*/
public class GPGenotype
implements Runnable, Serializable, Comparable {
/** String containing the CVS revision. Read out via reflection!*/
private final static String CVS_REVISION = "$Revision: 1.54 $";
private transient static Logger LOGGER = Logger.getLogger(GPGenotype.class);
/**
* The array of GPProgram's that makeup the GPGenotype's population.
*/
private GPPopulation m_population;
/**
* The current configuration instance.
*/
private
/*transient*/GPConfiguration m_configuration;
private transient static GPConfiguration m_staticConfiguration;
/**
* Fitness value of the best solution.
*/
private double m_bestFitness;
/**
* Sum of fitness values over all chromosomes.
*/
private double m_totalFitness;
/**
* Best solution found.
*/
private IGPProgram m_allTimeBest;
private double m_allTimeBestFitness;
/**
* Is full mode with program construction allowed?
*/
private boolean m_fullModeAllowed[];
/**
* Return type per chromosome.
*/
private Class[] m_types;
/**
* Argument types for ADF's
*/
private Class[][] m_argTypes;
/**
* Available GP-functions.
*/
private CommandGene[][] m_nodeSets;
/**
* Minimum depth per each chromosome
*/
private int[] m_minDepths;
/**
* Maximum depth per each chromosome
*/
private int[] m_maxDepths;
/**
* Maximum number of nodes allowed per chromosome (when exceeded program
* aborts)
*/
private int m_maxNodes;
/**
* True: Output status information to console
*/
private boolean m_verbose;
private Map m_variables;
private IGPProgram m_fittestToAdd;
private boolean m_cloneWarningGPProgramShown;
// private boolean[] disabledChromosomes;
/**
* Default constructor. Ony use with dynamic instantiation.
* @throws InvalidConfigurationException
*
* @author Klaus Meffert
* @since 3.0
*/
public GPGenotype()
throws InvalidConfigurationException {
init();
}
/**
* Preferred constructor to use, if not using the static method
* randomInitialGenotype.
*
* @param a_configuration the configuration to use
* @param a_population the initialized population to use
* @param a_types the type for each chromosome, the length of the array
* represents the number of chromosomes
* @param a_argTypes the types of the arguments to each chromosome, must be an
* array of arrays, the first dimension of which is the number of chromosomes
* and the second dimension of which is the number of arguments to the
* chromosome
* @param a_nodeSets the nodes which are allowed to be used by each chromosome,
* must be an array of arrays, the first dimension of which is the number of
* chromosomes and the second dimension of which is the number of nodes
* @param a_minDepths contains the minimum depth allowed for each chromosome
* @param a_maxDepths contains the maximum depth allowed for each chromosome
* @param a_maxNodes reserve space for a_maxNodes number of nodes
*
* @throws InvalidConfigurationException
*
* @author Klaus Meffert
* @since 3.0
*/
public GPGenotype(GPConfiguration a_configuration, GPPopulation a_population,
Class[] a_types, Class[][] a_argTypes,
CommandGene[][] a_nodeSets, int[] a_minDepths,
int[] a_maxDepths, int a_maxNodes)
throws InvalidConfigurationException {
// Sanity checks: Make sure neither the Configuration, the array
// of Chromosomes, nor any of the Genes inside the array are null.
// ---------------------------------------------------------------
if (a_configuration == null) {
throw new IllegalArgumentException(
"The configuration instance must not be null.");
}
if (a_population == null) {
throw new IllegalArgumentException(
"The population must not be null.");
}
for (int i = 0; i < a_population.size(); i++) {
if (a_population.getGPProgram(i) == null) {
throw new IllegalArgumentException(
"The GPProgram instance at index " + i + " in population" +
" is null, which is forbidden in general.");
}
}
init();
m_types = a_types;
m_argTypes = a_argTypes;
m_nodeSets = a_nodeSets;
m_maxDepths = a_maxDepths;
m_minDepths = a_minDepths;
m_maxNodes = a_maxNodes;
setGPPopulation(a_population);
setGPConfiguration(a_configuration);
m_variables = new Hashtable();
m_allTimeBestFitness = FitnessFunction.NO_FITNESS_VALUE;
// Lock the settings of the configuration object so that it cannot
// be altered.
// ---------------------------------------------------------------
getGPConfiguration().lockSettings();
}
protected void init() {
// disabledChromosomes = new boolean[100];
}
/**
* Creates a genotype with initial population for the world set.
*
* @param a_conf the configuration to use
* @param a_types the type of each chromosome, the length is the number of
* chromosomes
* @param a_argTypes the types of the arguments to each chromosome, must be an
* array of arrays, the first dimension of which is the number of chromosomes
* and the second dimension of which is the number of arguments to the
* chromosome
* @param a_nodeSets the nodes which are allowed to be used by each
* chromosome, must be an array of arrays, the first dimension of which is the
* number of chromosomes and the second dimension of which is the number of
* nodes. Note that it is not necessary to include the arguments of a
* chromosome as terminals in the chromosome's node set. This is done
* automatically
* @param a_maxNodes reserve space for a_maxNodes number of nodes
* @param a_verboseOutput true: output status information to console
*
* @return created genotype with initialized population
*
* @throws InvalidConfigurationException
*
* @author Klaus Meffert
* @since 3.0
*/
public static GPGenotype randomInitialGenotype(final GPConfiguration a_conf,
Class[] a_types, Class[][] a_argTypes, CommandGene[][] a_nodeSets,
int a_maxNodes, boolean a_verboseOutput)
throws InvalidConfigurationException {
int[] minDepths = null;
int[] maxDepths = null;
return randomInitialGenotype(a_conf, a_types, a_argTypes, a_nodeSets,
minDepths, maxDepths, a_maxNodes,
a_verboseOutput);
}
/**
* Creates a genotype with initial population for the world set.
*
* @param a_conf the configuration to use
* @param a_types the type of each chromosome, the length is the number of
* chromosomes
* @param a_argTypes the types of the arguments to each chromosome, must be an
* array of arrays, the first dimension of which is the number of chromosomes
* and the second dimension of which is the number of arguments to the
* chromosome
* @param a_nodeSets the nodes which are allowed to be used by each
* chromosome, must be an array of arrays, the first dimension of which is the
* number of chromosomes and the second dimension of which is the number of
* nodes. Note that it is not necessary to include the arguments of a
* chromosome as terminals in the chromosome's node set. This is done
* automatically
* @param a_minDepths array of minimum depths to use: for each chromosome
* one entry
* @param a_maxDepths array of maximum depths to use: for each chromosome
* one entry
* @param a_maxNodes reserve space for a_maxNodes number of nodes
* @param a_verboseOutput true: output status information to console
*
* @return created genotype with initialized population
*
* @throws InvalidConfigurationException
*
* @author Klaus Meffert
* @since 3.0
*/
public static GPGenotype randomInitialGenotype(final GPConfiguration a_conf,
Class[] a_types,
Class[][] a_argTypes,
CommandGene[][] a_nodeSets,
int[] a_minDepths,
int[] a_maxDepths,
int a_maxNodes,
boolean a_verboseOutput)
throws InvalidConfigurationException {
boolean[] fullModeAllowed = new boolean[a_types.length];
for (int i = 0; i < a_types.length; i++) {
fullModeAllowed[i] = true;
}
return randomInitialGenotype(a_conf, a_types, a_argTypes, a_nodeSets,
a_minDepths, a_maxDepths, a_maxNodes,
fullModeAllowed, a_verboseOutput);
}
/**
* Creates a genotype with a randomly created initial population.
*
* @param a_conf the configuration to use
* @param a_types the type of each chromosome, the length is the number of
* chromosomes
* @param a_argTypes the types of the arguments to each chromosome, must be an
* array of arrays, the first dimension of which is the number of chromosomes
* and the second dimension of which is the number of arguments to the
* chromosome
* @param a_nodeSets the nodes which are allowed to be used by each
* chromosome, must be an array of arrays, the first dimension of which is the
* number of chromosomes and the second dimension of which is the number of
* nodes. Note that it is not necessary to include the arguments of a
* chromosome as terminals in the chromosome's node set. This is done
* automatically
* @param a_minDepths array of minimum depths to use: for each chromosome
* one entry
* @param a_maxDepths array of maximum depths to use: for each chromosome
* one entry
* @param a_maxNodes reserve space for a_maxNodes number of nodes
* @param a_fullModeAllowed array of boolean values. For each chromosome there
* is one value indicating whether the full mode for creating chromosome
* generations during evolution is allowed (true) or not (false)
* @param a_verboseOutput true: output status information to console
*
* @return created genotype with initialized population
*
* @throws InvalidConfigurationException
*
* @author Klaus Meffert
* @since 3.0
*/
public static GPGenotype randomInitialGenotype(final GPConfiguration a_conf,
Class[] a_types, Class[][] a_argTypes, CommandGene[][] a_nodeSets,
int[] a_minDepths, int[] a_maxDepths, int a_maxNodes,
boolean[] a_fullModeAllowed, boolean a_verboseOutput)
throws InvalidConfigurationException {
return randomInitialGenotype(a_conf, a_types, a_argTypes, a_nodeSets,
a_minDepths, a_maxDepths, a_maxNodes,
a_fullModeAllowed, a_verboseOutput,
new DefaultPopulationCreator());
}
/**
* Allows to use a custom mechanism for population creation.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -