Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.StructureIdentification/Recombination/SizeFairCrossOver.cs @ 161

Last change on this file since 161 was 161, checked in by gkronber, 17 years ago

added description of SizeFairCrossOver

File size: 13.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Operators;
28using HeuristicLab.Random;
29using HeuristicLab.Data;
30using HeuristicLab.Constraints;
31using HeuristicLab.Functions;
32
33namespace HeuristicLab.StructureIdentification {
34  public class SizeFairCrossOver : OperatorBase {
35
36    public override string Description {
37      get {
38        return @"Takes two parent individuals P0 and P1 each. Selects a random node N0 of P0 and a random node N1 of P1.
39And replaces the branch with root N0 in P0 with N1 from P1 if the tree-size limits are not violated.
40When recombination with N0 and N1 would create a tree that is too large the operator randomly either goes
41up in P0 (parent of N0) or down in P1 (random child of N1) until a valid configuration is found.";
42      }
43    }
44    public SizeFairCrossOver()
45      : base() {
46      AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(MersenneTwister), VariableKind.In));
47      AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(GPOperatorLibrary), VariableKind.In));
48      AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));
49      AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In));
50      AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.New));
51      AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.New));
52      AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.New));
53    }
54
55    public override IOperation Apply(IScope scope) {
56      MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true);
57      GPOperatorLibrary opLibrary = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
58      int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data;
59      int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data;
60
61      TreeGardener gardener = new TreeGardener(random, opLibrary);
62
63      if((scope.SubScopes.Count % 2) != 0)
64        throw new InvalidOperationException("Number of parents is not even");
65
66      CompositeOperation initOperations = new CompositeOperation();
67
68      int children = scope.SubScopes.Count / 2;
69      for(int i = 0; i < children; i++) {
70        IScope parent1 = scope.SubScopes[0];
71        scope.RemoveSubScope(parent1);
72        IScope parent2 = scope.SubScopes[0];
73        scope.RemoveSubScope(parent2);
74        IScope child = new Scope(i.ToString());
75        IOperation childInitOperation = Cross(gardener, maxTreeSize, maxTreeHeight, scope, random, parent1, parent2, child);
76        initOperations.AddOperation(childInitOperation);
77        scope.AddSubScope(child);
78      }
79
80      return initOperations;
81    }
82
83    private IOperation Cross(TreeGardener gardener, int maxTreeSize, int maxTreeHeight,
84      IScope scope, MersenneTwister random, IScope parent1, IScope parent2, IScope child) {
85      List<IFunctionTree> newBranches;
86      IFunctionTree newTree = Cross(gardener, parent1, parent2,
87        random, maxTreeSize, maxTreeHeight, out newBranches);
88
89
90      int newTreeSize = gardener.GetTreeSize(newTree);
91      int newTreeHeight = gardener.GetTreeHeight(newTree);
92      child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), newTree));
93      child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(newTreeSize)));
94      child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(newTreeHeight)));
95
96      // check if the new tree is valid and if the size of is still in the allowed bounds
97      if(!gardener.IsValidTree(newTree) ||
98        newTreeHeight > maxTreeHeight ||
99        newTreeSize > maxTreeSize) {
100        throw new InvalidProgramException();
101      }
102      return gardener.CreateInitializationOperation(newBranches, child);
103    }
104
105
106    private IFunctionTree Cross(TreeGardener gardener, IScope f, IScope g, MersenneTwister random, int maxTreeSize, int maxTreeHeight, out List<IFunctionTree> newBranches) {
107      IFunctionTree tree0 = f.GetVariableValue<IFunctionTree>("FunctionTree", false);
108      int tree0Height = f.GetVariableValue<IntData>("TreeHeight", false).Data;
109      int tree0Size = f.GetVariableValue<IntData>("TreeSize", false).Data;
110
111      IFunctionTree tree1 = g.GetVariableValue<IFunctionTree>("FunctionTree", false);
112      int tree1Height = g.GetVariableValue<IntData>("TreeHeight", false).Data;
113      int tree1Size = g.GetVariableValue<IntData>("TreeSize", false).Data;
114
115      if(tree0Size == 1 && tree1Size == 1) {
116        return CombineTerminals(gardener, tree0, tree1, random, maxTreeHeight, out newBranches);
117      } else {
118        // we are going to insert tree1 into tree0 at a random place so we have to make sure that tree0 is not a terminal
119        // in case both trees are higher than 1 we swap the trees with probability 50%
120        if(tree0Height == 1 || (tree1Height > 1 && random.Next(2) == 0)) {
121          IFunctionTree tmp = tree0; tree0 = tree1; tree1 = tmp;
122          int tmpHeight = tree0Height; tree0Height = tree1Height; tree1Height = tmpHeight;
123          int tmpSize = tree0Size; tree0Size = tree1Size; tree1Size = tmpSize;
124        }
125
126        // save the root because later on we change tree0 and tree1 while searching a valid tree configuration
127        IFunctionTree root = tree0;
128        int rootSize = tree0Size;
129
130        // select a random suboperators of the two trees at a random level
131        int tree0Level = random.Next(tree0Height - 1); // since we checked before that the height of tree0 is > 1 this is OK
132        int tree1Level = random.Next(tree1Height);
133        tree0 = gardener.GetRandomBranch(tree0, tree0Level);
134        tree1 = gardener.GetRandomBranch(tree1, tree1Level);
135
136        // recalculate the size and height of tree1 (the one that we want to insert) because we need to check constraints later on
137        tree1Size = gardener.GetTreeSize(tree1);
138        tree1Height = gardener.GetTreeHeight(tree1);
139
140        List<int> possibleChildIndices = new List<int>();
141
142        // Now tree0 is supposed to take tree1 as one if its children. If this is not possible,
143        // then go down in either of the two trees as far as possible. If even then it is not possible
144        // to merge the trees then throw an exception
145        // find the list of allowed indices (regarding allowed sub-trees, maxTreeSize and maxTreeHeight)
146        for(int i = 0; i < tree0.SubTrees.Count; i++) {
147          int subTreeSize = gardener.GetTreeSize(tree0.SubTrees[i]);
148
149          // the index is ok when the function is allowed as sub-tree and we don't violate the maxSize and maxHeight constraints
150          if(gardener.GetAllowedSubFunctions(tree0.Function, i).Contains(tree1.Function) &&
151            rootSize - subTreeSize + tree1Size < maxTreeSize &&
152            tree0Level + tree1Height < maxTreeHeight) {
153            possibleChildIndices.Add(i);
154          }
155        }
156
157        while(possibleChildIndices.Count == 0) {
158          // we couln't find a possible configuration given the current tree0 and tree1
159          // possible reasons for this are:
160          //  - tree1 is not allowed as sub-tree of tree0
161          //  - appending tree1 as child of tree0 would create a tree that exceedes the maxTreeHeight
162          //  - replacing any child of tree0 with tree1 woulde create a tree that exceedes the maxTeeSize
163          // thus we have to either:
164          //  - go up in tree0 => the insert position allows larger trees
165          //  - go down in tree1 => the tree that is inserted becomes smaller
166          //  - however we have to get lucky to solve the 'allowed sub-trees' problem
167          if(tree1Height == 1 || (tree0Level>0 && random.Next(2) == 0)) {
168            // go up in tree0
169            tree0Level--;
170            tree0 = gardener.GetRandomBranch(root, tree0Level);
171          } else if(tree1.SubTrees.Count > 0) {
172            // go down in node2:
173            tree1 = tree1.SubTrees[random.Next(tree1.SubTrees.Count)];
174            tree1Size = gardener.GetTreeSize(tree1);
175            tree1Height = gardener.GetTreeHeight(tree1);
176          } else {
177            // could neither go up or down ... don't know what to do ... give up
178            throw new InvalidProgramException();
179          }
180
181          // recalculate the list of possible indices
182          possibleChildIndices.Clear();
183          for(int i = 0; i < tree0.SubTrees.Count; i++) {
184            int subTreeSize = gardener.GetTreeSize(tree0.SubTrees[i]);
185
186            // when the function is allowed as sub-tree and we don't violate the maxSize and maxHeight constraints
187            // the index is ok
188            if(gardener.GetAllowedSubFunctions(tree0.Function, i).Contains(tree1.Function) &&
189              rootSize - subTreeSize + tree1Size < maxTreeSize &&
190              tree0Level + tree1Height < maxTreeHeight) {
191              possibleChildIndices.Add(i);
192            }
193          }
194        }
195
196        // no possible configuration found this indicates that there is a bigger problem
197        if(possibleChildIndices.Count == 0) {
198          throw new InvalidProgramException();
199        }
200
201        // replace the existing sub-tree at a random index in tree0 with tree1
202        int selectedIndex = possibleChildIndices[random.Next(possibleChildIndices.Count)];
203        tree0.RemoveSubTree(selectedIndex);
204        tree0.InsertSubTree(selectedIndex, tree1);
205
206        // no new operators where needed
207        newBranches = new List<IFunctionTree>();
208        return root;
209      }
210    }
211
212
213    // take f and g and create a tree that has f and g as sub-trees
214    // example
215    //       O
216    //      /|\
217    //     g 2 f
218    //
219    private IFunctionTree CombineTerminals(TreeGardener gardener, IFunctionTree f, IFunctionTree g, MersenneTwister random, int maxTreeHeight, out List<IFunctionTree> newBranches) {
220      newBranches = new List<IFunctionTree>();
221      // determine the set of possible parent functions
222      ICollection<IFunction> possibleParents = gardener.GetPossibleParents(new List<IFunction>() { f.Function, g.Function });
223      if(possibleParents.Count == 0) throw new InvalidProgramException();
224      // and select a random one
225      IFunctionTree parent = new FunctionTree(possibleParents.ElementAt(random.Next(possibleParents.Count())));
226
227      int minArity;
228      int maxArity;
229      gardener.GetMinMaxArity(parent.Function, out minArity, out maxArity);
230      int nSlots = Math.Max(2, minArity);
231      // determine which slot can take which sub-trees
232      List<IFunctionTree>[] slots = new List<IFunctionTree>[nSlots];
233      for(int slot = 0; slot < nSlots; slot++) {
234        ICollection<IFunction> allowedSubFunctions = gardener.GetAllowedSubFunctions(parent.Function, slot);
235        List<IFunctionTree> allowedTrees = new List<IFunctionTree>();
236        if(allowedSubFunctions.Contains(f.Function)) allowedTrees.Add(f);
237        if(allowedSubFunctions.Contains(g.Function)) allowedTrees.Add(g);
238        slots[slot] = allowedTrees;
239      }
240      // fill the slots in the order of degrees of freedom
241      int[] slotSequence = Enumerable.Range(0, slots.Count()).OrderBy(slot => slots[slot].Count()).ToArray();
242
243      // tmp arry to store the tree for each sub-tree slot of the parent
244      IFunctionTree[] selectedFunctionTrees = new IFunctionTree[nSlots];
245
246      // fill the sub-tree slots of the parent starting with the slots that can take potentially both functions (f and g)
247      for(int i = 0; i < slotSequence.Length; i++) {
248        int slot = slotSequence[i];
249        List<IFunctionTree> allowedTrees = slots[slot];
250        // when neither f nor g fit into the slot => create a new random tree
251        if(allowedTrees.Count() == 0) {
252          var allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, slot);
253          selectedFunctionTrees[slot] = gardener.CreateRandomTree(allowedFunctions, 1, 1, true);
254          newBranches.AddRange(gardener.GetAllSubTrees(selectedFunctionTrees[slot]));
255        } else {
256          // select randomly which tree to insert into this slot
257          IFunctionTree selectedTree = allowedTrees[random.Next(allowedTrees.Count())];
258          selectedFunctionTrees[slot] = selectedTree;
259          // remove the tree that we used in this slot from following function-sets
260          for(int j = i + 1; j < slotSequence.Length; j++) {
261            int otherSlot = slotSequence[j];
262            slots[otherSlot].Remove(selectedTree);
263          }
264        }
265      }
266      // actually append the sub-trees to the parent tree
267      for(int i = 0; i < selectedFunctionTrees.Length; i++) {
268        parent.InsertSubTree(i, selectedFunctionTrees[i]);
269      }
270
271      return parent;
272    }
273  }
274}
Note: See TracBrowser for help on using the repository browser.