Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.StructureIdentification/RandomTreeCreator.cs @ 138

Last change on this file since 138 was 23, checked in by gkronber, 17 years ago

changed boolean variable BalanceTrees to double BalancedTreesRate (ticket #11)

File size: 4.0 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.Linq;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using System;
27using HeuristicLab.Random;
28
29namespace HeuristicLab.StructureIdentification {
30  public class RandomTreeCreator : OperatorBase {
31    public override string Description {
32      get { return @"Generates a new random operator tree."; }
33    }
34
35    public RandomTreeCreator()
36      : base() {
37      AddVariableInfo(new VariableInfo("Random", "Uniform random number generator", typeof(MersenneTwister), VariableKind.In));
38      AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(GPOperatorLibrary), VariableKind.In));
39      AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));
40      AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In));
41      AddVariableInfo(new VariableInfo("BalancedTreesRate", "Determines how many trees should be balanced", typeof(DoubleData), VariableKind.In));
42      AddVariableInfo(new VariableInfo("OperatorTree", "The tree to mutate", typeof(IOperator), VariableKind.In));
43      AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In));
44      AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In));
45    }
46
47    public override IOperation Apply(IScope scope) {
48      IRandom random = GetVariableValue<IRandom>("Random", scope, true);
49      GPOperatorLibrary opLibrary = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
50      int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data;
51      int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data;
52      double balancedTreesRate = GetVariableValue<DoubleData>("BalancedTreesRate", scope, true).Data;
53
54      TreeGardener gardener = new TreeGardener(random, opLibrary);
55
56      int treeHeight = random.Next(1, maxTreeHeight + 1);
57      int treeSize = random.Next(1, maxTreeSize + 1);
58      IOperator rootOperator;
59      if(random.NextDouble() <= balancedTreesRate) {
60        rootOperator = gardener.CreateRandomTree(treeSize, treeHeight, true);
61      } else {
62        rootOperator = gardener.CreateRandomTree(treeSize, treeHeight, false);
63      }
64
65      int actualTreeSize = gardener.GetTreeSize(rootOperator);
66      int actualTreeHeight = gardener.GetTreeHeight(rootOperator);
67
68      scope.AddVariable(new Variable("OperatorTree", rootOperator));
69      scope.AddVariable(new Variable("TreeSize", new IntData(actualTreeSize)));
70      scope.AddVariable(new Variable("TreeHeight", new IntData(actualTreeHeight)));
71
72      if(!gardener.IsValidTree(rootOperator)) { throw new InvalidProgramException(); }
73
74      if(actualTreeSize > maxTreeSize ||
75        actualTreeHeight > maxTreeHeight) {
76        throw new InvalidProgramException();
77      }
78
79      return gardener.CreateInitializationOperation(gardener.GetAllOperators(rootOperator), scope);
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.