Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/SubstituteSubTreeManipulation.cs @ 148

Last change on this file since 148 was 144, checked in by gkronber, 17 years ago

fixed compile errors (#112)

File size: 6.5 KB
RevLine 
[2]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;
[144]29using HeuristicLab.Functions;
[2]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));
[23]44      AddVariableInfo(new VariableInfo("BalancedTreesRate", "Determines how many trees should be balanced", typeof(DoubleData), VariableKind.In));
[144]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));
[2]48    }
49
50    public override IOperation Apply(IScope scope) {
[144]51      IFunctionTree root = GetVariableValue<IFunctionTree>("FunctionTree", scope, true);
[2]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;
[23]56      double balancedTreesRate = GetVariableValue<DoubleData>("BalancedTreesRate", scope, true).Data;
[2]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
[144]62      IFunctionTree parent = gardener.GetRandomParentNode(root);
[2]63      if(parent == null) {
64        // parent == null means we should subsitute the whole tree
65        // => create a new random tree
66
[144]67        // create a new random function tree
68        IFunctionTree newTree;
[23]69        if(random.NextDouble() <= balancedTreesRate) {
[144]70          newTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, true);
[23]71        } else {
[144]72          newTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, false);
[23]73        }
74
[144]75        if(!gardener.IsValidTree(newTree)) {
[2]76          throw new InvalidProgramException();
77        }
78
79        // update the variables in the scope with the new values
[144]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;
[2]83       
[144]84        // return a CompositeOperation that randomly initializes the new tree
85        return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope);
[2]86      } else {
87        // determine a random child of the parent to be replaced
[144]88        int childIndex = random.Next(parent.SubTrees.Count);
89        // get the list of allowed functions for the new sub-tree
90        IList<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);
91        if(allowedFunctions.Count == 0) {
[2]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
[144]99        int parentLevel = gardener.GetBranchLevel(root, parent);
[2]100
101        int maxSubTreeHeight = maxTreeHeight - parentLevel;
[144]102        int maxSubTreeSize = maxTreeSize - (treeSize - gardener.GetTreeSize(parent.SubTrees[childIndex]));
[2]103
[144]104        // create a random function tree
105        IFunctionTree newTree;
[23]106        if(random.NextDouble() <= balancedTreesRate) {
[144]107          newTree = gardener.CreateRandomTree(allowedFunctions, maxSubTreeSize, maxSubTreeHeight, true);
[23]108        } else {
[144]109          newTree = gardener.CreateRandomTree(allowedFunctions, maxSubTreeSize, maxSubTreeHeight, false);
[23]110        }
[2]111
[144]112        IFunctionTree oldChild = parent.SubTrees[childIndex];
113        parent.RemoveSubTree(childIndex);
114        parent.InsertSubTree(childIndex, newTree);
[2]115
[144]116        if(!gardener.IsValidTree(root)) {
[2]117          throw new InvalidProgramException();
118        }
119
120        // update the values of treeSize and treeHeight
[144]121        GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root);
122        GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root);
123        // the root hasn't changed so we don't need to update
[2]124        // return a CompositeOperation that randomly initializes all nodes of the new subtree
[144]125        return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope);
[2]126      }
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.