📄 programchromosome.java
字号:
for (i = a_index + 1; i < getFunctions().length && getFunctions()[i] != null;
i++) {
if (m_depth[i] <= m_depth[a_index]) {
break;
}
}
return i - a_index;
}
/**
* Gets the depth of the branch starting at the a_index'th node.
*
* @param a_index the index of the node at which to check the depth
* @return the depth of the branch starting at the a_index'th node
*
* @author Klaus Meffert
* @since 3.0
*/
public int getDepth(int a_index) {
int i, maxdepth = m_depth[a_index];
for (i = a_index + 1; i < getFunctions().length && getFunctions()[i] != null;
i++) {
if (m_depth[i] <= m_depth[a_index]) {
break;
}
if (m_depth[i] > maxdepth) {
maxdepth = m_depth[i];
}
}
return maxdepth - m_depth[a_index];
}
/**
* Gets the node which is the parent of the given node in this chromosome. If
* the child is at depth d then the parent is the first function at depth d-1
* when iterating backwards through the function list starting from the child.
*
* @param a_child the child node
* @return the parent node, or null if the child is the root node
*
* @author Klaus Meffert
* @since 3.0
*/
public int getParentNode(int a_child) {
if (a_child >= getFunctions().length || getFunctions()[a_child] == null) {
return -1;
}
for (int i = a_child - 1; i >= 0; i--) {
if (m_depth[i] == m_depth[a_child] - 1) {
return i;
}
}
return -1;
}
/**
* Executes this node as a boolean.
*
* @param args the arguments for execution
* @return the boolean return value of this node
* @throws UnsupportedOperationException if the type of this node is not
* boolean
*
* @author Klaus Meffert
* @since 3.0
*/
public boolean execute_boolean(Object[] args) {
boolean rtn = getFunctions()[0].execute_boolean(this, 0, args);
cleanup();
return rtn;
}
/**
* Executes this node as a boolean.
*
* @param n the index of the parent node
* @param child the child number of the node to execute
* @param args the arguments for execution
* @return the boolean return value of this node
* @throws UnsupportedOperationException if the type of this node is not
* boolean
*
* @author Klaus Meffert
* @since 3.0
*/
public boolean execute_boolean(int n, int child, Object[] args) {
if (child == 0) {
return getFunctions()[n + 1].execute_boolean(this, n + 1, args);
}
int other = getChild(n, child);
return getFunctions()[other].execute_boolean(this, other, args);
}
/**
* Executes this node, returning nothing.
*
* @param args the arguments for execution
* @throws UnsupportedOperationException if the type of this node is not void
*
* @author Klaus Meffert
* @since 3.0
*/
public void execute_void(Object[] args) {
getFunctions()[0].execute_void(this, 0, args);
cleanup();
}
public void execute_void(int n, int child, Object[] args) {
if (child == 0) {
getFunctions()[n + 1].execute_void(this, n + 1, args);
}
else {
int other = getChild(n, child);
getFunctions()[other].execute_void(this, other, args);
}
}
/**
* Executes this node as an integer.
*
* @param args the arguments for execution
* @return the integer return value of this node
* @throws UnsupportedOperationException if the type of this node is not
* integer
*
* @author Klaus Meffert
* @since 3.0
*/
public int execute_int(Object[] args) {
int rtn = getFunctions()[0].execute_int(this, 0, args);
cleanup();
return rtn;
}
public int execute_int(int n, int child, Object[] args) {
if (child == 0) {
return getFunctions()[n + 1].execute_int(this, n + 1, args);
}
int other = getChild(n, child);
return getFunctions()[other].execute_int(this, other, args);
}
/**
* Executes this node as a long.
*
* @param args the arguments for execution
* @return the long return value of this node
* @throws UnsupportedOperationException if the type of this node is not long
*
* @author Klaus Meffert
* @since 3.0
*/
public long execute_long(Object[] args) {
long rtn = getFunctions()[0].execute_long(this, 0, args);
cleanup();
return rtn;
}
public long execute_long(int n, int child, Object[] args) {
if (child == 0) {
return getFunctions()[n + 1].execute_long(this, n + 1, args);
}
int other = getChild(n, child);
return getFunctions()[other].execute_long(this, other, args);
}
/**
* Executes this node as a float.
*
* @param args the arguments for execution
* @return the float return value of this node
* @throws UnsupportedOperationException if the type of this node is not float
*
* @author Klaus Meffert
* @since 3.0
*/
public float execute_float(Object[] args) {
float rtn = getFunctions()[0].execute_float(this, 0, args);
cleanup();
return rtn;
}
public float execute_float(int n, int child, Object[] args) {
if (child == 0) {
return getFunctions()[n + 1].execute_float(this, n + 1, args);
}
int other = getChild(n, child);
return getFunctions()[other].execute_float(this, other, args);
}
/**
* Executes this node as a double.
*
* @param args the arguments for execution
* @return the double return value of this node
* @throws UnsupportedOperationException if this node's type is not double
*
* @author Klaus Meffert
* @since 3.0
*/
public double execute_double(Object[] args) {
double rtn = getFunctions()[0].execute_double(this, 0, args);
cleanup();
return rtn;
}
public double execute_double(int n, int child, Object[] args) {
if (child == 0) {
return getFunctions()[n + 1].execute_double(this, n + 1, args);
}
int other = getChild(n, child);
return getFunctions()[other].execute_double(this, other, args);
}
/**
* Executes this node as an object.
*
* @param args the arguments for execution
* @return the object return value of this node
* @throws UnsupportedOperationException if the type of this node is not
* of type Object
*
* @author Klaus Meffert
* @since 3.0
*/
public Object execute_object(Object[] args) {
Object rtn = getFunctions()[0].execute_object(this, 0, args);
cleanup();
return rtn;
}
public Object execute_object(int n, int child, Object[] args) {
if (child == 0) {
return getFunctions()[n + 1].execute_object(this, n + 1, args);
}
int other = getChild(n, child);
return getFunctions()[other].execute_object(this, other, args);
}
/**
* Executes this node without knowing its return type.
*
* @param args the arguments for execution
* @return the Object which wraps the return value of this node, or null
* if the return type is null or unknown
*
* @author Klaus Meffert
* @since 3.0
*/
public Object execute(Object[] args) {
return getFunctions()[0].execute_object(this, 0, args);
}
public Object execute(int n, int child, Object[] args) {
return execute_object(n, child, args);
}
public void setGene(int index, CommandGene a_gene) {
if (a_gene == null) {
throw new IllegalArgumentException("Gene may not be null!");
}
m_genes[index] = a_gene;
}
public Class[] getArgTypes() {
return argTypes;
}
public int getArity() {
return argTypes.length;
}
/**
* @return number of functions and terminals present
*
* @author Klaus Meffert
* @since 3.0
*/
public int size() {
int i = 0;
while (i < getFunctions().length && getFunctions()[i] != null) {
i++;
}
return i;
}
public GPConfiguration getGPConfiguration() {
return m_configuration;
}
/**
* Compares the given chromosome to this chromosome. This chromosome is
* considered to be "less than" the given chromosome if it has a fewer
* number of genes or if any of its gene values (alleles) are less than
* their corresponding gene values in the other chromosome.
*
* @param a_other the chromosome against which to compare this chromosome
* @return a negative number if this chromosome is "less than" the given
* chromosome, zero if they are equal to each other, and a positive number if
* this chromosome is "greater than" the given chromosome
*
* @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();
ProgramChromosome otherChromosome = (ProgramChromosome) a_other;
CommandGene[] otherGenes = otherChromosome.m_genes;
// If the other Chromosome doesn't have the same number of genes,
// then whichever has more is the "greater" Chromosome.
// --------------------------------------------------------------
if (otherChromosome.size() != size) {
return size() - otherChromosome.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.
// ---------------------------------------------------------------
for (int i = 0; i < size; i++) {
int comparison = m_genes[i].compareTo(otherGenes[i]);
if (comparison != 0) {
return comparison;
}
}
/**@todo compare m_functionSet*/
if (isCompareApplicationData()) {
// Compare application data.
// -------------------------
if (getApplicationData() == null) {
if (otherChromosome.getApplicationData() != null) {
return -1;
}
}
else if (otherChromosome.getApplicationData() == null) {
return 1;
}
else {
if (getApplicationData() instanceof Comparable) {
try {
return ( (Comparable) getApplicationData()).compareTo(
otherChromosome.getApplicationData());
}
catch (ClassCastException cex) {
return -1;
}
}
else {
return getApplicationData().getClass().getName().compareTo(
otherChromosome.getApplicationData().getClass().getName());
}
}
}
// Everything is equal. Return zero.
// ---------------------------------
return 0;
}
/**
* Compares this chromosome against the specified object.
*
* @param a_other the object to compare against
* @return true: if the objects are the same, false otherwise
*
* @author Klaus Meffert
* @since 3.0
*/
public boolean equals(Object a_other) {
try {
return compareTo(a_other) == 0;
}
catch (ClassCastException cex) {
return false;
}
}
/**
* @return the GPProgram containing this chromosome
*
* @author Klaus Meffert
* @since 3.0
*/
public IGPProgram getIndividual() {
return m_ind;
}
public void setIndividual(IGPProgram a_ind) {
m_ind = a_ind;
}
/**
* Should we also consider the application data when comparing? Default is
* "false" as "true" means a Chromosome is losing its identity when
* application data is set differently!
*
* @param a_doCompare true: consider application data in method compareTo
*
* @author Klaus Meffert
* @since 3.0
*/
public void setCompareApplicationData(boolean a_doCompare) {
m_compareAppData = a_doCompare;
}
/*
* @return should we also consider the application data when comparing?
*
* @author Klaus Meffert
* @since 3.0
*/
public boolean isCompareApplicationData() {
return m_compareAppData;
}
/**
* Retrieves the application-specific data that is attached to this
* Chromosome. Attaching application-specific data may be useful for
* some applications when it comes time to evaluate this Chromosome
* in the fitness function. JGAP ignores this data functionally.
*
* @return the application-specific data previously attached to this
* Chromosome, or null if there is no data attached
*
* @author Klaus Meffert
* @since 3.0
*/
public Object getApplicationData() {
return m_applicationData;
}
/**
* Returns the Gene at the given index (locus) within the Chromosome. The
* first gene is at index zero and the last gene is at the index equal to
* the size of this Chromosome - 1.
*
* @param a_locus index of the gene value to be returned
* @return Gene at the given index
*
* @author Klaus Meffert
* @since 3.0
*/
public synchronized CommandGene getGene(int a_locus) {
return m_genes[a_locus];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -