Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.StructureIdentification/Manipulation/SubstituteSubTreeManipulation.cs @ 202

Last change on this file since 202 was 163, checked in by gkronber, 17 years ago

fixed #119

File size: 5.7 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 System.Linq;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Random;
29using HeuristicLab.Functions;
30
31namespace HeuristicLab.StructureIdentification {
32  public class SubstituteSubTreeManipulation : OperatorBase {
33
34    public override string Description {
35      get { return "Selects a random node of the tree and replaces it with randomly initialized subtree."; }
36    }
37
38    public SubstituteSubTreeManipulation()
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("FunctionTree", "The tree to manipulate", typeof(IFunctionTree), 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    public override IOperation Apply(IScope scope) {
50      IFunctionTree root = GetVariableValue<IFunctionTree>("FunctionTree", scope, true);
51      MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true);
52      GPOperatorLibrary library = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
53      int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data;
54      int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data;
55      int treeSize = GetVariableValue<IntData>("TreeSize", scope, true).Data;
56      int treeHeight = GetVariableValue<IntData>("TreeHeight", scope, true).Data;
57      TreeGardener gardener = new TreeGardener(random, library);
58      IFunctionTree parent = gardener.GetRandomParentNode(root);
59      if(parent == null) {
60        // parent == null means we should subsitute the whole tree
61        // => create a new random tree
62        IFunctionTree newTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight);
63        if(!gardener.IsValidTree(newTree)) {
64          throw new InvalidProgramException();
65        }
66
67        // update the variables in the scope with the new values
68        GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(newTree);
69        GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(newTree);
70        scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTree;
71       
72        // return a CompositeOperation that randomly initializes the new tree
73        return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope);
74      } else {
75        // determine a random child of the parent to be replaced
76        int childIndex = random.Next(parent.SubTrees.Count);
77        // get the list of allowed functions for the new sub-tree
78        ICollection<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);
79        if(allowedFunctions.Count == 0) {
80          // don't change anything
81          // this shouldn't happen
82          throw new InvalidProgramException();
83        }
84
85        // calculate the maximum size and height of the new sub-tree based on the location where
86        // it will be inserted
87        int parentLevel = gardener.GetBranchLevel(root, parent);
88        int maxSubTreeHeight = maxTreeHeight - parentLevel;
89        int maxSubTreeSize = maxTreeSize - (treeSize - gardener.GetTreeSize(parent.SubTrees[childIndex]));
90
91        // create a random function tree
92        IFunctionTree newTree = gardener.CreateRandomTree(allowedFunctions, maxSubTreeSize, maxSubTreeHeight);
93        parent.RemoveSubTree(childIndex);
94        parent.InsertSubTree(childIndex, newTree);
95
96        if(!gardener.IsValidTree(root)) {
97          throw new InvalidProgramException();
98        }
99
100        // update the values of treeSize and treeHeight
101        GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root);
102        GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root);
103        // the root hasn't changed so we don't need to update
104        // return a CompositeOperation that randomly initializes all nodes of the new subtree
105        return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope);
106      }
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.