1 10 package org.jgap.gp.impl; 11 12 import java.util.*; 13 import org.apache.commons.lang.builder.CompareToBuilder; 14 import org.jgap.*; 15 import org.jgap.impl.*; 16 import org.jgap.distr.*; 17 import org.jgap.event.*; 18 import org.jgap.gp.*; 19 20 26 public class GPConfiguration 27 extends Configuration { 28 29 private final static String CVS_REVISION = "$Revision: 1.25 $"; 30 31 35 private GPFitnessFunction m_objectiveFunction; 36 37 40 private Stack m_stack = new Stack(); 41 42 45 private Culture m_memory = new Culture(50); 46 47 51 private double m_crossoverProb = 0.9d; 52 53 58 private double m_reproductionProb = 0.1d; 59 60 64 private double m_newChromsPercent = 0.3d; 65 66 70 private double m_functionProb = 0.9d; 71 72 75 private int m_maxCrossoverDepth = 17; 76 77 80 private int m_maxInitDepth = 7; 81 82 85 private int m_minInitDepth = 3; 86 87 90 private INaturalGPSelector m_selectionMethod; 91 92 95 private CrossMethod m_crossMethod; 96 97 103 private boolean m_strictProgramCreation; 104 105 109 private int m_programCreationMaxTries = 5; 110 111 114 private IGPFitnessEvaluator m_fitnessEvaluator; 115 116 private INodeValidator m_nodeValidator; 117 118 124 private boolean m_warningPrinted; 125 126 132 private IGPProgram m_prototypeProgram; 133 134 private transient Map m_programCache; 135 136 private boolean m_useProgramCache = false; 137 138 146 public GPConfiguration() 147 throws InvalidConfigurationException { 148 this("", null); 149 } 150 151 public GPConfiguration(String a_id, String a_name) 152 throws InvalidConfigurationException { 153 super(a_id, a_name); 154 init(); 155 m_selectionMethod = new FitnessProportionateSelection(); 156 } 157 158 167 public void setGPFitnessEvaluator(IGPFitnessEvaluator a_evaluator) { 168 m_fitnessEvaluator = a_evaluator; 169 } 170 171 179 protected void init() 180 throws InvalidConfigurationException { 181 m_crossMethod = new BranchTypingCross(this); 182 setEventManager(new EventManager()); 183 setRandomGenerator(new StockRandomGenerator()); 184 setGPFitnessEvaluator(new DefaultGPFitnessEvaluator()); 185 m_programCache = new HashMap(50); 186 } 187 188 197 public GPConfiguration(INaturalGPSelector a_selectionMethod) 198 throws InvalidConfigurationException { 199 super(); 200 init(); 201 m_selectionMethod = a_selectionMethod; 202 } 203 204 211 public void setSelectionMethod(INaturalGPSelector a_method) { 212 if (a_method == null) { 213 throw new IllegalArgumentException ("Selection method must not be null"); 214 } 215 m_selectionMethod = a_method; 216 } 217 218 225 public void setCrossoverMethod(CrossMethod a_method) { 226 if (a_method == null) { 227 throw new IllegalArgumentException ("Crossover method must not be null"); 228 } 229 m_crossMethod = a_method; 230 } 231 232 public synchronized void verifyStateIsValid() 233 throws InvalidConfigurationException { 234 } 237 238 public synchronized void addGeneticOperator(GeneticOperator a_operatorToAdd) 239 throws InvalidConfigurationException { 240 throw new UnsupportedOperationException ( 241 "Use addGeneticOperator(GPGeneticOperator) instead!"); 242 } 243 244 249 public double getCrossoverProb() { 250 return m_crossoverProb; 251 } 252 253 public void setCrossoverProb(float a_crossoverProb) { 254 m_crossoverProb = a_crossoverProb; 255 } 256 257 public double getReproductionProb() { 258 return m_reproductionProb; 259 } 260 261 public void setReproductionProb(float a_reproductionProb) { 262 m_reproductionProb = a_reproductionProb; 263 } 264 265 272 public void setFunctionProb(double a_functionProb) { 273 m_functionProb = a_functionProb; 274 } 275 276 282 public double getFunctionProb() { 283 return m_functionProb; 284 } 285 286 public void setNewChromsPercent(double a_newChromsPercent) { 287 m_newChromsPercent = a_newChromsPercent; 288 } 289 290 public double getNewChromsPercent() { 291 return m_newChromsPercent; 292 } 293 294 public int getMaxCrossoverDepth() { 295 return m_maxCrossoverDepth; 296 } 297 298 public void setMaxCrossoverDepth(int a_maxCrossoverDepth) { 299 m_maxCrossoverDepth = a_maxCrossoverDepth; 300 } 301 302 public INaturalGPSelector getSelectionMethod() { 303 return m_selectionMethod; 304 } 305 306 public CrossMethod getCrossMethod() { 307 return m_crossMethod; 308 } 309 310 public int getMaxInitDepth() { 311 return m_maxInitDepth; 312 } 313 314 public void setMaxInitDepth(int a_maxDepth) { 315 m_maxInitDepth = a_maxDepth; 316 } 317 318 public int getMinInitDepth() { 319 return m_minInitDepth; 320 } 321 322 public void setMinInitDepth(int a_minDepth) { 323 m_minInitDepth = a_minDepth; 324 } 325 326 public void pushToStack(Object a_value) { 327 m_stack.push(a_value); 328 } 329 330 public Object popFromStack() { 331 return m_stack.pop(); 332 } 333 334 public Object peekStack() { 335 return m_stack.peek(); 336 } 337 338 public int stackSize() { 339 return m_stack.size(); 340 } 341 342 public void clearStack() { 343 m_stack.clear(); 344 } 345 346 355 public void storeInMemory(String a_name, Object a_value) { 356 m_memory.set(a_name, a_value, -1); 357 } 358 359 370 public CultureMemoryCell storeMatrixMemory(int a_x, int a_y, Object a_value) { 371 return m_memory.setMatrix(a_x, a_y, a_value); 372 } 373 374 384 public Object readMatrixMemory(int a_x, int a_y) { 385 return m_memory.getMatrix(a_x, a_y).getCurrentValue(); 386 } 387 388 397 public Object readFromMemory(String a_name) { 398 return m_memory.get(a_name).getCurrentValue(); 399 } 400 401 408 public Object readFromMemoryIfExists(String a_name) { 409 CultureMemoryCell cell = null; 410 try { 411 cell = m_memory.get(a_name); 412 } catch (IllegalArgumentException iex) { 413 ; 416 } 417 if (cell == null) { 418 return null; 419 } 420 return cell.getCurrentValue(); 421 } 422 423 433 public CultureMemoryCell storeIndexedMemory(int a_index, Object a_value) { 434 return m_memory.set(a_index, a_value, -1, "noname"); 435 } 436 437 446 public Object readIndexedMemory(int a_index) { 447 CultureMemoryCell cell = m_memory.get(a_index); 448 if (cell == null) { 449 return null; 450 } 451 else { 452 return cell.getCurrentValue(); 453 } 454 } 455 456 462 public void clearMemory() { 463 m_memory.clear(); 464 } 465 466 public GPFitnessFunction getGPFitnessFunction() { 467 return m_objectiveFunction; 468 } 469 470 486 public synchronized void setFitnessFunction(GPFitnessFunction a_functionToSet) 487 throws InvalidConfigurationException { 488 verifyChangesAllowed(); 489 if (a_functionToSet == null) { 492 throw new InvalidConfigurationException( 493 "The FitnessFunction instance may not be null."); 494 } 495 checkProperty(PROPERTY_FITFUNC_INST, a_functionToSet, 499 "Fitness function has already been set differently."); 500 m_objectiveFunction = a_functionToSet; 501 } 502 503 public boolean isStrictProgramCreation() { 504 return m_strictProgramCreation; 505 } 506 507 public void setStrictProgramCreation(boolean a_strict) { 508 m_strictProgramCreation = a_strict; 509 } 510 511 public int getProgramCreationMaxtries() { 512 return m_programCreationMaxTries; 513 } 514 515 public void setProgramCreationMaxTries(int a_maxtries) { 516 m_programCreationMaxTries = a_maxtries; 517 } 518 519 525 public IGPFitnessEvaluator getGPFitnessEvaluator() { 526 return m_fitnessEvaluator; 527 } 528 529 554 public boolean validateNode(ProgramChromosome a_chrom, CommandGene a_node, 555 CommandGene a_rootNode, int a_tries, int a_num, 556 int a_recurseLevel, Class a_type, 557 CommandGene[] a_functionSet, int a_depth, 558 boolean a_grow, int a_childIndex) { 559 if (m_nodeValidator == null) { 560 return true; 561 } 562 return m_nodeValidator.validate(a_chrom, a_node, a_rootNode, a_tries, a_num, 563 a_recurseLevel, a_type, a_functionSet, 564 a_depth, a_grow, a_childIndex); 565 } 566 567 575 public void setNodeValidator(INodeValidator a_nodeValidator) { 576 m_nodeValidator = a_nodeValidator; 577 } 578 579 585 public INodeValidator getNodeValidator() { 586 return m_nodeValidator; 587 } 588 589 598 public boolean equals(Object a_other) { 599 try { 600 return compareTo(a_other) == 0; 601 } catch (ClassCastException cex) { 602 return false; 603 } 604 } 605 606 public int compareTo(Object a_other) { 607 if (a_other == null) { 608 return 1; 609 } 610 else { 611 GPConfiguration other = (GPConfiguration) a_other; 612 return new CompareToBuilder() 613 .append(m_objectiveFunction, other.m_objectiveFunction) 614 .append(m_crossoverProb, other.m_crossoverProb) 615 .append(m_reproductionProb, other.m_reproductionProb) 616 .append(m_newChromsPercent, other.m_newChromsPercent) 617 .append(m_maxCrossoverDepth, other.m_maxCrossoverDepth) 618 .append(m_maxInitDepth, other.m_maxInitDepth) 619 .append(m_selectionMethod.getClass(), 620 other.m_selectionMethod.getClass()) 621 .append(m_crossMethod.getClass(), other.m_crossMethod.getClass()) 622 .append(m_programCreationMaxTries, other.m_programCreationMaxTries) 623 .append(m_strictProgramCreation, other.m_strictProgramCreation) 624 .append(m_fitnessEvaluator.getClass(), 625 other.m_fitnessEvaluator.getClass()) 626 .toComparison(); 627 } 628 } 629 630 637 public boolean isMaxNodeWarningPrinted() { 638 return m_warningPrinted; 639 } 640 641 647 public void flagMaxNodeWarningPrinted() { 648 m_warningPrinted = true; 649 } 650 651 658 public void setPrototypeProgram(IGPProgram a_program) { 659 m_prototypeProgram = a_program; 660 } 661 662 668 public IGPProgram getPrototypeProgram() { 669 return m_prototypeProgram; 670 } 671 672 678 public int getMemorySize() { 679 return m_memory.size(); 680 } 681 682 public GPProgramInfo readProgramCache(GPProgram a_prog) { 683 GPProgramInfo pci = new GPProgramInfo(a_prog, true); 684 pci.setFound(false); 685 return (GPProgramInfo)m_programCache.get(pci.getToStringNorm()); 686 } 687 688 public GPProgramInfo putToProgramCache(GPProgram a_prog) { 689 GPProgramInfo pci = new GPProgramInfo(a_prog, true); 690 return (GPProgramInfo)m_programCache.put(pci.getToStringNorm(), pci); 691 } 692 693 public boolean isUseProgramCache() { 694 return m_useProgramCache; 695 } 696 697 public void setUseProgramCache(boolean a_useCache) { 698 m_useProgramCache = a_useCache; 699 } 700 } 701 702 | Popular Tags |