Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs @ 6803

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

#1479: Merged grammar editor branch into trunk.

File size: 16.5 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    private const string ClonedSymbolicExpressionTreeGrammarParameterName = "ClonedSymbolicExpressionTreeGrammar";
41    #region Parameter Properties
42    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
43      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
44    }
45    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
46      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
47    }
48    public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
49      get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
50    }
51    public ILookupParameter<ISymbolicExpressionGrammar> ClonedSymbolicExpressionTreeGrammarParameter {
52      get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[ClonedSymbolicExpressionTreeGrammarParameterName]; }
53    }
54    #endregion
55    #region Properties
56    public IntValue MaximumSymbolicExpressionTreeLength {
57      get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
58    }
59    public IntValue MaximumSymbolicExpressionTreeDepth {
60      get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
61    }
62    public ISymbolicExpressionGrammar SymbolicExpressionTreeGrammar {
63      get { return ClonedSymbolicExpressionTreeGrammarParameter.ActualValue; }
64    }
65    #endregion
66
67    [StorableConstructor]
68    protected ProbabilisticTreeCreator(bool deserializing) : base(deserializing) { }
69    protected ProbabilisticTreeCreator(ProbabilisticTreeCreator original, Cloner cloner) : base(original, cloner) { }
70    public ProbabilisticTreeCreator()
71      : base() {
72      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
73      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
74      Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
75      Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName, "An immutable clone of the concrete grammar that is actually used to create and manipulate trees."));
76    }
77
78    public override IDeepCloneable Clone(Cloner cloner) {
79      return new ProbabilisticTreeCreator(this, cloner);
80    }
81    [StorableHook(HookType.AfterDeserialization)]
82    private void AfterDeserialization() {
83      if (!Parameters.ContainsKey(ClonedSymbolicExpressionTreeGrammarParameterName))
84        Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName, "An immutable clone of the concrete grammar that is actually used to create and manipulate trees."));
85    }
86
87    public override IOperation Apply() {
88      if (ClonedSymbolicExpressionTreeGrammarParameter.ActualValue == null) {
89        SymbolicExpressionTreeGrammarParameter.ActualValue.ReadOnly = true;
90        IScope globalScope = ExecutionContext.Scope;
91        while (globalScope.Parent != null)
92          globalScope = globalScope.Parent;
93
94        globalScope.Variables.Add(new Variable(ClonedSymbolicExpressionTreeGrammarParameterName, (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone()));
95      }
96      return base.Apply();
97    }
98
99    protected override ISymbolicExpressionTree Create(IRandom random) {
100      return Create(random, SymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
101    }
102
103    public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar,
104      int maxTreeLength, int maxTreeDepth) {
105      SymbolicExpressionTree tree = new SymbolicExpressionTree();
106      var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
107      if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
108      rootNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar));
109      var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
110      startNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar));
111      if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
112      rootNode.AddSubtree(startNode);
113      PTC2(random, startNode, maxTreeLength, maxTreeDepth);
114      tree.Root = rootNode;
115      return tree;
116    }
117
118    private class TreeExtensionPoint {
119      public ISymbolicExpressionTreeNode Parent { get; set; }
120      public int ChildIndex { get; set; }
121      public int ExtensionPointDepth { get; set; }
122    }
123
124    public static void PTC2(IRandom random, ISymbolicExpressionTreeNode seedNode,
125      int maxLength, int maxDepth) {
126      // make sure it is possible to create a trees smaller than maxLength and maxDepth
127      if (seedNode.Grammar.GetMinimumExpressionLength(seedNode.Symbol) > maxLength)
128        throw new ArgumentException("Cannot create trees of length " + maxLength + " or shorter because of grammar constraints.", "maxLength");
129      if (seedNode.Grammar.GetMinimumExpressionDepth(seedNode.Symbol) > maxDepth)
130        throw new ArgumentException("Cannot create trees of depth " + maxDepth + " or smaller because of grammar constraints.", "maxDepth");
131
132      // tree length is limited by the grammar and by the explicit size constraints
133      int allowedMinLength = seedNode.Grammar.GetMinimumExpressionLength(seedNode.Symbol);
134      int allowedMaxLength = Math.Min(maxLength, seedNode.Grammar.GetMaximumExpressionLength(seedNode.Symbol));
135      int tries = 0;
136      while (tries++ < MAX_TRIES) {
137        // select a target tree length uniformly in the possible range (as determined by explicit limits and limits of the grammar)
138        int targetTreeLength;
139        targetTreeLength = random.Next(allowedMinLength, allowedMaxLength + 1);
140        if (targetTreeLength <= 1 || maxDepth <= 1) return;
141
142        bool success = TryCreateFullTreeFromSeed(random, seedNode, seedNode.Grammar, targetTreeLength, maxDepth);
143
144        // if successful => check constraints and return the tree if everything looks ok       
145        if (success && seedNode.GetLength() <= maxLength && seedNode.GetDepth() <= maxDepth) {
146          return;
147        } else {
148          // clean seedNode
149          while (seedNode.Subtrees.Count() > 0) seedNode.RemoveSubtree(0);
150        }
151        // try a different length MAX_TRIES times
152      }
153      throw new ArgumentException("Couldn't create a random valid tree.");
154    }
155
156    private static bool TryCreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeGrammar globalGrammar,
157      int targetLength, int maxDepth) {
158      List<TreeExtensionPoint> extensionPoints = new List<TreeExtensionPoint>();
159      int currentLength = 1;
160      int totalListMinLength = globalGrammar.GetMinimumExpressionLength(root.Symbol) - 1;
161      int actualArity = SampleArity(random, root, targetLength);
162      if (actualArity < 0) return false;
163
164      for (int i = 0; i < actualArity; i++) {
165        // insert a dummy sub-tree and add the pending extension to the list
166        var dummy = new SymbolicExpressionTreeNode();
167        root.AddSubtree(dummy);
168        extensionPoints.Add(new TreeExtensionPoint { Parent = root, ChildIndex = i, ExtensionPointDepth = 0 });
169      }
170      // while there are pending extension points and we have not reached the limit of adding new extension points
171      while (extensionPoints.Count > 0 && totalListMinLength + currentLength < targetLength) {
172        int randomIndex = random.Next(extensionPoints.Count);
173        TreeExtensionPoint nextExtension = extensionPoints[randomIndex];
174        extensionPoints.RemoveAt(randomIndex);
175        ISymbolicExpressionTreeNode parent = nextExtension.Parent;
176        int argumentIndex = nextExtension.ChildIndex;
177        int extensionDepth = nextExtension.ExtensionPointDepth;
178        if (parent.Grammar.GetMinimumExpressionDepth(parent.Symbol) >= maxDepth - extensionDepth) {
179          ReplaceWithMinimalTree(random, root, parent, argumentIndex);
180        } else {
181          var allowedSymbols = (from s in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, argumentIndex)
182                                where s.InitialFrequency > 0.0
183                                where parent.Grammar.GetMinimumExpressionDepth(s) < maxDepth - extensionDepth + 1
184                                where parent.Grammar.GetMaximumExpressionLength(s) > targetLength - totalListMinLength - currentLength
185                                select s)
186                               .ToList();
187          if (allowedSymbols.Count == 0) return false;
188          var weights = allowedSymbols.Select(x => x.InitialFrequency).ToList();
189          var selectedSymbol = allowedSymbols.SelectRandom(weights, random);
190          ISymbolicExpressionTreeNode newTree = selectedSymbol.CreateTreeNode();
191          if (newTree.HasLocalParameters) newTree.ResetLocalParameters(random);
192          parent.RemoveSubtree(argumentIndex);
193          parent.InsertSubtree(argumentIndex, newTree);
194
195          var topLevelNode = newTree as SymbolicExpressionTreeTopLevelNode;
196          if (topLevelNode != null)
197            topLevelNode.SetGrammar((ISymbolicExpressionTreeGrammar)root.Grammar.Clone());
198
199          currentLength++;
200          totalListMinLength--;
201
202          actualArity = SampleArity(random, newTree, targetLength - currentLength);
203          if (actualArity < 0) return false;
204          for (int i = 0; i < actualArity; i++) {
205            // insert a dummy sub-tree and add the pending extension to the list
206            var dummy = new SymbolicExpressionTreeNode();
207            newTree.AddSubtree(dummy);
208            extensionPoints.Add(new TreeExtensionPoint { Parent = newTree, ChildIndex = i, ExtensionPointDepth = extensionDepth + 1 });
209          }
210          totalListMinLength += newTree.Grammar.GetMinimumExpressionLength(newTree.Symbol);
211        }
212      }
213      // fill all pending extension points
214      while (extensionPoints.Count > 0) {
215        int randomIndex = random.Next(extensionPoints.Count);
216        TreeExtensionPoint nextExtension = extensionPoints[randomIndex];
217        extensionPoints.RemoveAt(randomIndex);
218        ISymbolicExpressionTreeNode parent = nextExtension.Parent;
219        int a = nextExtension.ChildIndex;
220        int d = nextExtension.ExtensionPointDepth;
221        ReplaceWithMinimalTree(random, root, parent, a);
222      }
223      return true;
224    }
225
226    private static void ReplaceWithMinimalTree(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode parent,
227      int childIndex) {
228      // determine possible symbols that will lead to the smallest possible tree
229      var possibleSymbols = (from s in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, childIndex)
230                             where s.InitialFrequency > 0.0
231                             group s by parent.Grammar.GetMinimumExpressionLength(s) into g
232                             orderby g.Key
233                             select g).First().ToList();
234      var weights = possibleSymbols.Select(x => x.InitialFrequency).ToList();
235      var selectedSymbol = possibleSymbols.SelectRandom(weights, random);
236      var tree = selectedSymbol.CreateTreeNode();
237      if (tree.HasLocalParameters) tree.ResetLocalParameters(random);
238      parent.RemoveSubtree(childIndex);
239      parent.InsertSubtree(childIndex, tree);
240
241      var topLevelNode = tree as SymbolicExpressionTreeTopLevelNode;
242      if (topLevelNode != null)
243        topLevelNode.SetGrammar((ISymbolicExpressionTreeGrammar)root.Grammar.Clone());
244
245      for (int i = 0; i < tree.Grammar.GetMinimumSubtreeCount(tree.Symbol); i++) {
246        // insert a dummy sub-tree and add the pending extension to the list
247        var dummy = new SymbolicExpressionTreeNode();
248        tree.AddSubtree(dummy);
249        // replace the just inserted dummy by recursive application
250        ReplaceWithMinimalTree(random, root, tree, i);
251      }
252    }
253
254    private static bool IsTopLevelBranch(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode branch) {
255      return branch is SymbolicExpressionTreeTopLevelNode;
256    }
257
258    private static int SampleArity(IRandom random, ISymbolicExpressionTreeNode node, int targetLength) {
259      // select actualArity randomly with the constraint that the sub-trees in the minimal arity can become large enough
260      int minArity = node.Grammar.GetMinimumSubtreeCount(node.Symbol);
261      int maxArity = node.Grammar.GetMaximumSubtreeCount(node.Symbol);
262      if (maxArity > targetLength) {
263        maxArity = targetLength;
264      }
265      // 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
266      // 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
267      long aggregatedLongestExpressionLength = 0;
268      for (int i = 0; i < maxArity; i++) {
269        aggregatedLongestExpressionLength += (from s in node.Grammar.GetAllowedChildSymbols(node.Symbol, i)
270                                              where s.InitialFrequency > 0.0
271                                              select node.Grammar.GetMaximumExpressionLength(s)).Max();
272        if (i > minArity && aggregatedLongestExpressionLength < targetLength) minArity = i + 1;
273        else break;
274      }
275
276      // 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
277      // 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
278      long aggregatedShortestExpressionLength = 0;
279      for (int i = 0; i < maxArity; i++) {
280        aggregatedShortestExpressionLength += (from s in node.Grammar.GetAllowedChildSymbols(node.Symbol, i)
281                                               where s.InitialFrequency > 0.0
282                                               select node.Grammar.GetMinimumExpressionLength(s)).Min();
283        if (aggregatedShortestExpressionLength > targetLength) {
284          maxArity = i;
285          break;
286        }
287      }
288      if (minArity > maxArity) return -1;
289      return random.Next(minArity, maxArity + 1);
290    }
291  }
292}
Note: See TracBrowser for help on using the repository browser.