Free cookie consent management tool by TermsFeed Policy Generator

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

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

merged FunctionsAndStructIdRefactoring-branch (r142, r143, r144, r145, r146, r147, r148, r149, r152, r153) back into the trunk (ticket #112)

File size: 6.5 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("BalancedTreesRate", "Determines how many trees should be balanced", typeof(DoubleData), VariableKind.In));
45      AddVariableInfo(new VariableInfo("FunctionTree", "The tree to manipulate", typeof(IFunctionTree), VariableKind.In | VariableKind.Out));
46      AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));
47      AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));
48    }
49
50    public override IOperation Apply(IScope scope) {
51      IFunctionTree root = GetVariableValue<IFunctionTree>("FunctionTree", scope, true);
52      MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true);
53      GPOperatorLibrary library = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
54      int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data;
55      int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data;
56      double balancedTreesRate = GetVariableValue<DoubleData>("BalancedTreesRate", scope, true).Data;
57      int treeSize = GetVariableValue<IntData>("TreeSize", scope, true).Data;
58      int treeHeight = GetVariableValue<IntData>("TreeHeight", scope, true).Data;
59
60      TreeGardener gardener = new TreeGardener(random, library);
61
62      IFunctionTree parent = gardener.GetRandomParentNode(root);
63      if(parent == null) {
64        // parent == null means we should subsitute the whole tree
65        // => create a new random tree
66
67        // create a new random function tree
68        IFunctionTree newTree;
69        if(random.NextDouble() <= balancedTreesRate) {
70          newTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, true);
71        } else {
72          newTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, false);
73        }
74
75        if(!gardener.IsValidTree(newTree)) {
76          throw new InvalidProgramException();
77        }
78
79        // update the variables in the scope with the new values
80        GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(newTree);
81        GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(newTree);
82        scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTree;
83       
84        // return a CompositeOperation that randomly initializes the new tree
85        return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope);
86      } else {
87        // determine a random child of the parent to be replaced
88        int childIndex = random.Next(parent.SubTrees.Count);
89        // get the list of allowed functions for the new sub-tree
90        ICollection<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);
91        if(allowedFunctions.Count == 0) {
92          // don't change anything
93          // this shouldn't happen
94          throw new InvalidProgramException();
95        }
96
97        // calculate the maximum size and height of the new sub-tree based on the location where
98        // it will be inserted
99        int parentLevel = gardener.GetBranchLevel(root, parent);
100
101        int maxSubTreeHeight = maxTreeHeight - parentLevel;
102        int maxSubTreeSize = maxTreeSize - (treeSize - gardener.GetTreeSize(parent.SubTrees[childIndex]));
103
104        // create a random function tree
105        IFunctionTree newTree;
106        if(random.NextDouble() <= balancedTreesRate) {
107          newTree = gardener.CreateRandomTree(allowedFunctions, maxSubTreeSize, maxSubTreeHeight, true);
108        } else {
109          newTree = gardener.CreateRandomTree(allowedFunctions, maxSubTreeSize, maxSubTreeHeight, false);
110        }
111
112        parent.RemoveSubTree(childIndex);
113        parent.InsertSubTree(childIndex, newTree);
114
115        if(!gardener.IsValidTree(root)) {
116          throw new InvalidProgramException();
117        }
118
119        // update the values of treeSize and treeHeight
120        GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root);
121        GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root);
122        // the root hasn't changed so we don't need to update
123        // return a CompositeOperation that randomly initializes all nodes of the new subtree
124        return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope);
125      }
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.