📄 gppopulation.java
字号:
* @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];
}
public IGPProgram[] getGPPrograms() {
return m_programs;
}
public int size() {
return m_programs.length;
}
/**
* Determines the fittest GPProgram in the population (the one with the
* highest fitness value) and memorizes it. This is an optimized version
* compared to calling determineFittesPrograms(1).
*
* @return the fittest GPProgram of the population
*
* @author Klaus Meffert
* @since 3.0
*/
public IGPProgram determineFittestProgram() {
if (!m_changed && m_fittestProgram != null) {
return m_fittestProgram;
}
double bestFitness = -1.0d;
IGPFitnessEvaluator evaluator = getGPConfiguration().getGPFitnessEvaluator();
double fitness;
for (int i = 0; i < m_programs.length && m_programs[i] != null; i++) {
IGPProgram program = m_programs[i];
fitness = program.getFitnessValue();
if (m_fittestProgram == null || evaluator.isFitter(fitness, bestFitness)) {
bestFitness = fitness;
m_fittestProgram = program;
}
}
setChanged(false);
if (m_fittestProgram != null) {
IJGAPFactory factory = getGPConfiguration().getJGAPFactory();
if (factory == null) {
throw new IllegalStateException("JGAPFactory must not be null!");
}
ICloneHandler cloner = factory.getCloneHandlerFor(m_fittestProgram, null);
if (cloner != null) {
try {
m_fittestProgram = (IGPProgram) cloner.perform(m_fittestProgram, null, null);
}
catch (Exception ex) {
; // ignore
}
}
}
return m_fittestProgram;
}
/**
* Determines the fittest GPProgram in the population, but only considers
* programs with already computed fitness value.
*
* @return the fittest GPProgram of the population
*
* @author Klaus Meffert
* @since 3.2
*/
public IGPProgram determineFittestProgramComputed() {
double bestFitness = -1.0d;
IGPFitnessEvaluator evaluator = getGPConfiguration().getGPFitnessEvaluator();
double fitness;
IGPProgram fittest = null;
for (int i = 0; i < m_programs.length && m_programs[i] != null; i++) {
IGPProgram program = m_programs[i];
if (program instanceof GPProgramBase) {
GPProgramBase program1 = (GPProgramBase) program;
fitness = program1.getFitnessValueDirectly();
}
else {
fitness = program.getFitnessValue();
}
if (Math.abs(fitness - FitnessFunction.NO_FITNESS_VALUE) >
FitnessFunction.DELTA) {
if (fittest == null || evaluator.isFitter(fitness, bestFitness)) {
fittest = program;
bestFitness = fitness;
}
}
}
return fittest;
}
/**
* Sorts the GPPrograms list and returns the fittest n GPPrograms in
* the population.
*
* @param a_numberOfPrograms number of top performer GPPrograms to be
* returned
* @return list of the fittest n GPPrograms of the population, or the fittest
* x GPPrograms with x = number of GPPrograms in case n > x.
*
* @author Klaus Meffert
* @since 3.0
*/
public List determineFittestChromosomes(final int a_numberOfPrograms) {
int numberOfChromosomes = Math.min(a_numberOfPrograms, m_programs.length);
if (numberOfChromosomes <= 0) {
return null;
}
if (!m_changed && m_sorted) {
return Arrays.asList(m_programs).subList(0, numberOfChromosomes);
}
// Sort the list of chromosomes using the fitness comparator
sortByFitness();
// Return the top n chromosomes
return Arrays.asList(m_programs).subList(0, numberOfChromosomes);
}
/**
* Sorts the programs within the population according to their fitness
* value using GPProgramFitnessComparator.
*
* @author Klaus Meffert
* @since 3.0
*/
public void sortByFitness() {
// The following construction could be cached but wrt that the
// evaluator registered with the configuration could change
// --> Don't cache it!
sort(new GPProgramFitnessComparator(getGPConfiguration().
getGPFitnessEvaluator()));
setChanged(false);
setSorted(true);
m_fittestProgram = m_programs[0];
}
public float[] getFitnessRanks() {
return m_fitnessRank;
}
public float getFitnessRank(int a_index) {
return m_fitnessRank[a_index];
}
/**
* Mark that for the population the fittest program may have changed.
*
* @param a_changed true: population's fittest program may have changed,
* false: fittest program evaluated earlier is still valid
*
* @author Klaus Meffert
* @since 3.0
*/
protected void setChanged(final boolean a_changed) {
m_changed = a_changed;
setSorted(false);
}
/**
* @return true: population's programs (maybe) were changed,
* false: not changed for sure
*
* @since 3.0
*/
public boolean isChanged() {
return m_changed;
}
/**
* Mark the population as sorted.
* @param a_sorted true: mark population as sorted
*
* @author Klaus Meffert
* @since 3.0
*/
protected void setSorted(final boolean a_sorted) {
m_sorted = a_sorted;
}
/**
* This method is not producing symmetric results as -1 is more often returned
* than 1 (see description of return value).
*
* @param a_pop the other population to compare
* @return 1: a_pop is null or having fewer programs or equal number
* of programs but at least one not contained. 0: both populations
* containing exactly the same programs. -1: this population contains fewer
* programs than a_pop
*
* @author Klaus Meffert
* @since 2.6
*/
public int compareTo(Object a_pop) {
GPPopulation other = (GPPopulation) a_pop;
if (a_pop == null) {
return 1;
}
int size1 = size();
int size2 = other.size();
if (size1 != size2) {
if (size1 < size2) {
return -1;
}
else {
return 1;
}
}
IGPProgram[] progs2 = other.getGPPrograms();
for (int i = 0; i < size1; i++) {
if (!containedInArray(progs2, m_programs[i])) {
return 1;
}
}
return 0;
}
/**
* Checks if a program is contained within an array of programs. Assumes that
* in the array no element will follow after the first null element.
*
* @param a_progs the array to search thru
* @param a_prog the program to find
* @return true: program found in array via equals-method
*
* @author Klaus Meffert
* @since 3.0
*/
protected boolean containedInArray(IGPProgram[] a_progs, IGPProgram a_prog) {
for (int i = 0; i < a_progs.length; i++) {
if (a_progs[i] == null) {
return false;
}
if (a_progs[i].equals(a_prog)) {
return true;
}
}
return false;
}
/**
* The equals-method.
*
* @param a_pop the population instance to compare with
* @return true: given object equal to comparing one
*
* @author Klaus Meffert
* @since 3.0
*/
public boolean equals(Object a_pop) {
try {
return compareTo(a_pop) == 0;
}
catch (ClassCastException e) {
// If the other object isn't an Population instance
// then we're not equal.
// ------------------------------------------------
return false;
}
}
/**
* Adds a GP program to this Population. Does nothing when given null.
* The injection is actually executed in method create(..)
*
* @param a_toAdd the program to add
*
* @author Klaus Meffert
* @since 3.2
*/
public void addFittestProgram(final IGPProgram a_toAdd) {
if (a_toAdd != null) {
m_fittestToAdd = a_toAdd;
}
}
/**
* Clears the list of programs. Normally, this should not be necessary.
* But especially in distributed computing, a fresh population has to be
* provided sometimes.
*
* @author Klaus Meffert
* @since 3.2
*/
public void clear() {
for (int i = 0; i < m_programs.length; i++) {
m_programs[i] = null;
}
m_changed = true;
m_sorted = true;
m_fittestProgram = null;
}
public boolean isFirstEmpty() {
if (size() < 1) {
return true;
}
if (m_programs[0] == null) {
return true;
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -