Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTree/3.3/Creators/RampedTreeCreator.cs @ 3219

Last change on this file since 3219 was 3219, checked in by gkronber, 14 years ago

Worked on SymbolicTreeEncoding plugin. #937 (Data types and operators for symbolic expression tree encoding)

File size: 3.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 HeuristicLab.Core;
23using HeuristicLab.Data;
24using System;
25using HeuristicLab.Random;
26using System.Diagnostics;
27
28namespace HeuristicLab.Encodings.SymbolicExpressionTree {
29  public class RampedTreeCreator : OperatorBase {
30    public override string Description {
31      get { return @"Generates a new random operator tree."; }
32    }
33
34    public RampedTreeCreator()
35      : base() {
36      AddVariableInfo(new VariableInfo("Random", "Uniform random number generator", typeof(MersenneTwister), VariableKind.In));
37      AddVariableInfo(new VariableInfo("FunctionLibrary", "The function library containing all available functions", typeof(FunctionLibrary), VariableKind.In));
38      AddVariableInfo(new VariableInfo("MinTreeHeight", "The minimal allowed height of the tree", typeof(IntData), VariableKind.In));
39      AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));
40      AddVariableInfo(new VariableInfo("BalancedTreesRate", "Determines how many trees should be balanced", typeof(DoubleData), VariableKind.In));
41      AddVariableInfo(new VariableInfo("FunctionTree", "The created tree", typeof(IGeneticProgrammingModel), VariableKind.New | VariableKind.Out));
42      AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.New | VariableKind.Out));
43      AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.New | VariableKind.Out));
44    }
45
46    public override IOperation Apply(IScope scope) {
47      IRandom random = GetVariableValue<IRandom>("Random", scope, true);
48      FunctionLibrary opLibrary = GetVariableValue<FunctionLibrary>("FunctionLibrary", scope, true);
49      int minTreeHeight = GetVariableValue<IntData>("MinTreeHeight", scope, true).Data;
50      int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data;
51      double balancedTreesRate = GetVariableValue<DoubleData>("BalancedTreesRate", scope, true).Data;
52
53      TreeGardener gardener = new TreeGardener(random, opLibrary);
54
55      int treeHeight = random.Next(minTreeHeight, maxTreeHeight + 1);
56      IFunctionTree root;
57      if(random.NextDouble() <= balancedTreesRate) {
58        root = gardener.CreateBalancedRandomTree(Int32.MaxValue, treeHeight);
59      } else {
60        root = gardener.CreateUnbalancedRandomTree(Int32.MaxValue, treeHeight);
61      }
62
63      Debug.Assert(gardener.IsValidTree(root) && root.GetHeight() <= maxTreeHeight);
64
65      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), new GeneticProgrammingModel(root)));
66      return Util.CreateInitializationOperation(TreeGardener.GetAllSubTrees(root), scope);
67    }
68  }
69}
Note: See TracBrowser for help on using the repository browser.