[645] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11170] | 3 | * Copyright (C) 2002-2014 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] | 22 | using System;
|
---|
[4068] | 23 | using System.Collections.Generic;
|
---|
[3462] | 24 | using System.Linq;
|
---|
[4722] | 25 | using HeuristicLab.Common;
|
---|
[645] | 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
[5521] | 28 | using HeuristicLab.Parameters;
|
---|
[4068] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[7012] | 30 | using HeuristicLab.PluginInfrastructure;
|
---|
[645] | 31 |
|
---|
[5499] | 32 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
[7012] | 33 | [NonDiscoverableType]
|
---|
[3223] | 34 | [StorableClass]
|
---|
[5549] | 35 | [Item("ProbabilisticTreeCreator", "An operator that creates new symbolic expression trees with uniformly distributed length")]
|
---|
[5618] | 36 | public class ProbabilisticTreeCreator : SymbolicExpressionTreeCreator,
|
---|
[5686] | 37 | ISymbolicExpressionTreeSizeConstraintOperator, ISymbolicExpressionTreeGrammarBasedOperator {
|
---|
[3369] | 38 | private const int MAX_TRIES = 100;
|
---|
[5499] | 39 | private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
|
---|
| 40 | private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
|
---|
| 41 | private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
|
---|
[6233] | 42 | private const string ClonedSymbolicExpressionTreeGrammarParameterName = "ClonedSymbolicExpressionTreeGrammar";
|
---|
[5499] | 43 | #region Parameter Properties
|
---|
| 44 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
|
---|
| 45 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
|
---|
| 46 | }
|
---|
| 47 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
|
---|
| 48 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
|
---|
| 49 | }
|
---|
[5686] | 50 | public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
|
---|
| 51 | get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
|
---|
[5499] | 52 | }
|
---|
[6233] | 53 | public ILookupParameter<ISymbolicExpressionGrammar> ClonedSymbolicExpressionTreeGrammarParameter {
|
---|
| 54 | get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[ClonedSymbolicExpressionTreeGrammarParameterName]; }
|
---|
| 55 | }
|
---|
[5499] | 56 | #endregion
|
---|
| 57 | #region Properties
|
---|
| 58 | public IntValue MaximumSymbolicExpressionTreeLength {
|
---|
| 59 | get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
|
---|
| 60 | }
|
---|
| 61 | public IntValue MaximumSymbolicExpressionTreeDepth {
|
---|
| 62 | get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
|
---|
| 63 | }
|
---|
[5686] | 64 | public ISymbolicExpressionGrammar SymbolicExpressionTreeGrammar {
|
---|
[6233] | 65 | get { return ClonedSymbolicExpressionTreeGrammarParameter.ActualValue; }
|
---|
[5499] | 66 | }
|
---|
| 67 | #endregion
|
---|
| 68 |
|
---|
[4722] | 69 | [StorableConstructor]
|
---|
[5618] | 70 | protected ProbabilisticTreeCreator(bool deserializing) : base(deserializing) { }
|
---|
| 71 | protected ProbabilisticTreeCreator(ProbabilisticTreeCreator original, Cloner cloner) : base(original, cloner) { }
|
---|
[5499] | 72 | public ProbabilisticTreeCreator()
|
---|
| 73 | : base() {
|
---|
| 74 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
|
---|
| 75 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
|
---|
[5686] | 76 | Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
|
---|
[6233] | 77 | Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName, "An immutable clone of the concrete grammar that is actually used to create and manipulate trees."));
|
---|
[5499] | 78 | }
|
---|
[3338] | 79 |
|
---|
[4722] | 80 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 81 | return new ProbabilisticTreeCreator(this, cloner);
|
---|
[645] | 82 | }
|
---|
[6233] | 83 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 84 | private void AfterDeserialization() {
|
---|
| 85 | if (!Parameters.ContainsKey(ClonedSymbolicExpressionTreeGrammarParameterName))
|
---|
| 86 | Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName, "An immutable clone of the concrete grammar that is actually used to create and manipulate trees."));
|
---|
| 87 | }
|
---|
[645] | 88 |
|
---|
[10507] | 89 | public override IOperation InstrumentedApply() {
|
---|
[6233] | 90 | if (ClonedSymbolicExpressionTreeGrammarParameter.ActualValue == null) {
|
---|
| 91 | SymbolicExpressionTreeGrammarParameter.ActualValue.ReadOnly = true;
|
---|
| 92 | IScope globalScope = ExecutionContext.Scope;
|
---|
| 93 | while (globalScope.Parent != null)
|
---|
| 94 | globalScope = globalScope.Parent;
|
---|
| 95 |
|
---|
| 96 | globalScope.Variables.Add(new Variable(ClonedSymbolicExpressionTreeGrammarParameterName, (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone()));
|
---|
| 97 | }
|
---|
[10507] | 98 | return base.InstrumentedApply();
|
---|
[6233] | 99 | }
|
---|
| 100 |
|
---|
[5510] | 101 | protected override ISymbolicExpressionTree Create(IRandom random) {
|
---|
[5686] | 102 | return Create(random, SymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
|
---|
[2447] | 103 | }
|
---|
| 104 |
|
---|
[7012] | 105 | public override ISymbolicExpressionTree CreateTree(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
|
---|
| 106 | return Create(random, grammar, maxTreeLength, maxTreeDepth);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
|
---|
[3223] | 110 | SymbolicExpressionTree tree = new SymbolicExpressionTree();
|
---|
[5686] | 111 | var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
|
---|
[3442] | 112 | if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
|
---|
[4249] | 113 | rootNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar));
|
---|
[5686] | 114 | var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
|
---|
| 115 | startNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar));
|
---|
| 116 | if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
|
---|
[5733] | 117 | rootNode.AddSubtree(startNode);
|
---|
[5686] | 118 | PTC2(random, startNode, maxTreeLength, maxTreeDepth);
|
---|
| 119 | tree.Root = rootNode;
|
---|
[3223] | 120 | return tree;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[3338] | 123 | private class TreeExtensionPoint {
|
---|
[5499] | 124 | public ISymbolicExpressionTreeNode Parent { get; set; }
|
---|
[3338] | 125 | public int ChildIndex { get; set; }
|
---|
| 126 | public int ExtensionPointDepth { get; set; }
|
---|
[6911] | 127 | public int MaximumExtensionLength { get; set; }
|
---|
| 128 | public int MinimumExtensionLength { get; set; }
|
---|
[3223] | 129 | }
|
---|
[3360] | 130 |
|
---|
[5686] | 131 | public static void PTC2(IRandom random, ISymbolicExpressionTreeNode seedNode,
|
---|
| 132 | int maxLength, int maxDepth) {
|
---|
[6009] | 133 | // make sure it is possible to create a trees smaller than maxLength and maxDepth
|
---|
| 134 | if (seedNode.Grammar.GetMinimumExpressionLength(seedNode.Symbol) > maxLength)
|
---|
| 135 | throw new ArgumentException("Cannot create trees of length " + maxLength + " or shorter because of grammar constraints.", "maxLength");
|
---|
| 136 | if (seedNode.Grammar.GetMinimumExpressionDepth(seedNode.Symbol) > maxDepth)
|
---|
| 137 | throw new ArgumentException("Cannot create trees of depth " + maxDepth + " or smaller because of grammar constraints.", "maxDepth");
|
---|
| 138 |
|
---|
[5549] | 139 | // tree length is limited by the grammar and by the explicit size constraints
|
---|
[5686] | 140 | int allowedMinLength = seedNode.Grammar.GetMinimumExpressionLength(seedNode.Symbol);
|
---|
[6911] | 141 | int allowedMaxLength = Math.Min(maxLength, seedNode.Grammar.GetMaximumExpressionLength(seedNode.Symbol, maxDepth));
|
---|
[3369] | 142 | int tries = 0;
|
---|
| 143 | while (tries++ < MAX_TRIES) {
|
---|
[5549] | 144 | // select a target tree length uniformly in the possible range (as determined by explicit limits and limits of the grammar)
|
---|
| 145 | int targetTreeLength;
|
---|
| 146 | targetTreeLength = random.Next(allowedMinLength, allowedMaxLength + 1);
|
---|
[5686] | 147 | if (targetTreeLength <= 1 || maxDepth <= 1) return;
|
---|
[3369] | 148 |
|
---|
[6911] | 149 | bool success = TryCreateFullTreeFromSeed(random, seedNode, targetTreeLength - 1, maxDepth - 1);
|
---|
[3369] | 150 |
|
---|
[4189] | 151 | // if successful => check constraints and return the tree if everything looks ok
|
---|
[5549] | 152 | if (success && seedNode.GetLength() <= maxLength && seedNode.GetDepth() <= maxDepth) {
|
---|
[5686] | 153 | return;
|
---|
[3369] | 154 | } else {
|
---|
| 155 | // clean seedNode
|
---|
[5733] | 156 | while (seedNode.Subtrees.Count() > 0) seedNode.RemoveSubtree(0);
|
---|
[3360] | 157 | }
|
---|
[5549] | 158 | // try a different length MAX_TRIES times
|
---|
[3369] | 159 | }
|
---|
[3825] | 160 | throw new ArgumentException("Couldn't create a random valid tree.");
|
---|
[3338] | 161 | }
|
---|
| 162 |
|
---|
[6911] | 163 | private static bool TryCreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root,
|
---|
[5686] | 164 | int targetLength, int maxDepth) {
|
---|
[3338] | 165 | List<TreeExtensionPoint> extensionPoints = new List<TreeExtensionPoint>();
|
---|
[6911] | 166 | int currentLength = 0;
|
---|
| 167 | int actualArity = SampleArity(random, root, targetLength, maxDepth);
|
---|
[5727] | 168 | if (actualArity < 0) return false;
|
---|
| 169 |
|
---|
[3223] | 170 | for (int i = 0; i < actualArity; i++) {
|
---|
| 171 | // insert a dummy sub-tree and add the pending extension to the list
|
---|
[3338] | 172 | var dummy = new SymbolicExpressionTreeNode();
|
---|
[5733] | 173 | root.AddSubtree(dummy);
|
---|
[6911] | 174 | var x = new TreeExtensionPoint { Parent = root, ChildIndex = i, ExtensionPointDepth = 0 };
|
---|
| 175 | FillExtensionLengths(x, maxDepth);
|
---|
| 176 | extensionPoints.Add(x);
|
---|
[3223] | 177 | }
|
---|
[8311] | 178 | //necessary to use long data type as the extension point length could be int.MaxValue
|
---|
| 179 | long minExtensionPointsLength = extensionPoints.Select(x => (long)x.MinimumExtensionLength).Sum();
|
---|
| 180 | long maxExtensionPointsLength = extensionPoints.Select(x => (long)x.MaximumExtensionLength).Sum();
|
---|
[6911] | 181 |
|
---|
[3223] | 182 | // while there are pending extension points and we have not reached the limit of adding new extension points
|
---|
[6911] | 183 | while (extensionPoints.Count > 0 && minExtensionPointsLength + currentLength <= targetLength) {
|
---|
[3294] | 184 | int randomIndex = random.Next(extensionPoints.Count);
|
---|
[3338] | 185 | TreeExtensionPoint nextExtension = extensionPoints[randomIndex];
|
---|
[3294] | 186 | extensionPoints.RemoveAt(randomIndex);
|
---|
[5499] | 187 | ISymbolicExpressionTreeNode parent = nextExtension.Parent;
|
---|
[3338] | 188 | int argumentIndex = nextExtension.ChildIndex;
|
---|
| 189 | int extensionDepth = nextExtension.ExtensionPointDepth;
|
---|
[6911] | 190 |
|
---|
| 191 | if (parent.Grammar.GetMinimumExpressionDepth(parent.Symbol) > maxDepth - extensionDepth) {
|
---|
[5686] | 192 | ReplaceWithMinimalTree(random, root, parent, argumentIndex);
|
---|
[6911] | 193 | int insertedTreeLength = parent.GetSubtree(argumentIndex).GetLength();
|
---|
| 194 | currentLength += insertedTreeLength;
|
---|
| 195 | minExtensionPointsLength -= insertedTreeLength;
|
---|
| 196 | maxExtensionPointsLength -= insertedTreeLength;
|
---|
[3223] | 197 | } else {
|
---|
[6911] | 198 | //remove currently chosen extension point from calculation
|
---|
| 199 | minExtensionPointsLength -= nextExtension.MinimumExtensionLength;
|
---|
| 200 | maxExtensionPointsLength -= nextExtension.MaximumExtensionLength;
|
---|
| 201 |
|
---|
| 202 | var symbols = from s in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, argumentIndex)
|
---|
| 203 | where s.InitialFrequency > 0.0
|
---|
| 204 | where parent.Grammar.GetMinimumExpressionDepth(s) <= maxDepth - extensionDepth
|
---|
| 205 | where parent.Grammar.GetMinimumExpressionLength(s) <= targetLength - currentLength - minExtensionPointsLength
|
---|
| 206 | select s;
|
---|
| 207 | if (maxExtensionPointsLength < targetLength - currentLength)
|
---|
| 208 | symbols = from s in symbols
|
---|
| 209 | where parent.Grammar.GetMaximumExpressionLength(s, maxDepth - extensionDepth) >= targetLength - currentLength - maxExtensionPointsLength
|
---|
| 210 | select s;
|
---|
| 211 | var allowedSymbols = symbols.ToList();
|
---|
| 212 |
|
---|
[6233] | 213 | if (allowedSymbols.Count == 0) return false;
|
---|
[5499] | 214 | var weights = allowedSymbols.Select(x => x.InitialFrequency).ToList();
|
---|
| 215 | var selectedSymbol = allowedSymbols.SelectRandom(weights, random);
|
---|
| 216 | ISymbolicExpressionTreeNode newTree = selectedSymbol.CreateTreeNode();
|
---|
[3442] | 217 | if (newTree.HasLocalParameters) newTree.ResetLocalParameters(random);
|
---|
[5733] | 218 | parent.RemoveSubtree(argumentIndex);
|
---|
| 219 | parent.InsertSubtree(argumentIndex, newTree);
|
---|
[3338] | 220 |
|
---|
[5686] | 221 | var topLevelNode = newTree as SymbolicExpressionTreeTopLevelNode;
|
---|
| 222 | if (topLevelNode != null)
|
---|
| 223 | topLevelNode.SetGrammar((ISymbolicExpressionTreeGrammar)root.Grammar.Clone());
|
---|
[3338] | 224 |
|
---|
[5549] | 225 | currentLength++;
|
---|
[6911] | 226 | actualArity = SampleArity(random, newTree, targetLength - currentLength, maxDepth - extensionDepth);
|
---|
[5727] | 227 | if (actualArity < 0) return false;
|
---|
[3223] | 228 | for (int i = 0; i < actualArity; i++) {
|
---|
| 229 | // insert a dummy sub-tree and add the pending extension to the list
|
---|
[3338] | 230 | var dummy = new SymbolicExpressionTreeNode();
|
---|
[5733] | 231 | newTree.AddSubtree(dummy);
|
---|
[6911] | 232 | var x = new TreeExtensionPoint { Parent = newTree, ChildIndex = i, ExtensionPointDepth = extensionDepth + 1 };
|
---|
| 233 | FillExtensionLengths(x, maxDepth);
|
---|
| 234 | extensionPoints.Add(x);
|
---|
| 235 | maxExtensionPointsLength += x.MaximumExtensionLength;
|
---|
| 236 | minExtensionPointsLength += x.MinimumExtensionLength;
|
---|
[3223] | 237 | }
|
---|
| 238 | }
|
---|
| 239 | }
|
---|
| 240 | // fill all pending extension points
|
---|
[3294] | 241 | while (extensionPoints.Count > 0) {
|
---|
| 242 | int randomIndex = random.Next(extensionPoints.Count);
|
---|
[3338] | 243 | TreeExtensionPoint nextExtension = extensionPoints[randomIndex];
|
---|
[3294] | 244 | extensionPoints.RemoveAt(randomIndex);
|
---|
[5499] | 245 | ISymbolicExpressionTreeNode parent = nextExtension.Parent;
|
---|
[3338] | 246 | int a = nextExtension.ChildIndex;
|
---|
[5686] | 247 | ReplaceWithMinimalTree(random, root, parent, a);
|
---|
[3223] | 248 | }
|
---|
[5727] | 249 | return true;
|
---|
[2210] | 250 | }
|
---|
[3223] | 251 |
|
---|
[5499] | 252 | private static void ReplaceWithMinimalTree(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode parent,
|
---|
[5686] | 253 | int childIndex) {
|
---|
[3338] | 254 | // determine possible symbols that will lead to the smallest possible tree
|
---|
[5686] | 255 | var possibleSymbols = (from s in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, childIndex)
|
---|
[5925] | 256 | where s.InitialFrequency > 0.0
|
---|
[5686] | 257 | group s by parent.Grammar.GetMinimumExpressionLength(s) into g
|
---|
[3338] | 258 | orderby g.Key
|
---|
[5499] | 259 | select g).First().ToList();
|
---|
| 260 | var weights = possibleSymbols.Select(x => x.InitialFrequency).ToList();
|
---|
| 261 | var selectedSymbol = possibleSymbols.SelectRandom(weights, random);
|
---|
[3338] | 262 | var tree = selectedSymbol.CreateTreeNode();
|
---|
[3442] | 263 | if (tree.HasLocalParameters) tree.ResetLocalParameters(random);
|
---|
[5733] | 264 | parent.RemoveSubtree(childIndex);
|
---|
| 265 | parent.InsertSubtree(childIndex, tree);
|
---|
[5686] | 266 |
|
---|
| 267 | var topLevelNode = tree as SymbolicExpressionTreeTopLevelNode;
|
---|
| 268 | if (topLevelNode != null)
|
---|
| 269 | topLevelNode.SetGrammar((ISymbolicExpressionTreeGrammar)root.Grammar.Clone());
|
---|
| 270 |
|
---|
| 271 | for (int i = 0; i < tree.Grammar.GetMinimumSubtreeCount(tree.Symbol); i++) {
|
---|
[3338] | 272 | // insert a dummy sub-tree and add the pending extension to the list
|
---|
| 273 | var dummy = new SymbolicExpressionTreeNode();
|
---|
[5733] | 274 | tree.AddSubtree(dummy);
|
---|
[3338] | 275 | // replace the just inserted dummy by recursive application
|
---|
[5686] | 276 | ReplaceWithMinimalTree(random, root, tree, i);
|
---|
[3338] | 277 | }
|
---|
| 278 | }
|
---|
[3360] | 279 |
|
---|
[6911] | 280 | private static void FillExtensionLengths(TreeExtensionPoint extension, int maxDepth) {
|
---|
| 281 | var grammar = extension.Parent.Grammar;
|
---|
| 282 | int maxLength = int.MinValue;
|
---|
| 283 | int minLength = int.MaxValue;
|
---|
| 284 | foreach (ISymbol s in grammar.GetAllowedChildSymbols(extension.Parent.Symbol, extension.ChildIndex)) {
|
---|
| 285 | if (s.InitialFrequency > 0.0) {
|
---|
| 286 | int max = grammar.GetMaximumExpressionLength(s, maxDepth - extension.ExtensionPointDepth);
|
---|
| 287 | maxLength = Math.Max(maxLength, max);
|
---|
| 288 | int min = grammar.GetMinimumExpressionLength(s);
|
---|
| 289 | minLength = Math.Min(minLength, min);
|
---|
| 290 | }
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | extension.MaximumExtensionLength = maxLength;
|
---|
| 294 | extension.MinimumExtensionLength = minLength;
|
---|
[3338] | 295 | }
|
---|
| 296 |
|
---|
[6911] | 297 | private static int SampleArity(IRandom random, ISymbolicExpressionTreeNode node, int targetLength, int maxDepth) {
|
---|
[3223] | 298 | // select actualArity randomly with the constraint that the sub-trees in the minimal arity can become large enough
|
---|
[5686] | 299 | int minArity = node.Grammar.GetMinimumSubtreeCount(node.Symbol);
|
---|
| 300 | int maxArity = node.Grammar.GetMaximumSubtreeCount(node.Symbol);
|
---|
[5549] | 301 | if (maxArity > targetLength) {
|
---|
| 302 | maxArity = targetLength;
|
---|
[3223] | 303 | }
|
---|
[6911] | 304 | if (minArity == maxArity) return minArity;
|
---|
| 305 |
|
---|
[5549] | 306 | // 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
|
---|
| 307 | // 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] | 308 | long aggregatedLongestExpressionLength = 0;
|
---|
[3223] | 309 | for (int i = 0; i < maxArity; i++) {
|
---|
[5686] | 310 | aggregatedLongestExpressionLength += (from s in node.Grammar.GetAllowedChildSymbols(node.Symbol, i)
|
---|
[5925] | 311 | where s.InitialFrequency > 0.0
|
---|
[6911] | 312 | select node.Grammar.GetMaximumExpressionLength(s, maxDepth)).Max();
|
---|
[6803] | 313 | if (i > minArity && aggregatedLongestExpressionLength < targetLength) minArity = i + 1;
|
---|
[3223] | 314 | else break;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
[5549] | 317 | // 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
|
---|
| 318 | // 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] | 319 | long aggregatedShortestExpressionLength = 0;
|
---|
[3223] | 320 | for (int i = 0; i < maxArity; i++) {
|
---|
[5686] | 321 | aggregatedShortestExpressionLength += (from s in node.Grammar.GetAllowedChildSymbols(node.Symbol, i)
|
---|
[5925] | 322 | where s.InitialFrequency > 0.0
|
---|
[5686] | 323 | select node.Grammar.GetMinimumExpressionLength(s)).Min();
|
---|
[5549] | 324 | if (aggregatedShortestExpressionLength > targetLength) {
|
---|
[3223] | 325 | maxArity = i;
|
---|
| 326 | break;
|
---|
| 327 | }
|
---|
| 328 | }
|
---|
[5727] | 329 | if (minArity > maxArity) return -1;
|
---|
[3223] | 330 | return random.Next(minArity, maxArity + 1);
|
---|
| 331 | }
|
---|
[6911] | 332 |
|
---|
[2447] | 333 | }
|
---|
[645] | 334 | } |
---|