Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3140_NumberSymbol/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/RampedHalfAndHalfTreeCreator.cs @ 18091

Last change on this file since 18091 was 18064, checked in by mkommend, 3 years ago

#2997: Simplified tree creator interface and removed cloning of grammars,

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Common;
23using HeuristicLab.Core;
24using HEAL.Attic;
25using HeuristicLab.PluginInfrastructure;
26
27namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
28  [NonDiscoverableType]
29  [StorableType("0E43AC76-6CD1-4371-90A6-05F7C5B05EC8")]
30  [Item("RampedHalfAndHalfTreeCreator", "An operator that creates new symbolic expression trees in an alternate way: half the trees are created usign the 'Grow' method while the other half are created using the 'Full' method")]
31  public class RampedHalfAndHalfTreeCreator : SymbolicExpressionTreeCreator {
32    [StorableConstructor]
33    protected RampedHalfAndHalfTreeCreator(StorableConstructorFlag _) : base(_) { }
34    protected RampedHalfAndHalfTreeCreator(RampedHalfAndHalfTreeCreator original, Cloner cloner) : base(original, cloner) { }
35
36    public RampedHalfAndHalfTreeCreator() : base() { }
37
38    public override IDeepCloneable Clone(Cloner cloner) {
39      return new RampedHalfAndHalfTreeCreator(this, cloner);
40    }
41
42    public override ISymbolicExpressionTree CreateTree(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
43      return Create(random, grammar, maxTreeLength, maxTreeDepth);
44    }
45
46    /// <summary>
47    /// Create a symbolic expression tree using 'RampedHalfAndHalf' strategy.
48    /// Half the trees are created with the 'Grow' method, and the other half are created with the 'Full' method.
49    /// </summary>
50    /// <param name="random">Random generator</param>
51    /// <param name="grammar">Available tree grammar</param>
52    /// <param name="maxTreeLength">Maximum tree length (this parameter is ignored)</param>
53    /// <param name="maxTreeDepth">Maximum tree depth</param>
54    /// <returns></returns>
55    public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
56      var tree = new SymbolicExpressionTree();
57      var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
58      if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
59      rootNode.SetGrammar(grammar.CreateExpressionTreeGrammar());
60
61      var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
62      if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
63      startNode.SetGrammar(grammar.CreateExpressionTreeGrammar());
64
65      rootNode.AddSubtree(startNode);
66
67      double p = random.NextDouble();
68
69      if (p < 0.5)
70        GrowTreeCreator.Create(random, startNode, maxTreeDepth - 2);
71      else
72        FullTreeCreator.Create(random, startNode, maxTreeDepth - 2);
73
74      tree.Root = rootNode;
75      return tree;
76    }
77  }
78}
Note: See TracBrowser for help on using the repository browser.