1 10 package org.jgap; 11 12 18 public abstract class BaseGene 19 implements Gene { 20 21 private final static String CVS_REVISION = "$Revision: 1.22 $"; 22 23 26 public static final double DELTA = 0.0000001; 27 28 29 private double m_energy; 30 31 39 private Object m_applicationData; 40 41 48 private boolean m_compareAppData; 49 50 private 51 Configuration m_configuration; 52 53 56 public final static String S_APPLICATION_DATA = "Application data"; 57 58 66 public BaseGene(Configuration a_configuration) 67 throws InvalidConfigurationException { 68 if (a_configuration == null) { 69 throw new InvalidConfigurationException("Configuration must not be null!"); 70 } 71 m_configuration = a_configuration; 72 } 73 74 80 public Object getAllele() { 81 return getInternalValue(); 82 } 83 84 95 public int hashCode() { 96 if (getInternalValue() == null) { 100 return -79; 101 } 102 else { 103 return getInternalValue().hashCode(); 104 } 105 } 106 107 115 public void cleanup() { 116 } 119 120 129 public String toString() { 130 String representation; 131 if (getInternalValue() == null) { 132 representation = "null"; 133 } 134 else { 135 representation = getInternalValue().toString(); 136 } 137 String appData; 138 if (getApplicationData() != null) { 139 appData = getApplicationData().toString(); 140 } 141 else { 142 appData = "null"; 143 } 144 representation += ", " + S_APPLICATION_DATA + ":" + appData; 145 return representation; 146 } 147 148 155 public int size() { 156 return 1; 157 } 158 159 170 public boolean equals(final Object a_other) { 171 try { 172 int result = compareTo(a_other); 173 if (result == 0) { 174 if (isCompareApplicationData()) { 175 Gene otherGene = (Gene) a_other; 176 int resultAppData = compareApplicationData(getApplicationData(), 177 otherGene.getApplicationData()); 178 return resultAppData == 0; 179 } 180 else { 181 return true; 182 } 183 } 184 else { 185 return false; 186 } 187 } catch (ClassCastException e) { 188 return false; 192 } 193 } 194 195 204 protected abstract Object getInternalValue(); 205 206 212 public double getEnergy() { 213 return m_energy; 214 } 215 216 223 public void setEnergy(final double a_energy) { 224 m_energy = a_energy; 225 } 226 227 239 public void setApplicationData(final Object a_newData) { 240 m_applicationData = a_newData; 241 } 242 243 255 public Object getApplicationData() { 256 return m_applicationData; 257 } 258 259 269 public void setCompareApplicationData(final boolean a_doCompare) { 270 m_compareAppData = a_doCompare; 271 } 272 273 279 public boolean isCompareApplicationData() { 280 return m_compareAppData; 281 } 282 283 protected int compareApplicationData(final Object a_appdata1, 284 final Object a_appdata2) { 285 if (a_appdata1 == null) { 288 if (a_appdata2 != null) { 289 return -1; 290 } 291 else { 292 return 0; 293 } 294 } 295 else if (a_appdata2 == null) { 296 return 1; 297 } 298 else { 299 ICompareToHandler handler = getConfiguration().getJGAPFactory(). 303 getCompareToHandlerFor(a_appdata1, a_appdata2.getClass()); 304 if (handler != null) { 305 try { 306 return ( (Integer ) handler.perform(a_appdata1, null, a_appdata2)). 307 intValue(); 308 } catch (Exception ex) { 309 throw new Error (ex); 310 } 311 } 312 else { 313 return 0; 314 } 315 } 316 } 317 318 325 private IGeneConstraintChecker m_geneAlleleChecker; 326 327 335 public void setConstraintChecker( 336 final IGeneConstraintChecker a_constraintChecker) { 337 m_geneAlleleChecker = a_constraintChecker; 338 } 339 340 347 public IGeneConstraintChecker getConstraintChecker() { 348 return m_geneAlleleChecker; 349 } 350 351 369 public Gene newGene() { 370 Gene result = newGeneInternal(); 371 result.setConstraintChecker(getConstraintChecker()); 372 result.setEnergy(getEnergy()); 373 return result; 374 } 375 376 protected abstract Gene newGeneInternal(); 377 378 384 public Configuration getConfiguration() { 385 return m_configuration; 386 } 387 } 388 | Popular Tags |