Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/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
RevLine 
[6887]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6887]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;
[16565]24using HEAL.Attic;
[7012]25using HeuristicLab.PluginInfrastructure;
[6887]26
27namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
[7012]28  [NonDiscoverableType]
[16565]29  [StorableType("0E43AC76-6CD1-4371-90A6-05F7C5B05EC8")]
[6887]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")]
[12422]31  public class RampedHalfAndHalfTreeCreator : SymbolicExpressionTreeCreator {
[6887]32    [StorableConstructor]
[16565]33    protected RampedHalfAndHalfTreeCreator(StorableConstructorFlag _) : base(_) { }
[6887]34    protected RampedHalfAndHalfTreeCreator(RampedHalfAndHalfTreeCreator original, Cloner cloner) : base(original, cloner) { }
35
[12422]36    public RampedHalfAndHalfTreeCreator() : base() { }
[6887]37
38    public override IDeepCloneable Clone(Cloner cloner) {
39      return new RampedHalfAndHalfTreeCreator(this, cloner);
40    }
41
[7012]42    public override ISymbolicExpressionTree CreateTree(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
43      return Create(random, grammar, maxTreeLength, maxTreeDepth);
44    }
45
[6887]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>
[7236]52    /// <param name="maxTreeLength">Maximum tree length (this parameter is ignored)</param>
[6887]53    /// <param name="maxTreeDepth">Maximum tree depth</param>
54    /// <returns></returns>
[7012]55    public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
[6887]56      var tree = new SymbolicExpressionTree();
57      var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
58      if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
[11496]59      rootNode.SetGrammar(grammar.CreateExpressionTreeGrammar());
[6887]60
61      var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
[7236]62      if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
[11496]63      startNode.SetGrammar(grammar.CreateExpressionTreeGrammar());
[6887]64
65      rootNode.AddSubtree(startNode);
66
67      double p = random.NextDouble();
68
69      if (p < 0.5)
[7236]70        GrowTreeCreator.Create(random, startNode, maxTreeDepth - 2);
[6887]71      else
[7236]72        FullTreeCreator.Create(random, startNode, maxTreeDepth - 2);
[6887]73
74      tree.Root = rootNode;
75      return tree;
76    }
77  }
[7034]78}
Note: See TracBrowser for help on using the repository browser.