1 10 package org.jgap.gp; 11 12 import java.io.*; 13 import org.jgap.*; 14 import org.jgap.gp.impl.*; 15 16 24 public abstract class CommandGene 25 implements Comparable , Serializable { 26 27 private final static String CVS_REVISION = "$Revision: 1.19 $"; 28 29 32 public static final double DELTA = 0.0000001; 33 34 public final static Class BooleanClass = Boolean .class; 35 36 public final static Class IntegerClass = Integer .class; 37 38 public final static Class LongClass = Long .class; 39 40 public final static Class FloatClass = Float .class; 41 42 public final static Class DoubleClass = Double .class; 43 44 public final static Class VoidClass = Void .class; 45 46 private GPConfiguration m_configuration; 47 48 51 private boolean m_noValidation; 52 53 56 private Class m_returnType; 57 58 private int m_arity; 59 60 private boolean m_integerType; 61 62 private boolean m_floatType; 63 64 65 private double m_energy; 66 67 75 private Object m_applicationData; 76 77 84 private boolean m_compareAppData; 85 86 private int m_subReturnType; 87 88 private int[] m_subChildTypes; 89 90 93 protected void init() { 94 } 95 96 public CommandGene(final GPConfiguration a_conf, final int a_arity, 97 final Class a_returnType) 98 throws InvalidConfigurationException { 99 if (a_conf == null) { 100 throw new InvalidConfigurationException("Configuration must not be null!"); 101 } 102 m_configuration = a_conf; 103 init(); 104 m_arity = a_arity; 105 m_returnType = a_returnType; 106 if (a_returnType == Integer .class 107 || a_returnType == Long .class) { 108 m_integerType = true; 109 } 110 else if (a_returnType == Double .class 111 || a_returnType == Float .class) { 112 m_floatType = true; 113 } 114 } 115 116 129 public CommandGene(final GPConfiguration a_conf, final int a_arity, 130 final Class a_returnType, final int a_subReturnType, 131 final int[] a_childSubTypes) 132 throws InvalidConfigurationException { 133 this(a_conf, a_arity, a_returnType); 134 if (a_childSubTypes != null) { 135 boolean specialCase = false; 136 if (a_childSubTypes.length == 1) { 139 if (a_childSubTypes[0] == 0) { 140 m_subChildTypes = null; 141 specialCase = true; 142 } 143 } 144 if (!specialCase) { 145 if (a_childSubTypes.length != a_arity) { 146 throw new IllegalArgumentException ( 147 "Length of child sub types must equal" 148 + " the given arity (or set the former to null)"); 149 } 150 } 151 else { 152 m_subChildTypes = a_childSubTypes; 153 } 154 } 155 else { 156 m_subChildTypes = a_childSubTypes; 157 } 158 m_subReturnType = a_subReturnType; 159 } 160 161 175 public CommandGene(final GPConfiguration a_conf, final int a_arity, 176 final Class a_returnType, final int a_subReturnType, 177 final int a_childSubType) 178 throws InvalidConfigurationException { 179 this(a_conf, a_arity, a_returnType, a_subReturnType, 180 new int[] {a_childSubType}); 181 } 182 183 public void setAllele(Object a_newValue) { 184 throw new java.lang.UnsupportedOperationException ( 185 "Method setAllele() not used."); 186 } 187 188 public Object getAllele() { 189 return null; 190 } 191 192 public String getPersistentRepresentation() 193 throws UnsupportedOperationException { 194 195 throw new java.lang.UnsupportedOperationException ( 196 "Method getPersistentRepresentation() not yet implemented."); 197 } 198 199 public void setValueFromPersistentRepresentation(String a_representation) 200 throws UnsupportedOperationException , UnsupportedRepresentationException { 201 202 throw new java.lang.UnsupportedOperationException ( 203 "Method setValueFromPersistentRepresentation() not yet implemented."); 204 } 205 206 public void setToRandomValue(RandomGenerator a_numberGenerator) { 207 } 210 211 public void cleanup() { 212 } 215 216 public int size() { 217 return m_arity; 218 } 219 220 230 public int getArity(IGPProgram a_indvividual) { 231 return m_arity; 232 } 233 234 public int compareTo(Object a_other) { 235 CommandGene o2 = (CommandGene) a_other; 236 if (size() != o2.size()) { 237 if (size() > o2.size()) { 238 return 1; 239 } 240 else { 241 return -1; 242 } 243 } 244 if (getClass() != o2.getClass()) { 245 246 return -1; 247 } 248 else { 249 return 0; 250 } 251 } 252 253 public boolean equals(Object a_other) { 254 if (a_other == null) { 255 return false; 256 } 257 else { 258 try { 259 260 CommandGene other = (CommandGene) a_other; 261 if (getClass() == a_other.getClass()) { 262 if (getInternalValue() == null) { 263 if (other.getInternalValue() == null) { 264 return true; 265 } 266 else { 267 return false; 268 } 269 } 270 else { 271 if (other.getInternalValue() == null) { 272 return false; 273 } 274 else { 275 return true; 276 } 277 } 278 } 279 else { 280 return false; 281 } 282 } catch (ClassCastException cex) { 283 return false; 284 } 285 } 286 } 287 288 292 public abstract String toString(); 293 294 309 public Object execute(ProgramChromosome c, int n, Object [] args) { 310 if (m_returnType == BooleanClass) { 311 return new Boolean (execute_boolean(c, n, args)); 312 } 313 if (m_returnType == IntegerClass) { 314 return new Integer (execute_int(c, n, args)); 315 } 316 if (m_returnType == LongClass) { 317 return new Long (execute_long(c, n, args)); 318 } 319 if (m_returnType == FloatClass) { 320 return new Float (execute_float(c, n, args)); 321 } 322 if (m_returnType == DoubleClass) { 323 return new Double (execute_double(c, n, args)); 324 } 325 if (m_returnType == VoidClass) { 326 execute_void(c, n, args); 327 } 328 else { 329 return execute_object(c, n, args); 330 } 331 return null; 332 } 333 334 340 public Class getReturnType() { 341 return m_returnType; 342 } 343 344 352 public void setReturnType(Class a_type) { 353 m_returnType = a_type; 354 } 355 356 368 public boolean execute_boolean(ProgramChromosome c, int n, Object [] args) { 369 throw new UnsupportedOperationException (getName() + 370 " cannot return boolean"); 371 } 372 373 384 public void execute_void(ProgramChromosome c, int n, Object [] args) { 385 throw new UnsupportedOperationException (getName() + 386 " cannot return void"); 387 } 388 389 401 public int execute_int(ProgramChromosome c, int n, Object [] args) { 402 throw new UnsupportedOperationException (getName() + 403 " cannot return int"); 404 } 405 406 418 public long execute_long(ProgramChromosome c, int n, Object [] args) { 419 throw new UnsupportedOperationException (getName() + 420 " cannot return long"); 421 } 422 423 435 public float execute_float(ProgramChromosome c, int n, Object [] args) { 436 throw new UnsupportedOperationException (getName() + 437 " cannot return float"); 438 } 439 440 452 public double execute_double(ProgramChromosome c, int n, Object [] args) { 453 throw new UnsupportedOperationException (getName() + 454 " cannot return double"); 455 } 456 457 469 public Object execute_object(ProgramChromosome c, int n, Object [] args) { 470 throw new UnsupportedOperationException (getName() + 471 " cannot return Object"); 472 } 473 474 public String getName() { 475 return toString(); 476 } 477 478 490 public Class getChildType(IGPProgram a_ind, int a_chromNum) { 491 if (m_arity == 0) { 492 return null; 493 } 494 else { 495 return getReturnType(); 496 } 497 } 498 499 protected Object getInternalValue() { 500 return null; 501 } 502 503 513 public int hashCode() { 514 if (getInternalValue() == null) { 518 return getClass().getName().hashCode(); 519 } 520 else { 521 return getInternalValue().hashCode(); 522 } 523 } 524 525 public boolean isIntegerType() { 526 return m_integerType; 527 } 528 529 public boolean isFloatType() { 530 return m_floatType; 531 } 532 533 539 public boolean isAffectGlobalState() { 540 return false; 541 } 542 543 553 public boolean isValid(ProgramChromosome a_program) { 554 return true; 555 } 556 557 public boolean isValid(ProgramChromosome a_program, int a_index) { 558 return true; 559 } 560 561 protected void check(ProgramChromosome a_program) { 562 if (m_noValidation) { 563 return; 564 } 565 if (!isValid(a_program)) { 566 throw new IllegalStateException ("State for GP-command not valid"); 567 } 568 } 569 570 protected void check(ProgramChromosome a_program, int a_index) { 571 if (m_noValidation) { 572 return; 573 } 574 if (!isValid(a_program, a_index)) { 575 throw new IllegalStateException ("State for GP-command not valid"); 576 } 577 } 578 579 public void setNoValidation(boolean a_noValidation) { 580 m_noValidation = a_noValidation; 581 } 582 583 589 public GPConfiguration getGPConfiguration() { 590 return m_configuration; 591 } 592 593 605 public void setApplicationData(final Object a_newData) { 606 m_applicationData = a_newData; 607 } 608 609 621 public Object getApplicationData() { 622 return m_applicationData; 623 } 624 625 635 public void setCompareApplicationData(final boolean a_doCompare) { 636 m_compareAppData = a_doCompare; 637 } 638 639 645 public boolean isCompareApplicationData() { 646 return m_compareAppData; 647 } 648 649 655 public double getEnergy() { 656 return m_energy; 657 } 658 659 667 public void setEnergy(final double a_energy) { 668 m_energy = a_energy; 669 } 670 671 677 public int getSubReturnType() { 678 return m_subReturnType; 679 } 680 681 public int getSubChildType(int a_childNum) { 682 if (m_subChildTypes == null) { 683 return 0; 684 } 685 else { 686 return m_subChildTypes[a_childNum]; 687 } 688 } 689 690 protected int[] getSubChildTypes() { 691 return m_subChildTypes; 692 } 693 } 694 | Popular Tags |