Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.StructureIdentification/TreeGardener.cs @ 464

Last change on this file since 464 was 452, checked in by gkronber, 16 years ago

fixed a bug that caused generation of invalid individuals (#225)

File size: 22.6 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.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Constraints;
27using System.Diagnostics;
28using HeuristicLab.Data;
29using System.Linq;
30using HeuristicLab.Random;
31using HeuristicLab.Operators;
32using HeuristicLab.Selection;
33using HeuristicLab.Functions;
34using System.Collections;
35
36namespace HeuristicLab.StructureIdentification {
37  internal class TreeGardener {
38    private IRandom random;
39    private GPOperatorLibrary funLibrary;
40    private List<IFunction> functions;
41
42    private List<IFunction> terminals;
43    internal IList<IFunction> Terminals {
44      get { return terminals; }
45    }
46
47    private List<IFunction> allFunctions;
48    internal IList<IFunction> AllFunctions {
49      get { return allFunctions; }
50    }
51
52    #region constructors
53    internal TreeGardener(IRandom random, GPOperatorLibrary funLibrary) {
54      this.random = random;
55      this.funLibrary = funLibrary;
56      this.allFunctions = new List<IFunction>();
57      terminals = new List<IFunction>();
58      functions = new List<IFunction>();
59      // init functions and terminals based on constraints
60      foreach(IFunction fun in funLibrary.Group.Operators) {
61        int maxA, minA;
62        GetMinMaxArity(fun, out minA, out maxA);
63        if(maxA == 0) {
64          terminals.Add(fun);
65          allFunctions.Add(fun);
66        } else {
67          functions.Add(fun);
68          allFunctions.Add(fun);
69        }
70      }
71    }
72    #endregion
73
74    #region random initialization
75    /// <summary>
76    /// Creates a random balanced tree with a maximal size and height. When the max-height or max-size are 1 it will return a random terminal.
77    /// In other cases it will return either a terminal (tree of size 1) or any other tree with a function in it's root (at least height 2).
78    /// </summary>
79    /// <param name="maxTreeSize">Maximal size of the tree (number of nodes).</param>
80    /// <param name="maxTreeHeight">Maximal height of the tree.</param>
81    /// <returns></returns>
82    internal IFunctionTree CreateBalancedRandomTree(int maxTreeSize, int maxTreeHeight) {
83      IFunction rootFunction = GetRandomRoot(maxTreeSize, maxTreeHeight);
84      IFunctionTree tree = MakeBalancedTree(rootFunction, maxTreeHeight - 1);
85      return tree;
86    }
87
88    /// <summary>
89    /// Creates a random (unbalanced) tree with a maximal size and height. When the max-height or max-size are 1 it will return a random terminal.
90    /// In other cases it will return either a terminal (tree of size 1) or any other tree with a function in it's root (at least height 2).
91    /// </summary>
92    /// <param name="maxTreeSize">Maximal size of the tree (number of nodes).</param>
93    /// <param name="maxTreeHeight">Maximal height of the tree.</param>
94    /// <returns></returns>
95    internal IFunctionTree CreateUnbalancedRandomTree(int maxTreeSize, int maxTreeHeight) {
96      IFunction rootFunction = GetRandomRoot(maxTreeSize, maxTreeHeight);
97      IFunctionTree tree = MakeUnbalancedTree(rootFunction, maxTreeHeight - 1);
98      return tree;
99    }
100
101    internal IFunctionTree PTC2(IRandom random, int size, int maxDepth) {
102      return PTC2(random, GetRandomRoot(size, maxDepth), size, maxDepth);
103    }
104
105    internal IFunctionTree PTC2(IRandom random, IFunction rootF, int size, int maxDepth) {
106      IFunctionTree root = rootF.GetTreeNode();
107      if(size <= 1 || maxDepth <= 1) return root;
108      List<object[]> list = new List<object[]>();
109      int currentSize = 1;
110      int totalListMinSize = 0;
111      int minArity;
112      int maxArity;
113      GetMinMaxArity(root.Function, out minArity, out maxArity);
114      if(maxArity >= size) {
115        maxArity = size;
116      }
117      int actualArity = random.Next(minArity, maxArity + 1);
118      totalListMinSize += GetMinimalTreeSize(root.Function) - 1;
119      for(int i = 0; i < actualArity; i++) {
120        // insert a dummy sub-tree and add the pending extension to the list
121        root.AddSubTree(null);
122        list.Add(new object[] { root, i, 2 });
123      }
124
125      while(list.Count > 0 && totalListMinSize + currentSize < size) {
126        int randomIndex = random.Next(list.Count);
127        object[] nextExtension = list[randomIndex];
128        list.RemoveAt(randomIndex);
129        IFunctionTree parent = (IFunctionTree)nextExtension[0];
130        int a = (int)nextExtension[1];
131        int d = (int)nextExtension[2];
132        if(d == maxDepth) {
133          parent.RemoveSubTree(a);
134          IFunctionTree branch = CreateRandomTree(GetAllowedSubFunctions(parent.Function, a), 1, 1);
135          parent.InsertSubTree(a, branch); // insert a smallest possible tree
136          currentSize += branch.Size;
137          totalListMinSize -= branch.Size;
138        } else {
139          IFunction selectedFunction = RandomSelect(GetAllowedSubFunctions(parent.Function, a).Where(
140            f => !IsTerminal(f) && GetMinimalTreeHeight(f) + (d - 1) <= maxDepth).ToArray());
141          IFunctionTree newTree = selectedFunction.GetTreeNode();
142          parent.RemoveSubTree(a);
143          parent.InsertSubTree(a, newTree);
144          currentSize++;
145          totalListMinSize--;
146
147          GetMinMaxArity(selectedFunction, out minArity, out maxArity);
148          if(maxArity >= size) {
149            maxArity = size;
150          }
151          actualArity = random.Next(minArity, maxArity + 1);
152          for(int i = 0; i < actualArity; i++) {
153            // insert a dummy sub-tree and add the pending extension to the list
154            newTree.AddSubTree(null);
155            list.Add(new object[] { newTree, i, d + 1 });
156          }
157          totalListMinSize += GetMinimalTreeSize(newTree.Function) - 1;
158        }
159      }
160      while(list.Count > 0) {
161        int randomIndex = random.Next(list.Count);
162        object[] nextExtension = list[randomIndex];
163        list.RemoveAt(randomIndex);
164        IFunctionTree parent = (IFunctionTree)nextExtension[0];
165        int a = (int)nextExtension[1];
166        int d = (int)nextExtension[2];
167        parent.RemoveSubTree(a);
168        parent.InsertSubTree(a,
169          CreateRandomTree(GetAllowedSubFunctions(parent.Function, a), 1, 1)); // append a tree with minimal possible height
170      }
171      return root;
172    }
173
174    /// <summary>
175    /// selects a random function from allowedFunctions and creates a random (unbalanced) tree with maximal size and height.
176    /// </summary>
177    /// <param name="allowedFunctions">Set of allowed functions.</param>
178    /// <param name="maxTreeSize">Maximal size of the tree (number of nodes).</param>
179    /// <param name="maxTreeHeight">Maximal height of the tree.</param>
180    /// <returns>New random unbalanced tree</returns>
181    internal IFunctionTree CreateRandomTree(ICollection<IFunction> allowedFunctions, int maxTreeSize, int maxTreeHeight) {
182      // get the minimal needed height based on allowed functions and extend the max-height if necessary
183      int minTreeHeight = allowedFunctions.Select(f => GetMinimalTreeHeight(f)).Min();
184      if(minTreeHeight > maxTreeHeight)
185        maxTreeHeight = minTreeHeight;
186      // get the minimal needed size based on allowed functions and extend the max-size if necessary
187      int minTreeSize = allowedFunctions.Select(f => GetMinimalTreeSize(f)).Min();
188      if(minTreeSize > maxTreeSize)
189        maxTreeSize = minTreeSize;
190
191      // select a random value for the size and height
192      int treeHeight = random.Next(minTreeHeight, maxTreeHeight + 1);
193      int treeSize = random.Next(minTreeSize, maxTreeSize + 1);
194
195      // filter the set of allowed functions and select only from those that fit into the given maximal size and height limits
196      IFunction[] possibleFunctions = allowedFunctions.Where(f => GetMinimalTreeHeight(f) <= treeHeight &&
197        GetMinimalTreeSize(f) <= treeSize).ToArray();
198      IFunction selectedFunction = RandomSelect(possibleFunctions);
199
200      // build the tree
201      IFunctionTree root;
202      root = PTC2(random, selectedFunction, maxTreeSize, maxTreeHeight);
203      return root;
204    }
205
206    internal CompositeOperation CreateInitializationOperation(ICollection<IFunctionTree> trees, IScope scope) {
207      // needed for the parameter shaking operation
208      CompositeOperation initializationOperation = new CompositeOperation();
209      Scope tempScope = new Scope("Temp. initialization scope");
210
211      var parametricTrees = trees.Where(t => t.Function.GetVariable(FunctionBase.INITIALIZATION) != null);
212      foreach(IFunctionTree tree in parametricTrees) {
213        // enqueue an initialization operation for each operator with local variables
214        IOperator initialization = (IOperator)tree.Function.GetVariable(FunctionBase.INITIALIZATION).Value;
215        Scope initScope = new Scope();
216        // copy the local variables into a temporary scope used for initialization
217        foreach(IVariable variable in tree.LocalVariables) {
218          initScope.AddVariable(variable);
219        }
220        tempScope.AddSubScope(initScope);
221        initializationOperation.AddOperation(new AtomicOperation(initialization, initScope));
222      }
223      Scope backupScope = new Scope("backup");
224      foreach(Scope subScope in scope.SubScopes) {
225        backupScope.AddSubScope(subScope);
226      }
227      scope.AddSubScope(tempScope);
228      scope.AddSubScope(backupScope);
229      // add an operation to remove the temporary scopes       
230      initializationOperation.AddOperation(new AtomicOperation(new RightReducer(), scope));
231      return initializationOperation;
232    }
233    #endregion
234
235    #region tree information gathering
236    internal IFunctionTree GetRandomParentNode(IFunctionTree tree) {
237      List<IFunctionTree> parentNodes = new List<IFunctionTree>();
238
239      // add null for the parent of the root node
240      parentNodes.Add(null);
241
242      TreeForEach(tree, delegate(IFunctionTree possibleParentNode) {
243        if(possibleParentNode.SubTrees.Count > 0) {
244          parentNodes.Add(possibleParentNode);
245        }
246      });
247
248      return parentNodes[random.Next(parentNodes.Count)];
249    }
250
251    internal ICollection<IFunctionTree> GetAllSubTrees(IFunctionTree root) {
252      List<IFunctionTree> allTrees = new List<IFunctionTree>();
253      TreeForEach(root, t => { allTrees.Add(t); });
254      return allTrees;
255    }
256
257    /// <summary>
258    /// returns the height level of branch in the tree
259    /// if the branch == tree => 1
260    /// if branch is in the sub-trees of tree => 2
261    /// ...
262    /// if branch is not found => -1
263    /// </summary>
264    /// <param name="tree">root of the function tree to process</param>
265    /// <param name="branch">branch that is searched in the tree</param>
266    /// <returns></returns>
267    internal int GetBranchLevel(IFunctionTree tree, IFunctionTree branch) {
268      return GetBranchLevelHelper(tree, branch, 1);
269    }
270
271    // 'tail-recursive' helper
272    private int GetBranchLevelHelper(IFunctionTree tree, IFunctionTree branch, int level) {
273      if(branch == tree) return level;
274
275      foreach(IFunctionTree subTree in tree.SubTrees) {
276        int result = GetBranchLevelHelper(subTree, branch, level + 1);
277        if(result != -1) return result;
278      }
279
280      return -1;
281    }
282
283    internal bool IsValidTree(IFunctionTree tree) {
284      SubOperatorsConstraintAnalyser analyzer = new SubOperatorsConstraintAnalyser();
285      analyzer.AllPossibleOperators = AllFunctions.Cast<IOperator>().ToArray<IOperator>();
286      for(int i = 0; i < tree.SubTrees.Count; i++) {
287        if(!analyzer.GetAllowedOperators(tree.Function, i).Contains(tree.SubTrees[i].Function)) return false;
288      }
289
290      foreach(IConstraint constraint in tree.Function.Constraints) {
291        if(constraint is NumberOfSubOperatorsConstraint) {
292          int max = ((NumberOfSubOperatorsConstraint)constraint).MaxOperators.Data;
293          int min = ((NumberOfSubOperatorsConstraint)constraint).MinOperators.Data;
294          if(tree.SubTrees.Count < min || tree.SubTrees.Count > max)
295            return false;
296        }
297      }
298      foreach(IFunctionTree subTree in tree.SubTrees) {
299        if(!IsValidTree(subTree)) return false;
300      }
301      return true;
302    }
303
304    // returns a random branch from the specified level in the tree
305    internal IFunctionTree GetRandomBranch(IFunctionTree tree, int level) {
306      if(level == 0) return tree;
307      List<IFunctionTree> branches = GetBranchesAtLevel(tree, level);
308      return branches[random.Next(branches.Count)];
309    }
310    #endregion
311
312    #region function information (arity, allowed childs and parents)
313    internal ICollection<IFunction> GetPossibleParents(List<IFunction> list) {
314      List<IFunction> result = new List<IFunction>();
315      foreach(IFunction f in functions) {
316        if(IsPossibleParent(f, list)) {
317          result.Add(f);
318        }
319      }
320      return result;
321    }
322
323    private bool IsPossibleParent(IFunction f, List<IFunction> children) {
324      int minArity;
325      int maxArity;
326      GetMinMaxArity(f, out minArity, out maxArity);
327
328      // note: we can't assume that the operators in the children list have different types!
329
330      // when the maxArity of this function is smaller than the list of operators that
331      // should be included as sub-operators then it can't be a parent
332      if(maxArity < children.Count()) {
333        return false;
334      }
335      int nSlots = Math.Max(minArity, children.Count);
336
337      SubOperatorsConstraintAnalyser analyzer = new SubOperatorsConstraintAnalyser();
338      analyzer.AllPossibleOperators = children.Cast<IOperator>().ToArray<IOperator>();
339
340      List<HashSet<IFunction>> slotSets = new List<HashSet<IFunction>>();
341
342      // we iterate through all slots for sub-trees and calculate the set of
343      // allowed functions for this slot.
344      // we only count those slots that can hold at least one of the children that we should combine
345      for(int slot = 0; slot < nSlots; slot++) {
346        HashSet<IFunction> functionSet = new HashSet<IFunction>(analyzer.GetAllowedOperators(f, slot).Cast<IFunction>());
347        if(functionSet.Count() > 0) {
348          slotSets.Add(functionSet);
349        }
350      }
351
352      // ok at the end of this operation we know how many slots of the parent can actually
353      // hold one of our children.
354      // if the number of slots is smaller than the number of children we can be sure that
355      // we can never combine all children as sub-trees of the function and thus the function
356      // can't be a parent.
357      if(slotSets.Count() < children.Count()) {
358        return false;
359      }
360
361      // finally we sort the sets by size and beginning from the first set select one
362      // function for the slot and thus remove it as possible sub-tree from the remaining sets.
363      // when we can successfully assign all available children to a slot the function is a valid parent
364      // when only a subset of all children can be assigned to slots the function is no valid parent
365      slotSets.Sort((p, q) => p.Count() - q.Count());
366
367      int assignments = 0;
368      for(int i = 0; i < slotSets.Count() - 1; i++) {
369        if(slotSets[i].Count > 0) {
370          IFunction selected = slotSets[i].ElementAt(0);
371          assignments++;
372          for(int j = i + 1; j < slotSets.Count(); j++) {
373            slotSets[j].Remove(selected);
374          }
375        }
376      }
377
378      // sanity check
379      if(assignments > children.Count) throw new InvalidProgramException();
380      return assignments == children.Count - 1;
381    }
382    internal IList<IFunction> GetAllowedParents(IFunction child, int childIndex) {
383      List<IFunction> parents = new List<IFunction>();
384      foreach(IFunction function in functions) {
385        ICollection<IFunction> allowedSubFunctions = GetAllowedSubFunctions(function, childIndex);
386        if(allowedSubFunctions.Contains(child)) {
387          parents.Add(function);
388        }
389      }
390      return parents;
391    }
392    internal bool IsTerminal(IFunction f) {
393      int minArity;
394      int maxArity;
395      GetMinMaxArity(f, out minArity, out maxArity);
396      return minArity == 0 && maxArity == 0;
397    }
398    internal IList<IFunction> GetAllowedSubFunctions(IFunction f, int index) {
399      if(f == null) {
400        return allFunctions;
401      } else {
402        SubOperatorsConstraintAnalyser analyzer = new SubOperatorsConstraintAnalyser();
403        analyzer.AllPossibleOperators = AllFunctions.Cast<IOperator>().ToArray<IOperator>();
404        List<IFunction> result = new List<IFunction>();
405        foreach(IFunction function in analyzer.GetAllowedOperators(f, index)) {
406          result.Add(function);
407        }
408        return result;
409      }
410    }
411    internal void GetMinMaxArity(IFunction f, out int minArity, out int maxArity) {
412      foreach(IConstraint constraint in f.Constraints) {
413        NumberOfSubOperatorsConstraint theConstraint = constraint as NumberOfSubOperatorsConstraint;
414        if(theConstraint != null) {
415          minArity = theConstraint.MinOperators.Data;
416          maxArity = theConstraint.MaxOperators.Data;
417          return;
418        }
419      }
420      // the default arity is 2
421      minArity = 2;
422      maxArity = 2;
423    }
424    #endregion
425
426    #region private utility methods
427    private IFunction GetRandomRoot(int maxTreeSize, int maxTreeHeight) {
428      if(maxTreeHeight == 1 || maxTreeSize == 1) {
429        IFunction selectedTerminal = RandomSelect(terminals);
430        return selectedTerminal;
431      } else {
432        IFunction[] possibleFunctions = functions.Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight &&
433          GetMinimalTreeSize(f) <= maxTreeSize).ToArray();
434        IFunction selectedFunction = RandomSelect(possibleFunctions);
435        return selectedFunction;
436      }
437    }
438
439
440    private IFunctionTree MakeUnbalancedTree(IFunction parent, int maxTreeHeight) {
441      if(maxTreeHeight == 0) return parent.GetTreeNode();
442      int minArity;
443      int maxArity;
444      GetMinMaxArity(parent, out minArity, out maxArity);
445      int actualArity = random.Next(minArity, maxArity + 1);
446      if(actualArity > 0) {
447        IFunctionTree parentTree = parent.GetTreeNode();
448        for(int i = 0; i < actualArity; i++) {
449          IFunction[] possibleFunctions = GetAllowedSubFunctions(parent, i).Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight).ToArray();
450          IFunction selectedFunction = RandomSelect(possibleFunctions);
451          IFunctionTree newSubTree = MakeUnbalancedTree(selectedFunction, maxTreeHeight - 1);
452          parentTree.InsertSubTree(i, newSubTree);
453        }
454        return parentTree;
455      }
456      return parent.GetTreeNode();
457    }
458
459
460    // NOTE: this method doesn't build fully balanced trees because we have constraints on the
461    // types of possible sub-functions which can indirectly impose a limit for the depth of a given sub-tree
462    private IFunctionTree MakeBalancedTree(IFunction parent, int maxTreeHeight) {
463      if(maxTreeHeight == 0) return parent.GetTreeNode();
464      int minArity;
465      int maxArity;
466      GetMinMaxArity(parent, out minArity, out maxArity);
467      int actualArity = random.Next(minArity, maxArity + 1);
468      if(actualArity > 0) {
469        IFunctionTree parentTree = parent.GetTreeNode();
470        for(int i = 0; i < actualArity; i++) {
471          // first try to find a function that fits into the maxHeight limit
472          IFunction[] possibleFunctions = GetAllowedSubFunctions(parent, i).Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight &&
473            !IsTerminal(f)).ToArray();
474          // no possible function found => extend function set to terminals
475          if(possibleFunctions.Length == 0) {
476            possibleFunctions = GetAllowedSubFunctions(parent, i).Where(f => IsTerminal(f)).ToArray();
477            IFunction selectedTerminal = RandomSelect(possibleFunctions);
478            IFunctionTree newTree = selectedTerminal.GetTreeNode();
479            parentTree.InsertSubTree(i, newTree);
480          } else {
481            IFunction selectedFunction = RandomSelect(possibleFunctions);
482            IFunctionTree newTree = MakeBalancedTree(selectedFunction, maxTreeHeight - 1);
483            parentTree.InsertSubTree(i, newTree);
484          }
485        }
486        return parentTree;
487      }
488      return parent.GetTreeNode();
489    }
490
491    private int GetMinimalTreeHeight(IOperator op) {
492      return ((IntData)op.GetVariable(GPOperatorLibrary.MIN_TREE_HEIGHT).Value).Data;
493    }
494
495    private int GetMinimalTreeSize(IOperator op) {
496      return ((IntData)op.GetVariable(GPOperatorLibrary.MIN_TREE_SIZE).Value).Data;
497    }
498
499    private void TreeForEach(IFunctionTree tree, Action<IFunctionTree> action) {
500      action(tree);
501      foreach(IFunctionTree subTree in tree.SubTrees) {
502        TreeForEach(subTree, action);
503      }
504    }
505
506    private List<IFunctionTree> GetBranchesAtLevel(IFunctionTree tree, int level) {
507      if(level == 1) return new List<IFunctionTree>(tree.SubTrees);
508
509      List<IFunctionTree> branches = new List<IFunctionTree>();
510      foreach(IFunctionTree subTree in tree.SubTrees) {
511        if(subTree.Height >= level - 1)
512          branches.AddRange(GetBranchesAtLevel(subTree, level - 1));
513      }
514      return branches;
515    }
516
517    private IFunction RandomSelect(IList<IFunction> functionSet) {
518      double[] accumulatedTickets = new double[functionSet.Count];
519      double ticketAccumulator = 0;
520      int i = 0;
521      // precalculate the slot-sizes
522      foreach(IFunction function in functionSet) {
523        ticketAccumulator += ((DoubleData)function.GetVariable(GPOperatorLibrary.TICKETS).Value).Data;
524        accumulatedTickets[i] = ticketAccumulator;
525        i++;
526      }
527      // throw ball
528      double r = random.NextDouble() * ticketAccumulator;
529      // find the slot that has been hit
530      for(i = 0; i < accumulatedTickets.Length; i++) {
531        if(r < accumulatedTickets[i]) return functionSet[i];
532      }
533      // sanity check
534      throw new InvalidProgramException(); // should never happen
535    }
536
537    #endregion
538
539  }
540}
Note: See TracBrowser for help on using the repository browser.