001: /*
002: * This file is part of JGAP.
003: *
004: * JGAP offers a dual license model containing the LGPL as well as the MPL.
005: *
006: * For licensing information please see the file license.txt included with JGAP
007: * or have a look at the top of class org.jgap.Chromosome which representatively
008: * includes the JGAP license policy applicable for any file delivered with JGAP.
009: */
010: package org.jgap.gp.function;
011:
012: import org.jgap.*;
013: import org.jgap.gp.*;
014: import org.apache.commons.lang.builder.CompareToBuilder;
015: import org.apache.commons.lang.builder.EqualsBuilder;
016: import org.jgap.gp.impl.*;
017:
018: /**
019: * The increment operation.
020: *
021: * @author Konrad Odell
022: * @author Klaus Meffert
023: * @since 3.0
024: */
025: public class Increment extends MathCommand {
026: /** String containing the CVS revision. Read out via reflection!*/
027: private static final String CVS_REVISION = "$Revision: 1.9 $";
028:
029: private int m_increment;
030:
031: /**
032: * Constructor for using an increment of 1.
033: *
034: * @param a_conf the configuration to use
035: * @param a_type the type of the terminal to increment (e.g. IntegerClass)
036: * @throws InvalidConfigurationException
037: *
038: * @author Klaus Meffert
039: * @since 3.0
040: */
041: public Increment(final GPConfiguration a_conf, Class a_type)
042: throws InvalidConfigurationException {
043: this (a_conf, a_type, 1);
044: }
045:
046: /**
047: * Constructor to freely choose increment.
048: *
049: * @param a_conf the configuration to use
050: * @param a_type the type of the terminal to increment (e.g. IntegerClass)
051: * @param a_increment the increment to use, may also be negative
052: * @throws InvalidConfigurationException
053: *
054: * @author Klaus Meffert
055: * @since 3.0
056: */
057: public Increment(final GPConfiguration a_conf, Class a_type,
058: int a_increment) throws InvalidConfigurationException {
059: this (a_conf, a_type, a_increment, 0, 0);
060: }
061:
062: public Increment(final GPConfiguration a_conf, Class a_type,
063: int a_increment, int a_subReturnType, int a_subChildType)
064: throws InvalidConfigurationException {
065: super (a_conf, 1, a_type, a_subReturnType, a_subChildType);
066: m_increment = a_increment;
067: }
068:
069: public String toString() {
070: if (m_increment == 1) {
071: return "INC(&1)";
072: } else {
073: return "INC(" + m_increment + ", &1)";
074: }
075: }
076:
077: /**
078: * @return textual name of this command
079: *
080: * @author Klaus Meffert
081: * @since 3.2
082: */
083: public String getName() {
084: return "INC";
085: }
086:
087: public int execute_int(ProgramChromosome c, int n, Object[] args) {
088: return c.execute_int(n, 0, args) + m_increment;
089: }
090:
091: public long execute_long(ProgramChromosome c, int n, Object[] args) {
092: return c.execute_long(n, 0, args) + m_increment;
093: }
094:
095: public float execute_float(ProgramChromosome c, int n, Object[] args) {
096: return c.execute_float(n, 0, args) + m_increment;
097: }
098:
099: public double execute_double(ProgramChromosome c, int n,
100: Object[] args) {
101: return c.execute_double(n, 0, args) + m_increment;
102: }
103:
104: public Object execute_object(ProgramChromosome c, int n,
105: Object[] args) {
106: return ((Compatible) c.execute_object(n, 0, args))
107: .execute_increment();
108: }
109:
110: protected interface Compatible {
111: public Object execute_increment();
112: }
113:
114: /**
115: * The compareTo-method.
116: *
117: * @param a_other the other object to compare
118: * @return -1, 0, 1
119: *
120: * @author Klaus Meffert
121: * @since 3.0
122: */
123: public int compareTo(Object a_other) {
124: if (a_other == null) {
125: return 1;
126: } else {
127: Increment other = (Increment) a_other;
128: return new CompareToBuilder().append(m_increment,
129: other.m_increment).toComparison();
130: }
131: }
132:
133: /**
134: * The equals-method.
135: *
136: * @param a_other the other object to compare
137: * @return true if the objects are seen as equal
138: *
139: * @author Klaus Meffert
140: * @since 3.0
141: */
142: public boolean equals(Object a_other) {
143: if (a_other == null) {
144: return false;
145: } else {
146: try {
147: Increment other = (Increment) a_other;
148: return new EqualsBuilder().append(m_increment,
149: other.m_increment).isEquals();
150: } catch (ClassCastException cex) {
151: return false;
152: }
153: }
154: }
155: }
|