Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs @ 5727

Last change on this file since 5727 was 5727, checked in by mkommend, 13 years ago

#1418: Adapted ProbabilisticTreeCreator.

File size: 14.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
32  [StorableClass]
33  [Item("ProbabilisticTreeCreator", "An operator that creates new symbolic expression trees with uniformly distributed length")]
34  public class ProbabilisticTreeCreator : SymbolicExpressionTreeCreator,
35    ISymbolicExpressionTreeSizeConstraintOperator, ISymbolicExpressionTreeGrammarBasedOperator {
36    private const int MAX_TRIES = 100;
37    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
38    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
39    private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
40    #region Parameter Properties
41    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
42      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
43    }
44    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
45      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
46    }
47    public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
48      get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
49    }
50    #endregion
51    #region Properties
52    public IntValue MaximumSymbolicExpressionTreeLength {
53      get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
54    }
55    public IntValue MaximumSymbolicExpressionTreeDepth {
56      get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
57    }
58    public ISymbolicExpressionGrammar SymbolicExpressionTreeGrammar {
59      get { return SymbolicExpressionTreeGrammarParameter.ActualValue; }
60    }
61    #endregion
62
63    [StorableConstructor]
64    protected ProbabilisticTreeCreator(bool deserializing) : base(deserializing) { }
65    protected ProbabilisticTreeCreator(ProbabilisticTreeCreator original, Cloner cloner) : base(original, cloner) { }
66    public ProbabilisticTreeCreator()
67      : base() {
68      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
69      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
70      Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
71    }
72
73    public override IDeepCloneable Clone(Cloner cloner) {
74      return new ProbabilisticTreeCreator(this, cloner);
75    }
76
77    protected override ISymbolicExpressionTree Create(IRandom random) {
78      return Create(random, SymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
79
80    }
81
82    public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar,
83      int maxTreeLength, int maxTreeDepth) {
84      SymbolicExpressionTree tree = new SymbolicExpressionTree();
85      var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
86      if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
87      rootNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar));
88      var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
89      startNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar));
90      if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
91      rootNode.AddSubTree(startNode);
92      PTC2(random, startNode, maxTreeLength, maxTreeDepth);
93      tree.Root = rootNode;
94      return tree;
95    }
96
97    private class TreeExtensionPoint {
98      public ISymbolicExpressionTreeNode Parent { get; set; }
99      public int ChildIndex { get; set; }
100      public int ExtensionPointDepth { get; set; }
101    }
102
103    public static void PTC2(IRandom random, ISymbolicExpressionTreeNode seedNode,
104      int maxLength, int maxDepth) {
105      // tree length is limited by the grammar and by the explicit size constraints
106      int allowedMinLength = seedNode.Grammar.GetMinimumExpressionLength(seedNode.Symbol);
107      int allowedMaxLength = Math.Min(maxLength, seedNode.Grammar.GetMaximumExpressionLength(seedNode.Symbol));
108      int tries = 0;
109      while (tries++ < MAX_TRIES) {
110        // select a target tree length uniformly in the possible range (as determined by explicit limits and limits of the grammar)
111        int targetTreeLength;
112        targetTreeLength = random.Next(allowedMinLength, allowedMaxLength + 1);
113        if (targetTreeLength <= 1 || maxDepth <= 1) return;
114
115        bool success = TryCreateFullTreeFromSeed(random, seedNode, seedNode.Grammar, targetTreeLength, maxDepth);
116
117        // if successful => check constraints and return the tree if everything looks ok       
118        if (success && seedNode.GetLength() <= maxLength && seedNode.GetDepth() <= maxDepth) {
119          return;
120        } else {
121          // clean seedNode
122          while (seedNode.SubTrees.Count() > 0) seedNode.RemoveSubTree(0);
123        }
124        // try a different length MAX_TRIES times
125      }
126      throw new ArgumentException("Couldn't create a random valid tree.");
127    }
128
129    private static bool TryCreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeGrammar globalGrammar,
130      int targetLength, int maxDepth) {
131      List<TreeExtensionPoint> extensionPoints = new List<TreeExtensionPoint>();
132      int currentLength = 1;
133      int totalListMinLength = globalGrammar.GetMinimumExpressionLength(root.Symbol) - 1;
134      int actualArity = SampleArity(random, root, targetLength);
135      if (actualArity < 0) return false;
136
137      for (int i = 0; i < actualArity; i++) {
138        // insert a dummy sub-tree and add the pending extension to the list
139        var dummy = new SymbolicExpressionTreeNode();
140        root.AddSubTree(dummy);
141        extensionPoints.Add(new TreeExtensionPoint { Parent = root, ChildIndex = i, ExtensionPointDepth = 0 });
142      }
143      // while there are pending extension points and we have not reached the limit of adding new extension points
144      while (extensionPoints.Count > 0 && totalListMinLength + currentLength < targetLength) {
145        int randomIndex = random.Next(extensionPoints.Count);
146        TreeExtensionPoint nextExtension = extensionPoints[randomIndex];
147        extensionPoints.RemoveAt(randomIndex);
148        ISymbolicExpressionTreeNode parent = nextExtension.Parent;
149        int argumentIndex = nextExtension.ChildIndex;
150        int extensionDepth = nextExtension.ExtensionPointDepth;
151        if (extensionDepth + parent.Grammar.GetMinimumExpressionDepth(parent.Symbol) >= maxDepth) {
152          ReplaceWithMinimalTree(random, root, parent, argumentIndex);
153        } else {
154          var allowedSymbols = (from s in parent.Grammar.Symbols
155                                where parent.Grammar.IsAllowedChildSymbol(parent.Symbol, s, argumentIndex)
156                                where parent.Grammar.GetMinimumExpressionDepth(s) + extensionDepth - 1 < maxDepth
157                                where parent.Grammar.GetMaximumExpressionLength(s) > targetLength - totalListMinLength - currentLength
158                                select s)
159                               .ToList();
160          var weights = allowedSymbols.Select(x => x.InitialFrequency).ToList();
161          var selectedSymbol = allowedSymbols.SelectRandom(weights, random);
162          ISymbolicExpressionTreeNode newTree = selectedSymbol.CreateTreeNode();
163          if (newTree.HasLocalParameters) newTree.ResetLocalParameters(random);
164          parent.RemoveSubTree(argumentIndex);
165          parent.InsertSubTree(argumentIndex, newTree);
166
167          var topLevelNode = newTree as SymbolicExpressionTreeTopLevelNode;
168          if (topLevelNode != null)
169            topLevelNode.SetGrammar((ISymbolicExpressionTreeGrammar)root.Grammar.Clone());
170
171          currentLength++;
172          totalListMinLength--;
173
174          actualArity = SampleArity(random, newTree, targetLength - currentLength);
175          if (actualArity < 0) return false;
176          for (int i = 0; i < actualArity; i++) {
177            // insert a dummy sub-tree and add the pending extension to the list
178            var dummy = new SymbolicExpressionTreeNode();
179            newTree.AddSubTree(dummy);
180            extensionPoints.Add(new TreeExtensionPoint { Parent = newTree, ChildIndex = i, ExtensionPointDepth = extensionDepth + 1 });
181          }
182          totalListMinLength += newTree.Grammar.GetMinimumExpressionLength(newTree.Symbol);
183        }
184      }
185      // fill all pending extension points
186      while (extensionPoints.Count > 0) {
187        int randomIndex = random.Next(extensionPoints.Count);
188        TreeExtensionPoint nextExtension = extensionPoints[randomIndex];
189        extensionPoints.RemoveAt(randomIndex);
190        ISymbolicExpressionTreeNode parent = nextExtension.Parent;
191        int a = nextExtension.ChildIndex;
192        int d = nextExtension.ExtensionPointDepth;
193        ReplaceWithMinimalTree(random, root, parent, a);
194      }
195      return true;
196    }
197
198    private static void ReplaceWithMinimalTree(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode parent,
199      int childIndex) {
200      // determine possible symbols that will lead to the smallest possible tree
201      var possibleSymbols = (from s in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, childIndex)
202                             group s by parent.Grammar.GetMinimumExpressionLength(s) into g
203                             orderby g.Key
204                             select g).First().ToList();
205      var weights = possibleSymbols.Select(x => x.InitialFrequency).ToList();
206      var selectedSymbol = possibleSymbols.SelectRandom(weights, random);
207      var tree = selectedSymbol.CreateTreeNode();
208      if (tree.HasLocalParameters) tree.ResetLocalParameters(random);
209      parent.RemoveSubTree(childIndex);
210      parent.InsertSubTree(childIndex, tree);
211
212      var topLevelNode = tree as SymbolicExpressionTreeTopLevelNode;
213      if (topLevelNode != null)
214        topLevelNode.SetGrammar((ISymbolicExpressionTreeGrammar)root.Grammar.Clone());
215
216      for (int i = 0; i < tree.Grammar.GetMinimumSubtreeCount(tree.Symbol); i++) {
217        // insert a dummy sub-tree and add the pending extension to the list
218        var dummy = new SymbolicExpressionTreeNode();
219        tree.AddSubTree(dummy);
220        // replace the just inserted dummy by recursive application
221        ReplaceWithMinimalTree(random, root, tree, i);
222      }
223    }
224
225    private static bool IsTopLevelBranch(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode branch) {
226      return branch is SymbolicExpressionTreeTopLevelNode;
227    }
228
229    private static int SampleArity(IRandom random, ISymbolicExpressionTreeNode node, int targetLength) {
230      // select actualArity randomly with the constraint that the sub-trees in the minimal arity can become large enough
231      int minArity = node.Grammar.GetMinimumSubtreeCount(node.Symbol);
232      int maxArity = node.Grammar.GetMaximumSubtreeCount(node.Symbol);
233      if (maxArity > targetLength) {
234        maxArity = targetLength;
235      }
236      // the min number of sub-trees has to be set to a value that is large enough so that the largest possible tree is at least tree length
237      // if 1..3 trees are possible and the largest possible first sub-tree is smaller larger than the target length then minArity should be at least 2
238      long aggregatedLongestExpressionLength = 0;
239      for (int i = 0; i < maxArity; i++) {
240        aggregatedLongestExpressionLength += (from s in node.Grammar.GetAllowedChildSymbols(node.Symbol, i)
241                                              select node.Grammar.GetMaximumExpressionLength(s)).Max();
242        if (aggregatedLongestExpressionLength < targetLength) minArity = i;
243        else break;
244      }
245
246      // the max number of sub-trees has to be set to a value that is small enough so that the smallest possible tree is at most tree length
247      // if 1..3 trees are possible and the smallest possible first sub-tree is already larger than the target length then maxArity should be at most 0
248      long aggregatedShortestExpressionLength = 0;
249      for (int i = 0; i < maxArity; i++) {
250        aggregatedShortestExpressionLength += (from s in node.Grammar.GetAllowedChildSymbols(node.Symbol, i)
251                                               select node.Grammar.GetMinimumExpressionLength(s)).Min();
252        if (aggregatedShortestExpressionLength > targetLength) {
253          maxArity = i;
254          break;
255        }
256      }
257      if (minArity > maxArity) return -1;
258      return random.Next(minArity, maxArity + 1);
259    }
260  }
261}
Note: See TracBrowser for help on using the repository browser.