Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Async/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/RampedHalfAndHalfTreeCreator.cs @ 11642

Last change on this file since 11642 was 11496, checked in by mkommend, 10 years ago

#2268: Refactored tree creators to use new method for tree grammar creation and handled grammar cloning in the base class.

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 HeuristicLab.Data;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.PluginInfrastructure;
28
29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
30  [NonDiscoverableType]
31  [StorableClass]
32  [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")]
33  public class RampedHalfAndHalfTreeCreator : SymbolicExpressionTreeCreator,
34                                 ISymbolicExpressionTreeSizeConstraintOperator,
35                                 ISymbolicExpressionTreeGrammarBasedOperator {
36    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
37    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
38
39    #region Parameter Properties
40    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
41      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
42    }
43
44    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
45      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
46    }
47
48    #endregion
49    #region Properties
50    public IntValue MaximumSymbolicExpressionTreeDepth {
51      get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
52    }
53
54    public IntValue MaximumSymbolicExpressionTreeLength {
55      get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
56    }
57    #endregion
58
59    [StorableConstructor]
60    protected RampedHalfAndHalfTreeCreator(bool deserializing) : base(deserializing) { }
61    protected RampedHalfAndHalfTreeCreator(RampedHalfAndHalfTreeCreator original, Cloner cloner) : base(original, cloner) { }
62
63    public RampedHalfAndHalfTreeCreator()
64      : base() {
65      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName,
66        "The maximal length (number of nodes) of the symbolic expression tree (this parameter is ignored)."));
67      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName,
68        "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
69    }
70
71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new RampedHalfAndHalfTreeCreator(this, cloner);
73    }
74
75    protected override ISymbolicExpressionTree Create(IRandom random) {
76      return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
77    }
78
79    public override ISymbolicExpressionTree CreateTree(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
80      return Create(random, grammar, maxTreeLength, maxTreeDepth);
81    }
82
83    /// <summary>
84    /// Create a symbolic expression tree using 'RampedHalfAndHalf' strategy.
85    /// Half the trees are created with the 'Grow' method, and the other half are created with the 'Full' method.
86    /// </summary>
87    /// <param name="random">Random generator</param>
88    /// <param name="grammar">Available tree grammar</param>
89    /// <param name="maxTreeLength">Maximum tree length (this parameter is ignored)</param>
90    /// <param name="maxTreeDepth">Maximum tree depth</param>
91    /// <returns></returns>
92    public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
93      var tree = new SymbolicExpressionTree();
94      var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
95      if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
96      rootNode.SetGrammar(grammar.CreateExpressionTreeGrammar());
97
98      var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
99      if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
100      startNode.SetGrammar(grammar.CreateExpressionTreeGrammar());
101
102      rootNode.AddSubtree(startNode);
103
104      double p = random.NextDouble();
105
106      if (p < 0.5)
107        GrowTreeCreator.Create(random, startNode, maxTreeDepth - 2);
108      else
109        FullTreeCreator.Create(random, startNode, maxTreeDepth - 2);
110
111      tree.Root = rootNode;
112      return tree;
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.