[645] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using System.Collections;
|
---|
[2222] | 27 | using HeuristicLab.GP.Interfaces;
|
---|
[645] | 28 |
|
---|
| 29 | namespace HeuristicLab.GP {
|
---|
[2222] | 30 | public class TreeGardener {
|
---|
[645] | 31 | private IRandom random;
|
---|
[2222] | 32 | private FunctionLibrary funLibrary;
|
---|
[645] | 33 | private List<IFunction> functions;
|
---|
| 34 |
|
---|
| 35 | private List<IFunction> terminals;
|
---|
[2222] | 36 | public IList<IFunction> Terminals {
|
---|
[645] | 37 | get { return terminals; }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | private List<IFunction> allFunctions;
|
---|
[2222] | 41 | public IList<IFunction> AllFunctions {
|
---|
[645] | 42 | get { return allFunctions; }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | #region constructors
|
---|
[2222] | 46 | public TreeGardener(IRandom random, FunctionLibrary funLibrary) {
|
---|
[645] | 47 | this.random = random;
|
---|
| 48 | this.funLibrary = funLibrary;
|
---|
| 49 | this.allFunctions = new List<IFunction>();
|
---|
| 50 | terminals = new List<IFunction>();
|
---|
| 51 | functions = new List<IFunction>();
|
---|
| 52 | // init functions and terminals based on constraints
|
---|
[2566] | 53 | foreach (IFunction fun in funLibrary.Functions) {
|
---|
| 54 | if (fun.MaxSubTrees == 0) {
|
---|
[645] | 55 | terminals.Add(fun);
|
---|
| 56 | allFunctions.Add(fun);
|
---|
| 57 | } else {
|
---|
| 58 | functions.Add(fun);
|
---|
| 59 | allFunctions.Add(fun);
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | #endregion
|
---|
| 64 |
|
---|
| 65 | #region random initialization
|
---|
| 66 | /// <summary>
|
---|
| 67 | /// Creates a random balanced tree with a maximal size and height. When the max-height or max-size are 1 it will return a random terminal.
|
---|
| 68 | /// In other cases it will return either a terminal (tree of size 1) or any other tree with a function in it's root (at least height 2).
|
---|
| 69 | /// </summary>
|
---|
| 70 | /// <param name="maxTreeSize">Maximal size of the tree (number of nodes).</param>
|
---|
| 71 | /// <param name="maxTreeHeight">Maximal height of the tree.</param>
|
---|
| 72 | /// <returns></returns>
|
---|
[2222] | 73 | public IFunctionTree CreateBalancedRandomTree(int maxTreeSize, int maxTreeHeight) {
|
---|
[645] | 74 | IFunction rootFunction = GetRandomRoot(maxTreeSize, maxTreeHeight);
|
---|
| 75 | IFunctionTree tree = MakeBalancedTree(rootFunction, maxTreeHeight - 1);
|
---|
| 76 | return tree;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | /// <summary>
|
---|
| 80 | /// Creates a random (unbalanced) tree with a maximal size and height. When the max-height or max-size are 1 it will return a random terminal.
|
---|
| 81 | /// In other cases it will return either a terminal (tree of size 1) or any other tree with a function in it's root (at least height 2).
|
---|
| 82 | /// </summary>
|
---|
| 83 | /// <param name="maxTreeSize">Maximal size of the tree (number of nodes).</param>
|
---|
| 84 | /// <param name="maxTreeHeight">Maximal height of the tree.</param>
|
---|
| 85 | /// <returns></returns>
|
---|
[2222] | 86 | public IFunctionTree CreateUnbalancedRandomTree(int maxTreeSize, int maxTreeHeight) {
|
---|
[645] | 87 | IFunction rootFunction = GetRandomRoot(maxTreeSize, maxTreeHeight);
|
---|
| 88 | IFunctionTree tree = MakeUnbalancedTree(rootFunction, maxTreeHeight - 1);
|
---|
| 89 | return tree;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[2222] | 92 | public IFunctionTree PTC2(int size, int maxDepth) {
|
---|
| 93 | return PTC2(GetRandomRoot(size, maxDepth), size, maxDepth);
|
---|
[645] | 94 | }
|
---|
| 95 |
|
---|
[2222] | 96 | private IFunctionTree PTC2(IFunction rootFunction, int size, int maxDepth) {
|
---|
| 97 | IFunctionTree root = rootFunction.GetTreeNode();
|
---|
| 98 | if (size <= 1 || maxDepth <= 1) return root;
|
---|
[645] | 99 | List<object[]> list = new List<object[]>();
|
---|
| 100 | int currentSize = 1;
|
---|
| 101 | int totalListMinSize = 0;
|
---|
[2222] | 102 | int minArity = root.Function.MinSubTrees;
|
---|
| 103 | int maxArity = root.Function.MaxSubTrees;
|
---|
| 104 | if (maxArity >= size) {
|
---|
[645] | 105 | maxArity = size;
|
---|
| 106 | }
|
---|
| 107 | int actualArity = random.Next(minArity, maxArity + 1);
|
---|
[2222] | 108 | totalListMinSize += root.Function.MinTreeSize - 1;
|
---|
| 109 | for (int i = 0; i < actualArity; i++) {
|
---|
[645] | 110 | // insert a dummy sub-tree and add the pending extension to the list
|
---|
| 111 | root.AddSubTree(null);
|
---|
| 112 | list.Add(new object[] { root, i, 2 });
|
---|
| 113 | }
|
---|
[2566] | 114 | if (IsRecursiveExpansionPossible(root.Function)) {
|
---|
| 115 | while (list.Count > 0 && totalListMinSize + currentSize < size) {
|
---|
| 116 | int randomIndex = random.Next(list.Count);
|
---|
| 117 | object[] nextExtension = list[randomIndex];
|
---|
| 118 | list.RemoveAt(randomIndex);
|
---|
| 119 | IFunctionTree parent = (IFunctionTree)nextExtension[0];
|
---|
| 120 | int a = (int)nextExtension[1];
|
---|
| 121 | int d = (int)nextExtension[2];
|
---|
[2675] | 122 | if (d + parent.Function.MinTreeHeight >= maxDepth) {
|
---|
[2566] | 123 | parent.RemoveSubTree(a);
|
---|
| 124 | IFunctionTree branch = CreateRandomTree(GetAllowedSubFunctions(parent.Function, a), 1, 1);
|
---|
| 125 | parent.InsertSubTree(a, branch); // insert a smallest possible tree
|
---|
| 126 | currentSize += branch.GetSize();
|
---|
| 127 | totalListMinSize -= branch.GetSize();
|
---|
| 128 | } else {
|
---|
[2675] | 129 | var allowedSubFunctions = from f in GetAllowedSubFunctions(parent.Function, a)
|
---|
| 130 | where f.MinTreeHeight + (d - 1) < maxDepth
|
---|
| 131 | where IsRecursiveExpansionPossible(f) ||
|
---|
| 132 | totalListMinSize + currentSize >= size * 0.9 // if the necessary size is almost reached then also allow
|
---|
| 133 | // terminals or terminal-branches
|
---|
| 134 | select f;
|
---|
| 135 | IFunction selectedFunction = TreeGardener.RandomSelect(random, allowedSubFunctions.ToList());
|
---|
[2566] | 136 | IFunctionTree newTree = selectedFunction.GetTreeNode();
|
---|
| 137 | parent.RemoveSubTree(a);
|
---|
| 138 | parent.InsertSubTree(a, newTree);
|
---|
| 139 | currentSize++;
|
---|
| 140 | totalListMinSize--;
|
---|
[645] | 141 |
|
---|
[2566] | 142 | minArity = selectedFunction.MinSubTrees;
|
---|
| 143 | maxArity = selectedFunction.MaxSubTrees;
|
---|
| 144 | if (maxArity >= size) {
|
---|
| 145 | maxArity = size;
|
---|
| 146 | }
|
---|
| 147 | actualArity = random.Next(minArity, maxArity + 1);
|
---|
| 148 | for (int i = 0; i < actualArity; i++) {
|
---|
| 149 | // insert a dummy sub-tree and add the pending extension to the list
|
---|
| 150 | newTree.AddSubTree(null);
|
---|
| 151 | list.Add(new object[] { newTree, i, d + 1 });
|
---|
| 152 | }
|
---|
[2675] | 153 | totalListMinSize += newTree.Function.MinTreeSize;
|
---|
[645] | 154 | }
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
[2222] | 157 | while (list.Count > 0) {
|
---|
[645] | 158 | int randomIndex = random.Next(list.Count);
|
---|
| 159 | object[] nextExtension = list[randomIndex];
|
---|
| 160 | list.RemoveAt(randomIndex);
|
---|
| 161 | IFunctionTree parent = (IFunctionTree)nextExtension[0];
|
---|
| 162 | int a = (int)nextExtension[1];
|
---|
| 163 | int d = (int)nextExtension[2];
|
---|
| 164 | parent.RemoveSubTree(a);
|
---|
| 165 | parent.InsertSubTree(a,
|
---|
| 166 | CreateRandomTree(GetAllowedSubFunctions(parent.Function, a), 1, 1)); // append a tree with minimal possible height
|
---|
| 167 | }
|
---|
| 168 | return root;
|
---|
| 169 | }
|
---|
[2566] | 170 |
|
---|
[2675] | 171 | private bool IsRecursiveExpansionPossible(IFunction function) {
|
---|
| 172 | return FindCycle(function, new Stack<IFunction>());
|
---|
[2566] | 173 | }
|
---|
| 174 |
|
---|
| 175 | private Dictionary<IFunction, bool> inCycle = new Dictionary<IFunction, bool>();
|
---|
[2675] | 176 | private bool FindCycle(IFunction function, Stack<IFunction> functionChain) {
|
---|
| 177 | if (inCycle.ContainsKey(function)) {
|
---|
| 178 | return inCycle[function];
|
---|
| 179 | } else if (IsTerminal(function)) {
|
---|
| 180 | inCycle[function] = false;
|
---|
[2566] | 181 | return false;
|
---|
[2675] | 182 | } else if (functionChain.Contains(function)) {
|
---|
| 183 | inCycle[function] = true;
|
---|
[2566] | 184 | return true;
|
---|
| 185 | } else {
|
---|
[2675] | 186 | functionChain.Push(function);
|
---|
[2566] | 187 | bool result = false;
|
---|
| 188 | // all slot indexes
|
---|
[2675] | 189 | for (int i = 0; i < function.MaxSubTrees; i++) {
|
---|
| 190 | foreach (IFunction subFunction in GetAllowedSubFunctions(function, i)) {
|
---|
| 191 | result |= FindCycle(subFunction, functionChain);
|
---|
[2566] | 192 | }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[2675] | 195 | functionChain.Pop();
|
---|
| 196 | inCycle[function] = result;
|
---|
[2566] | 197 | return result;
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[645] | 201 | /// <summary>
|
---|
| 202 | /// selects a random function from allowedFunctions and creates a random (unbalanced) tree with maximal size and height.
|
---|
| 203 | /// </summary>
|
---|
| 204 | /// <param name="allowedFunctions">Set of allowed functions.</param>
|
---|
| 205 | /// <param name="maxTreeSize">Maximal size of the tree (number of nodes).</param>
|
---|
| 206 | /// <param name="maxTreeHeight">Maximal height of the tree.</param>
|
---|
| 207 | /// <returns>New random unbalanced tree</returns>
|
---|
[2222] | 208 | public IFunctionTree CreateRandomTree(ICollection<IFunction> allowedFunctions, int maxTreeSize, int maxTreeHeight) {
|
---|
[645] | 209 | // get the minimal needed height based on allowed functions and extend the max-height if necessary
|
---|
[2222] | 210 | int minTreeHeight = allowedFunctions.Select(f => f.MinTreeHeight).Min();
|
---|
[2566] | 211 | if (minTreeHeight > maxTreeHeight)
|
---|
[645] | 212 | maxTreeHeight = minTreeHeight;
|
---|
| 213 | // get the minimal needed size based on allowed functions and extend the max-size if necessary
|
---|
[2222] | 214 | int minTreeSize = allowedFunctions.Select(f => f.MinTreeSize).Min();
|
---|
[2566] | 215 | if (minTreeSize > maxTreeSize)
|
---|
[645] | 216 | maxTreeSize = minTreeSize;
|
---|
| 217 |
|
---|
| 218 | // select a random value for the size and height
|
---|
| 219 | int treeHeight = random.Next(minTreeHeight, maxTreeHeight + 1);
|
---|
| 220 | int treeSize = random.Next(minTreeSize, maxTreeSize + 1);
|
---|
| 221 |
|
---|
| 222 | // filter the set of allowed functions and select only from those that fit into the given maximal size and height limits
|
---|
[2222] | 223 | IFunction[] possibleFunctions = allowedFunctions.Where(f => f.MinTreeHeight <= treeHeight &&
|
---|
| 224 | f.MinTreeSize <= treeSize).ToArray();
|
---|
[645] | 225 | IFunction selectedFunction = RandomSelect(possibleFunctions);
|
---|
| 226 |
|
---|
| 227 | // build the tree
|
---|
| 228 | IFunctionTree root;
|
---|
[2222] | 229 | root = PTC2(selectedFunction, maxTreeSize, maxTreeHeight);
|
---|
[645] | 230 | return root;
|
---|
| 231 | }
|
---|
| 232 | #endregion
|
---|
| 233 |
|
---|
| 234 | #region tree information gathering
|
---|
[2222] | 235 | public IFunctionTree GetRandomParentNode(IFunctionTree tree) {
|
---|
[645] | 236 | List<IFunctionTree> parentNodes = new List<IFunctionTree>();
|
---|
| 237 |
|
---|
| 238 | // add null for the parent of the root node
|
---|
| 239 | parentNodes.Add(null);
|
---|
| 240 |
|
---|
| 241 | TreeForEach(tree, delegate(IFunctionTree possibleParentNode) {
|
---|
[2566] | 242 | if (possibleParentNode.SubTrees.Count > 0) {
|
---|
[645] | 243 | parentNodes.Add(possibleParentNode);
|
---|
| 244 | }
|
---|
| 245 | });
|
---|
| 246 |
|
---|
| 247 | return parentNodes[random.Next(parentNodes.Count)];
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[2222] | 250 | public static ICollection<IFunctionTree> GetAllSubTrees(IFunctionTree root) {
|
---|
[645] | 251 | List<IFunctionTree> allTrees = new List<IFunctionTree>();
|
---|
| 252 | TreeForEach(root, t => { allTrees.Add(t); });
|
---|
| 253 | return allTrees;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | /// <summary>
|
---|
| 257 | /// returns the height level of branch in the tree
|
---|
| 258 | /// if the branch == tree => 1
|
---|
| 259 | /// if branch is in the sub-trees of tree => 2
|
---|
| 260 | /// ...
|
---|
| 261 | /// if branch is not found => -1
|
---|
| 262 | /// </summary>
|
---|
| 263 | /// <param name="tree">root of the function tree to process</param>
|
---|
| 264 | /// <param name="branch">branch that is searched in the tree</param>
|
---|
| 265 | /// <returns></returns>
|
---|
[2222] | 266 | public int GetBranchLevel(IFunctionTree tree, IFunctionTree branch) {
|
---|
[645] | 267 | return GetBranchLevelHelper(tree, branch, 1);
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | // 'tail-recursive' helper
|
---|
| 271 | private int GetBranchLevelHelper(IFunctionTree tree, IFunctionTree branch, int level) {
|
---|
[2566] | 272 | if (branch == tree) return level;
|
---|
[645] | 273 |
|
---|
[2566] | 274 | foreach (IFunctionTree subTree in tree.SubTrees) {
|
---|
[645] | 275 | int result = GetBranchLevelHelper(subTree, branch, level + 1);
|
---|
[2566] | 276 | if (result != -1) return result;
|
---|
[645] | 277 | }
|
---|
| 278 |
|
---|
| 279 | return -1;
|
---|
| 280 | }
|
---|
| 281 |
|
---|
[2222] | 282 | public bool IsValidTree(IFunctionTree tree) {
|
---|
[2566] | 283 | for (int i = 0; i < tree.SubTrees.Count; i++) {
|
---|
| 284 | if (!tree.Function.GetAllowedSubFunctions(i).Contains(tree.SubTrees[i].Function)) return false;
|
---|
[645] | 285 | }
|
---|
| 286 |
|
---|
[2566] | 287 | if (tree.SubTrees.Count < tree.Function.MinSubTrees || tree.SubTrees.Count > tree.Function.MaxSubTrees)
|
---|
[645] | 288 | return false;
|
---|
[2566] | 289 | foreach (IFunctionTree subTree in tree.SubTrees) {
|
---|
| 290 | if (!IsValidTree(subTree)) return false;
|
---|
[645] | 291 | }
|
---|
| 292 | return true;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | // returns a random branch from the specified level in the tree
|
---|
[2222] | 296 | public IFunctionTree GetRandomBranch(IFunctionTree tree, int level) {
|
---|
[2566] | 297 | if (level == 0) return tree;
|
---|
[645] | 298 | List<IFunctionTree> branches = new List<IFunctionTree>();
|
---|
| 299 | GetBranchesAtLevel(tree, level, branches);
|
---|
| 300 | return branches[random.Next(branches.Count)];
|
---|
| 301 | }
|
---|
| 302 | #endregion
|
---|
| 303 |
|
---|
| 304 | #region function information (arity, allowed childs and parents)
|
---|
| 305 | internal ICollection<IFunction> GetPossibleParents(List<IFunction> list) {
|
---|
| 306 | List<IFunction> result = new List<IFunction>();
|
---|
[2566] | 307 | foreach (IFunction f in functions) {
|
---|
| 308 | if (IsPossibleParent(f, list)) {
|
---|
[645] | 309 | result.Add(f);
|
---|
| 310 | }
|
---|
| 311 | }
|
---|
| 312 | return result;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | private bool IsPossibleParent(IFunction f, List<IFunction> children) {
|
---|
[2222] | 316 | int minArity = f.MinSubTrees;
|
---|
| 317 | int maxArity = f.MaxSubTrees;
|
---|
[645] | 318 | // note: we can't assume that the operators in the children list have different types!
|
---|
| 319 |
|
---|
| 320 | // when the maxArity of this function is smaller than the list of operators that
|
---|
| 321 | // should be included as sub-operators then it can't be a parent
|
---|
[2566] | 322 | if (maxArity < children.Count()) {
|
---|
[645] | 323 | return false;
|
---|
| 324 | }
|
---|
| 325 | int nSlots = Math.Max(minArity, children.Count);
|
---|
| 326 |
|
---|
| 327 | List<HashSet<IFunction>> slotSets = new List<HashSet<IFunction>>();
|
---|
| 328 |
|
---|
| 329 | // we iterate through all slots for sub-trees and calculate the set of
|
---|
| 330 | // allowed functions for this slot.
|
---|
| 331 | // we only count those slots that can hold at least one of the children that we should combine
|
---|
[2566] | 332 | for (int slot = 0; slot < nSlots; slot++) {
|
---|
[2222] | 333 | HashSet<IFunction> functionSet = new HashSet<IFunction>(f.GetAllowedSubFunctions(slot));
|
---|
[2566] | 334 | if (functionSet.Count() > 0) {
|
---|
[645] | 335 | slotSets.Add(functionSet);
|
---|
| 336 | }
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | // ok at the end of this operation we know how many slots of the parent can actually
|
---|
| 340 | // hold one of our children.
|
---|
| 341 | // if the number of slots is smaller than the number of children we can be sure that
|
---|
| 342 | // we can never combine all children as sub-trees of the function and thus the function
|
---|
| 343 | // can't be a parent.
|
---|
[2566] | 344 | if (slotSets.Count() < children.Count()) {
|
---|
[645] | 345 | return false;
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | // finally we sort the sets by size and beginning from the first set select one
|
---|
| 349 | // function for the slot and thus remove it as possible sub-tree from the remaining sets.
|
---|
| 350 | // when we can successfully assign all available children to a slot the function is a valid parent
|
---|
| 351 | // when only a subset of all children can be assigned to slots the function is no valid parent
|
---|
| 352 | slotSets.Sort((p, q) => p.Count() - q.Count());
|
---|
| 353 |
|
---|
| 354 | int assignments = 0;
|
---|
[2566] | 355 | for (int i = 0; i < slotSets.Count() - 1; i++) {
|
---|
| 356 | if (slotSets[i].Count > 0) {
|
---|
[645] | 357 | IFunction selected = slotSets[i].ElementAt(0);
|
---|
| 358 | assignments++;
|
---|
[2566] | 359 | for (int j = i + 1; j < slotSets.Count(); j++) {
|
---|
[645] | 360 | slotSets[j].Remove(selected);
|
---|
| 361 | }
|
---|
| 362 | }
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | // sanity check
|
---|
[2566] | 366 | if (assignments > children.Count) throw new InvalidProgramException();
|
---|
[645] | 367 | return assignments == children.Count - 1;
|
---|
| 368 | }
|
---|
[2222] | 369 | public IList<IFunction> GetAllowedParents(IFunction child, int childIndex) {
|
---|
[645] | 370 | List<IFunction> parents = new List<IFunction>();
|
---|
[2566] | 371 | foreach (IFunction function in functions) {
|
---|
[645] | 372 | ICollection<IFunction> allowedSubFunctions = GetAllowedSubFunctions(function, childIndex);
|
---|
[2566] | 373 | if (allowedSubFunctions.Contains(child)) {
|
---|
[645] | 374 | parents.Add(function);
|
---|
| 375 | }
|
---|
| 376 | }
|
---|
| 377 | return parents;
|
---|
| 378 | }
|
---|
[2222] | 379 | public static bool IsTerminal(IFunction f) {
|
---|
| 380 | return f.MinSubTrees == 0 && f.MaxSubTrees == 0;
|
---|
[645] | 381 | }
|
---|
[2222] | 382 | public ICollection<IFunction> GetAllowedSubFunctions(IFunction f, int index) {
|
---|
[2566] | 383 | if (f == null) {
|
---|
[645] | 384 | return allFunctions;
|
---|
| 385 | } else {
|
---|
[2222] | 386 | return f.GetAllowedSubFunctions(index);
|
---|
[645] | 387 | }
|
---|
| 388 | }
|
---|
| 389 | #endregion
|
---|
| 390 |
|
---|
| 391 | #region private utility methods
|
---|
[2222] | 392 | public IFunction GetRandomRoot(int maxTreeSize, int maxTreeHeight) {
|
---|
[2566] | 393 | if (maxTreeHeight == 1 || maxTreeSize == 1) {
|
---|
[645] | 394 | IFunction selectedTerminal = RandomSelect(terminals);
|
---|
| 395 | return selectedTerminal;
|
---|
| 396 | } else {
|
---|
[2566] | 397 | int minExpandableTreeSize = (from f in functions
|
---|
| 398 | where IsRecursiveExpansionPossible(f)
|
---|
| 399 | select f.MinTreeSize).Min();
|
---|
| 400 | int minExpandableTreeHeight = (from f in functions
|
---|
| 401 | where IsRecursiveExpansionPossible(f)
|
---|
| 402 | select f.MinTreeHeight).Min();
|
---|
| 403 | IFunction[] possibleFunctions;
|
---|
| 404 | if (maxTreeSize < minExpandableTreeSize || maxTreeHeight < minExpandableTreeHeight) {
|
---|
| 405 | possibleFunctions = functions.Where(f => f.MinTreeHeight <= maxTreeHeight &&
|
---|
| 406 | f.MinTreeSize <= maxTreeSize).ToArray();
|
---|
| 407 | } else {
|
---|
| 408 | possibleFunctions = functions.Where(f => f.MinTreeHeight <= maxTreeHeight &&
|
---|
| 409 | f.MinTreeSize <= maxTreeSize && IsRecursiveExpansionPossible(f)).ToArray();
|
---|
| 410 | }
|
---|
| 411 | return RandomSelect(possibleFunctions);
|
---|
[645] | 412 | }
|
---|
| 413 | }
|
---|
| 414 |
|
---|
| 415 |
|
---|
| 416 | private IFunctionTree MakeUnbalancedTree(IFunction parent, int maxTreeHeight) {
|
---|
[2566] | 417 | if (maxTreeHeight == 0) return parent.GetTreeNode();
|
---|
[2222] | 418 | int minArity = parent.MinSubTrees;
|
---|
| 419 | int maxArity = parent.MaxSubTrees;
|
---|
[645] | 420 | int actualArity = random.Next(minArity, maxArity + 1);
|
---|
[2566] | 421 | if (actualArity > 0) {
|
---|
[645] | 422 | IFunctionTree parentTree = parent.GetTreeNode();
|
---|
[2566] | 423 | for (int i = 0; i < actualArity; i++) {
|
---|
[2222] | 424 | IFunction[] possibleFunctions = GetAllowedSubFunctions(parent, i).Where(f => f.MinTreeHeight <= maxTreeHeight).ToArray();
|
---|
[645] | 425 | IFunction selectedFunction = RandomSelect(possibleFunctions);
|
---|
| 426 | IFunctionTree newSubTree = MakeUnbalancedTree(selectedFunction, maxTreeHeight - 1);
|
---|
| 427 | parentTree.InsertSubTree(i, newSubTree);
|
---|
| 428 | }
|
---|
| 429 | return parentTree;
|
---|
| 430 | }
|
---|
| 431 | return parent.GetTreeNode();
|
---|
| 432 | }
|
---|
| 433 |
|
---|
| 434 |
|
---|
| 435 | // NOTE: this method doesn't build fully balanced trees because we have constraints on the
|
---|
| 436 | // types of possible sub-functions which can indirectly impose a limit for the depth of a given sub-tree
|
---|
| 437 | private IFunctionTree MakeBalancedTree(IFunction parent, int maxTreeHeight) {
|
---|
[2566] | 438 | if (maxTreeHeight == 0) return parent.GetTreeNode();
|
---|
[2222] | 439 | int minArity = parent.MinSubTrees;
|
---|
| 440 | int maxArity = parent.MaxSubTrees;
|
---|
[645] | 441 | int actualArity = random.Next(minArity, maxArity + 1);
|
---|
[2566] | 442 | if (actualArity > 0) {
|
---|
[645] | 443 | IFunctionTree parentTree = parent.GetTreeNode();
|
---|
[2566] | 444 | for (int i = 0; i < actualArity; i++) {
|
---|
[645] | 445 | // first try to find a function that fits into the maxHeight limit
|
---|
[2222] | 446 | IFunction[] possibleFunctions = GetAllowedSubFunctions(parent, i).Where(f => f.MinTreeHeight <= maxTreeHeight &&
|
---|
[645] | 447 | !IsTerminal(f)).ToArray();
|
---|
| 448 | // no possible function found => extend function set to terminals
|
---|
[2566] | 449 | if (possibleFunctions.Length == 0) {
|
---|
[645] | 450 | possibleFunctions = GetAllowedSubFunctions(parent, i).Where(f => IsTerminal(f)).ToArray();
|
---|
| 451 | IFunction selectedTerminal = RandomSelect(possibleFunctions);
|
---|
| 452 | IFunctionTree newTree = selectedTerminal.GetTreeNode();
|
---|
| 453 | parentTree.InsertSubTree(i, newTree);
|
---|
| 454 | } else {
|
---|
| 455 | IFunction selectedFunction = RandomSelect(possibleFunctions);
|
---|
| 456 | IFunctionTree newTree = MakeBalancedTree(selectedFunction, maxTreeHeight - 1);
|
---|
| 457 | parentTree.InsertSubTree(i, newTree);
|
---|
| 458 | }
|
---|
| 459 | }
|
---|
| 460 | return parentTree;
|
---|
| 461 | }
|
---|
| 462 | return parent.GetTreeNode();
|
---|
| 463 | }
|
---|
| 464 |
|
---|
[2222] | 465 | private static void TreeForEach(IFunctionTree tree, Action<IFunctionTree> action) {
|
---|
[645] | 466 | action(tree);
|
---|
[2566] | 467 | foreach (IFunctionTree subTree in tree.SubTrees) {
|
---|
[645] | 468 | TreeForEach(subTree, action);
|
---|
| 469 | }
|
---|
| 470 | }
|
---|
| 471 |
|
---|
[2222] | 472 | private static void GetBranchesAtLevel(IFunctionTree tree, int level, List<IFunctionTree> result) {
|
---|
[2566] | 473 | if (level == 1) result.AddRange(tree.SubTrees);
|
---|
| 474 | foreach (IFunctionTree subTree in tree.SubTrees) {
|
---|
| 475 | if (subTree.GetHeight() >= level - 1)
|
---|
[645] | 476 | GetBranchesAtLevel(subTree, level - 1, result);
|
---|
| 477 | }
|
---|
| 478 | }
|
---|
| 479 |
|
---|
| 480 | private IFunction RandomSelect(IList<IFunction> functionSet) {
|
---|
[2222] | 481 | return RandomSelect(random, functionSet);
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 | public static IFunction RandomSelect(IRandom random, IList<IFunction> functionSet) {
|
---|
[645] | 485 | double[] accumulatedTickets = new double[functionSet.Count];
|
---|
| 486 | double ticketAccumulator = 0;
|
---|
| 487 | int i = 0;
|
---|
| 488 | // precalculate the slot-sizes
|
---|
[2222] | 489 | foreach (IFunction function in functionSet) {
|
---|
| 490 | ticketAccumulator += function.Tickets;
|
---|
[645] | 491 | accumulatedTickets[i] = ticketAccumulator;
|
---|
| 492 | i++;
|
---|
| 493 | }
|
---|
| 494 | // throw ball
|
---|
| 495 | double r = random.NextDouble() * ticketAccumulator;
|
---|
| 496 | // find the slot that has been hit
|
---|
[2222] | 497 | for (i = 0; i < accumulatedTickets.Length; i++) {
|
---|
| 498 | if (r < accumulatedTickets[i]) return functionSet[i];
|
---|
[645] | 499 | }
|
---|
| 500 | // sanity check
|
---|
| 501 | throw new InvalidProgramException(); // should never happen
|
---|
| 502 | }
|
---|
| 503 |
|
---|
| 504 | #endregion
|
---|
| 505 |
|
---|
| 506 | }
|
---|
| 507 | }
|
---|