📄 programchromosome.java
字号:
*/
public Object execute_object(Object[] args) {
Object rtn = m_genes[0].execute_object(this, 0, args);
cleanup();
return rtn;
}
public Object execute_object(int n, int child, Object[] args) {
if (child == 0) {
return m_genes[n + 1].execute_object(this, n + 1, args);
}
int other = getChild(n, child);
return m_genes[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 m_genes[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 must 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 < m_genes.length && m_genes[i] != null) {
i++;
}
return i;
}
/**
* 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;
}
}
/**
* 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];
}
private CommandGene[] remove(CommandGene[] a_functionSet, CommandGene node) {
int size = a_functionSet.length;
for (int i = 0; i < size; i++) {
if (a_functionSet[i] == node) {
// Remove found element
CommandGene[] result = new CommandGene[size - 1];
if (i > 0) {
System.arraycopy(a_functionSet, 0, result, 0, i);
}
if (size - i > 1) {
System.arraycopy(a_functionSet, i + 1, result, i, size - i - 1);
}
return result;
}
}
return a_functionSet;
}
protected String encode(String a_string) {
return StringKit.encode(a_string);
}
protected String decode(String a_string) {
return StringKit.decode(a_string);
}
/**
* @return the persistent representation of the chromosome, including all
* genes
*
* @author Klaus Meffert
* @since 3.3
*/
public String getPersistentRepresentation() {
StringBuffer b = new StringBuffer();
// Store current state.
// --------------------
// String state = PERSISTENT_FIELD_DELIMITER+m_functionSet
// Process the contained genes.
// ----------------------------
for (CommandGene gene : m_genes) {
if (gene == null) {
break;
}
b.append(GENE_DELIMITER_HEADING);
b.append(encode(
gene.getClass().getName() +
GENE_DELIMITER +
gene.getPersistentRepresentation()));
b.append(GENE_DELIMITER_CLOSING);
}
return b.toString();
}
/**
*
* @param a_representation String
* @throws UnsupportedRepresentationException
*
* @author Klaus Meffert
* @since 3.3
*/
public void setValueFromPersistentRepresentation(final String
a_representation)
throws UnsupportedRepresentationException {
if (a_representation != null) {
try {
List r = split(a_representation);
Iterator iter = r.iterator();
StringTokenizer st;
String clas;
String representation;
String g;
CommandGene gene;
List genes = new Vector();
while (iter.hasNext()) {
g = decode( (String) iter.next());
st = new StringTokenizer(g, GENE_DELIMITER);
if (st.countTokens() != 2)
throw new UnsupportedRepresentationException("In " + g + ", " +
"expecting two tokens, separated by " + GENE_DELIMITER);
clas = st.nextToken();
representation = st.nextToken();
gene = createGene(clas, representation);
genes.add(gene);
}
m_genes = (CommandGene[]) genes.toArray(new CommandGene[0]);
} catch (Exception ex) {
throw new UnsupportedRepresentationException(ex.toString());
}
}
}
/**
* Creates a new instance of gene.
*
* @param a_geneClassName name of the gene class
* @param a_persistentRepresentation persistent representation of the gene to
* create (could be obtained via getPersistentRepresentation)
*
* @return newly created gene
* @throws Exception
*
* @author Klaus Meffert
* @since 3.3
*/
protected CommandGene createGene(String a_geneClassName,
String a_persistentRepresentation)
throws Exception {
Class geneClass = Class.forName(a_geneClassName);
Constructor constr = geneClass.getConstructor(new Class[] {GPConfiguration.class});
CommandGene gene = (CommandGene) constr.newInstance(new Object[] {
getGPConfiguration()});
gene.setValueFromPersistentRepresentation(a_persistentRepresentation);
return gene;
}
/**
* Splits a_string into individual gene representations.
*
* @param a_string the string to split
* @return the elements of the returned array are the persistent
* representation strings of the gene's components
* @throws UnsupportedRepresentationException
*
* @author Klaus Meffert
* @since 3.3
*/
protected static final List split(String a_string)
throws UnsupportedRepresentationException {
List a = Collections.synchronizedList(new ArrayList());
StringTokenizer st = new StringTokenizer
(a_string, GENE_DELIMITER_HEADING + GENE_DELIMITER_CLOSING, true);
while (st.hasMoreTokens()) {
if (!st.nextToken().equals(GENE_DELIMITER_HEADING)) {
throw new UnsupportedRepresentationException(a_string +
" no opening tag");
}
String n = st.nextToken();
if (n.equals(GENE_DELIMITER_CLOSING)) {
// Empty token.
a.add("");
}
else {
a.add(n);
if (!st.nextToken().equals(GENE_DELIMITER_CLOSING)) {
throw new UnsupportedRepresentationException
(a_string + " no closing tag");
}
}
}
return a;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -