Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.StructureIdentification/Manipulation/SubstituteSubTreeManipulation.cs @ 13365

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