1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 15 24 public class IntegerGene 25 extends NumberGene implements IPersistentRepresentation { 26 27 private static final String CVS_REVISION = "$Revision: 1.42 $"; 28 29 32 protected final static long INTEGER_RANGE = (long) Integer.MAX_VALUE 33 - (long) Integer.MIN_VALUE; 34 35 39 private int m_upperBounds; 40 41 45 private int m_lowerBounds; 46 47 59 public IntegerGene() 60 throws InvalidConfigurationException { 61 this(Genotype.getStaticConfiguration()); 62 } 63 64 75 public IntegerGene(final Configuration a_config) 76 throws InvalidConfigurationException { 77 this(a_config, Integer.MIN_VALUE, Integer.MAX_VALUE); 78 } 79 80 94 public IntegerGene(final Configuration a_config, final int a_lowerBounds, 95 final int a_upperBounds) 96 throws InvalidConfigurationException { 97 super(a_config); 98 m_lowerBounds = a_lowerBounds; 99 m_upperBounds = a_upperBounds; 100 } 101 102 112 protected Gene newGeneInternal() { 113 try { 114 IntegerGene result = new IntegerGene(getConfiguration(), m_lowerBounds, 115 m_upperBounds); 116 return result; 117 } catch (InvalidConfigurationException iex) { 118 throw new IllegalStateException (iex.getMessage()); 119 } 120 } 121 122 136 public String getPersistentRepresentation() { 137 String s; 141 if (getInternalValue() == null) { 142 s = "null"; 143 } 144 else { 145 s = getInternalValue().toString(); 146 } 147 return s + PERSISTENT_FIELD_DELIMITER + m_lowerBounds 148 + PERSISTENT_FIELD_DELIMITER + m_upperBounds; 149 } 150 151 170 public void setValueFromPersistentRepresentation(final String 171 a_representation) 172 throws UnsupportedRepresentationException { 173 if (a_representation != null) { 174 StringTokenizer tokenizer = 175 new StringTokenizer(a_representation, 176 PERSISTENT_FIELD_DELIMITER); 177 if (tokenizer.countTokens() != 3) { 181 throw new UnsupportedRepresentationException( 182 "The format of the given persistent representation " + 183 "is not recognized: it does not contain three tokens."); 184 } 185 String valueRepresentation = tokenizer.nextToken(); 186 String lowerBoundRepresentation = tokenizer.nextToken(); 187 String upperBoundRepresentation = tokenizer.nextToken(); 188 if (valueRepresentation.equals("null")) { 191 setAllele(null); 192 } 193 else { 194 try { 195 setAllele(new Integer (Integer.parseInt(valueRepresentation))); 196 } catch (NumberFormatException e) { 197 throw new UnsupportedRepresentationException( 198 "The format of the given persistent representation " + 199 "is not recognized: field 1 does not appear to be " + 200 "an integer value."); 201 } 202 } 203 try { 206 m_lowerBounds = 207 Integer.parseInt(lowerBoundRepresentation); 208 } catch (NumberFormatException e) { 209 throw new UnsupportedRepresentationException( 210 "The format of the given persistent representation " + 211 "is not recognized: field 2 does not appear to be " + 212 "an integer value."); 213 } 214 try { 217 m_upperBounds = 218 Integer.parseInt(upperBoundRepresentation); 219 } catch (NumberFormatException e) { 220 throw new UnsupportedRepresentationException( 221 "The format of the given persistent representation " + 222 "is not recognized: field 3 does not appear to be " + 223 "an integer value."); 224 } 225 } 226 } 227 228 237 public int intValue() { 238 return ( (Integer ) getAllele()).intValue(); 239 } 240 241 254 public void setToRandomValue(final RandomGenerator a_numberGenerator) { 255 double randomValue = (m_upperBounds - m_lowerBounds) * 256 a_numberGenerator.nextDouble() + 257 m_lowerBounds; 258 setAllele(new Integer ( (int) Math.round(randomValue))); 259 } 260 261 273 protected int compareToNative(final Object a_o1, final Object a_o2) { 274 return ( (Integer ) a_o1).compareTo( (Integer ) a_o2); 275 } 276 277 290 protected void mapValueToWithinBounds() { 291 if (getAllele() != null) { 292 Integer i_value = ( (Integer ) getAllele()); 293 if (i_value.intValue() > m_upperBounds || 300 i_value.intValue() < m_lowerBounds) { 301 RandomGenerator rn; 302 if (getConfiguration() != null) { 303 rn = getConfiguration().getRandomGenerator(); 304 } 305 else { 306 rn = new StockRandomGenerator(); 307 } 308 setAllele(new Integer (rn.nextInt(m_upperBounds - m_lowerBounds) + 309 m_lowerBounds)); 310 } 311 } 312 } 313 314 323 public void applyMutation(final int a_index, final double a_percentage) { 324 double range = (m_upperBounds - m_lowerBounds) * a_percentage; 325 if (getAllele() == null) { 326 setAllele(new Integer ( (int) range + m_lowerBounds)); 327 } 328 else { 329 int newValue = (int) Math.round(intValue() + range); 330 setAllele(new Integer (newValue)); 331 } 332 } 333 334 342 public int hashCode() { 343 if (getInternalValue() == null) { 344 return -1; 345 } 346 else { 347 return super.hashCode(); 348 } 349 } 350 351 358 public String toString() { 359 String s = "IntegerGene(" + m_lowerBounds + "," + m_upperBounds + ")" 360 + "="; 361 if (getInternalValue() == null) { 362 s += "null"; 363 } 364 else { 365 s += getInternalValue().toString(); 366 } 367 return s; 368 } 369 370 376 public int getLowerBounds() { 377 return m_lowerBounds; 378 } 379 380 386 public int getUpperBounds() { 387 return m_upperBounds; 388 } 389 } 390 | Popular Tags |