[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));
|
---|
[645] | 67 | // root hasn't changed so don't need to update 'FunctionTree' variable
|
---|
| 68 | return null;
|
---|
| 69 | } else {
|
---|
| 70 | // replace with a minimal random seedling
|
---|
| 71 | parent.RemoveSubTree(childIndex);
|
---|
| 72 | ICollection<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);
|
---|
| 73 | IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, 1, 1);
|
---|
| 74 | parent.InsertSubTree(childIndex, newFunctionTree);
|
---|
[2210] | 75 | Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree));
|
---|
| 76 | // return an initialization operation for the newly created tree
|
---|
[2212] | 77 | return Util.CreateInitializationOperation(TreeGardener.GetAllSubTrees(newFunctionTree), scope);
|
---|
[645] | 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|