Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.Operators/3.3/Initialization/ProbabilisticTreeCreator.cs @ 3446

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

Made PTC2 operator more robust. #859 (PTC2 initialization operator fails if it is used with restrictive function libraries)

File size: 3.7 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 HeuristicLab.Random;
25using HeuristicLab.GP.Interfaces;
26using System;
27
28namespace HeuristicLab.GP.Operators {
29  public class ProbabilisticTreeCreator : OperatorBase {
30    private static int MAX_TRIES { get { return 100; } }
31
32    public override string Description {
33      get { return @"Generates a new random operator tree."; }
34    }
35
36    public ProbabilisticTreeCreator()
37      : base() {
38      AddVariableInfo(new VariableInfo("Random", "Uniform random number generator", typeof(MersenneTwister), VariableKind.In));
39      AddVariableInfo(new VariableInfo("FunctionLibrary", "The function library containing all available functions", typeof(FunctionLibrary), VariableKind.In));
40      AddVariableInfo(new VariableInfo("MinTreeSize", "The minimal allowed size of the tree", typeof(IntData), VariableKind.In));
41      AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size of the tree", typeof(IntData), VariableKind.In));
42      AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));
43      AddVariableInfo(new VariableInfo("FunctionTree", "The created tree", typeof(IGeneticProgrammingModel), VariableKind.New | VariableKind.Out));
44    }
45
46    public override IOperation Apply(IScope scope) {
47      IRandom random = GetVariableValue<IRandom>("Random", scope, true);
48      FunctionLibrary funLibrary = GetVariableValue<FunctionLibrary>("FunctionLibrary", scope, true);
49      int minTreeSize = GetVariableValue<IntData>("MinTreeSize", scope, true).Data;
50      int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data;
51      int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data;
52
53      IFunctionTree root = Create(random, funLibrary, minTreeSize, maxTreeSize, maxTreeHeight);
54      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), new GeneticProgrammingModel(root)));
55      return Util.CreateInitializationOperation(TreeGardener.GetAllSubTrees(root), scope);
56    }
57
58
59    public static IFunctionTree Create(IRandom random, FunctionLibrary funLib, int minSize, int maxSize, int maxHeight) {
60      int treeSize = random.Next(minSize, maxSize);
61      IFunctionTree root = null;
62      int tries = 0;
63      TreeGardener gardener = new TreeGardener(random, funLib);
64      do {
65        try {
66          root = gardener.PTC2(treeSize, maxHeight);
67        }
68        catch (ArgumentException) {
69          // try a different size
70          treeSize = random.Next(minSize, maxSize);
71          tries = 0;
72        }
73        if (tries++ >= MAX_TRIES) {
74          // try a different size
75          treeSize = random.Next(minSize, maxSize);
76          tries = 0;
77        }
78      } while (root == null || root.GetSize() > maxSize || root.GetHeight() > maxHeight);
79      return root;
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.