📄 commandgene.java
字号:
/**
* Executes this node as an integer. Override to implement.
*
* @param c ignored here
* @param n ignored here
* @param args ignored here
* @return nothing but exception
* @throws UnsupportedOperationException
*
* @author Klaus Meffert
* @since 3.0
*/
public int execute_int(ProgramChromosome c, int n, Object[] args) {
throw new UnsupportedOperationException(getName() +
" cannot return int");
}
/**
* Executes this node as a long. Override to implement.
*
* @param c ignored here
* @param n ignored here
* @param args ignored here
* @return nothing but exception
* @throws UnsupportedOperationException
*
* @author Klaus Meffert
* @since 3.0
*/
public long execute_long(ProgramChromosome c, int n, Object[] args) {
throw new UnsupportedOperationException(getName() +
" cannot return long");
}
/**
* Executes this node as a float. Override to implement.
*
* @param c ignored here
* @param n ignored here
* @param args ignored here
* @return nothing but exception
* @throws UnsupportedOperationException
*
* @author Klaus Meffert
* @since 3.0
*/
public float execute_float(ProgramChromosome c, int n, Object[] args) {
throw new UnsupportedOperationException(getName() +
" cannot return float");
}
/**
* Executes this node as a double. Override to implement.
*
* @param c ignored here
* @param n ignored here
* @param args ignored here
* @return nothing but exception
* @throws UnsupportedOperationException
*
* @author Klaus Meffert
* @since 3.0
*/
public double execute_double(ProgramChromosome c, int n, Object[] args) {
throw new UnsupportedOperationException(getName() +
" cannot return double");
}
/**
* Executes this node as an object. Override to implement.
*
* @param c ignored here
* @param n ignored here
* @param args ignored here
* @return nothing but exception
* @throws UnsupportedOperationException
*
* @author Klaus Meffert
* @since 3.0
*/
public Object execute_object(ProgramChromosome c, int n, Object[] args) {
throw new UnsupportedOperationException(getName() +
" cannot return Object");
}
public String getName() {
return toString() + " (class " + getClass().getName() + ")";
}
/**
* Gets the type of node allowed from the given child number. Should be
* overridden in subclasses.
*
* @param a_ind the individual the child belongs to
* @param a_chromNum the chromosome number
* @return the type of node allowed for that child, or null of no child
* exists
*
* @author Klaus Meffert
* @since 3.0
*/
public Class getChildType(IGPProgram a_ind, int a_chromNum) {
if (m_arity == 0) {
return null;
}
else {
return getReturnType();
}
}
protected Object getInternalValue() {
return null;
}
/**
* Retrieves the hash code value for a CommandGene.
* Override if another hashCode() implementation is necessary or more
* appropriate than this default implementation.
*
* @return this Gene's hash code
*
* @author Klaus Meffert
* @since 3.0
*/
public int hashCode() {
// If our internal value is null, then return zero. Otherwise,
// just return the hash code of the allele Object.
// -----------------------------------------------------------
if (getInternalValue() == null) {
return getClass().getName().hashCode();
}
else {
return getInternalValue().hashCode();
}
}
public boolean isIntegerType() {
return m_integerType;
}
public boolean isFloatType() {
return m_floatType;
}
/**
* @return true: command affects global state (i.e. stack or memory)
*
* @author Klaus Meffert
* @since 3.0
*/
public boolean isAffectGlobalState() {
return false;
}
/**
* Subclasses capable of validating programs should overwrite this method.
* See class Push as a sample.
*
* @param a_program the ProgramChromosome to validate
* @return true: a_program is (superficially) valid with the current Command
*
* @author Klaus Meffert
* @since 3.0
*/
public boolean isValid(ProgramChromosome a_program) {
return true;
}
public boolean isValid(ProgramChromosome a_program, int a_index) {
return true;
}
protected void check(ProgramChromosome a_program) {
if (m_noValidation) {
return;
}
if (!isValid(a_program)) {
throw new IllegalStateException("State for GP-command not valid");
}
}
protected void check(ProgramChromosome a_program, int a_index) {
if (m_noValidation) {
return;
}
if (!isValid(a_program, a_index)) {
throw new IllegalStateException("State for GP-command not valid");
}
}
public void setNoValidation(boolean a_noValidation) {
m_noValidation = a_noValidation;
}
/**
* @return the configuration set
*
* @author Klaus Meffert
* @since 3.0
*/
public GPConfiguration getGPConfiguration() {
return m_configuration;
}
/**
* This sets the application-specific data that is attached to this Gene.
* Attaching application-specific data may be useful for some applications
* when it comes time to distinguish a Gene from another. JGAP ignores this
* data functionally.
*
* @param a_newData the new application-specific data to attach to this
* Gene
*
* @author Klaus Meffert
* @since 3.0
*/
public void setApplicationData(final Object a_newData) {
m_applicationData = a_newData;
}
/**
* Retrieves the application-specific data that is attached to this Gene.
* Attaching application-specific data may be useful for
* some applications when it comes time to distinguish a Gene from another.
* JGAP ignores this data functionally.
*
* @return the application-specific data previously attached to this Gene,
* or null if there is no data attached
*
* @author Klaus Meffert
* @since 3.0
*/
public Object getApplicationData() {
return m_applicationData;
}
/**
* Should we also consider the application data when comparing? Default is
* "false" as "true" means a Gene is losing its identity when
* application data is set differently!
*
* @param a_doCompare true: consider application data in method compareTo
* and equals
*
* @author Klaus Meffert
* @since 2.4
*/
public void setCompareApplicationData(final 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;
}
/**
* @return energy of the gene
*
* @author Klaus Meffert
* @since 2.3
*/
public double getEnergy() {
return m_energy;
}
/**
* Sets the energy of the gene.
*
* @param a_energy the energy to set
*
* @author Klaus Meffert
* @since 2.3
*/
public void setEnergy(final double a_energy) {
m_energy = a_energy;
}
/**
* @return sub return type
*
* @author Klaus Meffert
* @since 3.2
*/
public int getSubReturnType() {
return m_subReturnType;
}
/**
*
* @param a_childNum int
* @return int
*
* @author Klaus Meffert
* @since 3.2
*/
public int getSubChildType(int a_childNum) {
if (m_subChildTypes == null) {
return 0;
}
else {
return m_subChildTypes[a_childNum];
}
}
/**
* @return int[]
*
* @author Klaus Meffert
* @since 3.2
*/
protected int[] getSubChildTypes() {
return m_subChildTypes;
}
/**
* Ensures that the calling command is unique within the program.
* Call it on first place from the execute method.
*
* @param a_program the program to validate
*
* @author Klaus Meffert
* @since 3.3
*/
public void ensureUniqueness(ProgramChromosome a_program) {
if (a_program.getCommandOfClass(1, getClass()) >= 0) {
throw new IllegalStateException("Command "
+ getClass()
+ " must not occur more than once!");
}
}
/**
* Ensures that the calling command is unique within the program. Throws an
* exception if uniqueness is violated.
* Call it on first place from the execute method.
*
* @param a_program the program to validate
* @param a_maxCount maximum number of allowed occurences
*
* @author Klaus Meffert
* @since 3.4
*/
public void ensureUniqueness(ProgramChromosome a_program, int a_maxCount) {
if (a_program.getCommandOfClass(1, getClass()) > a_maxCount) {
throw new IllegalStateException("Command "
+ getClass()
+ " must not occur more than "
+ a_maxCount
+ " times!");
}
}
/**
* Ensures that the calling command is unique within the program. Returns
* false if uniqueness is violated.
* Call it on first place from the execute method.
*
* @param a_program the program to validate
* @param a_maxCount maximum number of allowed occurences
*
* @return false: uniqueness constraint violated
*
* @author Klaus Meffert
* @since 3.4
*/
public boolean ensureUniqueness2(ProgramChromosome a_program, int a_maxCount) {
if (a_program.getCommandOfClass(1, getClass()) > a_maxCount) {
return false;
}
return true;
}
/**
* The type of the command this gene represents. Overwrite in sub classes.
* This is optional and allows fine-tuning of GP program creating. For
* example, you could have a function as a part of a desired GP program that
* only does some analysis stuff and not any execution. E.g. take a game like
* Noughts and Crosses (Tic Tac Toe). There, a first part could be analysing
* the current state of the board and a second part could cope with exploiting
* the analysis results to execute a command (like put stone onto board at
* specific position).
*
* @return type of the command this gene represents
*
* @author Klaus Meffert
* @since 3.4.3
*/
protected CommandGene.COMMAND_TYPE getCommandType() {
return COMMAND_TYPE.COMMAND_TYPE_UNDEFINED;
}
/**
* @return the persistent representation of the chromosome, including all
* genes
*
* @author Klaus Meffert
* @since 3.3
*/
public String getPersistentRepresentation() {
// Return Type
String s;
if (m_returnType == null) {
s = "null";
}
else {
s = m_returnType.getClass().getName();
}
String result = PERSISTENT_FIELD_DELIMITER + m_arity
+ PERSISTENT_FIELD_DELIMITER + s
+ PERSISTENT_FIELD_DELIMITER + m_subReturnType
+ PERSISTENT_FIELD_DELIMITER + m_subChildTypes
+ EXTENDED_INFO_DELIMITER + getPersistentRepresentationExt()
+ EXTENDED_INFO_DELIMITER;
return result;
}
/**
* Override in your sub classes of CommandGene if you have to add additional
* information to be persisted.
*
* @return additional infos
*
* @author Klaus Meffert
* @since 3.3
*/
protected String getPersistentRepresentationExt() {
return null;
}
/**
*
* @param a_representation String
* @throws UnsupportedRepresentationException
*
* @author Klaus Meffert
* @since 3.3
*/
public void setValueFromPersistentRepresentation(final String
a_representation)
throws UnsupportedRepresentationException {
/**@todo finish*/
/*
if (a_representation != null) {
StringTokenizer tokenizer =
new StringTokenizer(a_representation,
PERSISTENT_FIELD_DELIMITER);
// Make sure the representation contains the correct number of
// fields. If not, throw an exception.
// -----------------------------------------------------------
if (tokenizer.countTokens() < 4) {
throw new UnsupportedRepresentationException(
"The format of the given persistent representation "
+ " is not recognized: it does not contain at least four tokens: "
+ a_representation);
}
String arityRepresentation = tokenizer.nextToken();
String subRetBoundRepresentation = tokenizer.nextToken();
String subChildBoundRepresentation = tokenizer.nextToken();
// First parse and set the representation of the value.
// ----------------------------------------------------
if (a_representation.equals("null")) {
setAllele(null);
}
else {
try {
setAllele(new Integer(Integer.parseInt(a_representation)));
} catch (NumberFormatException e) {
throw new UnsupportedRepresentationException(
"The format of the given persistent representation " +
"is not recognized: field 1 does not appear to be " +
"an integer value.");
}
}
// Now parse and set the lower bound.
// ----------------------------------
try {
m_lowerBounds =
Integer.parseInt(lowerBoundRepresentation);
} catch (NumberFormatException e) {
throw new UnsupportedRepresentationException(
"The format of the given persistent representation " +
"is not recognized: field 2 does not appear to be " +
"an integer value.");
}
// Now parse and set the upper bound.
// ----------------------------------
try {
m_upperBounds =
Integer.parseInt(upperBoundRepresentation);
} catch (NumberFormatException e) {
throw new UnsupportedRepresentationException(
"The format of the given persistent representation " +
"is not recognized: field 3 does not appear to be " +
"an integer value.");
}
}
*/
}
/**
* Override in your sub classes of CommandGene if you have to add additional
* information to be persisted.
*
* @param a_index index of the parameter in the range 0..n-1 (n=number of
* parameters)
* @param a_value string value of the parameter
*
* @author Klaus Meffert
* @since 3.3
*/
protected void setValueFromString(int a_index, String a_value) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -