1 10 package org.jgap.gp.function; 11 12 import org.jgap.*; 13 import org.jgap.gp.*; 14 import org.apache.commons.lang.builder.CompareToBuilder; 15 import org.apache.commons.lang.builder.EqualsBuilder; 16 import org.jgap.gp.impl.*; 17 18 25 public class Increment 26 extends MathCommand { 27 28 private static final String CVS_REVISION = "$Revision: 1.8 $"; 29 30 private int m_increment; 31 32 42 public Increment(final GPConfiguration a_conf, Class a_type) 43 throws InvalidConfigurationException { 44 this(a_conf, a_type, 1); 45 } 46 47 58 public Increment(final GPConfiguration a_conf, Class a_type, int a_increment) 59 throws InvalidConfigurationException { 60 this(a_conf, a_type, a_increment, 0, 0); 61 } 62 63 public Increment(final GPConfiguration a_conf, Class a_type, int a_increment, 64 int a_subReturnType, int a_subChildType) 65 throws InvalidConfigurationException { 66 super(a_conf, 1, a_type, a_subReturnType, a_subChildType); 67 m_increment = a_increment; 68 } 69 70 public String toString() { 71 if (m_increment == 1) { 72 return "INC(&1)"; 73 } 74 else { 75 return "INC(" + m_increment + ", &1)"; 76 } 77 } 78 79 85 public String getName() { 86 return "INC"; 87 } 88 89 public int execute_int(ProgramChromosome c, int n, Object [] args) { 90 return c.execute_int(n, 0, args) + m_increment; 91 } 92 93 public long execute_long(ProgramChromosome c, int n, Object [] args) { 94 return c.execute_long(n, 0, args) + m_increment; 95 } 96 97 public float execute_float(ProgramChromosome c, int n, Object [] args) { 98 return c.execute_float(n, 0, args) + m_increment; 99 } 100 101 public double execute_double(ProgramChromosome c, int n, Object [] args) { 102 return c.execute_double(n, 0, args) + m_increment; 103 } 104 105 public Object execute_object(ProgramChromosome c, int n, Object [] args) { 106 return ( (Compatible) c.execute_object(n, 0, args)).execute_increment(); 107 } 108 109 protected interface Compatible { 110 public Object execute_increment(); 111 } 112 121 public int compareTo(Object a_other) { 122 if (a_other == null) { 123 return 1; 124 } 125 else { 126 Increment other = (Increment) a_other; 127 return new CompareToBuilder() 128 .append(m_increment, other.m_increment) 129 .toComparison(); 130 } 131 } 132 133 142 public boolean equals(Object a_other) { 143 if (a_other == null) { 144 return false; 145 } 146 else { 147 try { 148 Increment other = (Increment) a_other; 149 return new EqualsBuilder() 150 .append(m_increment, other.m_increment) 151 .isEquals(); 152 } catch (ClassCastException cex) { 153 return false; 154 } 155 } 156 } 157 } 158 | Popular Tags |