Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 83 was 23, checked in by gkronber, 17 years ago

changed boolean variable BalanceTrees to double BalancedTreesRate (ticket #11)

File size: 5.8 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;
28
29namespace HeuristicLab.StructureIdentification {
30  public class DeleteSubTreeManipulation : OperatorBase {
31    public override string Description {
32      get {
33        return @"Deletes a random sub-tree of the input tree. If the remaining tree is not valid
34the operator tries to fix the tree by generating random subtrees where necessary.";
35      }
36    }
37
38    public DeleteSubTreeManipulation()
39      : base() {
40      AddVariableInfo(new VariableInfo("Random", "Uniform random number generator", typeof(MersenneTwister), VariableKind.In));
41      AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(GPOperatorLibrary), VariableKind.In));
42      AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));
43      AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In));
44      AddVariableInfo(new VariableInfo("OperatorTree", "The tree to mutate", typeof(IOperator), VariableKind.In | VariableKind.Out));
45      AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));
46      AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));
47    }
48
49
50    public override IOperation Apply(IScope scope) {
51      IOperator rootOperator = GetVariableValue<IOperator>("OperatorTree", scope, true);
52      MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true);
53      GPOperatorLibrary library = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
54
55      TreeGardener gardener = new TreeGardener(random, library);
56
57      IOperator parent = gardener.GetRandomParentNode(rootOperator);
58
59      // parent==null means the whole tree should be deleted.
60      // => return a new minimal random tree
61      if(parent == null) {
62        IOperator newTree = gardener.CreateRandomTree(1, 1, true);
63
64        // check if the tree is ok
65        if(!gardener.IsValidTree(newTree)) {
66          throw new InvalidOperationException();
67        }
68
69        // update sizes to match the new tree
70        GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(newTree);
71        GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(newTree);
72        scope.GetVariable("OperatorTree").Value = newTree;
73
74        // schedule an operation to initialize the newly created operator
75        return gardener.CreateInitializationOperation(gardener.GetAllOperators(newTree), scope);
76      }
77
78      int childIndex = random.Next(parent.SubOperators.Count);
79
80      int min;
81      int max;
82      gardener.GetMinMaxArity(parent, out min, out max);
83      if(parent.SubOperators.Count > min) {
84        parent.RemoveSubOperator(childIndex);
85        // actually since the next suboperators are shifted in the place of the removed operator
86        // it might be possible that these suboperators are not allowed in the place of the old operator
87        // we ignore this problem for now.
88        // when this starts to become a problem a possible solution is to go through the shifted operators from the place of the shifted
89        // 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)
90
91        if(!gardener.IsValidTree(rootOperator)) {
92          throw new InvalidOperationException();
93        }
94
95        GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(rootOperator);
96        GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(rootOperator);
97        // root hasn't changed so don't need to update 'OperatorTree' variable
98
99        return null;
100      } else {
101        // replace with a minimal random seedling
102        parent.RemoveSubOperator(childIndex);
103
104        IList<IOperator> allowedOperators = gardener.GetAllowedSubOperators(parent, childIndex);
105        IOperator newOperatorTree = gardener.CreateRandomTree(allowedOperators, 1, 1, true);
106
107        parent.AddSubOperator(newOperatorTree, childIndex);
108
109        if(!gardener.IsValidTree(rootOperator)) {
110          throw new InvalidProgramException();
111        }
112
113        GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(rootOperator);
114        GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(rootOperator);
115        // again the root hasn't changed so we don't need to update the 'OperatorTree' variable
116
117        return gardener.CreateInitializationOperation(gardener.GetAllOperators(newOperatorTree), scope);
118      }
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.