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 ec.*; |
---|
10 | import ec.util.*; |
---|
11 | |
---|
12 | /* |
---|
13 | * ADFContext.java |
---|
14 | * |
---|
15 | * Created: Tue Oct 26 13:36:46 1999 |
---|
16 | * By: Sean Luke |
---|
17 | */ |
---|
18 | |
---|
19 | /** |
---|
20 | * ADFContext is the object pushed onto an ADF stack which represents |
---|
21 | * the current context of an ADM or ADF function call, that is, how to |
---|
22 | * get the argument values that argument_terminals need to return. |
---|
23 | * |
---|
24 | * <p><i>adf</i> contains the relevant ADF/ADM node. |
---|
25 | * If it's an ADF |
---|
26 | * function call, then <i>arguments[]</i> contains the evaluated arguments |
---|
27 | * to the ADF. If it's an ADM function call, |
---|
28 | * then <i>arguments[]</i> is set to false. |
---|
29 | * |
---|
30 | * <p>You set up the ADFContext object for a given ADF or ADM node with |
---|
31 | * the prepareADF(...) and prepareADM(...) functions. |
---|
32 | * |
---|
33 | * <p>To evaluate an argument number from an ADFContext, call evaluate(...), |
---|
34 | * and the results are evaluated and copied into input. |
---|
35 | * |
---|
36 | <p><b>Parameter bases</b><br> |
---|
37 | <table> |
---|
38 | <tr><td valign=top><i>base</i><tt>.data</tt><br> |
---|
39 | <td valign=top>(the ADFContext's basic GPData type)</td></tr> |
---|
40 | </table> |
---|
41 | * @author Sean Luke |
---|
42 | * @version 1.0 |
---|
43 | */ |
---|
44 | |
---|
45 | public class ADFContext implements Prototype |
---|
46 | { |
---|
47 | public final static String P_DATA = "data"; |
---|
48 | public final static String P_ADFCONTEXT = "adf-context"; |
---|
49 | |
---|
50 | public final static int INITIAL_ARGUMENT_SIZE = 2; // seems reasonable |
---|
51 | |
---|
52 | /** The ADF/ADM node proper */ |
---|
53 | public ADF adf; |
---|
54 | |
---|
55 | /** A prototypical GPData node. */ |
---|
56 | public GPData arg_proto; |
---|
57 | |
---|
58 | /** An array of GPData nodes (none of the null, when it's used) |
---|
59 | holding an ADF's arguments' return results */ |
---|
60 | public GPData[] arguments; |
---|
61 | |
---|
62 | public Parameter defaultBase() |
---|
63 | { |
---|
64 | return GPDefaults.base().push(P_ADFCONTEXT); |
---|
65 | } |
---|
66 | |
---|
67 | public ADFContext() |
---|
68 | { |
---|
69 | arguments = new GPData[INITIAL_ARGUMENT_SIZE]; |
---|
70 | } |
---|
71 | |
---|
72 | public Object clone() |
---|
73 | { |
---|
74 | try |
---|
75 | { |
---|
76 | ADFContext myobj = (ADFContext) (super.clone()); |
---|
77 | |
---|
78 | // deep-clone the context proto |
---|
79 | myobj.arg_proto = (GPData)(arg_proto.clone()); |
---|
80 | |
---|
81 | // deep-clone the contexts |
---|
82 | myobj.arguments = new GPData[arguments.length]; |
---|
83 | for(int x=0;x<myobj.arguments.length;x++) |
---|
84 | myobj.arguments[x] = (GPData)(arguments[x].clone()); |
---|
85 | |
---|
86 | return myobj; |
---|
87 | } |
---|
88 | catch (CloneNotSupportedException e) |
---|
89 | { throw new InternalError(); } |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | |
---|
94 | public void setup(final EvolutionState state, final Parameter base) |
---|
95 | { |
---|
96 | // load the prototype |
---|
97 | Parameter p = base.push(P_DATA); |
---|
98 | Parameter def = defaultBase(); |
---|
99 | Parameter d = def.push(P_DATA); |
---|
100 | |
---|
101 | |
---|
102 | if (state.parameters.exists(p,d)) |
---|
103 | { |
---|
104 | /* arg_proto = (GPData) |
---|
105 | (state.parameters.getInstanceForParameter( |
---|
106 | p,d,GPData.class)); |
---|
107 | arg_proto.setup(state,p); |
---|
108 | */ |
---|
109 | state.output.warning("ADF Data is deprecated -- this parameter is no longer used. Instead, we directly use the GPData.", |
---|
110 | p, d); |
---|
111 | } |
---|
112 | // else |
---|
113 | // snarf it from eval.problem.data, hacked up from the Problem's data type, |
---|
114 | // 'cause I'm not sure if Problem's been set up yet |
---|
115 | { |
---|
116 | Parameter pp = new Parameter(EvolutionState.P_EVALUATOR). |
---|
117 | push(Evaluator.P_PROBLEM).push(GPProblem.P_DATA); |
---|
118 | Parameter dd = GPDefaults.base().push(GPProblem.P_GPPROBLEM). |
---|
119 | push(GPProblem.P_DATA); |
---|
120 | |
---|
121 | //state.output.warning("No ADF GPData specified; using (and setting up) from\n " |
---|
122 | // + pp + "\nor default base " + dd, p,d); |
---|
123 | arg_proto = (GPData) |
---|
124 | (state.parameters.getInstanceForParameter(pp,dd,GPData.class)); |
---|
125 | arg_proto.setup(state,pp); // note setting up from Problem's base! |
---|
126 | } |
---|
127 | |
---|
128 | // clone off the prototype |
---|
129 | for (int x=0;x<INITIAL_ARGUMENT_SIZE;x++) |
---|
130 | arguments[x] = (GPData)(arg_proto.clone()); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | /** Evaluates the argument number in the current context */ |
---|
135 | public void evaluate(final EvolutionState state, |
---|
136 | final int thread, |
---|
137 | final GPData input, |
---|
138 | final ADFStack stack, |
---|
139 | final GPIndividual individual, |
---|
140 | final Problem problem, |
---|
141 | final int argument) |
---|
142 | { |
---|
143 | // do I have that many arguments? |
---|
144 | if (argument >= adf.children.length || argument < 0) // uh oh |
---|
145 | { |
---|
146 | individual.printIndividual(state,0); |
---|
147 | state.output.fatal("Invalid argument number for " + adf.errorInfo()); |
---|
148 | } |
---|
149 | |
---|
150 | // Am I an ADM or an ADF? |
---|
151 | if (adf==null) |
---|
152 | state.output.fatal("ADF is null for " + adf.errorInfo()); |
---|
153 | else if (adf instanceof ADF) // it's an ADF |
---|
154 | arguments[argument].copyTo(input); |
---|
155 | else // it's an ADM |
---|
156 | { |
---|
157 | // get rid of my context temporarily |
---|
158 | if (stack.moveOntoSubstack(1)!=1) |
---|
159 | state.output.fatal("Substack prematurely empty for " + adf.errorInfo()); |
---|
160 | |
---|
161 | // Call the GPNode |
---|
162 | adf.children[argument].eval(state,thread,input,stack,individual,problem); |
---|
163 | |
---|
164 | // restore my context |
---|
165 | if (stack.moveFromSubstack(1)!=1) |
---|
166 | state.output.fatal("Stack prematurely empty for " + adf.errorInfo()); |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | /** Increases arguments to accommodate space if necessary. |
---|
172 | Sets adf to a. |
---|
173 | You need to then fill out the arguments yourself. */ |
---|
174 | public final void prepareADF(ADF a) |
---|
175 | { |
---|
176 | // set to the length requested or longer |
---|
177 | if (a.children.length > arguments.length) |
---|
178 | { |
---|
179 | GPData[] newarguments = new GPData[a.children.length]; |
---|
180 | System.arraycopy(arguments,0,newarguments,0,arguments.length); |
---|
181 | // fill gap -- ugh, luckily this doesn't happen but a few times |
---|
182 | for(int x=arguments.length;x<newarguments.length;x++) |
---|
183 | newarguments[x] = (GPData)(arg_proto.clone()); |
---|
184 | arguments = newarguments; |
---|
185 | } |
---|
186 | adf = a; |
---|
187 | } |
---|
188 | |
---|
189 | /** Sets adf to a */ |
---|
190 | public final void prepareADM(ADM a) |
---|
191 | { |
---|
192 | adf = a; |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | } |
---|