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
RevLine 
[645]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[645]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
[3462]22using System;
[4068]23using System.Collections.Generic;
[3462]24using System.Linq;
[4722]25using HeuristicLab.Common;
[645]26using HeuristicLab.Core;
27using HeuristicLab.Data;
[5521]28using HeuristicLab.Parameters;
[4068]29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[645]30
[5499]31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
[3223]32  [StorableClass]
[5549]33  [Item("ProbabilisticTreeCreator", "An operator that creates new symbolic expression trees with uniformly distributed length")]
[5618]34  public class ProbabilisticTreeCreator : SymbolicExpressionTreeCreator,
[5686]35    ISymbolicExpressionTreeSizeConstraintOperator, ISymbolicExpressionTreeGrammarBasedOperator {
[3369]36    private const int MAX_TRIES = 100;
[5499]37    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
38    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
39    private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
[6233]40    private const string ClonedSymbolicExpressionTreeGrammarParameterName = "ClonedSymbolicExpressionTreeGrammar";
[5499]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    }
[5686]48    public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
49      get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
[5499]50    }
[6233]51    public ILookupParameter<ISymbolicExpressionGrammar> ClonedSymbolicExpressionTreeGrammarParameter {
52      get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[ClonedSymbolicExpressionTreeGrammarParameterName]; }
53    }
[5499]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    }
[5686]62    public ISymbolicExpressionGrammar SymbolicExpressionTreeGrammar {
[6233]63      get { return ClonedSymbolicExpressionTreeGrammarParameter.ActualValue; }
[5499]64    }
65    #endregion
66
[4722]67    [StorableConstructor]
[5618]68    protected ProbabilisticTreeCreator(bool deserializing) : base(deserializing) { }
69    protected ProbabilisticTreeCreator(ProbabilisticTreeCreator original, Cloner cloner) : base(original, cloner) { }
[5499]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)."));
[5686]74      Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
[6233]75      Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName, "An immutable clone of the concrete grammar that is actually used to create and manipulate trees."));
[5499]76    }
[3338]77
[4722]78    public override IDeepCloneable Clone(Cloner cloner) {
79      return new ProbabilisticTreeCreator(this, cloner);
[645]80    }
[6233]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    }
[645]86
[6233]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
[5510]99    protected override ISymbolicExpressionTree Create(IRandom random) {
[5686]100      return Create(random, SymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
[2447]101    }
102
[5686]103    public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar,
104      int maxTreeLength, int maxTreeDepth) {
[3223]105      SymbolicExpressionTree tree = new SymbolicExpressionTree();
[5686]106      var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
[3442]107      if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
[4249]108      rootNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar));
[5686]109      var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
110      startNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar));
111      if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
[5733]112      rootNode.AddSubtree(startNode);
[5686]113      PTC2(random, startNode, maxTreeLength, maxTreeDepth);
114      tree.Root = rootNode;
[3223]115      return tree;
116    }
117
[3338]118    private class TreeExtensionPoint {
[5499]119      public ISymbolicExpressionTreeNode Parent { get; set; }
[3338]120      public int ChildIndex { get; set; }
121      public int ExtensionPointDepth { get; set; }
[3223]122    }
[3360]123
[5686]124    public static void PTC2(IRandom random, ISymbolicExpressionTreeNode seedNode,
125      int maxLength, int maxDepth) {
[6009]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
[5549]132      // tree length is limited by the grammar and by the explicit size constraints
[5686]133      int allowedMinLength = seedNode.Grammar.GetMinimumExpressionLength(seedNode.Symbol);
134      int allowedMaxLength = Math.Min(maxLength, seedNode.Grammar.GetMaximumExpressionLength(seedNode.Symbol));
[3369]135      int tries = 0;
136      while (tries++ < MAX_TRIES) {
[5549]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);
[5686]140        if (targetTreeLength <= 1 || maxDepth <= 1) return;
[3369]141
[5727]142        bool success = TryCreateFullTreeFromSeed(random, seedNode, seedNode.Grammar, targetTreeLength, maxDepth);
[3369]143
[4189]144        // if successful => check constraints and return the tree if everything looks ok       
[5549]145        if (success && seedNode.GetLength() <= maxLength && seedNode.GetDepth() <= maxDepth) {
[5686]146          return;
[3369]147        } else {
148          // clean seedNode
[5733]149          while (seedNode.Subtrees.Count() > 0) seedNode.RemoveSubtree(0);
[3360]150        }
[5549]151        // try a different length MAX_TRIES times
[3369]152      }
[3825]153      throw new ArgumentException("Couldn't create a random valid tree.");
[3338]154    }
155
[5727]156    private static bool TryCreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeGrammar globalGrammar,
[5686]157      int targetLength, int maxDepth) {
[3338]158      List<TreeExtensionPoint> extensionPoints = new List<TreeExtensionPoint>();
[5549]159      int currentLength = 1;
[5686]160      int totalListMinLength = globalGrammar.GetMinimumExpressionLength(root.Symbol) - 1;
[5549]161      int actualArity = SampleArity(random, root, targetLength);
[5727]162      if (actualArity < 0) return false;
163
[3223]164      for (int i = 0; i < actualArity; i++) {
165        // insert a dummy sub-tree and add the pending extension to the list
[3338]166        var dummy = new SymbolicExpressionTreeNode();
[5733]167        root.AddSubtree(dummy);
[4219]168        extensionPoints.Add(new TreeExtensionPoint { Parent = root, ChildIndex = i, ExtensionPointDepth = 0 });
[3223]169      }
170      // while there are pending extension points and we have not reached the limit of adding new extension points
[5549]171      while (extensionPoints.Count > 0 && totalListMinLength + currentLength < targetLength) {
[3294]172        int randomIndex = random.Next(extensionPoints.Count);
[3338]173        TreeExtensionPoint nextExtension = extensionPoints[randomIndex];
[3294]174        extensionPoints.RemoveAt(randomIndex);
[5499]175        ISymbolicExpressionTreeNode parent = nextExtension.Parent;
[3338]176        int argumentIndex = nextExtension.ChildIndex;
177        int extensionDepth = nextExtension.ExtensionPointDepth;
[6009]178        if (parent.Grammar.GetMinimumExpressionDepth(parent.Symbol) >= maxDepth - extensionDepth) {
[5686]179          ReplaceWithMinimalTree(random, root, parent, argumentIndex);
[3223]180        } else {
[6803]181          var allowedSymbols = (from s in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, argumentIndex)
[5925]182                                where s.InitialFrequency > 0.0
[6009]183                                where parent.Grammar.GetMinimumExpressionDepth(s) < maxDepth - extensionDepth + 1
[5686]184                                where parent.Grammar.GetMaximumExpressionLength(s) > targetLength - totalListMinLength - currentLength
[5499]185                                select s)
186                               .ToList();
[6233]187          if (allowedSymbols.Count == 0) return false;
[5499]188          var weights = allowedSymbols.Select(x => x.InitialFrequency).ToList();
189          var selectedSymbol = allowedSymbols.SelectRandom(weights, random);
190          ISymbolicExpressionTreeNode newTree = selectedSymbol.CreateTreeNode();
[3442]191          if (newTree.HasLocalParameters) newTree.ResetLocalParameters(random);
[5733]192          parent.RemoveSubtree(argumentIndex);
193          parent.InsertSubtree(argumentIndex, newTree);
[3338]194
[5686]195          var topLevelNode = newTree as SymbolicExpressionTreeTopLevelNode;
196          if (topLevelNode != null)
197            topLevelNode.SetGrammar((ISymbolicExpressionTreeGrammar)root.Grammar.Clone());
[3338]198
[5549]199          currentLength++;
200          totalListMinLength--;
[3223]201
[5549]202          actualArity = SampleArity(random, newTree, targetLength - currentLength);
[5727]203          if (actualArity < 0) return false;
[3223]204          for (int i = 0; i < actualArity; i++) {
205            // insert a dummy sub-tree and add the pending extension to the list
[3338]206            var dummy = new SymbolicExpressionTreeNode();
[5733]207            newTree.AddSubtree(dummy);
[3338]208            extensionPoints.Add(new TreeExtensionPoint { Parent = newTree, ChildIndex = i, ExtensionPointDepth = extensionDepth + 1 });
[3223]209          }
[5686]210          totalListMinLength += newTree.Grammar.GetMinimumExpressionLength(newTree.Symbol);
[3223]211        }
212      }
213      // fill all pending extension points
[3294]214      while (extensionPoints.Count > 0) {
215        int randomIndex = random.Next(extensionPoints.Count);
[3338]216        TreeExtensionPoint nextExtension = extensionPoints[randomIndex];
[3294]217        extensionPoints.RemoveAt(randomIndex);
[5499]218        ISymbolicExpressionTreeNode parent = nextExtension.Parent;
[3338]219        int a = nextExtension.ChildIndex;
220        int d = nextExtension.ExtensionPointDepth;
[5686]221        ReplaceWithMinimalTree(random, root, parent, a);
[3223]222      }
[5727]223      return true;
[2210]224    }
[3223]225
[5499]226    private static void ReplaceWithMinimalTree(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode parent,
[5686]227      int childIndex) {
[3338]228      // determine possible symbols that will lead to the smallest possible tree
[5686]229      var possibleSymbols = (from s in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, childIndex)
[5925]230                             where s.InitialFrequency > 0.0
[5686]231                             group s by parent.Grammar.GetMinimumExpressionLength(s) into g
[3338]232                             orderby g.Key
[5499]233                             select g).First().ToList();
234      var weights = possibleSymbols.Select(x => x.InitialFrequency).ToList();
235      var selectedSymbol = possibleSymbols.SelectRandom(weights, random);
[3338]236      var tree = selectedSymbol.CreateTreeNode();
[3442]237      if (tree.HasLocalParameters) tree.ResetLocalParameters(random);
[5733]238      parent.RemoveSubtree(childIndex);
239      parent.InsertSubtree(childIndex, tree);
[5686]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++) {
[3338]246        // insert a dummy sub-tree and add the pending extension to the list
247        var dummy = new SymbolicExpressionTreeNode();
[5733]248        tree.AddSubtree(dummy);
[3338]249        // replace the just inserted dummy by recursive application
[5686]250        ReplaceWithMinimalTree(random, root, tree, i);
[3338]251      }
252    }
[3360]253
[5499]254    private static bool IsTopLevelBranch(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode branch) {
[3369]255      return branch is SymbolicExpressionTreeTopLevelNode;
[3338]256    }
257
[5549]258    private static int SampleArity(IRandom random, ISymbolicExpressionTreeNode node, int targetLength) {
[3223]259      // select actualArity randomly with the constraint that the sub-trees in the minimal arity can become large enough
[5686]260      int minArity = node.Grammar.GetMinimumSubtreeCount(node.Symbol);
261      int maxArity = node.Grammar.GetMaximumSubtreeCount(node.Symbol);
[5549]262      if (maxArity > targetLength) {
263        maxArity = targetLength;
[3223]264      }
[5549]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
[3237]267      long aggregatedLongestExpressionLength = 0;
[3223]268      for (int i = 0; i < maxArity; i++) {
[5686]269        aggregatedLongestExpressionLength += (from s in node.Grammar.GetAllowedChildSymbols(node.Symbol, i)
[5925]270                                              where s.InitialFrequency > 0.0
[5686]271                                              select node.Grammar.GetMaximumExpressionLength(s)).Max();
[6803]272        if (i > minArity && aggregatedLongestExpressionLength < targetLength) minArity = i + 1;
[3223]273        else break;
274      }
275
[5549]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
[3237]278      long aggregatedShortestExpressionLength = 0;
[3223]279      for (int i = 0; i < maxArity; i++) {
[5686]280        aggregatedShortestExpressionLength += (from s in node.Grammar.GetAllowedChildSymbols(node.Symbol, i)
[5925]281                                               where s.InitialFrequency > 0.0
[5686]282                                               select node.Grammar.GetMinimumExpressionLength(s)).Min();
[5549]283        if (aggregatedShortestExpressionLength > targetLength) {
[3223]284          maxArity = i;
285          break;
286        }
287      }
[5727]288      if (minArity > maxArity) return -1;
[3223]289      return random.Next(minArity, maxArity + 1);
290    }
[2447]291  }
[645]292}
Note: See TracBrowser for help on using the repository browser.