[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.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Random;
|
---|
| 26 | using System.Diagnostics;
|
---|
[2210] | 27 | using HeuristicLab.GP.Interfaces;
|
---|
[645] | 28 |
|
---|
[2212] | 29 | namespace HeuristicLab.GP.Operators {
|
---|
[2210] | 30 | public class CutOutNodeManipulation : GPManipulatorBase {
|
---|
[645] | 31 | public override string Description {
|
---|
| 32 | get {
|
---|
| 33 | return @"Takes a tree, selects a random node of the tree and then tries to replace a random sub-tree
|
---|
| 34 | of that node with one of the childs of the selected child.
|
---|
| 35 |
|
---|
| 36 | O O
|
---|
| 37 | / \ / \
|
---|
| 38 | O X O 2
|
---|
| 39 | / \ 2 is selected => / \
|
---|
| 40 | 1 2 4 5
|
---|
| 41 | / / \
|
---|
| 42 | 3 4 5
|
---|
| 43 | ";
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public CutOutNodeManipulation()
|
---|
| 48 | : base() {
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 |
|
---|
[2210] | 52 | internal override IOperation Manipulate(MersenneTwister random, IGeneticProgrammingModel gpModel, FunctionLibrary library, int maxTreeSize, int maxTreeHeight, IScope scope) {
|
---|
[645] | 53 | TreeGardener gardener = new TreeGardener(random, library);
|
---|
[2210] | 54 | IFunctionTree parent = gardener.GetRandomParentNode(gpModel.FunctionTree);
|
---|
[645] | 55 | // parent == null means we should cut out the root node
|
---|
| 56 | // => return a random sub-tree of the root
|
---|
| 57 | if (parent == null) {
|
---|
| 58 | // when there are sub-trees then replace the old tree with a random sub-tree
|
---|
[2210] | 59 | if (gpModel.FunctionTree.SubTrees.Count > 0) {
|
---|
| 60 | gpModel.FunctionTree = gpModel.FunctionTree.SubTrees[random.Next(gpModel.FunctionTree.SubTrees.Count)];
|
---|
| 61 | Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree));
|
---|
[645] | 62 | // we reused a sub-tree so we don't have to schedule initialization operations
|
---|
| 63 | return null;
|
---|
| 64 | } else {
|
---|
| 65 | // we want to cut the root node and there are no sub-trees => create a new random terminal
|
---|
[2210] | 66 | gpModel.FunctionTree = gardener.CreateRandomTree(gardener.Terminals, 1, 1);
|
---|
| 67 | Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree));
|
---|
| 68 |
|
---|
[645] | 69 | // schedule an operation to initialize the whole tree
|
---|
[2212] | 70 | return Util.CreateInitializationOperation(TreeGardener.GetAllSubTrees(gpModel.FunctionTree), scope);
|
---|
[645] | 71 | }
|
---|
| 72 | }
|
---|
[2210] | 73 |
|
---|
[645] | 74 | // select a child to cut away
|
---|
| 75 | int childIndex = random.Next(parent.SubTrees.Count);
|
---|
| 76 | IFunctionTree child = parent.SubTrees[childIndex];
|
---|
| 77 | // match the sub-trees of the child with the allowed sub-trees of the parent
|
---|
| 78 | ICollection<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);
|
---|
| 79 | IFunctionTree[] possibleChilds = child.SubTrees.Where(t => allowedFunctions.Contains(t.Function)).ToArray();
|
---|
| 80 | if (possibleChilds.Length > 0) {
|
---|
| 81 | // replace child with a random child of that child
|
---|
[2210] | 82 | IFunctionTree selectedChild = possibleChilds[random.Next(possibleChilds.Length)];
|
---|
[645] | 83 | parent.RemoveSubTree(childIndex);
|
---|
| 84 | parent.InsertSubTree(childIndex, selectedChild);
|
---|
[2210] | 85 | Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree));
|
---|
[645] | 86 | // don't need to schedule initialization operations
|
---|
| 87 | return null;
|
---|
| 88 | } else {
|
---|
| 89 | // can't reuse an existing branch => create a new tree
|
---|
| 90 | // determine the level of the parent
|
---|
[2210] | 91 | int parentLevel = gardener.GetBranchLevel(gpModel.FunctionTree, parent);
|
---|
[645] | 92 | // first remove the old child (first step essential!)
|
---|
| 93 | parent.RemoveSubTree(childIndex);
|
---|
| 94 | // then determine the number of nodes left over after the child has been removed!
|
---|
[2210] | 95 | int remainingNodes = gpModel.FunctionTree.GetSize();
|
---|
[645] | 96 | allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);
|
---|
| 97 | IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, maxTreeSize - remainingNodes, maxTreeHeight - parentLevel);
|
---|
| 98 | parent.InsertSubTree(childIndex, newFunctionTree);
|
---|
[2210] | 99 | Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree));
|
---|
[645] | 100 | // schedule an initialization operation for the new function-tree
|
---|
[2212] | 101 | return Util.CreateInitializationOperation(TreeGardener.GetAllSubTrees(newFunctionTree), scope);
|
---|
[645] | 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | }
|
---|