📄 commandgene.java
字号:
/*
* This file is part of JGAP.
*
* JGAP offers a dual license model containing the LGPL as well as the MPL.
*
* For licensing information please see the file license.txt included with JGAP
* or have a look at the top of class org.jgap.Chromosome which representatively
* includes the JGAP license policy applicable for any file delivered with JGAP.
*/
package org.jgap.gp;
import java.io.*;
import org.jgap.*;
import org.jgap.gp.impl.*;
/**
* Abstract base class for all GP commands. A CommandGene can hold additional
* CommandGene's, it acts sort of like a Composite (also see CompositeGene for
* a similar characteristics, although for a GA).
*
* @author Klaus Meffert
* @since 3.0
*/
public abstract class CommandGene
implements Comparable, Serializable {
/** String containing the CVS revision. Read out via reflection!*/
private final static String CVS_REVISION = "$Revision: 1.23 $";
/**
* Delta, useful for comparing doubles and floats.
*/
public static final double DELTA = 0.0000001;
public final static Class BooleanClass = Boolean.class;
public final static Class IntegerClass = Integer.class;
public final static Class LongClass = Long.class;
public final static Class FloatClass = Float.class;
public final static Class DoubleClass = Double.class;
public final static Class VoidClass = Void.class;
private GPConfiguration m_configuration;
/**
* Should isValid() be called? True = no!
*/
private boolean m_noValidation;
/**
* The return type of this node.
*/
private Class m_returnType;
private int m_arity;
private boolean m_integerType;
private boolean m_floatType;
/** Energy of a gene, see RFE 1102206*/
private double m_energy;
/**
* Application-specific data that is attached to the Gene. This data may
* assist the application in labelling this Gene.
* JGAP ignores the data, aside from allowing it to be set and
* retrieved and considering it in clone() and compareTo().
*
* @since 3.0
*/
private Object m_applicationData;
/**
* Method compareTo(): Should we also consider the application data when
* comparing? Default is "false" as "true" means a Gene's losing its
* identity when application data is set differently!
*
* @since 3.0
*/
private boolean m_compareAppData;
private int m_subReturnType;
private int[] m_subChildTypes;
public int nodeIndex;
/**
* Initializations, called from each Constructor.
*/
protected void init() {
}
public CommandGene(final GPConfiguration a_conf, final int a_arity,
final Class a_returnType)
throws InvalidConfigurationException {
if (a_conf == null) {
throw new InvalidConfigurationException("Configuration must not be null!");
}
m_configuration = a_conf;
init();
m_arity = a_arity;
m_returnType = a_returnType;
if (a_returnType == Integer.class
|| a_returnType == Long.class) {
m_integerType = true;
}
else if (a_returnType == Double.class
|| a_returnType == Float.class) {
m_floatType = true;
}
}
/**
* Allows specifying a sub return type and sub child types.
*
* @param a_conf GPConfiguration
* @param a_arity int
* @param a_returnType Class
* @param a_subReturnType int
* @param a_childSubTypes int[]
* @throws InvalidConfigurationException
*
* @author Klaus Meffert
* @since 3.2
*/
public CommandGene(final GPConfiguration a_conf, final int a_arity,
final Class a_returnType, final int a_subReturnType,
final int[] a_childSubTypes)
throws InvalidConfigurationException {
this(a_conf, a_arity, a_returnType);
if (a_childSubTypes != null) {
boolean specialCase = false;
// Special case from convenient construction.
// ------------------------------------------
if (a_childSubTypes.length == 1) {
if (a_childSubTypes[0] == 0) {
m_subChildTypes = null;
specialCase = true;
}
}
if (!specialCase) {
if (a_childSubTypes.length != a_arity) {
throw new IllegalArgumentException(
"Length of child sub types must equal"
+ " the given arity (or set the former to null)");
}
}
else {
m_subChildTypes = a_childSubTypes;
}
}
else {
m_subChildTypes = a_childSubTypes;
}
m_subReturnType = a_subReturnType;
}
/**
* Allows specifying a sub return type.
*
* @param a_conf GPConfiguration
* @param a_arity int
* @param a_returnType Class
* @param a_subReturnType int
* @throws InvalidConfigurationException
*
* @author Klaus Meffert
* @since 3.2
*/
public CommandGene(final GPConfiguration a_conf, final int a_arity,
final Class a_returnType, final int a_subReturnType)
throws InvalidConfigurationException {
this(a_conf, a_arity, a_returnType, a_subReturnType, null);
}
/**
* Command with one child: Allows specifying a sub return type and a sub child
* type. Convenience version of the called constructor.
*
* @param a_conf GPConfiguration
* @param a_arity int
* @param a_returnType Class
* @param a_subReturnType int
* @param a_childSubType int
* @throws InvalidConfigurationException
*
* @author Klaus Meffert
* @since 3.2
*/
public CommandGene(final GPConfiguration a_conf, final int a_arity,
final Class a_returnType, final int a_subReturnType,
final int a_childSubType)
throws InvalidConfigurationException {
this(a_conf, a_arity, a_returnType, a_subReturnType,
new int[] {a_childSubType});
}
public void setAllele(Object a_newValue) {
throw new java.lang.UnsupportedOperationException(
"Method setAllele() not used.");
}
public Object getAllele() {
return null;
}
public String getPersistentRepresentation()
throws UnsupportedOperationException {
/**@todo Implement this org.jgap.Gene method*/
throw new java.lang.UnsupportedOperationException(
"Method getPersistentRepresentation() not yet implemented.");
}
public void setValueFromPersistentRepresentation(String a_representation)
throws UnsupportedOperationException, UnsupportedRepresentationException {
/**@todo Implement this org.jgap.Gene method*/
throw new java.lang.UnsupportedOperationException(
"Method setValueFromPersistentRepresentation() not yet implemented.");
}
public void setToRandomValue(RandomGenerator a_numberGenerator) {
// Do nothing here by default.
// ---------------------------
}
public void cleanup() {
// Do nothing here by default.
// ---------------------------
}
public int size() {
return m_arity;
}
/**
* Arity of the command. Override if necessary.
*
* @param a_indvividual the invididual the command's arity may depend on (in
* most cases the arity will not depend on the individual)
* @return arity of the command
*
* @author Klaus Meffert
* @since 3.0
*/
public int getArity(IGPProgram a_indvividual) {
return m_arity;
}
public int compareTo(Object a_other) {
CommandGene o2 = (CommandGene) a_other;
if (size() != o2.size()) {
if (size() > o2.size()) {
return 1;
}
else {
return -1;
}
}
if (getClass() != o2.getClass()) {
/**@todo do it more precisely*/
return -1;
}
else {
return 0;
}
}
public boolean equals(Object a_other) {
if (a_other == null) {
return false;
}
else {
try {
/**@todo return type, input type*/
CommandGene other = (CommandGene) a_other;
if (getClass() == a_other.getClass()) {
if (getInternalValue() == null) {
if (other.getInternalValue() == null) {
return true;
}
else {
return false;
}
}
else {
if (other.getInternalValue() == null) {
return false;
}
else {
return true;
}
}
}
else {
return false;
}
} catch (ClassCastException cex) {
return false;
}
}
}
/**
* @return the string representation of the command. Especially usefull to
* output a resulting formula in human-readable form.
*/
public abstract String toString();
/**
* Executes this node without knowing its return type.
*
* @param c the current Chromosome which is executing
* @param n the index of the Function in the Chromosome's Function array which
* is executing
* @param args the arguments to the current Chromosome which is executing
* @return the object which wraps the return value of this node, or null
* if the return type is null or unknown
* @throws UnsupportedOperationException if the type of this node is not
* boolean
*
* @author Klaus Meffert
* @since 3.0
*/
public Object execute(ProgramChromosome c, int n, Object[] args) {
if (m_returnType == BooleanClass) {
return new Boolean(execute_boolean(c, n, args));
}
if (m_returnType == IntegerClass) {
return new Integer(execute_int(c, n, args));
}
if (m_returnType == LongClass) {
return new Long(execute_long(c, n, args));
}
if (m_returnType == FloatClass) {
return new Float(execute_float(c, n, args));
}
if (m_returnType == DoubleClass) {
return new Double(execute_double(c, n, args));
}
if (m_returnType == VoidClass) {
execute_void(c, n, args);
}
else {
return execute_object(c, n, args);
}
return null;
}
/**
* @return the return type of this node
*
* @author Klaus Meffert
* @since 3.0
*/
public Class getReturnType() {
return m_returnType;
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -