📄 gpprogram.java
字号:
* @param a_startNode the node to start with
* @return output in left-hand notion
*
* @author Klaus Meffert
* @since 3.0
*/
public String toString(int a_startNode) {
if (a_startNode < 0) {
return "";
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < m_chromosomes.length; i++) {
if (i > 0) {
sb.append(" ==> ");
}
sb.append(m_chromosomes[i].toString(a_startNode));
}
return sb.toString();
}
/**
* Builds a string that represents the normalized output of the GPProgram.
*
* @param a_startNode the node to start with
* @return output in normalized notion
*
* @author Klaus Meffert
* @since 3.0
*/
public String toStringNorm(int a_startNode) {
if (a_startNode < 0) {
return "";
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < m_chromosomes.length; i++) {
if (i > 0) {
sb.append(" ==> ");
}
m_chromosomes[i].setIndividual(this);
sb.append(m_chromosomes[i].toStringNorm(a_startNode));
}
return sb.toString();
}
/**
* Builds a string that represents the debug output of the GPProgram.
*
* @return class names of all program chromosomes
*
* @author Klaus Meffert
* @since 3.0
*/
public String toStringDebug() {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < m_chromosomes.length; i++) {
if (i > 0) {
sb.append(" ==> ");
}
m_chromosomes[i].setIndividual(this);
sb.append(m_chromosomes[i].toStringDebug());
}
return sb.toString();
}
/**
* Executes the given chromosome as an integer function.
*
* @param a_chromosomeNum the index of the chromosome to execute
* @param a_args the arguments to use
* @return the integer return value
*
* @author Klaus Meffert
* @since 3.0
*/
public int execute_int(int a_chromosomeNum, Object[] a_args) {
m_chromosomes[a_chromosomeNum].setIndividual(this);
return m_chromosomes[a_chromosomeNum].execute_int(a_args);
}
/**
* Executes the given chromosome as a float function.
*
* @param a_chromosomeNum the index of the chromosome to execute
* @param a_args the arguments to use
* @return the floar return value
*
* @author Klaus Meffert
* @since 3.0
*/
public float execute_float(int a_chromosomeNum, Object[] a_args) {
m_chromosomes[a_chromosomeNum].setIndividual(this);
return m_chromosomes[a_chromosomeNum].execute_float(a_args);
}
/**
* Executes the given chromosome as a double function.
*
* @param a_chromosomeNum the index of the chromosome to execute
* @param a_args the arguments to use
* @return the double return value
*
* @author Klaus Meffert
* @since 3.0
*/
public double execute_double(int a_chromosomeNum, Object[] a_args) {
m_chromosomes[a_chromosomeNum].setIndividual(this);
return m_chromosomes[a_chromosomeNum].execute_double(a_args);
}
/**
* Executes the given chromosome as a boolean function.
*
* @param a_chromosomeNum the index of the chromosome to execute
* @param a_args the arguments to use
* @return the boolean return value
*
* @author Klaus Meffert
* @since 3.2
*/
public boolean execute_boolean(int a_chromosomeNum, Object[] a_args) {
m_chromosomes[a_chromosomeNum].setIndividual(this);
return m_chromosomes[a_chromosomeNum].execute_boolean(a_args);
}
/**
* Executes the given chromosome as an object function.
*
* @param a_chromosomeNum the index of the chromosome to execute
* @param a_args the arguments to use
* @return the object return value
*
* @author Klaus Meffert
* @since 3.0
*/
public Object execute_object(int a_chromosomeNum, Object[] a_args) {
m_chromosomes[a_chromosomeNum].setIndividual(this);
return m_chromosomes[a_chromosomeNum].execute_object(a_args);
}
/**
* Executes the given chromosome as an object function.
*
* @param a_chromosomeNum the index of the chromosome to execute
* @param a_args the arguments to use
*
* @author Klaus Meffert
* @since 3.0
*/
public void execute_void(int a_chromosomeNum, Object[] a_args) {
m_chromosomes[a_chromosomeNum].setIndividual(this);
m_chromosomes[a_chromosomeNum].execute_void(a_args);
}
/**
* Searches for a chromosome that has the given class and returns its index.
*
* @param a_chromosomeNum the index of the chromosome to start the search with
* @param a_class the class to find
* @return the index of the first chromosome found that is of a_class, or -1
*
* @author Klaus Meffert
* @since 3.0
*/
public int getCommandOfClass(int a_chromosomeNum, Class a_class) {
for (int i = a_chromosomeNum; i < m_chromosomes.length; i++) {
int j = m_chromosomes[i].getCommandOfClass(0, a_class);
if (j >= 0) {
return j;
}
}
return -1;
}
/**
* Compares the given program to this program.
*
* @param a_other the program against which to compare this program
* @return a negative number if this program is "less than" the given
* program, zero if they are equal to each other, and a positive number if
* this program is "greater than" the given program
*
* @author Klaus Meffert
* @since 3.0
*/
public int compareTo(Object a_other) {
// First, if the other Chromosome is null, then this chromosome is
// automatically the "greater" Chromosome.
// ---------------------------------------------------------------
if (a_other == null) {
return 1;
}
int size = size();
GPProgram other = (GPProgram) a_other;
ProgramChromosome[] otherChroms = other.m_chromosomes;
// If the other Chromosome doesn't have the same number of genes,
// then whichever has more is the "greater" Chromosome.
// --------------------------------------------------------------
if (other.size() != size) {
return size() - other.size();
}
// Next, compare the gene values (alleles) for differences. If
// one of the genes is not equal, then we return the result of its
// comparison.
// ---------------------------------------------------------------
Arrays.sort(m_chromosomes);
Arrays.sort(otherChroms);
for (int i = 0; i < size; i++) {
int comparison = m_chromosomes[i].compareTo(otherChroms[i]);
if (comparison != 0) {
return comparison;
}
}
// Everything is equal. Return zero.
// ---------------------------------
return 0;
}
/**
* @return deep clone of the object instance
*
* @author Klaus Meffert
* @since 3.2
*/
public Object clone() {
try {
// Min and max depths can be null.
// -------------------------------
int[] minDepthsClone;
if (getMinDepths() != null) {
minDepthsClone = (int[]) getMinDepths().clone();
}
else {
minDepthsClone = null;
}
int[] maxDepthsClone;
if (getMaxDepths() != null) {
maxDepthsClone = (int[]) getMaxDepths().clone();
}
else {
maxDepthsClone = null;
}
GPProgram result = new GPProgram(getGPConfiguration(),
(Class[]) getTypes().clone(),
(Class[][]) getArgTypes().clone(),
(CommandGene[][]) getNodeSets().clone(),
minDepthsClone,
maxDepthsClone,
getMaxNodes());
result.setFitnessValue(getFitnessValueDirectly());
// Try to clone application data.
// ------------------------------
Object appData = getApplicationData();
if (appData != null) {
ICloneHandler cloner = getGPConfiguration().getJGAPFactory().
getCloneHandlerFor(appData, null);
if (cloner != null) {
result.setApplicationData(cloner.perform(appData, null, null));
}
else {
result.setApplicationData(appData);
}
}
for (int i = 0; i < m_chromosomes.length; i++) {
if (m_chromosomes[i] == null) {
break;
}
result.m_chromosomes[i] = (ProgramChromosome) m_chromosomes[i].clone();
}
return result;
} catch (Exception ex) {
throw new CloneException(ex);
}
}
/**
* @return the persistent representation of the GP program, including all
* chromosomes
*
* @author Klaus Meffert
* @since 3.3
*/
public String getPersistentRepresentation() {
StringBuffer b = new StringBuffer();
for (ProgramChromosome chrom : m_chromosomes) {
b.append(PROGRAMCHROM_DELIMITER_HEADING);
b.append(encode(
chrom.getClass().getName() +
PROGRAMCHROM_DELIMITER +
chrom.getPersistentRepresentation()));
b.append(PROGRAMCHROM_DELIMITER_CLOSING);
}
return b.toString();
}
protected String encode(String a_string) {
return StringKit.encode(a_string);
}
protected String decode(String a_string) {
return StringKit.decode(a_string);
}
/**
* @return hopefully unique key representing the state of the GPProgram
*
* @author Klaus Meffert
* @since 3.4
*/
public String getBusinessKey() {
return toStringNorm(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -