1 10 package org.jgap; 11 12 import java.io.*; 13 import java.util.*; 14 import org.jgap.data.config.*; 15 import org.jgap.event.*; 16 import org.jgap.impl.*; 17 import org.jgap.util.*; 18 import org.apache.commons.lang.builder.*; 19 20 42 public class Configuration 43 implements Configurable, Serializable, ICloneable, Comparable { 44 45 private final static String CVS_REVISION = "$Revision: 1.76 $"; 46 47 52 public static final String PROPERTY_JGAPFACTORY_CLASS = "JGAPFACTORYCLASS"; 53 54 public static final String PROPERTY_FITFUNC_INST = "JGAPFITFUNCINST"; 55 56 public static final String PROPERTY_BFITFNC_INST = "JGAPBFITFNCINST"; 57 58 public static final String PROPERTY_FITEVAL_INST = "JGAPFITEVALINST"; 59 60 public static final String PROPERTY_SAMPLE_CHROM_INST = "JGAPSAMPLECHRMINST"; 61 62 public static final String PROPERTY_EVENT_MGR_INST = "JGAPEVNTMGRINST"; 63 64 67 public static final String S_CONFIGURATION = "Configuration"; 68 69 public static final String S_CONFIGURATION_NAME = "Configuration name"; 70 71 public static final String S_POPULATION_SIZE = "Population size"; 72 73 public static final String S_MINPOPSIZE = "Minimum pop. size [%]"; 74 75 public static final String S_CHROMOSOME_SIZE = "Chromosome size"; 76 77 public static final String S_SAMPLE_CHROM = "Sample Chromosome"; 78 79 public static final String S_SIZE = "Size"; 80 81 public static final String S_TOSTRING = "toString"; 82 83 public static final String S_RANDOM_GENERATOR = "Random generator"; 84 85 public static final String S_EVENT_MANAGER = "Event manager"; 86 87 public static final String S_NONE = "none"; 88 89 public static final String S_CONFIGURATION_HANDLER = "Configuration handler"; 90 91 public static final String S_FITNESS_FUNCTION = "Fitness function"; 92 93 public static final String S_FITNESS_EVALUATOR = "Fitness evaluator"; 94 95 public static final String S_GENETIC_OPERATORS = "Genetic operators"; 96 97 public static final String S_NATURAL_SELECTORS = "Natural Selectors"; 98 99 public static final String S_PRE = "pre"; 100 101 public static final String S_POST = "post"; 102 103 private ConfigurationConfigurable m_config = new ConfigurationConfigurable(); 104 105 113 private FitnessFunction m_objectiveFunction; 114 115 120 private FitnessEvaluator m_fitnessEvaluator; 121 122 127 private int m_minPercentageSizePopulation; 128 129 138 private BulkFitnessFunction m_bulkObjectiveFunction; 139 140 148 private IChromosome m_sampleChromosome; 149 150 158 private RandomGenerator m_randomGenerator; 159 160 167 private IEventManager m_eventManager; 168 169 177 private transient IChromosomePool m_chromosomePool; 178 179 188 private List m_geneticOperators; 189 190 194 private int m_chromosomeSize; 195 196 202 private boolean m_settingsLocked; 203 204 211 private ChainOfSelectors m_preSelectors; 212 213 220 private ChainOfSelectors m_postSelectors; 221 222 private int m_sizeNaturalSelectorsPre; 223 224 private int m_sizeNaturalSelectorsPost; 225 226 230 private boolean m_preserveFittestIndividual; 231 232 239 private int m_generationNr; 240 241 247 private transient RootConfigurationHandler m_conHandler; 248 249 255 private String m_name; 256 257 266 private boolean m_keepPopulationSizeConstant; 267 268 274 private transient IJGAPFactory m_factory; 275 276 private transient String threadKey; 277 278 285 private String m_id; 286 287 public Configuration() { 288 this("", null); 289 } 290 291 301 public Configuration(String a_id, String a_name) { 302 m_id = a_id; 303 setName(a_name); 304 makeThreadKey(); 305 m_preSelectors = new ChainOfSelectors(); 306 m_postSelectors = new ChainOfSelectors(); 307 m_sizeNaturalSelectorsPre = 0; 308 m_sizeNaturalSelectorsPost = 0; 309 m_geneticOperators = new Vector(); 311 m_conHandler = new RootConfigurationHandler(); 312 m_conHandler.setConfigurable(this); 313 m_keepPopulationSizeConstant = true; 314 String clazz = System.getProperty(PROPERTY_JGAPFACTORY_CLASS); 318 if (clazz != null && clazz.length() > 0) { 319 try { 320 m_factory = (IJGAPFactory) Class.forName(clazz).newInstance(); 321 } catch (Throwable ex) { 322 throw new RuntimeException ("Class " + clazz 323 + " could not be instantiated" 324 + " as type IJGAPFactory"); 325 } 326 } 327 else { 328 m_factory = new JGAPFactory(false); 329 } 330 } 331 332 341 public Configuration(final String a_name) { 342 this(); 343 setName(a_name); 344 } 345 346 359 public Configuration(final String a_configFileName, boolean a_ignore) 360 throws ConfigException, InvalidConfigurationException { 361 this(); 362 ConfigFileReader.instance().setFileName(a_configFileName); 363 Genotype.setStaticConfiguration(this); 367 getConfigurationHandler().readConfig(); 371 } 372 373 383 public static void reset() { 384 reset(""); 385 } 386 387 public static void reset(final String a_id) { 388 String threadKey = getThreadKey(Thread.currentThread(), a_id); 389 System.setProperty(threadKey + Configuration.PROPERTY_FITFUNC_INST, ""); 390 System.setProperty(threadKey + Configuration.PROPERTY_BFITFNC_INST, ""); 391 System.setProperty(threadKey + Configuration.PROPERTY_FITEVAL_INST, ""); 392 System.setProperty(threadKey + Configuration.PROPERTY_SAMPLE_CHROM_INST, ""); 393 System.setProperty(threadKey + Configuration.PROPERTY_EVENT_MGR_INST, ""); 394 } 395 396 403 public static void resetProperty(final String a_propName) { 404 resetProperty(a_propName, ""); 405 } 406 407 public static void resetProperty(final String a_propName, final String a_id) { 408 String threadKey = getThreadKey(Thread.currentThread(), a_id); 409 System.setProperty(threadKey + a_propName, ""); 410 } 411 412 418 public void setName(final String a_name) { 419 m_name = a_name; 420 } 421 422 428 public String getName() { 429 return m_name; 430 } 431 432 453 public synchronized void setFitnessFunction(final FitnessFunction 454 a_functionToSet) 455 throws InvalidConfigurationException { 456 verifyChangesAllowed(); 457 if (a_functionToSet == null) { 460 throw new InvalidConfigurationException( 461 "The FitnessFunction instance may not be null."); 462 } 463 if (m_bulkObjectiveFunction != null) { 466 throw new InvalidConfigurationException( 467 "The bulk fitness function and normal fitness function " + 468 "may not both be set."); 469 } 470 checkProperty(PROPERTY_FITFUNC_INST, a_functionToSet, 474 "Fitness function has already been set differently."); 475 m_objectiveFunction = a_functionToSet; 476 } 477 478 490 protected void checkProperty(final String a_propname, final Object a_obj, 491 final String a_errmsg) { 492 String instanceHash = System.getProperty(threadKey + a_propname, null); 493 String key = makeKey(a_obj); 494 if (instanceHash == null || instanceHash.length() < 1) { 495 System.setProperty(threadKey + a_propname, key); 496 } 497 else if (!instanceHash.equals(key)) { 498 throw new RuntimeException (a_errmsg + "\nIf you want to set or construct" 499 + 500 " a configuration multiple times, please call" 501 + 502 " static method Configuration.reset() before" 503 + " each setting!"); 504 } 505 } 506 507 515 protected String makeKey(final Object a_obj) { 516 String key = String.valueOf(a_obj.hashCode()) 517 + a_obj.getClass().getName(); 518 return key; 519 } 520 521 530 public synchronized FitnessFunction getFitnessFunction() { 531 return m_objectiveFunction; 532 } 533 534 556 public synchronized void setBulkFitnessFunction( 557 final BulkFitnessFunction a_functionToSet) 558 throws InvalidConfigurationException { 559 verifyChangesAllowed(); 560 if (a_functionToSet == null) { 564 throw new InvalidConfigurationException( 565 "The BulkFitnessFunction instance may not be null."); 566 } 567 if (m_objectiveFunction != null) { 570 throw new InvalidConfigurationException( 571 "The bulk fitness function and normal fitness function " + 572 "may not both be set."); 573 } 574 checkProperty(PROPERTY_BFITFNC_INST, a_functionToSet, 578 "Bulk fitness function has already been set differently."); 579 m_bulkObjectiveFunction = a_functionToSet; 580 } 581 582 591 public synchronized BulkFitnessFunction getBulkFitnessFunction() { 592 return m_bulkObjectiveFunction; 593 } 594 595 610 public void setSampleChromosome(final IChromosome a_sampleChromosomeToSet) 611 throws InvalidConfigurationException { 612 verifyChangesAllowed(); 613 615 if (a_sampleChromosomeToSet == null) { 617 throw new InvalidConfigurationException( 618 "The sample chromosome instance may not be null."); 619 } 620 if (a_sampleChromosomeToSet.getConfiguration() == null) { 621 throw new InvalidConfigurationException( 622 "The sample chromosome's configuration may not be null."); 623 } 624 checkProperty(PROPERTY_SAMPLE_CHROM_INST, a_sampleChromosomeToSet, 628 "Sample chromosome has already been set differently."); 629 m_sampleChromosome = a_sampleChromosomeToSet; 630 m_chromosomeSize = m_sampleChromosome.size(); 631 } 632 633 642 public IChromosome getSampleChromosome() { 643 return m_sampleChromosome; 644 } 645 646 656 public int getChromosomeSize() { 657 return m_chromosomeSize; 658 } 659 660 676 public synchronized void setNaturalSelector(final NaturalSelector 677 a_selectorToSet) 678 throws InvalidConfigurationException { 679 addNaturalSelector(a_selectorToSet, false); 680 } 681 682 693 public synchronized NaturalSelector getNaturalSelector() { 694 if (m_sizeNaturalSelectorsPost < 1) { 695 return null; 696 } 697 return getNaturalSelectors(false).get(0); 698 } 699 700 709 public synchronized NaturalSelector getNaturalSelector(final boolean 710 a_processBeforeGeneticOperators, final int a_index) { 711 if (a_processBeforeGeneticOperators) { 712 if (m_sizeNaturalSelectorsPre <= a_index) { 713 throw new IllegalArgumentException ( 714 "Index of NaturalSelector out of bounds"); 715 } 716 else { 717 return m_preSelectors.get(a_index); 718 } 719 } 720 else { 721 if (m_sizeNaturalSelectorsPost <= a_index) { 722 throw new IllegalArgumentException ( 723 "Index of NaturalSelector out of bounds"); 724 } 725 else { 726 return m_postSelectors.get(a_index); 727 } 728 } 729 } 730 731 740 public ChainOfSelectors getNaturalSelectors(final boolean 741 a_processBeforeGeneticOperators) { 742 if (a_processBeforeGeneticOperators) { 743 return m_preSelectors; 744 } 745 else { 746 return m_postSelectors; 747 } 748 } 749 750 758 public int getNaturalSelectorsSize(final boolean 759 a_processBeforeGeneticOperators) { 760 if (a_processBeforeGeneticOperators) { 761 return m_sizeNaturalSelectorsPre; 762 } 763 else { 764 return m_sizeNaturalSelectorsPost; 765 } 766 } 767 768 778 public void removeNaturalSelectors(final boolean 779 a_processBeforeGeneticOperators) { 780 if (a_processBeforeGeneticOperators) { 781 getNaturalSelectors(true).clear(); 782 m_sizeNaturalSelectorsPre = 0; 783 } 784 else { 785 getNaturalSelectors(false).clear(); 786 m_sizeNaturalSelectorsPost = 0; 787 } 788 } 789 790 804 public synchronized void setRandomGenerator(final RandomGenerator 805 a_generatorToSet) 806 throws InvalidConfigurationException { 807 verifyChangesAllowed(); 808 if (a_generatorToSet == null) { 811 throw new InvalidConfigurationException( 812 "The RandomGenerator instance may not be null."); 813 } 814 m_randomGenerator = a_generatorToSet; 815 } 816 817 825 public synchronized RandomGenerator getRandomGenerator() { 826 return m_randomGenerator; 827 } 828 829 845 public synchronized void addGeneticOperator(final GeneticOperator 846 a_operatorToAdd) 847 throws InvalidConfigurationException { 848 verifyChangesAllowed(); 849 if (a_operatorToAdd == null) { 852 throw new InvalidConfigurationException( 853 "The GeneticOperator instance may not be null."); 854 } 855 m_geneticOperators.add(a_operatorToAdd); 856 } 857 858 870 public List getGeneticOperators() { 871 return m_geneticOperators; 872 } 873 874 888 public synchronized void setPopulationSize(final int a_sizeOfPopulation) 889 throws InvalidConfigurationException { 890 verifyChangesAllowed(); 891 if (a_sizeOfPopulation < 1) { 894 throw new InvalidConfigurationException( 895 "The population size must be positive."); 896 } 897 m_config.m_populationSize = a_sizeOfPopulation; 898 } 899 900 905 public synchronized int getPopulationSize() { 906 return m_config.m_populationSize; 907 } 908 909 923 public void setEventManager(final IEventManager a_eventManagerToSet) 924 throws InvalidConfigurationException { 925 verifyChangesAllowed(); 926 if (a_eventManagerToSet == null) { 929 throw new InvalidConfigurationException( 930 "The event manager instance may not be null."); 931 } 932 checkProperty(PROPERTY_EVENT_MGR_INST, a_eventManagerToSet, 936 "Event manager has already been set differently."); 937 m_eventManager = a_eventManagerToSet; 938 } 939 940 949 public IEventManager getEventManager() { 950 return m_eventManager; 951 } 952 953 967 public void setChromosomePool(final IChromosomePool a_chromosomePoolToSet) 968 throws InvalidConfigurationException { 969 verifyChangesAllowed(); 970 m_chromosomePool = a_chromosomePoolToSet; 971 } 972 973 987 public IChromosomePool getChromosomePool() { 988 return m_chromosomePool; 989 } 990 991 1013 public synchronized void lockSettings() 1014 throws InvalidConfigurationException { 1015 if (!m_settingsLocked) { 1016 verifyStateIsValid(); 1017 m_geneticOperators = Collections.unmodifiableList(m_geneticOperators); 1020 m_settingsLocked = true; 1021 } 1022 } 1023 1024 1033 public boolean isLocked() { 1034 return m_settingsLocked; 1035 } 1036 1037 1050 public synchronized void verifyStateIsValid() 1051 throws InvalidConfigurationException { 1052 if (m_objectiveFunction == null && m_bulkObjectiveFunction == null) { 1056 throw new InvalidConfigurationException( 1057 "A desired fitness function or bulk fitness function must " + 1058 "be specified in the active configuration."); 1059 } 1060 if (m_sampleChromosome == null) { 1061 throw new InvalidConfigurationException( 1062 "A sample instance of the desired Chromosome " + 1063 "setup must be specified in the active configuration."); 1064 } 1065 if (m_preSelectors.size() == 0 && m_postSelectors.size() == 0) { 1066 throw new InvalidConfigurationException( 1067 "At least one desired natural selector must be specified in the" 1068 + " active configuration."); 1069 } 1070 if (m_randomGenerator == null) { 1071 throw new InvalidConfigurationException( 1072 "A desired random number generator must be specified in the " + 1073 "active configuration."); 1074 } 1075 if (m_eventManager == null) { 1076 throw new InvalidConfigurationException( 1077 "A desired event manager must be specified in the active " + 1078 "configuration."); 1079 } 1080 if (m_geneticOperators.isEmpty()) { 1081 throw new InvalidConfigurationException( 1082 "At least one genetic operator must be specified in the " + 1083 "configuration."); 1084 } 1085 if (m_chromosomeSize <= 0) { 1086 throw new InvalidConfigurationException( 1087 "A chromosome size greater than zero must be specified in " + 1088 "the active configuration."); 1089 } 1090 if (m_config.m_populationSize <= 0) { 1091 throw new InvalidConfigurationException( 1092 "A genotype size greater than zero must be specified in " + 1093 "the active configuration."); 1094 } 1095 if (m_fitnessEvaluator == null) { 1096 throw new IllegalArgumentException ( 1097 "The fitness evaluator may not be null."); 1098 } 1099 Gene[] sampleGenes = m_sampleChromosome.getGenes(); 1106 for (int i = 0; i < sampleGenes.length; i++) { 1107 Gene sampleCopy = sampleGenes[i].newGene(); 1108 sampleCopy.setAllele(sampleGenes[i].getAllele()); 1109 if (! (sampleCopy.equals(sampleGenes[i]))) { 1110 throw new InvalidConfigurationException( 1111 "The sample Gene at gene position (locus) " 1112 + i 1113 + " does not appear to have a working equals() or compareTo()" 1114 + " method.\n" 1115 + "It could also be that you forgot to implement method" 1116 + " newGene() in your Gene implementation.\n" 1117 + "When tested, the method returned false when comparing " 1118 + "the sample gene with a gene of the same type and " 1119 + "possessing the same value (allele)."); 1120 } 1121 } 1122 } 1123 1124 1137 protected void verifyChangesAllowed() 1138 throws InvalidConfigurationException { 1139 if (m_settingsLocked) { 1140 throw new InvalidConfigurationException( 1141 "This Configuration object is locked. Settings may not be " + 1142 "altered."); 1143 } 1144 } 1145 1146 1158 public void addNaturalSelector(NaturalSelector a_selector, 1159 boolean a_processBeforeGeneticOperators) 1160 throws InvalidConfigurationException { 1161 verifyChangesAllowed(); 1162 if (a_processBeforeGeneticOperators) { 1163 m_preSelectors.addNaturalSelector(a_selector); 1164 m_sizeNaturalSelectorsPre = m_preSelectors.size(); 1165 } 1166 else { 1167 m_postSelectors.addNaturalSelector(a_selector); 1168 m_sizeNaturalSelectorsPost = m_postSelectors.size(); 1169 } 1170 } 1171 1172 1181 public void setMinimumPopSizePercent(int a_minimumSizeGuaranteedPercent) { 1182 m_minPercentageSizePopulation = a_minimumSizeGuaranteedPercent; 1183 } 1184 1185 public int getMinimumPopSizePercent() { 1186 return m_minPercentageSizePopulation; 1187 } 1188 1189 1195 public FitnessEvaluator getFitnessEvaluator() { 1196 return m_fitnessEvaluator; 1197 } 1198 1199 1207 public void setFitnessEvaluator(FitnessEvaluator a_fitnessEvaluator) { 1208 if (a_fitnessEvaluator == null) { 1209 throw new IllegalStateException ( 1210 "The fitness evaluator object must not be null!"); 1211 } 1212 checkProperty(PROPERTY_FITEVAL_INST, a_fitnessEvaluator, 1216 "Fitness evaluator has already been set differently."); 1217 m_fitnessEvaluator = a_fitnessEvaluator; 1218 } 1219 1220 1227 public boolean isPreserveFittestIndividual() { 1228 return m_preserveFittestIndividual; 1229 } 1230 1231 1239 public void setPreservFittestIndividual(boolean a_preserveFittest) { 1240 m_preserveFittestIndividual = a_preserveFittest; 1241 } 1242 1243 public void incrementGenerationNr() { 1244 m_generationNr++; 1245 } 1246 1247 public int getGenerationNr() { 1248 return m_generationNr; 1249 } 1250 1251 1258 public ConfigurationHandler getConfigurationHandler() { 1259 return m_conHandler; 1260 } 1261 1262 1269 public String toString() { 1270 String result = S_CONFIGURATION + ":"; 1271 result += "\n " + S_CONFIGURATION_NAME + ": " + getName(); 1274 result += "\n " + S_POPULATION_SIZE + ": " + getPopulationSize(); 1275 result += "\n " + S_MINPOPSIZE + ": " + getMinimumPopSizePercent(); 1276 result += "\n " + S_CHROMOSOME_SIZE + ": " + getChromosomeSize(); 1277 result += "\n " + S_SAMPLE_CHROM + ":\n"; 1280 if (getSampleChromosome() == null) { 1281 result += "\n null"; 1282 } 1283 else { 1284 result += "\n " + S_SIZE + ": " + getSampleChromosome().size(); 1285 result += "\n " + S_TOSTRING + ": " + getSampleChromosome().toString(); 1286 } 1287 result += "\n " + S_RANDOM_GENERATOR + ": "; 1290 if (getRandomGenerator() != null) { 1291 result += getRandomGenerator().getClass().getName(); 1292 } 1293 else { 1294 result += S_NONE; 1295 } 1296 result += "\n " + S_EVENT_MANAGER + ": "; 1297 if (getEventManager() == null) { 1300 result += S_NONE; 1301 } 1302 else { 1303 result += getEventManager().getClass().getName(); 1304 } 1305 result += "\n " + S_CONFIGURATION_HANDLER + ": "; 1308 if (getConfigurationHandler() == null) { 1309 result += "null"; 1310 } 1311 else { 1312 result += getConfigurationHandler().getName(); 1313 } 1314 result += "\n " + S_FITNESS_FUNCTION + ": "; 1317 if (getFitnessFunction() == null) { 1318 result += "null"; 1319 } 1320 else { 1321 result += getFitnessFunction().getClass().getName(); 1322 } 1323 result += "\n " + S_FITNESS_EVALUATOR + ": "; 1326 if (getFitnessEvaluator() == null) { 1327 result += "null"; 1328 } 1329 else { 1330 result += getFitnessEvaluator().getClass().getName(); 1331 } 1332 result += "\n " + S_GENETIC_OPERATORS + ": "; 1335 if (getGeneticOperators() == null) { 1336 result += "null"; 1337 } 1338 else { 1339 int gensize = getGeneticOperators().size(); 1340 if (gensize < 1) { 1341 result += S_NONE; 1342 } 1343 else { 1344 for (int i = 0; i < gensize; i++) { 1345 if (i > 0) { 1346 result += "; "; 1347 } 1348 result += " " + getGeneticOperators().get(i).getClass().getName(); 1349 } 1350 } 1351 } 1352 int natsize = getNaturalSelectors(true).size(); 1355 result += "\n " + S_NATURAL_SELECTORS + "(" + S_PRE + "): "; 1356 if (natsize < 1) { 1357 result += S_NONE; 1358 } 1359 else { 1360 for (int i = 0; i < natsize; i++) { 1361 if (i > 0) { 1362 result += "; "; 1363 } 1364 result += " " + getNaturalSelectors(true).get(i).getClass().getName(); 1365 } 1366 } 1367 natsize = getNaturalSelectors(false).size(); 1370 result += "\n " + S_NATURAL_SELECTORS + "(" + S_POST + "): "; 1371 if (natsize < 1) { 1372 result += "none"; 1373 } 1374 else { 1375 for (int i = 0; i < natsize; i++) { 1376 if (i > 0) { 1377 result += "; "; 1378 } 1379 result += " " + getNaturalSelectors(false).get(i).getClass().getName(); 1380 } 1381 } 1382 return result; 1383 } 1384 1385 1393 public boolean isKeepPopulationSizeConstant() { 1394 return m_keepPopulationSizeConstant; 1395 } 1396 1397 1410 public void setKeepPopulationSizeConstant(boolean a_keepPopSizeConstant) { 1411 m_keepPopulationSizeConstant = a_keepPopSizeConstant; 1412 } 1413 1414 public IJGAPFactory getJGAPFactory() { 1415 return m_factory; 1416 } 1417 1418 class ConfigurationConfigurable 1419 implements Serializable { 1420 1423 int m_populationSize; 1424 } 1425 1436 protected static String getThreadKey(Thread current, String a_id) { 1437 return current.toString() + "|" + a_id + "|"; 1438 } 1439 1440 1446 public void setJGAPFactory(IJGAPFactory a_factory) { 1447 m_factory = a_factory; 1448 } 1449 1450 private void makeThreadKey() { 1451 Thread current = Thread.currentThread(); 1452 threadKey = getThreadKey(current, m_id); 1453 } 1454 1455 1466 private void readObject(ObjectInputStream a_inputStream) 1467 throws ClassNotFoundException , IOException { 1468 a_inputStream.defaultReadObject(); 1471 makeThreadKey(); 1472 } 1473 1474 1480 public String getId() { 1481 return m_id; 1482 } 1483 1484 1490 public Object clone() { 1491 return newInstance(m_id, m_name); 1492 } 1493 1494 1505 public Configuration newInstance(String a_id, String a_name) { 1506 try { 1507 Configuration result = new Configuration(m_name); 1508 if (m_factory instanceof ICloneable) { 1511 result.m_factory = (IJGAPFactory)((ICloneable)m_factory).clone(); 1512 } 1513 else { 1514 m_factory = new JGAPFactory(false); 1517 result.m_factory = (IJGAPFactory)((JGAPFactory)m_factory).clone(); 1518 } 1519 if (m_bulkObjectiveFunction != null) { 1520 result.m_bulkObjectiveFunction = m_bulkObjectiveFunction; 1521 } 1522 result.m_chromosomeSize = m_chromosomeSize; 1523 result.m_eventManager = (IEventManager)doClone(m_eventManager); 1526 result.m_fitnessEvaluator = (FitnessEvaluator)doClone(m_fitnessEvaluator); 1527 result.m_generationNr = 0; 1528 result.m_geneticOperators = (List)doClone(m_geneticOperators); 1529 result.m_keepPopulationSizeConstant = m_keepPopulationSizeConstant; 1530 result.m_minPercentageSizePopulation = m_minPercentageSizePopulation; 1531 result.m_objectiveFunction = (FitnessFunction)doClone(m_objectiveFunction); 1532 result.m_postSelectors = (ChainOfSelectors)doClone(m_postSelectors); 1533 result.m_preSelectors = (ChainOfSelectors)doClone(m_preSelectors); 1534 result.m_preserveFittestIndividual = m_preserveFittestIndividual; 1535 result.m_randomGenerator = (RandomGenerator)doClone(m_randomGenerator); 1536 result.m_sampleChromosome = (IChromosome)m_sampleChromosome.clone(); 1537 result.m_settingsLocked = m_settingsLocked; 1538 result.m_sizeNaturalSelectorsPost = m_sizeNaturalSelectorsPost; 1539 result.m_sizeNaturalSelectorsPre = m_sizeNaturalSelectorsPre; 1540 result.m_config = new ConfigurationConfigurable(); 1543 result.m_config.m_populationSize = m_config.m_populationSize; 1544 result.m_name = a_name; 1547 result.m_id = a_id; 1548 result.makeThreadKey(); return result; 1550 } catch (Throwable t) { 1551 throw new CloneException(t); 1552 } 1553 } 1554 1555 1565 protected Object doClone(Object a_objToClone) throws Exception { 1566 if (a_objToClone != null) { 1567 ICloneHandler handler = getJGAPFactory().getCloneHandlerFor( 1568 a_objToClone, null); 1569 if (handler != null) { 1570 return handler.perform(a_objToClone, null, null); 1571 } 1572 } 1573 return null; 1574 } 1575 1576 1585 public boolean equals(Object a_other) { 1586 return compareTo(a_other) == 0; 1587 } 1588 1589 1598 public int compareTo(Object a_other) { 1599 if (a_other == null) { 1600 return 1; 1601 } 1602 else { 1603 Configuration other = (Configuration) a_other; 1604 try { 1605 return new CompareToBuilder() 1606 .append(m_config.m_populationSize, other.m_config.m_populationSize) 1607 .append(m_factory, other.m_factory) 1608 .append(m_objectiveFunction, other.m_objectiveFunction) 1609 .append(m_fitnessEvaluator, other.m_fitnessEvaluator) 1610 .append(m_bulkObjectiveFunction, other.m_bulkObjectiveFunction) 1611 .append(m_sampleChromosome, other.m_sampleChromosome) 1612 .append(m_randomGenerator, other.m_randomGenerator) 1613 .append(m_geneticOperators.toArray(), other.m_geneticOperators.toArray()) 1616 .append(m_chromosomeSize, other.m_chromosomeSize) 1617 .append(m_preSelectors, other.m_preSelectors) 1618 .append(m_postSelectors, other.m_postSelectors) 1619 .append(m_sizeNaturalSelectorsPre, other.m_sizeNaturalSelectorsPre) 1620 .append(m_sizeNaturalSelectorsPost, 1621 other.m_sizeNaturalSelectorsPost) 1622 .append(m_preserveFittestIndividual, 1623 other.m_preserveFittestIndividual) 1624 .append(threadKey, other.threadKey) 1626 .append(m_keepPopulationSizeConstant, 1627 other.m_keepPopulationSizeConstant) 1628 .append(m_minPercentageSizePopulation, 1629 other.m_minPercentageSizePopulation) 1630 .append(m_generationNr, other.m_generationNr) 1631 .append(m_name, other.m_name) 1632 .append(m_settingsLocked, other.m_settingsLocked) 1633 .toComparison(); 1634 } catch (ClassCastException cex) { 1635 throw new RuntimeException ("Cannot compare all objects within" 1636 + " org.jgap.Configuration, because at" 1637 + " least one does not implement interface" 1638 + " java.lang.Comparable!"); 1639 } 1640 } 1641 } 1642 1643} 1644 | Popular Tags |