📄 gppopulation.java
字号:
// --> then clone the prototype.
// Necessary if the maxNodes parameter is chosen small
// or a validator is used which is quite restrictive.
// ---------------------------------------------------
getGPConfiguration().setPrototypeProgram(program);
/**@todo output depth of all chromosomes*/
LOGGER.info("Prototype program set (depth " +
program.getChromosome(0).getDepth(0) + ")");
}
else if (genNr % 5 == 0 && genNr > 0 && i == genI) {
/**@todo 5: make configurable*/
// Set prototype to new value after every some evolutions.
// -------------------------------------------------------
double protoFitness = getGPConfiguration().getPrototypeProgram().
getFitnessValue();
if (getGPConfiguration().getGPFitnessEvaluator().isFitter(program.
getFitnessValue(), protoFitness)) {
getGPConfiguration().setPrototypeProgram(program);
}
}
break;
} catch (IllegalStateException iex) {
if (depth < maxDepth) {
depth = depth + generator.nextInt(2);
}
else {
depth = depth - generator.nextInt(4);
if (depth < minDepth) {
depth = minDepth;
}
}
tries++;
if (maxTries >=0 && tries > maxTries || (i > a_offset && tries > 40)) {
IGPProgram prototype = getGPConfiguration().getPrototypeProgram();
if (prototype != null) {
ICloneHandler cloner = getGPConfiguration().getJGAPFactory().
getCloneHandlerFor(prototype, null);
if (cloner != null) {
try {
program = (IGPProgram) cloner.perform(prototype, null, null);
LOGGER.warn("Prototype program reused because random"
+ " program did not satisfy constraints");
break;
} catch (Exception ex) {
// Rethrow original error.
// -----------------------
throw iex;
}
}
else {
LOGGER.warn("Warning: no clone handler found for"
+ " prototype program type "
+ prototype);
}
}
// Rethrow original error.
// -----------------------
throw iex;
}
}
} while (true);
setGPProgram(i, program);
}
setChanged(true);
}
/**
* Creates a population.
*
* @param a_types Class[]
* @param a_argTypes Class[][]
* @param a_nodeSets CommandGene[][]
* @param a_minDepths int[]
* @param a_maxDepths int[]
* @param a_depth int
* @param a_grow boolean
* @param a_maxNodes int
* @param a_fullModeAllowed boolean[]
* @param a_tries int
* @return IGPProgram
* @throws InvalidConfigurationException
*
* @author Klaus Meffert
* @since 3.2.2
*/
public IGPProgram create(Class[] a_types, Class[][] a_argTypes,
CommandGene[][] a_nodeSets, int[] a_minDepths,
int[] a_maxDepths, int a_depth, boolean a_grow,
int a_maxNodes, boolean[] a_fullModeAllowed,
int a_tries)
throws InvalidConfigurationException {
return create(0, a_types, a_argTypes, a_nodeSets, a_minDepths, a_maxDepths,
a_depth, a_grow, a_maxNodes, a_fullModeAllowed, a_tries);
}
/**
*
* @param a_programIndex int
* @param a_types Class[]
* @param a_argTypes Class[][]
* @param a_nodeSets CommandGene[][]
* @param a_minDepths int[]
* @param a_maxDepths int[]
* @param a_depth int
* @param a_grow boolean
* @param a_maxNodes int
* @param a_fullModeAllowed boolean[]
* @param a_tries int
* @return IGPProgram
* @throws InvalidConfigurationException
*
* @author Klaus Meffert
* @since 3.3
*/
public IGPProgram create(int a_programIndex, Class[] a_types,
Class[][] a_argTypes,
CommandGene[][] a_nodeSets, int[] a_minDepths,
int[] a_maxDepths, int a_depth, boolean a_grow,
int a_maxNodes, boolean[] a_fullModeAllowed,
int a_tries)
throws InvalidConfigurationException {
return create(a_programIndex, a_types, a_argTypes, a_nodeSets, a_minDepths,
a_maxDepths, a_depth, a_grow, a_maxNodes, a_fullModeAllowed,
a_tries, new DefaultProgramCreator());
}
/**
* Creates a valid IGPProgram. No fitness computation is initiated here!
*
* @param a_programIndex index of the program in the population
* @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
* @param a_minDepths contains the minimum depth allowed for each chromosome
* @param a_maxDepths contains the maximum depth allowed for each chromosome
* @param a_depth the maximum depth of the program to create
* @param a_grow true: grow mode, false: full mode
* @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 chromosomes
* during evolution is allowed (true) or not (false)
* @param a_tries maximum number of tries to get a valid program
* @param a_programCreator strategy class to create programs for the
* population
*
* @return valid program
*
* @throws InvalidConfigurationException
*
* @author Klaus Meffert
* @since 3.0
*/
public IGPProgram create(int a_programIndex, Class[] a_types,
Class[][] a_argTypes,
CommandGene[][] a_nodeSets, int[] a_minDepths,
int[] a_maxDepths, int a_depth, boolean a_grow,
int a_maxNodes, boolean[] a_fullModeAllowed,
int a_tries, IProgramCreator a_programCreator)
throws InvalidConfigurationException {
// Is there a fittest program to be injected?
// ------------------------------------------
if (m_fittestToAdd != null) {
IGPProgram program;
ICloneHandler cloner = getGPConfiguration().getJGAPFactory().
getCloneHandlerFor(m_fittestToAdd, null);
if (cloner == null) {
program = (IGPProgram) m_fittestToAdd;
}
else {
try {
program = (IGPProgram) cloner.perform(m_fittestToAdd, null, null);
} catch (Exception ex) {
ex.printStackTrace();
program = (IGPProgram) m_fittestToAdd;
}
}
// Clear out the fittest program to add as it just has been added.
// ---------------------------------------------------------------
m_fittestToAdd = null;
return program;
}
else {
// Create new GP program.
// ----------------------
IGPProgram program;
program = a_programCreator.create(getGPConfiguration(), a_programIndex,
a_types, a_argTypes, a_nodeSets,
a_minDepths, a_maxDepths, a_maxNodes,
a_depth, a_grow, a_tries,
a_fullModeAllowed);
return program;
}
}
/**
* @return fixed size of the population
*
* @author Klaus Meffert
* @since 3.0
*/
public int getPopSize() {
return m_popSize;
}
/**
* @return the GPConfiguration set
*
* @author Klaus Meffert
* @since 3.0
*/
public GPConfiguration getGPConfiguration() {
return m_config;
}
/**
* Sets the given GPProgram at the given index in the list of GPProgram's.
* If the given index is exceeding the list by one, the chromosome is
* appended.
*
* @param a_index the index to set the GPProgram in
* @param a_program the GPProgram to be set
*
* @author Klaus Meffert
* @since 3.0
*/
public void setGPProgram(final int a_index, final IGPProgram a_program) {
synchronized (m_programs) {
m_programs[a_index] = a_program;
}
setChanged(true);
}
public IGPProgram getGPProgram(int a_index) {
return m_programs[a_index];
}
/**
* Sets the GPPrograms of the given population to this population.
*
* @param a_pop the population to use as template
*
* @author Klaus Meffert
* @since 3.3.3
*/
public void setGPPrograms(final GPPopulation a_pop) {
synchronized (m_programs) {
m_programs = a_pop.m_programs;
m_popSize = m_programs.length;
}
setChanged(true);
}
/**
* Sets the GPPrograms of the given array to this population.
*
* @param a_progs the programs to set
*
* @author Klaus Meffert
* @since 3.3.3
*/
public void setGPPrograms(final IGPProgram[] a_progs) {
synchronized (m_programs) {
m_programs = a_progs;
m_popSize = m_programs.length;
}
setChanged(true);
}
/**
* Sets the GPPrograms of the given population to this population.
*
* @param a_pop the population to use as template
*
* @author Klaus Meffert
* @since 3.3.3
*/
public void copyGPPrograms(final GPPopulation a_pop) {
int size = a_pop.size();
synchronized (m_programs) {
for (int i = 0; i < size; i++) {
m_programs[i] = a_pop.getGPProgram(i);
}
}
setChanged(true);
}
/**
* Sets the GPPrograms of the given population to this population.
*
* @param a_pop the population to use as template
*
* @author Klaus Meffert
* @since 3.3.3
*/
public void setGPPrograms(final List a_pop) {
synchronized (m_programs) {
int size = a_pop.size();
m_programs = new GPProgram[size];
for (int i = 0; i < size; i++) {
m_programs[i] = (IGPProgram) a_pop.get(i);
}
}
setChanged(true);
}
public IGPProgram[] getGPPrograms() {
return m_programs;
}
public int size() {
return m_programs.length;
}
/**
* Determines the fittest GPProgram in the population (the one with the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -