[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;
|
---|
[143] | 29 | using HeuristicLab.Functions;
|
---|
[2] | 30 |
|
---|
| 31 | namespace HeuristicLab.StructureIdentification {
|
---|
| 32 | public class CutOutNodeManipulation : OperatorBase {
|
---|
| 33 | public override string Description {
|
---|
| 34 | get {
|
---|
[143] | 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));
|
---|
[23] | 55 | AddVariableInfo(new VariableInfo("BalancedTreesRate", "Determines how many trees should be balanced", typeof(DoubleData), VariableKind.In));
|
---|
[143] | 56 | AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.Out));
|
---|
| 57 | AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));
|
---|
| 58 | AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));
|
---|
[2] | 59 | }
|
---|
| 60 |
|
---|
| 61 |
|
---|
| 62 | public override IOperation Apply(IScope scope) {
|
---|
[143] | 63 | IFunctionTree root = GetVariableValue<IFunctionTree>("FunctionTree", scope, true);
|
---|
[2] | 64 | MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true);
|
---|
| 65 | GPOperatorLibrary library = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
|
---|
| 66 | int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data;
|
---|
| 67 | int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data;
|
---|
[23] | 68 | double balancedTreesRate = GetVariableValue<DoubleData>("BalancedTreesRate", scope, true).Data;
|
---|
[2] | 69 |
|
---|
| 70 | TreeGardener gardener = new TreeGardener(random, library);
|
---|
[143] | 71 | IFunctionTree parent = gardener.GetRandomParentNode(root);
|
---|
[2] | 72 | // parent == null means we should cut out the root node
|
---|
[143] | 73 | // => return a random sub-tree of the root
|
---|
[2] | 74 | if (parent == null) {
|
---|
[143] | 75 | // when there are sub-trees then replace the old tree with a random sub-tree
|
---|
| 76 | if (root.SubTrees.Count > 0) {
|
---|
| 77 | root = root.SubTrees[random.Next(root.SubTrees.Count)];
|
---|
[2] | 78 |
|
---|
[143] | 79 | GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root);
|
---|
| 80 | GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root);
|
---|
[2] | 81 |
|
---|
[143] | 82 | // this is not really necessary (we can leave it in until the tree is stable)
|
---|
| 83 | if (!gardener.IsValidTree(root)) {
|
---|
[2] | 84 | throw new InvalidProgramException();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | // update the variable
|
---|
[143] | 88 | scope.GetVariable(scope.TranslateName("FunctionTree")).Value = root;
|
---|
| 89 | if (!gardener.IsValidTree(root)) {
|
---|
[2] | 90 | throw new InvalidProgramException();
|
---|
| 91 | }
|
---|
| 92 | // the tree is already initialized so we don't have to schedule initialization operations
|
---|
| 93 | return null;
|
---|
| 94 | } else {
|
---|
| 95 | // create a new random tree
|
---|
[143] | 96 | IFunctionTree newTree;
|
---|
[23] | 97 | if(random.NextDouble() <= balancedTreesRate) {
|
---|
[143] | 98 | newTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, true);
|
---|
[23] | 99 | } else {
|
---|
[143] | 100 | newTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, false);
|
---|
[23] | 101 | }
|
---|
[2] | 102 |
|
---|
[143] | 103 | GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(newTree);
|
---|
| 104 | GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(newTree);
|
---|
[2] | 105 |
|
---|
| 106 | // update the variable
|
---|
[143] | 107 | scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTree;
|
---|
[2] | 108 |
|
---|
[143] | 109 | if (!gardener.IsValidTree(newTree)) {
|
---|
[2] | 110 | throw new InvalidProgramException();
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[143] | 113 | // schedule an operation to initialize the whole tree
|
---|
| 114 | return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope);
|
---|
[2] | 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[143] | 118 | int childIndex = random.Next(parent.SubTrees.Count);
|
---|
| 119 | IFunctionTree child = parent.SubTrees[childIndex];
|
---|
[2] | 120 |
|
---|
[143] | 121 | // match the sub-trees of the child with the allowed sub-trees of the parent
|
---|
| 122 | IFunction[] possibleChilds = gardener.GetAllowedSubFunctions(parent.Function, childIndex).SelectMany(allowedFun => child.SubTrees
|
---|
| 123 | .Where(subTree => allowedFun == subTree.Function)).Select(t=>t.Function).ToArray(); // Actually we should probably use OperatorEqualityComparer here (gkronber 21.04.08)
|
---|
[2] | 124 |
|
---|
| 125 | if (possibleChilds.Length > 0) {
|
---|
| 126 | // replace child with a random child of the child
|
---|
[143] | 127 | // make a clone to simplify removing obsolete branches from the function-tree
|
---|
| 128 | IFunction selectedChild = possibleChilds[random.Next(possibleChilds.Length)];
|
---|
| 129 | parent.RemoveSubTree(childIndex);
|
---|
| 130 | parent.InsertSubTree(childIndex, new FunctionTree(selectedChild));
|
---|
[2] | 131 |
|
---|
[143] | 132 | if (!gardener.IsValidTree(root)) {
|
---|
[2] | 133 | throw new InvalidProgramException();
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | // update the size and height of our tree
|
---|
[143] | 137 | GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root);
|
---|
| 138 | GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root);
|
---|
[2] | 139 | // don't need to schedule initialization operations
|
---|
| 140 | return null;
|
---|
| 141 | } else {
|
---|
| 142 | // determine the level of the parent
|
---|
[143] | 143 | int parentLevel = gardener.GetBranchLevel(root, parent);
|
---|
[2] | 144 |
|
---|
| 145 | // first remove the old child (first step essential!)
|
---|
[143] | 146 | parent.RemoveSubTree(childIndex);
|
---|
[2] | 147 | // then determine the number of nodes left over after the child has been removed!
|
---|
[143] | 148 | int remainingNodes = gardener.GetTreeSize(root);
|
---|
[2] | 149 |
|
---|
[143] | 150 | IList<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);
|
---|
| 151 | IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, maxTreeSize - remainingNodes, maxTreeHeight - parentLevel, true);
|
---|
[2] | 152 |
|
---|
[143] | 153 | parent.InsertSubTree(childIndex, newFunctionTree);
|
---|
[2] | 154 |
|
---|
[143] | 155 | GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root);
|
---|
| 156 | GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root);
|
---|
[2] | 157 |
|
---|
[143] | 158 | if (!gardener.IsValidTree(root)) {
|
---|
[2] | 159 | throw new InvalidProgramException();
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[143] | 162 | // schedule an initialization operation for the new function-tree
|
---|
| 163 | return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newFunctionTree), scope);
|
---|
[2] | 164 | }
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 | }
|
---|