Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/gp/GPInitializer.java @ 10216

Last change on this file since 10216 was 6152, checked in by bfarka, 14 years ago

added ecj and custom statistics to communicate with the okb services #1441

File size: 10.9 KB
Line 
1/*
2  Copyright 2006 by Sean Luke
3  Licensed under the Academic Free License version 3.0
4  See the file "LICENSE" for more information
5*/
6
7
8package ec.gp;
9import java.util.Enumeration;
10import java.util.Hashtable;
11
12import ec.simple.SimpleInitializer;
13import ec.util.Parameter;
14import ec.EvolutionState;
15
16/*
17 * GPInitializer.java
18 *
19 * Created: Tue Oct  5 18:40:02 1999
20 * By: Sean Luke
21 */
22
23/**
24 * GPInitializer is a SimpleInitializer which sets up all the Cliques,
25 * ( the initial
26 * [tree/node]constraints, types, and function sets) for the GP system.
27 *
28 * <p>Note that the Cliques must be set up in a very particular order:
29
30 <ol><li>GPType</li><li>GPNodeConstraints</li><li>GPFunctionSets</li><li>GPTreeConstraints</li></ol>
31
32 <p><b>Parameter bases</b><br>
33 <table>
34 <tr><td valign=top><tt>gp.type</tt></td>
35 <td>GPTypes</td></tr>
36 <tr><td valign=top><tt>gp.nc</tt></td>
37 <td>GPNodeConstraints</td></tr>
38 <tr><td valign=top><tt>gp.tc</tt></td>
39 <td>GPTreeConstraints</td></tr>
40 <tr><td valign=top><tt>gp.fs</tt></td>
41 <td>GPFunctionSets</td></tr>
42
43 </table>
44
45 * @author Sean Luke
46 * @version 1.0
47 */
48
49public class GPInitializer extends SimpleInitializer
50    {
51    // used just here, so far as I know :-)
52    public static final int SIZE_OF_BYTE = 256;
53    public final static String P_TYPE = "type";
54    public final static String P_NODECONSTRAINTS = "nc";
55    public final static String P_TREECONSTRAINTS = "tc";
56    public final static String P_FUNCTIONSETS = "fs";
57    public final static String P_SIZE = "size";
58    public final static String P_ATOMIC = "a";
59    public final static String P_SET = "s";
60   
61    /**
62     * TODO Comment these members.
63     * TODO Make clients of these members more efficient by reducing unnecessary casting.
64     */
65    public Hashtable typeRepository;
66    public GPType[] types;
67    public int numAtomicTypes;
68    public int numSetTypes;
69   
70    public Hashtable nodeConstraintRepository;
71    public GPNodeConstraints[] nodeConstraints;
72    public byte numNodeConstraints;
73   
74    public Hashtable functionSetRepository;
75
76    public Hashtable treeConstraintRepository;
77    public GPTreeConstraints[] treeConstraints;
78    public byte numTreeConstraints;
79   
80    public void setup(final EvolutionState state, final Parameter base)
81        {
82        super.setup(state,base);
83
84        /**
85         * TODO Move setup methods to the corresponding GP type.
86         */
87        // This is a good place to set up the types.  We use our own base off the
88        // default GP base.  This MUST be done before loading constraints.
89        setupTypes(state,GPDefaults.base().push(P_TYPE));
90
91        // Now let's load our constraints and function sets also.
92        // This is done in a very specific order, don't change it or things
93        // will break.
94        setupNodeConstraints(
95            state,GPDefaults.base().push(P_NODECONSTRAINTS));
96        setupFunctionSets(
97            state,GPDefaults.base().push(P_FUNCTIONSETS));
98        setupTreeConstraints(
99            state,GPDefaults.base().push(P_TREECONSTRAINTS));
100        }
101
102    /** Sets up all the types, loading them from the parameter file.  This
103        must be called before anything is called which refers to a type by
104        name. */
105
106    public void setupTypes(final EvolutionState state,
107        final Parameter base)
108        {
109        state.output.message("Processing GP Types");
110       
111        typeRepository = new Hashtable();
112        numAtomicTypes = numSetTypes = 0;
113       
114        // How many atomic types do we have?
115        int x = state.parameters.getInt(base.push(P_ATOMIC).push(P_SIZE),null,1);
116        if (x<=0)
117            state.output.fatal("The number of GP atomic types must be at least 1.",base.push(P_ATOMIC).push(P_SIZE));
118       
119        // Load our atomic types
120       
121        for(int y=0;y<x;y++)
122            new GPAtomicType().setup(state,base.push(P_ATOMIC).push(""+y));
123       
124        // How many set types do we have?
125        if (state.parameters.exists(base.push(P_SET).push(P_SIZE), null))
126            {
127            x =  state.parameters.getInt(base.push(P_SET).push(P_SIZE),null,1);
128            if (x<0)
129                state.output.fatal("The number of GP set types must be at least 0.",base.push(P_SET).push(P_SIZE));
130            }
131        else // no set types
132            x = 0;
133       
134        // Load our set types
135       
136        for(int y=0;y<x;y++)
137            new GPSetType().setup(state,base.push(P_SET).push(""+y));
138       
139        // Postprocess the types
140        postProcessTypes();
141        }
142   
143    /** Assigns unique integers to each atomic type, and sets up compatibility
144        arrays for set types.  If you add new types (heaven forbid), you
145        should call this method again to get all the types set up properly.
146        However, you will have to set up the function sets again as well,
147        as their arrays are based on these type numbers. */
148    public void postProcessTypes()
149        {
150        // assign positive integers and 0 to atomic types
151        int x = 0;
152        Enumeration e = typeRepository.elements();
153        while(e.hasMoreElements())
154            {
155            GPType t = (GPType)(e.nextElement());
156            if (t instanceof GPAtomicType)
157                { t.type = x; x++; }
158            }
159       
160        // at this point, x holds the number of atomic types.
161        numAtomicTypes = x;
162       
163        // assign additional positive integers to set types
164        // and set up arrays for the set types
165        e = typeRepository.elements();
166        while(e.hasMoreElements())
167            {
168            GPType t = (GPType)(e.nextElement());
169            if (t instanceof GPSetType)
170                {
171                ((GPSetType)t).postProcessSetType(numAtomicTypes);
172                t.type = x; x++;
173                }
174            }
175       
176        // at this point, x holds the number of set types + atomic types
177        numSetTypes = x - numAtomicTypes;
178       
179        // make an array for convenience.  Presently rarely used.
180        types = new GPType[numSetTypes + numAtomicTypes];
181        e = typeRepository.elements();
182        while(e.hasMoreElements())
183            {
184            GPType t = (GPType)(e.nextElement());
185            types[t.type] = t;
186            }
187        }
188   
189   
190    /** Sets up all the GPNodeConstraints, loading them from the parameter
191        file.  This must be called before anything is called which refers
192        to a type by name. */
193   
194    public void setupNodeConstraints(
195        final EvolutionState state,
196        final Parameter base)
197        {
198        state.output.message("Processing GP Node Constraints");
199       
200        nodeConstraintRepository = new Hashtable();
201        nodeConstraints = new GPNodeConstraints[SIZE_OF_BYTE];
202        numNodeConstraints = 0;
203       
204        // How many GPNodeConstraints do we have?
205        int x = state.parameters.getInt(base.push(P_SIZE),null,1);
206        if (x<=0)
207            state.output.fatal("The number of GP node constraints must be at least 1.",
208                base.push(P_SIZE));
209       
210        // Load our constraints
211        for (int y=0;y<x;y++)
212            {
213            GPNodeConstraints c;
214            // Figure the constraint class
215            if (state.parameters.exists(base.push(""+y), null))
216                c = (GPNodeConstraints)(state.parameters.getInstanceForParameterEq(
217                        base.push(""+y),null,GPNodeConstraints.class));
218            else
219                {
220                state.output.message("No GP Node Constraints specified, assuming the default class: ec.gp.GPNodeConstraints for " +  base.push(""+y));
221                c = new GPNodeConstraints();
222                }
223            c.setup(state,base.push(""+y));
224            }
225       
226        // set our constraints array up
227        Enumeration e = nodeConstraintRepository.elements();
228        while(e.hasMoreElements())
229            {
230            GPNodeConstraints c = (GPNodeConstraints)(e.nextElement());
231            c.constraintNumber = numNodeConstraints;
232            nodeConstraints[numNodeConstraints] = c;
233            numNodeConstraints++;
234            }
235        }
236   
237   
238    public void setupFunctionSets(final EvolutionState state,
239        final Parameter base)
240        {
241        state.output.message("Processing GP Function Sets");
242       
243        functionSetRepository = new Hashtable();
244        // How many GPFunctionSets do we have?
245        int x = state.parameters.getInt(base.push(P_SIZE),null,1);
246        if (x<=0)
247            state.output.fatal("The number of GPFunctionSets must be at least 1.",base.push(P_SIZE));
248       
249        // Load our FunctionSet
250        for (int y=0;y<x;y++)
251            {
252            GPFunctionSet c;
253            // Figure the GPFunctionSet class
254            if (state.parameters.exists(base.push(""+y), null))
255                c = (GPFunctionSet)(state.parameters.getInstanceForParameterEq(
256                        base.push(""+y),null,GPFunctionSet.class));
257            else
258                {
259                state.output.message("No GPFunctionSet specified, assuming the default class: ec.gp.GPFunctionSet for " + base.push(""+y));
260                c = new GPFunctionSet();
261                }
262            c.setup(state,base.push(""+y));
263            }
264        }
265       
266
267    /** Sets up all the GPTreeConstraints, loading them from the parameter
268        file.  This must be called before anything is called which refers
269        to a type by name. */
270       
271    public void setupTreeConstraints(
272        final EvolutionState state,
273        final Parameter base)
274        {
275        state.output.message("Processing GP Tree Constraints");
276           
277        treeConstraintRepository = new Hashtable();
278        treeConstraints = new GPTreeConstraints[SIZE_OF_BYTE];
279        numTreeConstraints = 0;
280        // How many GPTreeConstraints do we have?
281        int x = state.parameters.getInt(base.push(P_SIZE),null,1);
282        if (x<=0)
283            state.output.fatal("The number of GP tree constraints must be at least 1.",base.push(P_SIZE));
284           
285        // Load our constraints
286        for (int y=0;y<x;y++)
287            {
288            GPTreeConstraints c;
289            // Figure the constraint class
290            if (state.parameters.exists(base.push(""+y), null))
291                c = (GPTreeConstraints)(state.parameters.getInstanceForParameterEq(
292                        base.push(""+y),null,GPTreeConstraints.class));
293            else
294                {
295                state.output.message("No GP Tree Constraints specified, assuming the default class: ec.gp.GPTreeConstraints for " + base.push(""+y));
296                c = new GPTreeConstraints();
297                }
298            c.setup(state,base.push(""+y));
299            }
300           
301        // set our constraints array up
302        Enumeration e = treeConstraintRepository.elements();
303        while(e.hasMoreElements())
304            {
305            GPTreeConstraints c = (GPTreeConstraints)(e.nextElement());
306            c.constraintNumber = numTreeConstraints;
307            treeConstraints[numTreeConstraints] = c;
308            numTreeConstraints++;
309            }
310        }
311    }
Note: See TracBrowser for help on using the repository browser.