[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] | 22 | using System;
|
---|
[4068] | 23 | using System.Collections.Generic;
|
---|
[3462] | 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
[4722] | 26 | using HeuristicLab.Common;
|
---|
[645] | 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
[5521] | 29 | using HeuristicLab.Parameters;
|
---|
[4068] | 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[645] | 31 |
|
---|
[5499] | 32 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
[3223] | 33 | [StorableClass]
|
---|
[5549] | 34 | [Item("ProbabilisticTreeCreator", "An operator that creates new symbolic expression trees with uniformly distributed length")]
|
---|
[5618] | 35 | public class ProbabilisticTreeCreator : SymbolicExpressionTreeCreator,
|
---|
[5499] | 36 | ISymbolicExpressionTreeSizeConstraintOperator, ISymbolicExpressionTreeGrammarBasedOperator, ISymbolicExpressionTreeArchitectureAlteringOperator {
|
---|
[3369] | 37 | private const int MAX_TRIES = 100;
|
---|
[5499] | 38 | private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
|
---|
| 39 | private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
|
---|
| 40 | private const string MaximumFunctionDefinitionsParameterName = "MaximumFunctionDefinitions";
|
---|
| 41 | private const string MaximumFunctionArgumentsParameterName = "MaximumFunctionArguments";
|
---|
| 42 | private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
|
---|
| 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 | }
|
---|
| 50 | public IValueLookupParameter<IntValue> MaximumFunctionDefinitionsParameter {
|
---|
| 51 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumFunctionDefinitionsParameterName]; }
|
---|
| 52 | }
|
---|
| 53 | public IValueLookupParameter<IntValue> MaximumFunctionArgumentsParameter {
|
---|
| 54 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumFunctionArgumentsParameterName]; }
|
---|
| 55 | }
|
---|
| 56 | public IValueLookupParameter<ISymbolicExpressionTreeGrammar> SymbolicExpressionTreeGrammarParameter {
|
---|
| 57 | get { return (IValueLookupParameter<ISymbolicExpressionTreeGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
|
---|
| 58 | }
|
---|
| 59 | #endregion
|
---|
| 60 | #region Properties
|
---|
| 61 | public IntValue MaximumSymbolicExpressionTreeLength {
|
---|
| 62 | get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
|
---|
| 63 | }
|
---|
| 64 | public IntValue MaximumSymbolicExpressionTreeDepth {
|
---|
| 65 | get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
|
---|
| 66 | }
|
---|
| 67 | public IntValue MaximumFunctionDefinitions {
|
---|
| 68 | get { return MaximumFunctionDefinitionsParameter.ActualValue; }
|
---|
| 69 | }
|
---|
| 70 | public IntValue MaximumFunctionArguments {
|
---|
| 71 | get { return MaximumFunctionArgumentsParameter.ActualValue; }
|
---|
| 72 | }
|
---|
| 73 | public ISymbolicExpressionTreeGrammar SymbolicExpressionTreeGrammar {
|
---|
| 74 | get { return SymbolicExpressionTreeGrammarParameter.ActualValue; }
|
---|
| 75 | }
|
---|
| 76 | #endregion
|
---|
| 77 |
|
---|
[4722] | 78 | [StorableConstructor]
|
---|
[5618] | 79 | protected ProbabilisticTreeCreator(bool deserializing) : base(deserializing) { }
|
---|
| 80 | protected ProbabilisticTreeCreator(ProbabilisticTreeCreator original, Cloner cloner) : base(original, cloner) { }
|
---|
[5499] | 81 | public ProbabilisticTreeCreator()
|
---|
| 82 | : base() {
|
---|
| 83 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
|
---|
| 84 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
|
---|
| 85 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumFunctionDefinitionsParameterName, "The maximum allowed number of automatically defined functions."));
|
---|
| 86 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumFunctionArgumentsParameterName, "The maximum allowed number of arguments of automatically defined functions."));
|
---|
[5521] | 87 | Parameters.Add(new ValueLookupParameter<ISymbolicExpressionTreeGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
|
---|
[5499] | 88 | }
|
---|
[3338] | 89 |
|
---|
[4722] | 90 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 91 | return new ProbabilisticTreeCreator(this, cloner);
|
---|
[645] | 92 | }
|
---|
| 93 |
|
---|
[5510] | 94 | protected override ISymbolicExpressionTree Create(IRandom random) {
|
---|
[5499] | 95 | return Create(random, SymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value,
|
---|
| 96 | MaximumFunctionDefinitions.Value, MaximumFunctionArguments.Value);
|
---|
[2447] | 97 | }
|
---|
| 98 |
|
---|
[5510] | 99 | public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionTreeGrammar grammar,
|
---|
[5549] | 100 | int maxTreeLength, int maxTreeDepth,
|
---|
[3338] | 101 | int maxFunctionDefinitions, int maxFunctionArguments
|
---|
| 102 | ) {
|
---|
[3223] | 103 | SymbolicExpressionTree tree = new SymbolicExpressionTree();
|
---|
[4249] | 104 | var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
|
---|
[3442] | 105 | if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
|
---|
[4249] | 106 | rootNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar));
|
---|
[5549] | 107 | tree.Root = PTC2(random, rootNode, maxTreeLength, maxTreeDepth, maxFunctionDefinitions, maxFunctionArguments);
|
---|
[3223] | 108 | return tree;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[3338] | 111 | private class TreeExtensionPoint {
|
---|
[5499] | 112 | public ISymbolicExpressionTreeNode Parent { get; set; }
|
---|
[3338] | 113 | public int ChildIndex { get; set; }
|
---|
| 114 | public int ExtensionPointDepth { get; set; }
|
---|
[3223] | 115 | }
|
---|
[3360] | 116 |
|
---|
[5499] | 117 | public static ISymbolicExpressionTreeNode PTC2(IRandom random, ISymbolicExpressionTreeNode seedNode,
|
---|
[5549] | 118 | int maxLength, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {
|
---|
| 119 | // tree length is limited by the grammar and by the explicit size constraints
|
---|
| 120 | int allowedMinLength = seedNode.Grammar.GetMinExpressionLength(seedNode.Symbol);
|
---|
| 121 | int allowedMaxLength = Math.Min(maxLength, seedNode.Grammar.GetMaxExpressionLength(seedNode.Symbol));
|
---|
[3369] | 122 | int tries = 0;
|
---|
| 123 | while (tries++ < MAX_TRIES) {
|
---|
[5549] | 124 | // select a target tree length uniformly in the possible range (as determined by explicit limits and limits of the grammar)
|
---|
| 125 | int targetTreeLength;
|
---|
| 126 | targetTreeLength = random.Next(allowedMinLength, allowedMaxLength + 1);
|
---|
| 127 | if (targetTreeLength <= 1 || maxDepth <= 1) return seedNode;
|
---|
[3369] | 128 |
|
---|
[5549] | 129 | bool success = CreateFullTreeFromSeed(random, seedNode, seedNode.Grammar, targetTreeLength, maxDepth, maxFunctionDefinitions, maxFunctionArguments);
|
---|
[3369] | 130 |
|
---|
[4189] | 131 | // if successful => check constraints and return the tree if everything looks ok
|
---|
[5549] | 132 | if (success && seedNode.GetLength() <= maxLength && seedNode.GetDepth() <= maxDepth) {
|
---|
[3369] | 133 | return seedNode;
|
---|
| 134 | } else {
|
---|
| 135 | // clean seedNode
|
---|
[5499] | 136 | while (seedNode.SubTrees.Count() > 0) seedNode.RemoveSubTree(0);
|
---|
[3360] | 137 | }
|
---|
[5549] | 138 | // try a different length MAX_TRIES times
|
---|
[3369] | 139 | }
|
---|
[3825] | 140 | throw new ArgumentException("Couldn't create a random valid tree.");
|
---|
[3338] | 141 | }
|
---|
| 142 |
|
---|
[5499] | 143 | private static bool CreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeGrammar globalGrammar,
|
---|
[5549] | 144 | int targetLength, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {
|
---|
[3369] | 145 | try {
|
---|
[5549] | 146 | TryCreateFullTreeFromSeed(random, root, globalGrammar, targetLength, maxDepth, maxFunctionDefinitions, maxFunctionArguments);
|
---|
[3369] | 147 | return true;
|
---|
| 148 | }
|
---|
| 149 | catch (ArgumentException) { return false; }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[5499] | 152 | private static void TryCreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeGrammar globalGrammar,
|
---|
[5549] | 153 | int targetLength, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {
|
---|
[3338] | 154 | List<TreeExtensionPoint> extensionPoints = new List<TreeExtensionPoint>();
|
---|
[5549] | 155 | int currentLength = 1;
|
---|
| 156 | int totalListMinLength = globalGrammar.GetMinExpressionLength(root.Symbol) - 1;
|
---|
| 157 | int actualArity = SampleArity(random, root, targetLength);
|
---|
[3223] | 158 | for (int i = 0; i < actualArity; i++) {
|
---|
| 159 | // insert a dummy sub-tree and add the pending extension to the list
|
---|
[3338] | 160 | var dummy = new SymbolicExpressionTreeNode();
|
---|
| 161 | root.AddSubTree(dummy);
|
---|
[4219] | 162 | extensionPoints.Add(new TreeExtensionPoint { Parent = root, ChildIndex = i, ExtensionPointDepth = 0 });
|
---|
[3223] | 163 | }
|
---|
| 164 | // while there are pending extension points and we have not reached the limit of adding new extension points
|
---|
[5549] | 165 | while (extensionPoints.Count > 0 && totalListMinLength + currentLength < targetLength) {
|
---|
[3294] | 166 | int randomIndex = random.Next(extensionPoints.Count);
|
---|
[3338] | 167 | TreeExtensionPoint nextExtension = extensionPoints[randomIndex];
|
---|
[3294] | 168 | extensionPoints.RemoveAt(randomIndex);
|
---|
[5499] | 169 | ISymbolicExpressionTreeNode parent = nextExtension.Parent;
|
---|
[3338] | 170 | int argumentIndex = nextExtension.ChildIndex;
|
---|
| 171 | int extensionDepth = nextExtension.ExtensionPointDepth;
|
---|
| 172 | if (extensionDepth + parent.Grammar.GetMinExpressionDepth(parent.Symbol) >= maxDepth) {
|
---|
| 173 | ReplaceWithMinimalTree(random, root, parent, argumentIndex, maxFunctionDefinitions, maxFunctionArguments);
|
---|
[3223] | 174 | } else {
|
---|
[5499] | 175 | var allowedSymbols = (from s in parent.Grammar.Symbols
|
---|
| 176 | where parent.Grammar.IsAllowedChild(parent.Symbol, s, argumentIndex)
|
---|
| 177 | where parent.Grammar.GetMinExpressionDepth(s) + extensionDepth - 1 < maxDepth
|
---|
[5549] | 178 | where parent.Grammar.GetMaxExpressionLength(s) > targetLength - totalListMinLength - currentLength
|
---|
[5499] | 179 | select s)
|
---|
| 180 | .ToList();
|
---|
| 181 | var weights = allowedSymbols.Select(x => x.InitialFrequency).ToList();
|
---|
| 182 | var selectedSymbol = allowedSymbols.SelectRandom(weights, random);
|
---|
| 183 | ISymbolicExpressionTreeNode newTree = selectedSymbol.CreateTreeNode();
|
---|
[3442] | 184 | if (newTree.HasLocalParameters) newTree.ResetLocalParameters(random);
|
---|
[3223] | 185 | parent.RemoveSubTree(argumentIndex);
|
---|
| 186 | parent.InsertSubTree(argumentIndex, newTree);
|
---|
[3338] | 187 |
|
---|
| 188 | InitializeNewTreeNode(random, root, newTree, maxFunctionDefinitions, maxFunctionArguments);
|
---|
| 189 |
|
---|
[5549] | 190 | currentLength++;
|
---|
| 191 | totalListMinLength--;
|
---|
[3223] | 192 |
|
---|
[5549] | 193 | actualArity = SampleArity(random, newTree, targetLength - currentLength);
|
---|
[3223] | 194 | for (int i = 0; i < actualArity; i++) {
|
---|
| 195 | // insert a dummy sub-tree and add the pending extension to the list
|
---|
[3338] | 196 | var dummy = new SymbolicExpressionTreeNode();
|
---|
| 197 | newTree.AddSubTree(dummy);
|
---|
| 198 | extensionPoints.Add(new TreeExtensionPoint { Parent = newTree, ChildIndex = i, ExtensionPointDepth = extensionDepth + 1 });
|
---|
[3223] | 199 | }
|
---|
[5549] | 200 | totalListMinLength += newTree.Grammar.GetMinExpressionLength(newTree.Symbol);
|
---|
[3223] | 201 | }
|
---|
| 202 | }
|
---|
| 203 | // fill all pending extension points
|
---|
[3294] | 204 | while (extensionPoints.Count > 0) {
|
---|
| 205 | int randomIndex = random.Next(extensionPoints.Count);
|
---|
[3338] | 206 | TreeExtensionPoint nextExtension = extensionPoints[randomIndex];
|
---|
[3294] | 207 | extensionPoints.RemoveAt(randomIndex);
|
---|
[5499] | 208 | ISymbolicExpressionTreeNode parent = nextExtension.Parent;
|
---|
[3338] | 209 | int a = nextExtension.ChildIndex;
|
---|
| 210 | int d = nextExtension.ExtensionPointDepth;
|
---|
| 211 | ReplaceWithMinimalTree(random, root, parent, a, maxFunctionDefinitions, maxFunctionArguments);
|
---|
[3223] | 212 | }
|
---|
[2210] | 213 | }
|
---|
[3223] | 214 |
|
---|
[5499] | 215 | private static void ReplaceWithMinimalTree(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode parent,
|
---|
| 216 | int argumentIndex, int maxFunctionDefinitions, int maxFunctionArguments) {
|
---|
[3338] | 217 | // determine possible symbols that will lead to the smallest possible tree
|
---|
[5529] | 218 | var possibleSymbols = (from s in parent.Grammar.GetAllowedSymbols(parent.Symbol, argumentIndex)
|
---|
[3338] | 219 | group s by parent.Grammar.GetMinExpressionLength(s) into g
|
---|
| 220 | orderby g.Key
|
---|
[5499] | 221 | select g).First().ToList();
|
---|
| 222 | var weights = possibleSymbols.Select(x => x.InitialFrequency).ToList();
|
---|
| 223 | var selectedSymbol = possibleSymbols.SelectRandom(weights, random);
|
---|
[3338] | 224 | var tree = selectedSymbol.CreateTreeNode();
|
---|
[3442] | 225 | if (tree.HasLocalParameters) tree.ResetLocalParameters(random);
|
---|
[3338] | 226 | parent.RemoveSubTree(argumentIndex);
|
---|
| 227 | parent.InsertSubTree(argumentIndex, tree);
|
---|
| 228 | InitializeNewTreeNode(random, root, tree, maxFunctionDefinitions, maxFunctionArguments);
|
---|
[5529] | 229 | for (int i = 0; i < tree.Grammar.GetMinSubtreeCount(tree.Symbol); i++) {
|
---|
[3338] | 230 | // insert a dummy sub-tree and add the pending extension to the list
|
---|
| 231 | var dummy = new SymbolicExpressionTreeNode();
|
---|
| 232 | tree.AddSubTree(dummy);
|
---|
| 233 | // replace the just inserted dummy by recursive application
|
---|
| 234 | ReplaceWithMinimalTree(random, root, tree, i, maxFunctionDefinitions, maxFunctionArguments);
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
[3360] | 237 |
|
---|
[5499] | 238 | private static void InitializeNewTreeNode(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode newTree, int maxFunctionDefinitions, int maxFunctionArguments) {
|
---|
[3338] | 239 | // NB it is assumed that defuns are only allowed as children of root and nowhere else
|
---|
| 240 | // also assumes that newTree is already attached to root somewhere
|
---|
[3360] | 241 | if (IsTopLevelBranch(root, newTree)) {
|
---|
[5499] | 242 | ((SymbolicExpressionTreeTopLevelNode)newTree).SetGrammar((ISymbolicExpressionTreeGrammar)root.Grammar.Clone());
|
---|
[3360] | 243 |
|
---|
| 244 | // allow invokes of existing ADFs with higher index
|
---|
[5499] | 245 | int argIndex = root.IndexOfSubTree(newTree);
|
---|
| 246 | for (int i = argIndex + 1; i < root.SubTrees.Count(); i++) {
|
---|
| 247 | var otherDefunNode = root.GetSubTree(i) as DefunTreeNode;
|
---|
[3360] | 248 | if (otherDefunNode != null) {
|
---|
| 249 | GrammarModifier.AddDynamicSymbol(newTree.Grammar, newTree.Symbol, otherDefunNode.FunctionName, otherDefunNode.NumberOfArguments);
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 | }
|
---|
[3338] | 253 | if (newTree.Symbol is Defun) {
|
---|
| 254 | var defunTree = newTree as DefunTreeNode;
|
---|
| 255 | string formatString = new StringBuilder().Append('0', (int)Math.Log10(maxFunctionDefinitions * 10 - 1)).ToString(); // >= 100 functions => ###
|
---|
| 256 | var allowedNames = from index in Enumerable.Range(0, maxFunctionDefinitions)
|
---|
| 257 | select "ADF" + index.ToString(formatString);
|
---|
| 258 | var takenNames = (from node in root.IterateNodesPrefix().OfType<DefunTreeNode>()
|
---|
| 259 | select node.FunctionName).Distinct();
|
---|
| 260 | var remainingNames = allowedNames.Except(takenNames).ToList();
|
---|
| 261 | string functionName = remainingNames[random.Next(remainingNames.Count)];
|
---|
| 262 | // set name and number of arguments of the ADF
|
---|
| 263 | int nArgs = random.Next(maxFunctionArguments);
|
---|
| 264 | defunTree.FunctionName = functionName;
|
---|
| 265 | defunTree.NumberOfArguments = nArgs;
|
---|
| 266 | if (nArgs > 0) {
|
---|
[3360] | 267 | GrammarModifier.AddDynamicArguments(defunTree.Grammar, defunTree.Symbol, Enumerable.Range(0, nArgs));
|
---|
[3338] | 268 | }
|
---|
[3360] | 269 | // in existing branches with smaller index allow invoke of current function
|
---|
[5499] | 270 | int argIndex = root.IndexOfSubTree(newTree);
|
---|
[3338] | 271 | for (int i = 0; i < argIndex; i++) {
|
---|
| 272 | // if not dummy node
|
---|
[5499] | 273 | if (root.GetSubTree(i).Symbol != null) {
|
---|
| 274 | var existingBranch = root.GetSubTree(i);
|
---|
[3360] | 275 | GrammarModifier.AddDynamicSymbol(existingBranch.Grammar, existingBranch.Symbol, functionName, nArgs);
|
---|
[3338] | 276 | }
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 |
|
---|
[5499] | 281 | private static bool IsTopLevelBranch(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode branch) {
|
---|
[3369] | 282 | return branch is SymbolicExpressionTreeTopLevelNode;
|
---|
[3338] | 283 | }
|
---|
| 284 |
|
---|
[5549] | 285 | private static int SampleArity(IRandom random, ISymbolicExpressionTreeNode node, int targetLength) {
|
---|
[3223] | 286 | // select actualArity randomly with the constraint that the sub-trees in the minimal arity can become large enough
|
---|
[5529] | 287 | int minArity = node.Grammar.GetMinSubtreeCount(node.Symbol);
|
---|
| 288 | int maxArity = node.Grammar.GetMaxSubtreeCount(node.Symbol);
|
---|
[5549] | 289 | if (maxArity > targetLength) {
|
---|
| 290 | maxArity = targetLength;
|
---|
[3223] | 291 | }
|
---|
[5549] | 292 | // 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
|
---|
| 293 | // 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] | 294 | long aggregatedLongestExpressionLength = 0;
|
---|
[3223] | 295 | for (int i = 0; i < maxArity; i++) {
|
---|
[5529] | 296 | aggregatedLongestExpressionLength += (from s in node.Grammar.GetAllowedSymbols(node.Symbol, i)
|
---|
[3338] | 297 | select node.Grammar.GetMaxExpressionLength(s)).Max();
|
---|
[5549] | 298 | if (aggregatedLongestExpressionLength < targetLength) minArity = i;
|
---|
[3223] | 299 | else break;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
[5549] | 302 | // 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
|
---|
| 303 | // 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] | 304 | long aggregatedShortestExpressionLength = 0;
|
---|
[3223] | 305 | for (int i = 0; i < maxArity; i++) {
|
---|
[5529] | 306 | aggregatedShortestExpressionLength += (from s in node.Grammar.GetAllowedSymbols(node.Symbol, i)
|
---|
[3338] | 307 | select node.Grammar.GetMinExpressionLength(s)).Min();
|
---|
[5549] | 308 | if (aggregatedShortestExpressionLength > targetLength) {
|
---|
[3223] | 309 | maxArity = i;
|
---|
| 310 | break;
|
---|
| 311 | }
|
---|
| 312 | }
|
---|
| 313 | if (minArity > maxArity) throw new ArgumentException();
|
---|
| 314 | return random.Next(minArity, maxArity + 1);
|
---|
| 315 | }
|
---|
[2447] | 316 | }
|
---|
[645] | 317 | } |
---|