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.koza; |
---|
9 | import ec.*; |
---|
10 | import ec.gp.*; |
---|
11 | import ec.util.*; |
---|
12 | |
---|
13 | /* |
---|
14 | * HalfBuilder.java |
---|
15 | * |
---|
16 | * Created: Thu Oct 7 18:03:49 1999 |
---|
17 | * By: Sean Luke |
---|
18 | */ |
---|
19 | |
---|
20 | /** HalfBuilder is a GPNodeBuilder which |
---|
21 | implements the RAMPED HALF-AND-HALF tree building method described in Koza I/II. |
---|
22 | |
---|
23 | <p>RAMPED HALF-AND-HALF works by choosing a random integer <i>d</i> between minDepth and maxDepth, inclusive. It then grows a tree of depth 1 to <i>d</i> inclusive. (1-pickGrowProbability) of the time (by default, 0.5) it grows a tree using the FULL method, which generates full trees of exactly depth <i>d</i>. (pickGrowProbability) of the time, it grows a tree using the GROW method, which may generate trees of any size between 1 and <i>d</i> inclusive. |
---|
24 | |
---|
25 | <p>Actually, claiming to implement the Koza I/II approach is a bit of a fib -- Koza's original code is somewhat ad-hoc. In the Koza approach, <i>d</i> is chosen in a kind of round-robin fashion rather than at random, if RAMPED HALF/HALF is used. Also, for all three algorithms (RAMPED HALF/HALF, GROW, FULL), the algorithm will not generate a tree consisting of a single terminal, unless forced to. |
---|
26 | |
---|
27 | <p>This implementation instead follows lil-gp's approach, which is to choose <i>d</i> at random from between minDepth and maxDepth, inclusive, and to allow trees consisting of single terminals. |
---|
28 | |
---|
29 | <p>Determining what various algorithms do is a little confusing, mostly because the source code for lil-gp and Koza don't actually quite do what they claim. The table below lists the depth values actually used (counting nodes, rather than edges, for depth). It's probably not what you had expected! |
---|
30 | |
---|
31 | |
---|
32 | <br> |
---|
33 | <br> |
---|
34 | <div align="center"> |
---|
35 | <table border="0" cellspacing="1" cellpadding="2"> |
---|
36 | <tr> |
---|
37 | <td bgcolor="#ffffff"><font size="-1" face="simple,geneva,arial,helvetica" color="#ffffff" ></font><br></td> |
---|
38 | <td bgcolor="#3366cc"><font size="-1" face="simple,geneva,arial,helvetica" color="#ffffff" >Koza I Min</font><br></td> |
---|
39 | <td bgcolor="#3366cc"><font size="-1" face="simple,geneva,arial,helvetica" color="#ffffff" >Koza I Max</font><br></td> |
---|
40 | <td bgcolor="#3366cc"><font size="-1" face="simple,geneva,arial,helvetica" color="#ffffff" >Koza II Min</font><br></td> |
---|
41 | <td bgcolor="#3366cc"><font size="-1" face="simple,geneva,arial,helvetica" color="#ffffff" >Koza II Max</font><br></td> |
---|
42 | <td bgcolor="#3366cc"><font size="-1" face="simple,geneva,arial,helvetica" color="#ffffff" >lil-gp Min</font><br></td> |
---|
43 | <td bgcolor="#3366cc"><font size="-1" face="simple,geneva,arial,helvetica" color="#ffffff" >lil-gp Max</font><br></td> |
---|
44 | <td bgcolor="#3366cc"><font size="-1" face="simple,geneva,arial,helvetica" color="#ffffff" >ECJ Min</font><br></td> |
---|
45 | <td bgcolor="#3366cc"><font size="-1" face="simple,geneva,arial,helvetica" color="#ffffff" >ECJ Max</font><br></td> |
---|
46 | </tr><tr> |
---|
47 | <td bgcolor="#3366cc"><font size="-1" face="simple,geneva,arial,helvetica" color="#ffffff">GROW (mut)</font><br></td> |
---|
48 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">5</font><br></td> |
---|
49 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">5</font><br></td> |
---|
50 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">5</font><br></td> |
---|
51 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">5</font><br></td> |
---|
52 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica"> </font><br></td> |
---|
53 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica"> </font><br></td> |
---|
54 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">5</font><br></td> |
---|
55 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">5</font><br></td> |
---|
56 | <tr></tr> |
---|
57 | <td bgcolor="#3366cc"><font size="-1" face="simple,geneva,arial,helvetica" color="#ffffff">GROW (new)</font><br></td> |
---|
58 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">7</font><br></td> |
---|
59 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">7</font><br></td> |
---|
60 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">6? 7?</font><br></td> |
---|
61 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">6? 7?</font><br></td> |
---|
62 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">3</font><br></td> |
---|
63 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">7</font><br></td> |
---|
64 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">5</font><br></td> |
---|
65 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">5</font><br></td> |
---|
66 | <tr></tr> |
---|
67 | <td bgcolor="#3366cc"><font size="-1" face="simple,geneva,arial,helvetica" color="#ffffff">FULL (new)</font><br></td> |
---|
68 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">7</font><br></td> |
---|
69 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">7</font><br></td> |
---|
70 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">6? 7?</font><br></td> |
---|
71 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">6? 7?</font><br></td> |
---|
72 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">3</font><br></td> |
---|
73 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">7</font><br></td> |
---|
74 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica"> </font><br></td> |
---|
75 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica"> </font><br></td> |
---|
76 | <tr></tr> |
---|
77 | <td bgcolor="#3366cc"><font size="-1" face="simple,geneva,arial,helvetica" color="#ffffff">HALF (new)</font><br></td> |
---|
78 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">2</font><br></td> |
---|
79 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">6</font><br></td> |
---|
80 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">2</font><br></td> |
---|
81 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">5? 6?</font><br></td> |
---|
82 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">3</font><br></td> |
---|
83 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">7</font><br></td> |
---|
84 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">2</font><br></td> |
---|
85 | <td bgcolor="#cccccc"><font size="-1" face="simple,geneva,arial,helvetica">6</font><br></td> |
---|
86 | </tr></table> |
---|
87 | </div> |
---|
88 | <br> |
---|
89 | <br> |
---|
90 | |
---|
91 | |
---|
92 | |
---|
93 | The table cell is empty when that parameter is not defined by the system by default. Koza II has two values each because of a possible typo in the text -- while page 656 gives one maximum, page 671 gives another. Note the odd fact that in Koza I/II GROW and FULL have <i>effectively</i> one-deeper tree values than HALF does, even though they use the same code parameters! This is because of a quirk in Koza's code. |
---|
94 | |
---|
95 | <p> This algorithm ignores <tt>requestedSize</tt>, so no pipelines can ask it to grow a tree of a specific fixed size. The algorithm also ignores any user-provided size distributions. |
---|
96 | |
---|
97 | |
---|
98 | <p><b>Parameters</b><br> |
---|
99 | <table> |
---|
100 | <tr><td valign=top><i>base</i>.<tt>growp</tt><br> |
---|
101 | <font size=-1>0.0 <= float <= 1.0</font></td> |
---|
102 | <td valign=top>(the likelihood of choosing GROW (as opposed to FULL)></tr> |
---|
103 | |
---|
104 | <tr><td valign=top><i>base</i>.<tt>min-depth</tt><br> |
---|
105 | <font size=-1>int >= 1</font></td> |
---|
106 | <td valign=top>(smallest "maximum" depth the builder may use for building a tree. 2 is the default.)</td></tr> |
---|
107 | |
---|
108 | <tr><td valign=top><i>base</i>.<tt>max-depth</tt><br> |
---|
109 | <font size=-1>int >= <i>base</i>.<tt>min-depth</tt></font></td> |
---|
110 | <td valign=top>(largest "maximum" depth the builder may use for building a tree. 6 is the default.)</td></tr> |
---|
111 | </table> |
---|
112 | |
---|
113 | <p><b>Default Base</b><br> |
---|
114 | gp.koza.half |
---|
115 | |
---|
116 | * @author Sean Luke |
---|
117 | * @version 1.0 |
---|
118 | */ |
---|
119 | |
---|
120 | |
---|
121 | public class HalfBuilder extends KozaBuilder |
---|
122 | { |
---|
123 | public static final String P_HALFBUILDER = "half"; |
---|
124 | public static final String P_PICKGROWPROBABILITY = "growp"; |
---|
125 | |
---|
126 | /** The likelihood of using GROW over FULL. */ |
---|
127 | public float pickGrowProbability; |
---|
128 | |
---|
129 | public Parameter defaultBase() |
---|
130 | { |
---|
131 | return GPKozaDefaults.base().push(P_HALFBUILDER); |
---|
132 | } |
---|
133 | |
---|
134 | public void setup(final EvolutionState state, final Parameter base) |
---|
135 | { |
---|
136 | super.setup(state,base); |
---|
137 | |
---|
138 | Parameter def = defaultBase(); |
---|
139 | |
---|
140 | pickGrowProbability = state.parameters.getFloatWithMax( |
---|
141 | base.push(P_PICKGROWPROBABILITY), |
---|
142 | def.push(P_PICKGROWPROBABILITY),0.0f,1.0f); |
---|
143 | if (pickGrowProbability < 0.0f) |
---|
144 | state.output.fatal("The Pick-Grow Probability for HalfBuilder must be a floating-point value between 0.0 and 1.0 inclusive.", base.push(P_MAXDEPTH),def.push(P_MAXDEPTH)); |
---|
145 | } |
---|
146 | |
---|
147 | public GPNode newRootedTree(final EvolutionState state, |
---|
148 | final GPType type, |
---|
149 | final int thread, |
---|
150 | final GPNodeParent parent, |
---|
151 | final GPFunctionSet set, |
---|
152 | final int argposition, |
---|
153 | final int requestedSize) |
---|
154 | { |
---|
155 | if (state.random[thread].nextFloat() < pickGrowProbability) |
---|
156 | return growNode(state,0,state.random[thread].nextInt(maxDepth-minDepth+1) + minDepth,type,thread,parent,argposition,set); |
---|
157 | else |
---|
158 | return fullNode(state,0,state.random[thread].nextInt(maxDepth-minDepth+1) + minDepth,type,thread,parent,argposition,set); |
---|
159 | } |
---|
160 | |
---|
161 | } |
---|
162 | |
---|
163 | |
---|