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.build; |
---|
9 | import ec.gp.*; |
---|
10 | import ec.*; |
---|
11 | import ec.util.*; |
---|
12 | |
---|
13 | /* |
---|
14 | * RandomBranch.java |
---|
15 | * |
---|
16 | * Created: Mon Dec 13 14:26:02 1999 |
---|
17 | * By: Sean Luke |
---|
18 | */ |
---|
19 | |
---|
20 | /** |
---|
21 | * RandomBranch implements the <tt>Random_Branch</tt> tree generation |
---|
22 | * method described in |
---|
23 | * |
---|
24 | * <p> Chellapilla, K. 1998. Evolving Modular Programs without Crossover. |
---|
25 | * in <i>Proceedings of the Third Annual Genetic Programming Conference</i> |
---|
26 | * (GP98), J.R. Koza <i>et al</i>, editors. San Fransisco: Morgan Kaufmann. |
---|
27 | * 23--31. |
---|
28 | * |
---|
29 | * <p> This algorithm attempts to create a tree of size <tt>requestedSize</tt>, |
---|
30 | * or "slightly less". |
---|
31 | * |
---|
32 | * If the pipeline does not specify a size it wants (it uses <tt>NOSIZEGIVEN</tt>), |
---|
33 | * the algorithm picks a size at random from either [minSize...maxSize] or from |
---|
34 | * sizeDistribution (one of the two <b>must</b> be defined), and attempts to create |
---|
35 | * a tree of that size or "slightly less". |
---|
36 | * |
---|
37 | * @author Sean Luke |
---|
38 | * @version 1.0 |
---|
39 | */ |
---|
40 | |
---|
41 | public class RandomBranch extends GPNodeBuilder |
---|
42 | { |
---|
43 | public static final String P_RANDOMBRANCH = "random-branch"; |
---|
44 | |
---|
45 | public Parameter defaultBase() |
---|
46 | { |
---|
47 | return GPBuildDefaults.base().push(P_RANDOMBRANCH); |
---|
48 | } |
---|
49 | |
---|
50 | public void setup(final EvolutionState state, final Parameter base) |
---|
51 | { |
---|
52 | super.setup(state,base); |
---|
53 | |
---|
54 | // we use size distributions -- did the user specify any? |
---|
55 | if (!canPick()) |
---|
56 | state.output.fatal("RandomBranch requires some kind of size distribution set, either with " + P_MINSIZE + "/" + P_MAXSIZE + ", or with " + P_NUMSIZES + ".", |
---|
57 | base, defaultBase()); |
---|
58 | } |
---|
59 | |
---|
60 | public GPNode newRootedTree(final EvolutionState state, |
---|
61 | final GPType type, |
---|
62 | final int thread, |
---|
63 | final GPNodeParent parent, |
---|
64 | final GPFunctionSet set, |
---|
65 | final int argposition, |
---|
66 | final int requestedSize) |
---|
67 | { |
---|
68 | if (requestedSize == NOSIZEGIVEN) // pick from the distribution |
---|
69 | return randomBranch(state,type,pickSize(state,thread),thread,parent,argposition,set); |
---|
70 | if (requestedSize < 1) |
---|
71 | state.output.fatal("ec.gp.build.RandomBranch requested to build a tree, but a requested size was given that is < 1."); |
---|
72 | return randomBranch(state,type,requestedSize,thread,parent,argposition,set); |
---|
73 | } |
---|
74 | |
---|
75 | private GPNode randomBranch(final EvolutionState state, |
---|
76 | final GPType type, |
---|
77 | final int maxLength, |
---|
78 | final int thread, |
---|
79 | final GPNodeParent parent, |
---|
80 | final int argposition, |
---|
81 | final GPFunctionSet set) |
---|
82 | { |
---|
83 | // randomBranch can mess up if there are no available terminals for a given type. If this occurs, |
---|
84 | // and we find ourselves unable to pick a terminal when we want to do so, we will issue a warning, |
---|
85 | // and pick a nonterminal, violating the maximum-size contract. This can lead to pathological situations |
---|
86 | // where the system will continue to go on and on unable to stop because it can't pick a terminal, |
---|
87 | // resulting in running out of memory or some such. But there are cases where we'd want to let |
---|
88 | // this work itself out. |
---|
89 | boolean triedTerminals = false; |
---|
90 | |
---|
91 | int t = type.type; |
---|
92 | GPNode[] terminals = set.terminals[t]; |
---|
93 | GPNode[] nonterminals = set.nonterminals[t]; |
---|
94 | GPNode[] nodes = set.nodes[t]; |
---|
95 | |
---|
96 | if (nodes.length == 0) |
---|
97 | errorAboutNoNodeWithType(type, state); // total failure |
---|
98 | |
---|
99 | if (( maxLength == 1 || // if the desired length is 1 |
---|
100 | warnAboutNonterminal(nonterminals.length==0, type, false, state)) && // OR if there are NO nonterminals! |
---|
101 | (triedTerminals = true) && // [first set triedTerminals] |
---|
102 | terminals.length != 0) // AND if there are available terminals |
---|
103 | { |
---|
104 | GPNode n = (GPNode)(terminals[state.random[thread].nextInt(terminals.length)].lightClone()); |
---|
105 | n.resetNode(state,thread); // give ERCs a chance to randomize |
---|
106 | n.argposition = (byte)argposition; |
---|
107 | n.parent = parent; |
---|
108 | return n; |
---|
109 | } |
---|
110 | else |
---|
111 | { |
---|
112 | if (triedTerminals) warnAboutNoTerminalWithType(type, false, state); // we tried terminals and we're here because there were none! |
---|
113 | |
---|
114 | // grab all the nodes whose arity is <= maxlength-1 |
---|
115 | int len = set.nonterminalsUnderArity[type.type].length-1; |
---|
116 | if (len > maxLength-1) len = maxLength-1; |
---|
117 | GPNode[] okayNonterms = set.nonterminalsUnderArity[type.type][len]; |
---|
118 | |
---|
119 | if (okayNonterms.length == 0) // no nodes, pick a terminal |
---|
120 | { |
---|
121 | if (terminals.length == 0) |
---|
122 | errorAboutNoNodeWithType(type, state); // total failure |
---|
123 | |
---|
124 | GPNode n = (GPNode)(terminals[state.random[thread].nextInt(terminals.length)].lightClone()); |
---|
125 | n.resetNode(state,thread); // give ERCs a chance to randomize |
---|
126 | n.argposition = (byte)argposition; |
---|
127 | n.parent = parent; |
---|
128 | return n; |
---|
129 | } |
---|
130 | else // we've got nonterminals, pick one at random |
---|
131 | { |
---|
132 | GPNode n = (GPNode)(okayNonterms[state.random[thread].nextInt(okayNonterms.length)].lightClone()); |
---|
133 | n.resetNode(state,thread); // give ERCs a chance to randomize |
---|
134 | n.argposition = (byte)argposition; |
---|
135 | n.parent = parent; |
---|
136 | |
---|
137 | // Populate the node... |
---|
138 | GPType[] childtypes = n.constraints(((GPInitializer)state.initializer)).childtypes; |
---|
139 | for(int x=0;x<childtypes.length;x++) |
---|
140 | n.children[x] = randomBranch( |
---|
141 | state,childtypes[x], |
---|
142 | (maxLength-1)/childtypes.length, // note int division |
---|
143 | thread,n,x,set); |
---|
144 | return n; |
---|
145 | } |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | } |
---|