1 10 package org.jgap.audit; 11 12 import org.jgap.*; 13 import java.util.*; 14 15 22 public class PermutingConfiguration 23 extends Configuration { 24 25 private final static String CVS_REVISION = "$Revision: 1.8 $"; 26 27 private List m_randomGeneratorSlots; 28 29 private int m_randomGeneratorIndex; 30 31 private List m_naturalSelectorSlots; 32 33 private int m_naturalSelectorIndex; 34 35 private List m_geneticOperatorSlots; 36 37 private int m_geneticOperatorIndex; 38 39 private List m_fitnessFunctionSlots; 40 41 private int m_fitnessFunctionIndex; 42 43 private int m_componentIndex; 44 45 48 private Configuration m_configuration; 49 50 public PermutingConfiguration() { 51 super(); 52 init(); 53 } 54 55 public void init() { 56 m_randomGeneratorSlots = new Vector(); 57 m_naturalSelectorSlots = new Vector(); 58 m_geneticOperatorSlots = new Vector(); 59 m_fitnessFunctionSlots = new Vector(); 60 m_randomGeneratorIndex = 0; 61 m_naturalSelectorIndex = 0; 62 m_geneticOperatorIndex = 0; 63 m_fitnessFunctionIndex = 0; 64 m_componentIndex = 0; 65 } 66 67 76 public PermutingConfiguration(Configuration a_conf) 77 throws InvalidConfigurationException { 78 this(); 79 setEventManager(a_conf.getEventManager()); 80 setFitnessEvaluator(a_conf.getFitnessEvaluator()); 81 if (a_conf.getFitnessFunction() != null) { 82 setFitnessFunction(a_conf.getFitnessFunction()); 83 } 84 setMinimumPopSizePercent(a_conf.getMinimumPopSizePercent()); 85 if (a_conf.getPopulationSize() > 0) { 86 setPopulationSize(a_conf.getPopulationSize()); 87 } 88 if (a_conf.getSampleChromosome() != null) { 89 setSampleChromosome(a_conf.getSampleChromosome()); 90 } 91 setRandomGenerator(a_conf.getRandomGenerator()); 92 if (a_conf.getChromosomePool() != null) { 93 setChromosomePool(a_conf.getChromosomePool()); 94 } 95 } 96 97 public void addRandomGeneratorSlot(RandomGenerator a_randomGenerator) { 98 m_randomGeneratorSlots.add(a_randomGenerator); 99 } 100 101 public void addNaturalSelector(NaturalSelector a_naturalSel, boolean a_egal) { 102 throw new UnsupportedOperationException ( 103 "Use addNaturalSelectorSlot instead!"); 104 } 105 106 public void addNaturalSelectorSlot(NaturalSelector a_naturalSelector) { 107 m_naturalSelectorSlots.add(a_naturalSelector); 108 } 109 110 public synchronized void addGeneticOperator(GeneticOperator a_geneticOp) { 111 throw new UnsupportedOperationException ( 112 "Use addGeneticOperatorSlot instead!"); 113 } 114 115 public void addGeneticOperatorSlot(GeneticOperator a_geneticOperator) { 116 m_geneticOperatorSlots.add(a_geneticOperator); 117 } 118 119 public void addFitnessFunctionSlot(FitnessFunction a_fitnessFunction) { 120 m_fitnessFunctionSlots.add(a_fitnessFunction); 121 } 122 123 public Configuration next() 124 throws InvalidConfigurationException { 125 m_configuration = new Configuration(); 126 m_configuration.setEventManager(getEventManager()); 127 m_configuration.setFitnessEvaluator(getFitnessEvaluator()); 128 if (getFitnessFunction() != null) { 129 m_configuration.resetProperty(Configuration.PROPERTY_FITFUNC_INST); 130 setFitnessFunction(getFitnessFunction()); 131 } 132 m_configuration.setMinimumPopSizePercent(getMinimumPopSizePercent()); 133 if (getPopulationSize() > 0) { 134 m_configuration.setPopulationSize(getPopulationSize()); 135 } 136 if (getSampleChromosome() != null) { 137 m_configuration.setSampleChromosome(getSampleChromosome()); 138 } 139 m_configuration.setRandomGenerator(getRandomGenerator()); 140 if (getChromosomePool() != null) { 141 m_configuration.setChromosomePool(getChromosomePool()); 142 } 143 List list; 144 Iterator it; 145 146 149 150 if (m_geneticOperatorIndex >= 153 Math.pow(2, m_geneticOperatorSlots.size()) - 1) { 154 m_geneticOperatorIndex = 0; 158 m_naturalSelectorIndex++; 159 } 162 list = nextList(m_geneticOperatorIndex++, m_geneticOperatorSlots); 163 it = list.iterator(); 164 GeneticOperator op; 165 while (it.hasNext()) { 166 op = (GeneticOperator) it.next(); 167 m_configuration.addGeneticOperator(op); 168 } 169 if (m_naturalSelectorIndex >= 172 Math.pow(2, m_naturalSelectorSlots.size()) - 1) { 173 m_naturalSelectorIndex = 0; 177 m_randomGeneratorIndex++; 178 } 180 list = nextList(m_naturalSelectorIndex, m_naturalSelectorSlots); 181 it = list.iterator(); 182 NaturalSelector ns; 183 while (it.hasNext()) { 184 ns = (NaturalSelector) it.next(); 185 m_configuration.addNaturalSelector(ns, true); 186 187 } 188 m_randomGeneratorIndex++; 192 if (m_randomGeneratorIndex >= m_randomGeneratorSlots.size()) { 193 m_randomGeneratorIndex = 0; 194 m_fitnessFunctionIndex++; 195 } 196 RandomGenerator rg = (RandomGenerator) m_randomGeneratorSlots.get( 198 m_randomGeneratorIndex); 199 m_configuration.setRandomGenerator(rg); 200 m_fitnessFunctionIndex++; 204 if (m_fitnessFunctionIndex >= m_fitnessFunctionSlots.size()) { 205 m_fitnessFunctionIndex = 0; 206 } 207 209 210 FitnessFunction ff = (FitnessFunction) m_fitnessFunctionSlots.get( 212 m_fitnessFunctionIndex); 213 m_configuration.reset(); 214 m_configuration.setFitnessFunction(ff); 215 m_componentIndex++; 216 return m_configuration; 217 } 218 219 227 private List nextList(int index, List list) { 228 if (index <= 0) { 229 index = 1; 230 } 231 else { 232 index++; 233 } 234 List newList = new Vector(); 235 for (int i = 0; i < list.size(); i++) { 236 if ( (index & (int) Math.pow(2, i)) > 0) { 237 newList.add(list.get(i)); 238 } 239 } 240 return newList; 241 } 242 243 247 public boolean hasNext() { 248 double r = (m_randomGeneratorSlots.size()) 249 * (m_fitnessFunctionSlots.size()) 250 * (Math.pow(2, m_naturalSelectorSlots.size()) - 1) 251 * (Math.pow(2, m_geneticOperatorSlots.size()) - 1); 252 return m_componentIndex < r; 253 } 254 } 255 | Popular Tags |