1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 15 24 public class DoubleGene 25 extends NumberGene implements IPersistentRepresentation { 26 27 private final static String CVS_REVISION = "$Revision: 1.37 $"; 28 29 33 private double m_upperBound; 34 35 39 private double m_lowerBound; 40 41 53 public DoubleGene() 54 throws InvalidConfigurationException { 55 this(Genotype.getStaticConfiguration()); 56 } 57 58 68 public DoubleGene(final Configuration a_config) 69 throws InvalidConfigurationException { 70 this(a_config, - (Double.MAX_VALUE / 2), 71 Double.MAX_VALUE / 2); 72 } 73 74 88 public DoubleGene(final Configuration a_config, final double a_lowerBound, 89 final double a_upperBound) 90 throws InvalidConfigurationException { 91 super(a_config); 92 m_lowerBound = a_lowerBound; 93 m_upperBound = a_upperBound; 94 } 95 96 106 protected Gene newGeneInternal() { 107 try { 108 DoubleGene result = new DoubleGene(getConfiguration(), m_lowerBound, 109 m_upperBound); 110 return result; 111 } 112 catch (InvalidConfigurationException iex) { 113 throw new IllegalStateException (iex.getMessage()); 114 } 115 } 116 117 131 public String getPersistentRepresentation() { 132 String s; 136 if (getInternalValue() == null) { 137 s = "null"; 138 } 139 else { 140 s = getInternalValue().toString(); 141 } 142 return s + PERSISTENT_FIELD_DELIMITER + m_lowerBound 143 + PERSISTENT_FIELD_DELIMITER + m_upperBound; 144 } 145 146 165 public void setValueFromPersistentRepresentation(String a_representation) 166 throws UnsupportedRepresentationException { 167 if (a_representation != null) { 168 StringTokenizer tokenizer = 169 new StringTokenizer(a_representation, 170 PERSISTENT_FIELD_DELIMITER); 171 if (tokenizer.countTokens() != 3) { 175 throw new UnsupportedRepresentationException( 176 "The format of the given persistent representation " + 177 "is not recognized: it does not contain three tokens."); 178 } 179 String valueRepresentation = tokenizer.nextToken(); 180 String lowerBoundRepresentation = tokenizer.nextToken(); 181 String upperBoundRepresentation = tokenizer.nextToken(); 182 if (valueRepresentation.equals("null")) { 185 setAllele(null); 186 } 187 else { 188 try { 189 setAllele(new Double (Double.parseDouble(valueRepresentation))); 190 } 191 catch (NumberFormatException e) { 192 throw new UnsupportedRepresentationException( 193 "The format of the given persistent representation " + 194 "is not recognized: field 1 does not appear to be " + 195 "a double value."); 196 } 197 } 198 try { 201 m_lowerBound = 202 Double.parseDouble(lowerBoundRepresentation); 203 } 204 catch (NumberFormatException e) { 205 throw new UnsupportedRepresentationException( 206 "The format of the given persistent representation " + 207 "is not recognized: field 2 does not appear to be " + 208 "a double value."); 209 } 210 try { 213 m_upperBound = 214 Double.parseDouble(upperBoundRepresentation); 215 } 216 catch (NumberFormatException e) { 217 throw new UnsupportedRepresentationException( 218 "The format of the given persistent representation " + 219 "is not recognized: field 3 does not appear to be " + 220 "a double value."); 221 } 222 } 223 } 224 225 232 public double doubleValue() { 233 return ( (Double ) getAllele()).doubleValue(); 234 } 235 236 248 public void setToRandomValue(RandomGenerator a_numberGenerator) { 249 setAllele(new Double ( (m_upperBound - m_lowerBound) * 252 a_numberGenerator.nextDouble() + m_lowerBound)); 253 } 254 255 266 protected int compareToNative(Object o1, Object o2) { 267 return ( (Double ) o1).compareTo( (Double ) o2); 268 } 269 270 283 protected void mapValueToWithinBounds() { 284 if (getAllele() != null) { 285 Double d_value = ( (Double ) getAllele()); 286 if (d_value.doubleValue() > m_upperBound || 293 d_value.doubleValue() < m_lowerBound) { 294 RandomGenerator rn; 295 if (getConfiguration() != null) { 296 rn = getConfiguration().getRandomGenerator(); 297 } 298 else { 299 rn = new StockRandomGenerator(); 300 } 301 setAllele(new Double (rn.nextDouble() 302 * (m_upperBound - m_lowerBound) + m_lowerBound)); 303 } 304 } 305 } 306 307 316 public void applyMutation(int index, double a_percentage) { 317 double range = (m_upperBound - m_lowerBound) * a_percentage; 318 double newValue = doubleValue() + range; 319 setAllele(new Double (newValue)); 320 } 321 322 330 public int hashCode() { 331 if (getInternalValue() == null) { 332 return -3; 333 } 334 else { 335 return super.hashCode(); 336 } 337 } 338 339 346 public String toString() { 347 String s = "DoubleGene(" + m_lowerBound + "," + m_upperBound + ")" 348 + "="; 349 if (getInternalValue() == null) { 350 s += "null"; 351 } 352 else { 353 s += getInternalValue().toString(); 354 } 355 return s; 356 } 357 358 363 public double getLowerBound() { 364 return m_lowerBound; 365 } 366 367 372 public double getUpperBound() { 373 return m_upperBound; 374 } 375 } 376 | Popular Tags |