Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.GP/3.4/ProbabilisticTreeCreator.cs @ 2515

Last change on this file since 2515 was 1914, checked in by epitzer, 15 years ago

Migration of DataAnalysis, GP, GP.StructureIdentification and Modeling to new Persistence-3.3 (#603)

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;
28using System.Diagnostics;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.GP {
32
33  public class ProbabilisticTreeCreator : OperatorBase {
34    private int MAX_TRIES { get { return 100; } }
35
36    public override string Description {
37      get { return @"Generates a new random operator tree."; }
38    }
39
40    public ProbabilisticTreeCreator()
41      : base() {
42      AddVariableInfo(new VariableInfo("Random", "Uniform random number generator", typeof(MersenneTwister), VariableKind.In));
43      AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(GPOperatorLibrary), VariableKind.In));
44      AddVariableInfo(new VariableInfo("MinTreeSize", "The minimal allowed size of the tree", typeof(IntData), VariableKind.In));
45      AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size of the tree", typeof(IntData), VariableKind.In));
46      AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));
47      AddVariableInfo(new VariableInfo("FunctionTree", "The created tree", typeof(IFunctionTree), VariableKind.New | VariableKind.Out));
48      AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.New | VariableKind.Out));
49      AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.New | VariableKind.Out));
50    }
51
52    public override IOperation Apply(IScope scope) {
53      IRandom random = GetVariableValue<IRandom>("Random", scope, true);
54      GPOperatorLibrary opLibrary = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
55      int minTreeSize = GetVariableValue<IntData>("MinTreeSize", scope, true).Data;
56      int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data;
57      int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data;
58
59      TreeGardener gardener = new TreeGardener(random, opLibrary);
60
61      int treeSize = random.Next(minTreeSize, maxTreeSize + 1);
62
63      IFunctionTree root;
64      int actualTreeSize;
65      int actualTreeHeight;
66      int tries = 0;
67      do {
68        root = gardener.PTC2(random, treeSize, maxTreeHeight);
69        actualTreeSize = root.Size;
70        actualTreeHeight = root.Height;
71        if (tries++ >= MAX_TRIES) {
72          // try a different size
73          treeSize = random.Next(minTreeSize, maxTreeSize + 1);
74          tries = 0;
75        }
76      } while (actualTreeSize > maxTreeSize || actualTreeHeight > maxTreeHeight);
77
78      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), root));
79      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(actualTreeSize)));
80      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(actualTreeHeight)));
81
82      return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(root), scope);
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.