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 | |
---|
8 | package ec.gp; |
---|
9 | import java.io.*; |
---|
10 | import ec.*; |
---|
11 | import ec.util.*; |
---|
12 | import java.util.*; |
---|
13 | |
---|
14 | /* |
---|
15 | * GPFunctionSet.java |
---|
16 | * |
---|
17 | * Created: Wed Oct 13 22:35:06 1999 |
---|
18 | * By: Sean Luke |
---|
19 | */ |
---|
20 | |
---|
21 | /** |
---|
22 | * GPFunctionSet is a Clique which represents a set of GPNode prototypes |
---|
23 | * forming a standard function set for forming certain trees in individuals. |
---|
24 | * GPFunctionSets instances have unique names with which they're referenced by |
---|
25 | * GPTreeConstraints objects indicating that they're used for certain trees. |
---|
26 | * GPFunctionSets store their GPNode Prototypes in three hashtables, |
---|
27 | * one for all nodes, one for nonterminals, and one for terminals. Each |
---|
28 | * hashed item is an array of GPNode objects, |
---|
29 | * hashed by the return type of the GPNodes in the array. |
---|
30 | * |
---|
31 | * GPFunctionSets also contain prototypical GPNode nodes which they |
---|
32 | * clone to form their arrays. |
---|
33 | |
---|
34 | <p><b>Parameters</b><br> |
---|
35 | <table> |
---|
36 | <tr><td valign=top><i>base</i>.<tt>name</tt><br> |
---|
37 | <font size=-1>String</font></td> |
---|
38 | <td valign=top>(name of function set. Must be different from other function set instances)</td></tr> |
---|
39 | |
---|
40 | <tr><td valign=top><i>base</i>.<tt>size</tt><br> |
---|
41 | <font size=-1>int >= 1</font></td> |
---|
42 | <td valign=top>(number of functions in the function set)</td></tr> |
---|
43 | |
---|
44 | <tr><td valign=top><i>base</i>.<tt>func.</tt><i>n</i><br> |
---|
45 | <font size=-1>classname, inherits and != ec.gp.GPNode</font></td> |
---|
46 | <td valign=top>(class of function node <i>n</i> in the set)</td></tr> |
---|
47 | |
---|
48 | </table> |
---|
49 | |
---|
50 | <p><b>Parameter bases</b><br> |
---|
51 | <table> |
---|
52 | <tr><td valign=top><i>base</i>.<tt>func.</tt><i>n</i></td> |
---|
53 | <td>function node <i>n</i></td></tr> |
---|
54 | </table> |
---|
55 | |
---|
56 | * |
---|
57 | * @author Sean Luke |
---|
58 | * @version 1.0 |
---|
59 | */ |
---|
60 | |
---|
61 | public class GPFunctionSet implements Clique |
---|
62 | { |
---|
63 | public final static String P_NAME = "name"; |
---|
64 | public final static String P_FUNC = "func"; |
---|
65 | public final static String P_SIZE = "size"; |
---|
66 | |
---|
67 | /** Name of the GPFunctionSet */ |
---|
68 | public String name; |
---|
69 | |
---|
70 | /** The nodes that our GPTree can use: arrays of nodes hashed by type. */ |
---|
71 | public Hashtable nodes_h; |
---|
72 | /** The nodes that our GPTree can use: nodes[type][thenodes]. */ |
---|
73 | public GPNode[][] nodes; |
---|
74 | /** The nonterminals our GPTree can use: arrays of nonterminals hashed by type. */ |
---|
75 | public Hashtable nonterminals_h; |
---|
76 | /** The nonterminals our GPTree can use: nonterminals[type][thenodes]. */ |
---|
77 | public GPNode[][] nonterminals; |
---|
78 | /** The terminals our GPTree can use: arrays of terminals hashed by type. */ |
---|
79 | public Hashtable terminals_h; |
---|
80 | /** The terminals our GPTree can use: terminals[type][thenodes]. */ |
---|
81 | public GPNode[][] terminals; |
---|
82 | |
---|
83 | |
---|
84 | // some convenience methods which speed up various kinds |
---|
85 | // of mutation operators |
---|
86 | |
---|
87 | /** The nodes that our GPTree can use, hashed by name(). */ |
---|
88 | public Hashtable nodesByName; |
---|
89 | |
---|
90 | /** Nodes == a given arity, that is: nodesByArity[type][arity][thenodes] */ |
---|
91 | public GPNode[][][]nodesByArity; |
---|
92 | |
---|
93 | /** Nonterminals <= a given arity, that is: nonterminalsUnderArity[type][arity][thenodes] -- |
---|
94 | this will be O(n^2). Obviously, the number of nonterminals at arity slot 0 is 0.*/ |
---|
95 | public GPNode[][][]nonterminalsUnderArity; |
---|
96 | |
---|
97 | /** Nonterminals >= a given arity, that is: nonterminalsOverArity[type][arity][thenodes] -- |
---|
98 | this will be O(n^2). Obviously, the number of nonterminals at arity slot 0 is all the |
---|
99 | nonterminals of that type. */ |
---|
100 | public GPNode[][][]nonterminalsOverArity; |
---|
101 | |
---|
102 | /** Returns the name. */ |
---|
103 | public String toString() { return name; } |
---|
104 | |
---|
105 | |
---|
106 | /** Sets up all the GPFunctionSet, loading them from the parameter |
---|
107 | file. This must be called before anything is called which refers |
---|
108 | to a type by name. */ |
---|
109 | |
---|
110 | /** Sets up the arrays based on the hashtables */ |
---|
111 | |
---|
112 | public void postProcessFunctionSet() |
---|
113 | { |
---|
114 | nodes = new GPNode[nodes_h.size()][]; |
---|
115 | terminals = new GPNode[terminals_h.size()][]; |
---|
116 | nonterminals = new GPNode[nonterminals_h.size()][]; |
---|
117 | |
---|
118 | Enumeration e = nodes_h.keys(); |
---|
119 | while(e.hasMoreElements()) |
---|
120 | { |
---|
121 | GPType gpt = (GPType)(e.nextElement()); |
---|
122 | GPNode[] gpfi = (GPNode[])(nodes_h.get(gpt)); |
---|
123 | nodes[gpt.type] = gpfi; |
---|
124 | } |
---|
125 | e = nonterminals_h.keys(); |
---|
126 | while(e.hasMoreElements()) |
---|
127 | { |
---|
128 | GPType gpt = (GPType)(e.nextElement()); |
---|
129 | GPNode[] gpfi = (GPNode[])(nonterminals_h.get(gpt)); |
---|
130 | nonterminals[gpt.type] = gpfi; |
---|
131 | } |
---|
132 | e = terminals_h.keys(); |
---|
133 | while(e.hasMoreElements()) |
---|
134 | { |
---|
135 | GPType gpt = (GPType)(e.nextElement()); |
---|
136 | GPNode[] gpfi = (GPNode[])(terminals_h.get(gpt)); |
---|
137 | terminals[gpt.type] = gpfi; |
---|
138 | } |
---|
139 | |
---|
140 | // set up arity-based arrays |
---|
141 | // first, determine the maximum arity |
---|
142 | int max_arity=0; |
---|
143 | for(int x=0;x<nodes.length;x++) |
---|
144 | for(int y=0;y<nodes[x].length;y++) |
---|
145 | if (max_arity < nodes[x][y].children.length) |
---|
146 | max_arity = nodes[x][y].children.length; |
---|
147 | |
---|
148 | // next set up the == array |
---|
149 | nodesByArity = new GPNode[nodes.length][max_arity+1][]; |
---|
150 | for(int x=0;x<nodes.length;x++) |
---|
151 | for(int a = 0; a <= max_arity; a++) |
---|
152 | { |
---|
153 | // how many nodes do we have? |
---|
154 | int num_of_a = 0; |
---|
155 | for(int y=0;y<nodes[x].length;y++) |
---|
156 | if (nodes[x][y].children.length == a) num_of_a++; |
---|
157 | // allocate and fill |
---|
158 | nodesByArity[x][a] = new GPNode[num_of_a]; |
---|
159 | int cur_a = 0; |
---|
160 | for(int y=0;y<nodes[x].length;y++) |
---|
161 | if (nodes[x][y].children.length == a ) |
---|
162 | nodesByArity[x][a][cur_a++] = nodes[x][y]; |
---|
163 | } |
---|
164 | |
---|
165 | // now set up the <= nonterminals array |
---|
166 | nonterminalsUnderArity = new GPNode[nonterminals.length][max_arity+1][]; |
---|
167 | for(int x=0;x<nonterminals.length;x++) |
---|
168 | for (int a = 0;a <= max_arity; a++) |
---|
169 | { |
---|
170 | // how many nonterminals do we have? |
---|
171 | int num_of_a = 0; |
---|
172 | for(int y=0;y<nonterminals[x].length;y++) |
---|
173 | if (nonterminals[x][y].children.length <= a) num_of_a++; |
---|
174 | // allocate and fill |
---|
175 | nonterminalsUnderArity[x][a] = new GPNode[num_of_a]; |
---|
176 | int cur_a = 0; |
---|
177 | for(int y=0;y<nonterminals[x].length;y++) |
---|
178 | if (nonterminals[x][y].children.length <= a ) |
---|
179 | nonterminalsUnderArity[x][a][cur_a++] = nonterminals[x][y]; |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | // now set up the >= nonterminals array |
---|
185 | nonterminalsOverArity = new GPNode[nonterminals.length][max_arity+1][]; |
---|
186 | for(int x=0;x<nonterminals.length;x++) |
---|
187 | for (int a = 0;a <= max_arity; a++) |
---|
188 | { |
---|
189 | // how many nonterminals do we have? |
---|
190 | int num_of_a = 0; |
---|
191 | for(int y=0;y<nonterminals[x].length;y++) |
---|
192 | if (nonterminals[x][y].children.length >= a) num_of_a++; |
---|
193 | // allocate and fill |
---|
194 | nonterminalsOverArity[x][a] = new GPNode[num_of_a]; |
---|
195 | int cur_a = 0; |
---|
196 | for(int y=0;y<nonterminals[x].length;y++) |
---|
197 | if (nonterminals[x][y].children.length >= a ) |
---|
198 | nonterminalsOverArity[x][a][cur_a++] = nonterminals[x][y]; |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | |
---|
203 | |
---|
204 | |
---|
205 | /** Must be done <i>after</i> GPType and GPNodeConstraints have been set up */ |
---|
206 | |
---|
207 | public void setup(final EvolutionState state, final Parameter base) |
---|
208 | { |
---|
209 | // What's my name? |
---|
210 | name = state.parameters.getString(base.push(P_NAME),null); |
---|
211 | if (name==null) |
---|
212 | state.output.fatal("No name was given for this function set.", |
---|
213 | base.push(P_NAME)); |
---|
214 | // Register me |
---|
215 | GPFunctionSet old_functionset = (GPFunctionSet)(((GPInitializer)state.initializer).functionSetRepository.put(name,this)); |
---|
216 | if (old_functionset != null) |
---|
217 | state.output.fatal("The GPFunctionSet \"" + name + "\" has been defined multiple times.", base.push(P_NAME)); |
---|
218 | |
---|
219 | // How many functions do I have? |
---|
220 | int numFuncs = state.parameters.getInt(base.push(P_SIZE),null,1); |
---|
221 | if (numFuncs < 1) |
---|
222 | state.output.error("The GPFunctionSet \"" + name + "\" has no functions.", |
---|
223 | base.push(P_SIZE)); |
---|
224 | |
---|
225 | nodesByName = new Hashtable(); |
---|
226 | |
---|
227 | Parameter p = base.push(P_FUNC); |
---|
228 | Vector tmp = new Vector(); |
---|
229 | for(int x = 0; x < numFuncs; x++) |
---|
230 | { |
---|
231 | // load |
---|
232 | Parameter pp = p.push(""+x); |
---|
233 | GPNode gpfi = (GPNode)(state.parameters.getInstanceForParameter( |
---|
234 | pp, null, GPNode.class)); |
---|
235 | gpfi.setup(state,pp); |
---|
236 | |
---|
237 | // add to my collection |
---|
238 | tmp.addElement(gpfi); |
---|
239 | |
---|
240 | // Load into the nodesByName hashtable |
---|
241 | GPNode[] nodes = (GPNode[])(nodesByName.get(gpfi.name())); |
---|
242 | if (nodes == null) |
---|
243 | nodesByName.put(gpfi.name(), new GPNode[] { gpfi }); |
---|
244 | else |
---|
245 | { |
---|
246 | // O(n^2) but uncommon so what the heck. |
---|
247 | GPNode[] nodes2 = new GPNode[nodes.length + 1]; |
---|
248 | System.arraycopy(nodes, 0, nodes2, 0, nodes.length); |
---|
249 | nodes2[nodes2.length - 1] = gpfi; |
---|
250 | nodesByName.put(gpfi.name(), nodes2); |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | // Make my hash tables |
---|
255 | nodes_h = new Hashtable(); |
---|
256 | terminals_h = new Hashtable(); |
---|
257 | nonterminals_h = new Hashtable(); |
---|
258 | |
---|
259 | // Now set 'em up according to the types in GPType |
---|
260 | |
---|
261 | Enumeration e = ((GPInitializer)state.initializer).typeRepository.elements(); |
---|
262 | GPInitializer initializer = ((GPInitializer)state.initializer); |
---|
263 | while(e.hasMoreElements()) |
---|
264 | { |
---|
265 | GPType typ = (GPType)(e.nextElement()); |
---|
266 | |
---|
267 | // make vectors for the type. |
---|
268 | Vector nodes_v = new Vector(); |
---|
269 | Vector terminals_v = new Vector(); |
---|
270 | Vector nonterminals_v = new Vector(); |
---|
271 | |
---|
272 | // add GPNodes as appropriate to each vector |
---|
273 | Enumeration v = tmp.elements(); |
---|
274 | while (v.hasMoreElements()) |
---|
275 | { |
---|
276 | GPNode i = (GPNode)(v.nextElement()); |
---|
277 | if (typ.compatibleWith(initializer,i.constraints(initializer).returntype)) |
---|
278 | { |
---|
279 | nodes_v.addElement(i); |
---|
280 | if (i.children.length == 0) |
---|
281 | terminals_v.addElement(i); |
---|
282 | else nonterminals_v.addElement(i); |
---|
283 | } |
---|
284 | } |
---|
285 | |
---|
286 | // turn nodes_h' vectors into arrays |
---|
287 | GPNode[] ii = new GPNode[nodes_v.size()]; |
---|
288 | nodes_v.copyInto(ii); |
---|
289 | nodes_h.put(typ,ii); |
---|
290 | |
---|
291 | // turn terminals_h' vectors into arrays |
---|
292 | ii = new GPNode[terminals_v.size()]; |
---|
293 | terminals_v.copyInto(ii); |
---|
294 | terminals_h.put(typ,ii); |
---|
295 | |
---|
296 | // turn nonterminals_h' vectors into arrays |
---|
297 | ii = new GPNode[nonterminals_v.size()]; |
---|
298 | nonterminals_v.copyInto(ii); |
---|
299 | nonterminals_h.put(typ,ii); |
---|
300 | } |
---|
301 | |
---|
302 | // I don't check to see if the generation mechanism will be valid here |
---|
303 | // -- I check that in GPTreeConstraints, where I can do the weaker check |
---|
304 | // of going top-down through functions rather than making sure that every |
---|
305 | // single function has a compatible argument function (an unneccessary check) |
---|
306 | |
---|
307 | state.output.exitIfErrors(); // because I promised when I called n.setup(...) |
---|
308 | |
---|
309 | // postprocess the function set |
---|
310 | postProcessFunctionSet(); |
---|
311 | } |
---|
312 | |
---|
313 | |
---|
314 | /** Returns the function set for a given name. |
---|
315 | You must guarantee that after calling functionSetFor(...) one or |
---|
316 | several times, you call state.output.exitIfErrors() once. */ |
---|
317 | |
---|
318 | public static GPFunctionSet functionSetFor(final String functionSetName, |
---|
319 | final EvolutionState state) |
---|
320 | { |
---|
321 | GPFunctionSet set = (GPFunctionSet)(((GPInitializer)state.initializer).functionSetRepository.get(functionSetName)); |
---|
322 | if (set==null) |
---|
323 | state.output.error("The GP function set \"" + functionSetName + "\" could not be found."); |
---|
324 | return set; |
---|
325 | } |
---|
326 | |
---|
327 | |
---|
328 | private void writeObject(ObjectOutputStream out) throws IOException |
---|
329 | { |
---|
330 | // this wastes an hashtable pointer, but what the heck. |
---|
331 | |
---|
332 | out.defaultWriteObject(); |
---|
333 | } |
---|
334 | |
---|
335 | private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException |
---|
336 | { |
---|
337 | in.defaultReadObject(); |
---|
338 | } |
---|
339 | } |
---|