[2] | 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.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Operators;
|
---|
| 27 | using HeuristicLab.Random;
|
---|
| 28 | using System;
|
---|
[155] | 29 | using HeuristicLab.Functions;
|
---|
[2] | 30 |
|
---|
| 31 | namespace HeuristicLab.StructureIdentification {
|
---|
| 32 | public class CutOutNodeManipulation : OperatorBase {
|
---|
| 33 | public override string Description {
|
---|
| 34 | get {
|
---|
[155] | 35 | return @"Takes a tree, selects a random node of the tree and then tries to replace a random sub-tree
|
---|
[2] | 36 | of that node with one of the childs of the selected child.
|
---|
| 37 |
|
---|
| 38 | O O
|
---|
| 39 | / \ / \
|
---|
| 40 | O X O 2
|
---|
| 41 | / \ 2 is selected => / \
|
---|
| 42 | 1 2 4 5
|
---|
| 43 | / / \
|
---|
| 44 | 3 4 5
|
---|
| 45 | ";
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public CutOutNodeManipulation()
|
---|
| 50 | : base() {
|
---|
| 51 | AddVariableInfo(new VariableInfo("Random", "Uniform random number generator", typeof(MersenneTwister), VariableKind.In));
|
---|
| 52 | AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(GPOperatorLibrary), VariableKind.In));
|
---|
| 53 | AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));
|
---|
| 54 | AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In));
|
---|
[155] | 55 | AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.Out));
|
---|
| 56 | AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));
|
---|
| 57 | AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));
|
---|
[2] | 58 | }
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | public override IOperation Apply(IScope scope) {
|
---|
[155] | 62 | IFunctionTree root = GetVariableValue<IFunctionTree>("FunctionTree", scope, true);
|
---|
[2] | 63 | MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true);
|
---|
| 64 | GPOperatorLibrary library = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
|
---|
| 65 | int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data;
|
---|
| 66 | int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data;
|
---|
| 67 | TreeGardener gardener = new TreeGardener(random, library);
|
---|
[155] | 68 | IFunctionTree parent = gardener.GetRandomParentNode(root);
|
---|
[2] | 69 | // parent == null means we should cut out the root node
|
---|
[155] | 70 | // => return a random sub-tree of the root
|
---|
[2] | 71 | if (parent == null) {
|
---|
[155] | 72 | // when there are sub-trees then replace the old tree with a random sub-tree
|
---|
| 73 | if (root.SubTrees.Count > 0) {
|
---|
| 74 | root = root.SubTrees[random.Next(root.SubTrees.Count)];
|
---|
| 75 | GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root);
|
---|
| 76 | GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root);
|
---|
[2] | 77 | // update the variable
|
---|
[155] | 78 | scope.GetVariable(scope.TranslateName("FunctionTree")).Value = root;
|
---|
| 79 | if (!gardener.IsValidTree(root)) {
|
---|
[2] | 80 | throw new InvalidProgramException();
|
---|
| 81 | }
|
---|
[155] | 82 | // we reused a sub-tree so we don't have to schedule initialization operations
|
---|
[2] | 83 | return null;
|
---|
| 84 | } else {
|
---|
[155] | 85 | // we want to cut the root node and there are no sub-trees => create a new random terminal
|
---|
| 86 | IFunctionTree newTree;
|
---|
[163] | 87 | newTree = gardener.CreateRandomTree(gardener.Terminals, 1, 1);
|
---|
[155] | 88 | GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(newTree);
|
---|
| 89 | GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(newTree);
|
---|
[2] | 90 | // update the variable
|
---|
[155] | 91 | scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTree;
|
---|
| 92 | if (!gardener.IsValidTree(newTree)) {
|
---|
[2] | 93 | throw new InvalidProgramException();
|
---|
| 94 | }
|
---|
[155] | 95 | // schedule an operation to initialize the whole tree
|
---|
| 96 | return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope);
|
---|
[2] | 97 | }
|
---|
| 98 | }
|
---|
[155] | 99 | // select a child to cut away
|
---|
| 100 | int childIndex = random.Next(parent.SubTrees.Count);
|
---|
| 101 | IFunctionTree child = parent.SubTrees[childIndex];
|
---|
| 102 | // match the sub-trees of the child with the allowed sub-trees of the parent
|
---|
| 103 | ICollection<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);
|
---|
| 104 | IFunctionTree[] possibleChilds = child.SubTrees.Where(t => allowedFunctions.Contains(t.Function)).ToArray();
|
---|
[2] | 105 | if (possibleChilds.Length > 0) {
|
---|
[155] | 106 | // replace child with a random child of that child
|
---|
| 107 | IFunctionTree selectedChild = possibleChilds[random.Next(possibleChilds.Length)];
|
---|
| 108 | parent.RemoveSubTree(childIndex);
|
---|
| 109 | parent.InsertSubTree(childIndex, selectedChild);
|
---|
| 110 | if (!gardener.IsValidTree(root)) {
|
---|
[2] | 111 | throw new InvalidProgramException();
|
---|
| 112 | }
|
---|
| 113 | // update the size and height of our tree
|
---|
[155] | 114 | GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root);
|
---|
| 115 | GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root);
|
---|
[2] | 116 | // don't need to schedule initialization operations
|
---|
| 117 | return null;
|
---|
| 118 | } else {
|
---|
[155] | 119 | // can't reuse an existing branch => create a new tree
|
---|
[2] | 120 | // determine the level of the parent
|
---|
[155] | 121 | int parentLevel = gardener.GetBranchLevel(root, parent);
|
---|
[2] | 122 | // first remove the old child (first step essential!)
|
---|
[155] | 123 | parent.RemoveSubTree(childIndex);
|
---|
[2] | 124 | // then determine the number of nodes left over after the child has been removed!
|
---|
[155] | 125 | int remainingNodes = gardener.GetTreeSize(root);
|
---|
| 126 | allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);
|
---|
[163] | 127 | IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, maxTreeSize - remainingNodes, maxTreeHeight - parentLevel);
|
---|
[155] | 128 | parent.InsertSubTree(childIndex, newFunctionTree);
|
---|
| 129 | GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root);
|
---|
| 130 | GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root);
|
---|
| 131 | if (!gardener.IsValidTree(root)) {
|
---|
[2] | 132 | throw new InvalidProgramException();
|
---|
| 133 | }
|
---|
[155] | 134 | // schedule an initialization operation for the new function-tree
|
---|
| 135 | return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newFunctionTree), scope);
|
---|
[2] | 136 | }
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | }
|
---|