KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > audit > PermutingConfiguration


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.audit;
11
12 import org.jgap.*;
13 import java.util.*;
14
15 /**
16  * Configuration that allows for permutating several components of it for
17  * evaluation/auditing purposes.
18  *
19  * @author Klaus Meffert
20  * @since 2.2
21  */

22 public class PermutingConfiguration
23     extends Configuration {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc 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   /**
46    * The resulting configuration as determined by permutation.
47    */

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   /**
68    * Initializes the configuration by preselecting important parameters from
69    * the input configuration object
70    * @param a_conf Configuration
71    * @throws InvalidConfigurationException
72    *
73    * @author Klaus Meffert
74    * @since 2.2
75    */

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 JavaDoc(
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 JavaDoc(
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     /**@todo make permutation below computed dynamic and not static*/
146     /**@todo introduce new parameters: populationSize,
147      * setPreservFittestIndividual, MAX_ALLOWED_EVOLUTIONS
148      */

149
150     // Permute GeneticOperator's.
151
// --------------------------
152
if (m_geneticOperatorIndex >=
153         Math.pow(2, m_geneticOperatorSlots.size()) - 1) {
154 // m_componentIndex++;
155
// }
156
// if (bitSet(m_componentIndex, 0)) {
157
m_geneticOperatorIndex = 0;
158       m_naturalSelectorIndex++;
159 // m_randomGeneratorIndex = 0;
160
// m_fitnessFunctionIndex = 0;
161
}
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     // Permute NaturalSelector's.
170
// --------------------------
171
if (m_naturalSelectorIndex >=
172         Math.pow(2, m_naturalSelectorSlots.size()) - 1) {
173 // m_componentIndex++;
174
// }
175
// if (bitSet(m_componentIndex, 1)) {
176
m_naturalSelectorIndex = 0;
177       m_randomGeneratorIndex++;
178 // m_fitnessFunctionIndex = 0;
179
}
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       /**@todo allow for "false"*/
187     }
188     // Permute RandomGenerator's.
189
// --------------------------
190
// if (true || bitSet(m_componentIndex, 2)) {
191
m_randomGeneratorIndex++;
192     if (m_randomGeneratorIndex >= m_randomGeneratorSlots.size()) {
193       m_randomGeneratorIndex = 0;
194       m_fitnessFunctionIndex++;
195     }
196 // }
197
RandomGenerator rg = (RandomGenerator) m_randomGeneratorSlots.get(
198         m_randomGeneratorIndex);
199     m_configuration.setRandomGenerator(rg);
200     // Permute FitnessFunction's.
201
// --------------------------
202
// if (true || bitSet(m_componentIndex, 3)) {
203
m_fitnessFunctionIndex++;
204     if (m_fitnessFunctionIndex >= m_fitnessFunctionSlots.size()) {
205       m_fitnessFunctionIndex = 0;
206     }
207 // }
208
/**@todo BulkFitnessOffsetRemover vs. FitnessFunction*/
209
210 // System.err.println(m_fitnessFunctionIndex+" / "+index++);
211
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   /**
220    * Returns a subset of a given list acording to the index given.
221    * If a bit in the binary number represented by the index is set then the
222    * element at this index in the list will be included in the result list
223    * @param index int
224    * @param list List
225    * @return List
226    */

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 // private boolean bitSet(int number, int bitIndex) {
244
// return ( (number & (int) Math.pow(2, (bitIndex))) > 0);
245
// }
246

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