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.breed; |
---|
9 | import ec.*; |
---|
10 | import ec.util.*; |
---|
11 | import ec.gp.*; |
---|
12 | |
---|
13 | /* |
---|
14 | * MutateERCPipeline.java |
---|
15 | * |
---|
16 | * Created: Wed Dec 15 21:41:30 1999 |
---|
17 | * By: Sean Luke |
---|
18 | */ |
---|
19 | |
---|
20 | /** |
---|
21 | * MutateERCPipeline works very similarly to the "Gaussian" algorithm |
---|
22 | * described in Kumar Chellapilla, |
---|
23 | * "A Preliminary Investigation into Evolving Modular Programs without Subtree |
---|
24 | * Crossover", GP98. |
---|
25 | * |
---|
26 | * <p>MutateERCPipeline picks a random node from a random tree in the individual, |
---|
27 | * using its node selector. It then proceeds to "mutate" every ERC (ephemeral |
---|
28 | * random constant) located in the subtree rooted at that node. It does this |
---|
29 | * by calling each ERC's <tt>mutateERC()</tt> method. The default form of <tt>mutateERC()</tt> |
---|
30 | * method is to simply call <tt>resetNode()</tt>, thus randomizing the ERC; |
---|
31 | * you may want to override this default to provide more useful mutations, |
---|
32 | * such as adding gaussian noise. |
---|
33 | |
---|
34 | <p><b>Typical Number of Individuals Produced Per <tt>produce(...)</tt> call</b><br> |
---|
35 | ...as many as the source produces |
---|
36 | |
---|
37 | <p><b>Number of Sources</b><br> |
---|
38 | 1 |
---|
39 | |
---|
40 | <p><b>Parameters</b><br> |
---|
41 | <table> |
---|
42 | <tr><td valign=top><i>base</i>.<tt>ns</tt>.0<br> |
---|
43 | <font size=-1>classname, inherits and != GPNodeSelector</font></td> |
---|
44 | <td valign=top>(GPNodeSelector for tree)</td></tr> |
---|
45 | |
---|
46 | <tr><td valign=top><i>base</i>.<tt>tree.0</tt><br> |
---|
47 | <font size=-1>0 < int < (num trees in individuals), if exists</font></td> |
---|
48 | <td valign=top>(tree chosen for mutation; if parameter doesn't exist, tree is picked at random)</td></tr> |
---|
49 | </table> |
---|
50 | |
---|
51 | <p><b>Default Base</b><br> |
---|
52 | gp.breed.mutate-erc |
---|
53 | |
---|
54 | <p><b>Parameter bases</b><br> |
---|
55 | <table> |
---|
56 | <tr><td valign=top><i>base</i>.<tt>ns</tt><br> |
---|
57 | <td>The GPNodeSelector selector</td></tr> |
---|
58 | </table> |
---|
59 | |
---|
60 | |
---|
61 | * @author Sean Luke |
---|
62 | * @version 1.0 |
---|
63 | */ |
---|
64 | |
---|
65 | public class MutateERCPipeline extends GPBreedingPipeline |
---|
66 | { |
---|
67 | public static final String P_MUTATEERC = "mutate-erc"; |
---|
68 | public static final int NUM_SOURCES = 1; |
---|
69 | |
---|
70 | /** How the pipeline chooses a subtree to mutate */ |
---|
71 | public GPNodeSelector nodeselect; |
---|
72 | |
---|
73 | /** Is our tree fixed? If not, this is -1 */ |
---|
74 | int tree; |
---|
75 | |
---|
76 | public Parameter defaultBase() { return GPBreedDefaults.base().push(P_MUTATEERC); } |
---|
77 | |
---|
78 | public int numSources() { return NUM_SOURCES; } |
---|
79 | |
---|
80 | public Object clone() |
---|
81 | { |
---|
82 | MutateERCPipeline c = (MutateERCPipeline)(super.clone()); |
---|
83 | |
---|
84 | // deep-cloned stuff |
---|
85 | c.nodeselect = (GPNodeSelector)(nodeselect.clone()); |
---|
86 | return c; |
---|
87 | } |
---|
88 | |
---|
89 | public void setup(final EvolutionState state, final Parameter base) |
---|
90 | { |
---|
91 | super.setup(state,base); |
---|
92 | |
---|
93 | Parameter p = base.push(P_NODESELECTOR).push(""+0); |
---|
94 | Parameter def = defaultBase(); |
---|
95 | |
---|
96 | nodeselect = (GPNodeSelector) |
---|
97 | (state.parameters.getInstanceForParameter( |
---|
98 | p,def.push(P_NODESELECTOR).push(""+0), |
---|
99 | GPNodeSelector.class)); |
---|
100 | nodeselect.setup(state,p); |
---|
101 | |
---|
102 | tree = TREE_UNFIXED; |
---|
103 | if (state.parameters.exists(base.push(P_TREE).push(""+0), |
---|
104 | def.push(P_TREE).push(""+0))) |
---|
105 | { |
---|
106 | tree = state.parameters.getInt(base.push(P_TREE).push(""+0), |
---|
107 | def.push(P_TREE).push(""+0),0); |
---|
108 | if (tree==-1) |
---|
109 | state.output.fatal("Tree fixed value, if defined, must be >= 0"); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | public final void mutateERCs(final GPNode node, |
---|
115 | final EvolutionState state, final int thread) |
---|
116 | { |
---|
117 | // is node an erc? |
---|
118 | if (node instanceof ERC) |
---|
119 | ((ERC)node).mutateERC(state,thread); |
---|
120 | |
---|
121 | // mutate children |
---|
122 | for(int x=0;x<node.children.length;x++) |
---|
123 | mutateERCs(node.children[x],state,thread); |
---|
124 | } |
---|
125 | |
---|
126 | public int produce(final int min, |
---|
127 | final int max, |
---|
128 | final int start, |
---|
129 | final int subpopulation, |
---|
130 | final Individual[] inds, |
---|
131 | final EvolutionState state, |
---|
132 | final int thread) |
---|
133 | { |
---|
134 | // grab n individuals from our source and stick 'em right into inds. |
---|
135 | // we'll modify them from there |
---|
136 | int n = sources[0].produce(min,max,start,subpopulation,inds,state,thread); |
---|
137 | |
---|
138 | // should we bother? |
---|
139 | if (!state.random[thread].nextBoolean(likelihood)) |
---|
140 | return reproduce(n, start, subpopulation, inds, state, thread, false); // DON'T produce children from source -- we already did |
---|
141 | |
---|
142 | // now let's mutate 'em |
---|
143 | for(int q=start; q < n+start; q++) |
---|
144 | { |
---|
145 | GPIndividual i = (GPIndividual)inds[q]; |
---|
146 | |
---|
147 | if (tree!=TREE_UNFIXED && (tree<0 || tree >= i.trees.length)) |
---|
148 | // uh oh |
---|
149 | state.output.fatal("MutateERCPipeline attempted to fix tree.0 to a value which was out of bounds of the array of the individual's trees. Check the pipeline's fixed tree values -- they may be negative or greater than the number of trees in an individual"); |
---|
150 | |
---|
151 | int t; |
---|
152 | // pick random tree |
---|
153 | if (tree==TREE_UNFIXED) |
---|
154 | if (i.trees.length>1) t = state.random[thread].nextInt(i.trees.length); |
---|
155 | else t = 0; |
---|
156 | else t = tree; |
---|
157 | |
---|
158 | GPIndividual j; |
---|
159 | if (sources[0] instanceof BreedingPipeline) |
---|
160 | // it's already a copy, so just smash the tree in |
---|
161 | { |
---|
162 | j=i; |
---|
163 | } |
---|
164 | else // need to copy it |
---|
165 | { |
---|
166 | j = (GPIndividual)(i.lightClone()); |
---|
167 | |
---|
168 | // Fill in various tree information that didn't get filled in there |
---|
169 | j.trees = new GPTree[i.trees.length]; |
---|
170 | |
---|
171 | for(int x=0;x<j.trees.length;x++) |
---|
172 | { |
---|
173 | j.trees[x] = (GPTree)(i.trees[x].lightClone()); |
---|
174 | j.trees[x].owner = j; |
---|
175 | j.trees[x].child = (GPNode)(i.trees[x].child.clone()); |
---|
176 | j.trees[x].child.parent = j.trees[x]; |
---|
177 | j.trees[x].child.argposition = 0; |
---|
178 | } |
---|
179 | } |
---|
180 | j.evaluated = false; |
---|
181 | |
---|
182 | // prepare the nodeselector |
---|
183 | nodeselect.reset(); |
---|
184 | |
---|
185 | // Now pick a random node |
---|
186 | |
---|
187 | GPNode p = nodeselect.pickNode(state,subpopulation,thread,j,j.trees[t]); |
---|
188 | |
---|
189 | // mutate all the ERCs in p1's subtree |
---|
190 | |
---|
191 | mutateERCs(p,state,thread); |
---|
192 | |
---|
193 | // add the new individual, replacing its previous source |
---|
194 | inds[q] = j; |
---|
195 | } |
---|
196 | return n; |
---|
197 | } |
---|
198 | } |
---|