Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.StructureIdentification/Manipulation/DeleteSubTreeManipulation.cs @ 395

Last change on this file since 395 was 324, checked in by gkronber, 16 years ago

added Size and Height properties to interface IFunctionTree and removed the helper methods from TreeGardener (specific implementations of size and height properties in classes implementing IFunctionTree can be more efficient than the general functions in TreeGardener)

File size: 5.6 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Random;
28using HeuristicLab.Functions;
29using System.Diagnostics;
30
31namespace HeuristicLab.StructureIdentification {
32  public class DeleteSubTreeManipulation : OperatorBase {
33    public override string Description {
34      get {
35        return @"Deletes a random sub-tree of the input tree. If the remaining tree is not valid
36the operator tries to fix the tree by generating random subtrees where necessary.";
37      }
38    }
39
40    public DeleteSubTreeManipulation()
41      : base() {
42      AddVariableInfo(new VariableInfo("Random", "Uniform random number generator", typeof(MersenneTwister), VariableKind.In));
43      AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(GPOperatorLibrary), VariableKind.In));
44      AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));
45      AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In));
46      AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.Out));
47      AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));
48      AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));
49    }
50
51
52    public override IOperation Apply(IScope scope) {
53      IFunctionTree root = GetVariableValue<IFunctionTree>("FunctionTree", scope, true);
54      MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true);
55      GPOperatorLibrary library = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
56      TreeGardener gardener = new TreeGardener(random, library);
57      IFunctionTree parent = gardener.GetRandomParentNode(root);
58
59      // parent==null means the whole tree should be deleted.
60      // => return a new minimal random tree
61      if(parent == null) {
62        IFunctionTree newTree = gardener.CreateBalancedRandomTree(1, 1);
63        // check if the tree is ok
64        Debug.Assert(gardener.IsValidTree(newTree));
65        // update sizes to match the new tree
66        GetVariableValue<IntData>("TreeSize", scope, true).Data = newTree.Size;
67        GetVariableValue<IntData>("TreeHeight", scope, true).Data = newTree.Height;
68        scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTree;
69
70        // schedule an operation to initialize the newly created operator
71        return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope);
72      }
73
74      // select a branch to prune
75      int childIndex = random.Next(parent.SubTrees.Count);
76      int min;
77      int max;
78      gardener.GetMinMaxArity(parent.Function, out min, out max);
79      if(parent.SubTrees.Count > min) {
80        parent.RemoveSubTree(childIndex);
81        // actually since the next sub-trees are shifted in the place of the removed branch
82        // it might be possible that these sub-trees are not allowed in the place of the old branch
83        // we ignore this problem for now.
84        // when this starts to become a problem a possible solution is to go through the shifted branches from the place of the shifted
85        // 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)
86
87        Debug.Assert(gardener.IsValidTree(root));
88        GetVariableValue<IntData>("TreeSize", scope, true).Data = root.Size;
89        GetVariableValue<IntData>("TreeHeight", scope, true).Data = root.Height;
90        // root hasn't changed so don't need to update 'FunctionTree' variable
91        return null;
92      } else {
93        // replace with a minimal random seedling
94        parent.RemoveSubTree(childIndex);
95        ICollection<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);
96        IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, 1, 1);
97        parent.InsertSubTree(childIndex, newFunctionTree);
98        Debug.Assert(gardener.IsValidTree(root));
99        GetVariableValue<IntData>("TreeSize", scope, true).Data = root.Size;
100        GetVariableValue<IntData>("TreeHeight", scope, true).Data = root.Height;
101        // again the root hasn't changed so we don't need to update the 'FunctionTree' variable
102        // but we have to return an initialization operation for the newly created tree
103        return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newFunctionTree), scope);
104      }
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.