[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 HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Random;
|
---|
| 25 | using System.Diagnostics;
|
---|
[2210] | 26 | using HeuristicLab.GP.Interfaces;
|
---|
[645] | 27 |
|
---|
[2212] | 28 | namespace HeuristicLab.GP.Operators {
|
---|
[2210] | 29 | public class DeleteSubTreeManipulation : GPManipulatorBase {
|
---|
[645] | 30 | public override string Description {
|
---|
| 31 | get {
|
---|
| 32 | return @"Deletes a random sub-tree of the input tree. If the remaining tree is not valid
|
---|
| 33 | the operator tries to fix the tree by generating random subtrees where necessary.";
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | public DeleteSubTreeManipulation()
|
---|
| 38 | : base() {
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[2210] | 41 | internal override IOperation Manipulate(MersenneTwister random, IGeneticProgrammingModel gpModel, FunctionLibrary library, int maxTreeSize, int maxTreeHeight, IScope scope) {
|
---|
[645] | 42 | TreeGardener gardener = new TreeGardener(random, library);
|
---|
[2210] | 43 | IFunctionTree parent = gardener.GetRandomParentNode(gpModel.FunctionTree);
|
---|
[645] | 44 |
|
---|
| 45 | // parent==null means the whole tree should be deleted.
|
---|
| 46 | // => return a new minimal random tree
|
---|
[2210] | 47 | if (parent == null) {
|
---|
[645] | 48 | IFunctionTree newTree = gardener.CreateBalancedRandomTree(1, 1);
|
---|
| 49 | // check if the tree is ok
|
---|
| 50 | Debug.Assert(gardener.IsValidTree(newTree));
|
---|
[2210] | 51 | gpModel.FunctionTree = newTree;
|
---|
[645] | 52 | // schedule an operation to initialize the newly created operator
|
---|
[2212] | 53 | return Util.CreateInitializationOperation(TreeGardener.GetAllSubTrees(newTree), scope);
|
---|
[645] | 54 | }
|
---|
| 55 |
|
---|
| 56 | // select a branch to prune
|
---|
| 57 | int childIndex = random.Next(parent.SubTrees.Count);
|
---|
[2211] | 58 | if (parent.SubTrees.Count > parent.Function.MinSubTrees) {
|
---|
[645] | 59 | parent.RemoveSubTree(childIndex);
|
---|
| 60 | // actually since the next sub-trees are shifted in the place of the removed branch
|
---|
| 61 | // it might be possible that these sub-trees are not allowed in the place of the old branch
|
---|
| 62 | // we ignore this problem for now.
|
---|
| 63 | // when this starts to become a problem a possible solution is to go through the shifted branches from the place of the shifted
|
---|
| 64 | // and find the first one that doesn't fit. At this position we insert a new randomly initialized subtree of matching type (gkronber 25.12.07)
|
---|
| 65 |
|
---|
[2210] | 66 | Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree));
|
---|
| 67 | // recalculate size and height
|
---|
| 68 | gpModel.Size = gpModel.FunctionTree.GetSize();
|
---|
| 69 | gpModel.Height = gpModel.FunctionTree.GetHeight();
|
---|
[645] | 70 | // root hasn't changed so don't need to update 'FunctionTree' variable
|
---|
| 71 | return null;
|
---|
| 72 | } else {
|
---|
| 73 | // replace with a minimal random seedling
|
---|
| 74 | parent.RemoveSubTree(childIndex);
|
---|
| 75 | ICollection<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);
|
---|
| 76 | IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, 1, 1);
|
---|
| 77 | parent.InsertSubTree(childIndex, newFunctionTree);
|
---|
[2210] | 78 | Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree));
|
---|
| 79 | // recalculate size and height
|
---|
| 80 | gpModel.Size = gpModel.FunctionTree.GetSize();
|
---|
| 81 | gpModel.Height = gpModel.FunctionTree.GetHeight();
|
---|
| 82 | // return an initialization operation for the newly created tree
|
---|
[2212] | 83 | return Util.CreateInitializationOperation(TreeGardener.GetAllSubTrees(newFunctionTree), scope);
|
---|
[645] | 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | }
|
---|