1 10 package org.jgap.gp.terminal; 11 12 import org.jgap.gp.*; 13 import org.jgap.*; 14 import org.jgap.util.*; 15 import org.jgap.gp.impl.*; 16 17 23 public class Terminal 24 extends CommandGene implements IMutateable, ICloneable { 25 26 private static final String CVS_REVISION = "$Revision: 1.14 $"; 27 28 private float m_value_float; 29 30 private double m_value_double; 31 32 private int m_value_int; 33 34 private long m_value_long; 35 36 private double m_lowerBounds; 37 38 private double m_upperBounds; 39 40 private boolean m_wholeNumbers; 41 42 public Terminal() 43 throws InvalidConfigurationException { 44 this(GPGenotype.getStaticGPConfiguration(), CommandGene.IntegerClass); 45 } 46 47 public Terminal(final GPConfiguration a_conf, Class a_returnType) 48 throws InvalidConfigurationException { 49 this(a_conf, a_returnType, 0d, 99d, false); 50 } 51 52 64 public Terminal(final GPConfiguration a_conf, Class a_returnType, 65 double a_minValue, double a_maxValue) 66 throws InvalidConfigurationException { 67 this(a_conf, a_returnType, a_minValue, a_maxValue, false); 68 } 69 70 84 public Terminal(final GPConfiguration a_conf, Class a_returnType, 85 double a_minValue, double a_maxValue, boolean a_wholeNumbers) 86 throws InvalidConfigurationException { 87 this(a_conf, a_returnType, a_minValue, a_maxValue, a_wholeNumbers, 0); 88 } 89 90 public Terminal(final GPConfiguration a_conf, Class a_returnType, 91 double a_minValue, double a_maxValue, boolean a_wholeNumbers, 92 int a_subReturnType) 93 throws InvalidConfigurationException { 94 super(a_conf, 0, a_returnType, a_subReturnType, null); 95 m_lowerBounds = a_minValue; 96 m_upperBounds = a_maxValue; 97 m_wholeNumbers = a_wholeNumbers; 98 setRandomValue(); 99 } 100 101 protected void setRandomValue(int a_value) { 102 RandomGenerator randomGen = getGPConfiguration().getRandomGenerator(); 103 m_value_int = (int) Math.round(randomGen.nextDouble() * 104 (m_upperBounds - m_lowerBounds) + 105 m_lowerBounds); 106 } 107 108 protected void setRandomValue(long a_value) { 109 RandomGenerator randomGen = getGPConfiguration().getRandomGenerator(); 110 m_value_long = Math.round(randomGen.nextDouble() * 111 (m_upperBounds - m_lowerBounds) + 112 m_lowerBounds); 113 } 114 115 protected void setRandomValue(double a_value) { 116 RandomGenerator randomGen = getGPConfiguration().getRandomGenerator(); 117 m_value_double = randomGen.nextDouble() * (m_upperBounds - m_lowerBounds) + 118 m_lowerBounds; 119 if (m_wholeNumbers) { 120 m_value_double = Math.round(m_value_double); 121 } 122 } 123 124 protected void setRandomValue(float a_value) { 125 RandomGenerator randomGen = getGPConfiguration().getRandomGenerator(); 126 m_value_float = (float) (randomGen.nextFloat() * 127 (m_upperBounds - m_lowerBounds) + 128 m_lowerBounds); 129 if (m_wholeNumbers) { 130 m_value_float = Math.round(m_value_float); 131 } 132 } 133 134 protected void setRandomValue() { 135 Class retType = getReturnType(); 136 if (retType == CommandGene.FloatClass) { 137 setRandomValue(m_value_float); 138 } 139 else if (retType == CommandGene.IntegerClass) { 140 setRandomValue(m_value_int); 141 } 142 else if (retType == CommandGene.LongClass) { 143 setRandomValue(m_value_long); 144 } 145 else if (retType == CommandGene.DoubleClass) { 146 setRandomValue(m_value_double); 147 } 148 else { 149 throw new RuntimeException ("unknown terminal type: " + retType); 150 } 151 } 152 153 public void setValue(double a_value) { 154 if (m_wholeNumbers) { 155 m_value_double = Math.round(a_value); 156 } 157 else { 158 m_value_double = a_value; 159 } 160 } 161 162 public void setValue(float a_value) { 163 if (m_wholeNumbers) { 164 m_value_float = Math.round(a_value); 165 } 166 else { 167 m_value_float = a_value; 168 } 169 } 170 171 public void setValue(int a_value) { 172 m_value_int = a_value; 173 } 174 175 public void setValue(long a_value) { 176 m_value_long = a_value; 177 } 178 179 public CommandGene applyMutation(int index, double a_percentage) 180 throws InvalidConfigurationException { 181 if (a_percentage > 0.85d) { 185 setRandomValue(); 186 } 187 else { 188 Class retType = getReturnType(); 189 if (retType == CommandGene.FloatClass) { 190 float newValuef; 191 float rangef = ( (float) m_upperBounds - (float) m_lowerBounds) * 192 (float) a_percentage; 193 if (m_value_float >= (m_upperBounds - m_lowerBounds) / 2) { 194 newValuef = m_value_float - 195 getGPConfiguration().getRandomGenerator().nextFloat() * rangef; 196 } 197 else { 198 newValuef = m_value_float + 199 getGPConfiguration().getRandomGenerator().nextFloat() * rangef; 200 } 201 if (Math.abs(newValuef - m_lowerBounds) < DELTA || 204 Math.abs(m_upperBounds - newValuef) < DELTA) { 205 setRandomValue(m_value_float); 206 } 207 else { 208 setValue(newValuef); 209 } 210 } 211 else if (retType == CommandGene.DoubleClass) { 212 double newValueD; 213 double rangeD = (m_upperBounds - m_lowerBounds) * a_percentage; 214 if (m_value_double >= (m_upperBounds - m_lowerBounds) / 2) { 215 newValueD = m_value_double - 216 getGPConfiguration().getRandomGenerator().nextFloat() * rangeD; 217 } 218 else { 219 newValueD = m_value_double + 220 getGPConfiguration().getRandomGenerator().nextFloat() * rangeD; 221 } 222 if (Math.abs(newValueD - m_lowerBounds) < DELTA || 225 Math.abs(m_upperBounds - newValueD) < DELTA) { 226 setRandomValue(m_value_double); 227 } 228 else { 229 setValue(newValueD); 230 } 231 } 232 else if (retType == CommandGene.IntegerClass) { 233 int newValueI; 234 double range = (m_upperBounds - m_lowerBounds) * a_percentage; 235 if (m_value_int >= (m_upperBounds - m_lowerBounds) / 2) { 236 newValueI = m_value_int - 237 (int) Math.round(getGPConfiguration().getRandomGenerator(). 238 nextInt() * range); 239 } 240 else { 241 newValueI = m_value_int + 242 (int) Math.round(getGPConfiguration().getRandomGenerator(). 243 nextFloat() * range); 244 } 245 if (newValueI < m_lowerBounds || newValueI > m_upperBounds) { 248 setRandomValue(m_value_int); 249 } 250 else { 251 setValue(newValueI); 252 } 253 } 254 else if (retType == CommandGene.LongClass) { 255 long newValueL; 256 double range = (m_upperBounds - m_lowerBounds) * a_percentage; 257 if (m_value_long >= (m_upperBounds - m_lowerBounds) / 2) { 258 newValueL = m_value_long - 259 Math.round(getGPConfiguration().getRandomGenerator().nextInt() * 260 range); 261 } 262 else { 263 newValueL = m_value_long + 264 Math.round(getGPConfiguration().getRandomGenerator().nextFloat() * 265 range); 266 } 267 if (newValueL < m_lowerBounds || newValueL > m_upperBounds) { 270 setRandomValue(m_value_long); 271 } 272 else { 273 setValue(newValueL); 274 } 275 } 276 } 277 return this; 278 } 279 280 public String toString() { 281 Class retType = getReturnType(); 282 if (retType == CommandGene.FloatClass) { 283 return "" + m_value_float; 284 } 285 else if (retType == CommandGene.IntegerClass) { 286 return "" + m_value_int; 287 } 288 else if (retType == CommandGene.LongClass) { 289 return "" + m_value_long; 290 } 291 else if (retType == CommandGene.DoubleClass) { 292 return "" + m_value_double; 293 } 294 else { 295 return "unknown terminal type: " + retType; 296 } 297 } 298 299 public int execute_int(ProgramChromosome c, int n, Object [] args) { 300 return m_value_int; 301 } 302 303 public long execute_long(ProgramChromosome c, int n, Object [] args) { 304 return m_value_long; 305 } 306 307 public float execute_float(ProgramChromosome c, int n, Object [] args) { 308 return m_value_float; 309 } 310 311 public double execute_double(ProgramChromosome c, int n, Object [] args) { 312 return m_value_double; 313 } 314 315 326 public Object execute_object(ProgramChromosome c, int n, Object [] args) { 327 StringBuffer value = new StringBuffer ("("); 328 Class retType = getReturnType(); 329 if (retType == CommandGene.FloatClass) { 330 value.append(m_value_float).append("f"); 331 } 332 else if (retType == CommandGene.DoubleClass) { 333 value.append(m_value_double).append("d"); 334 } 335 else if (retType == CommandGene.IntegerClass) { 336 value.append(m_value_int); 337 } 338 else if (retType == CommandGene.LongClass) { 339 value.append(m_value_long).append("l"); 340 } 341 value.append(")"); 342 return value; 343 } 344 345 public Class getChildType(IGPProgram a_ind, int a_chromNum) { 346 return null; 347 } 348 349 357 public Object clone() { 358 try { 359 Terminal result = new Terminal(getGPConfiguration(), getReturnType(), 360 m_lowerBounds, m_upperBounds); 361 result.m_value_double = m_value_double; 362 result.m_value_float = m_value_float; 363 result.m_value_int = m_value_int; 364 result.m_value_long = m_value_long; 365 result.m_wholeNumbers = m_wholeNumbers; 366 return result; 367 } catch (Exception ex) { 368 throw new CloneException(ex); 369 } 370 } 371 } 372 | Popular Tags |