Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/RampedHalfAndHalfTreeCreator.cs @ 8464

Last change on this file since 8464 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 7.4 KB
RevLine 
[6887]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 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;
24using HeuristicLab.Data;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[7012]27using HeuristicLab.PluginInfrastructure;
[6887]28
29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
[7012]30  [NonDiscoverableType]
[6887]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    private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
39    private const string ClonedSymbolicExpressionTreeGrammarParameterName = "ClonedSymbolicExpressionTreeGrammar";
40
41    #region Parameter Properties
42    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
43      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
44    }
45
46    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
47      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
48    }
49
50    public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
51      get {
[7236]52        return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName];
[6887]53      }
54    }
55
56    public ILookupParameter<ISymbolicExpressionGrammar> ClonedSymbolicExpressionTreeGrammarParameter {
57      get {
[7236]58        return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[ClonedSymbolicExpressionTreeGrammarParameterName];
[6887]59      }
60    }
61
62    #endregion
63    #region Properties
64    public IntValue MaximumSymbolicExpressionTreeDepth {
65      get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
66    }
67
[7012]68    public IntValue MaximumSymbolicExpressionTreeLength {
69      get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
70    }
71
[7236]72    public ISymbolicExpressionGrammar ClonedSymbolicExpressionTreeGrammar {
[6887]73      get { return ClonedSymbolicExpressionTreeGrammarParameter.ActualValue; }
74    }
75    #endregion
76
77    [StorableConstructor]
78    protected RampedHalfAndHalfTreeCreator(bool deserializing) : base(deserializing) { }
79    protected RampedHalfAndHalfTreeCreator(RampedHalfAndHalfTreeCreator original, Cloner cloner) : base(original, cloner) { }
80
81    public RampedHalfAndHalfTreeCreator()
82      : base() {
[7236]83      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName,
84        "The maximal length (number of nodes) of the symbolic expression tree (this parameter is ignored)."));
85      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName,
86        "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
87      Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName,
88        "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
89      Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName,
90        "An immutable clone of the concrete grammar that is actually used to create and manipulate trees."));
[6887]91    }
92
93    public override IDeepCloneable Clone(Cloner cloner) {
94      return new RampedHalfAndHalfTreeCreator(this, cloner);
95    }
96
97    public override IOperation Apply() {
98      if (ClonedSymbolicExpressionTreeGrammarParameter.ActualValue == null) {
99        SymbolicExpressionTreeGrammarParameter.ActualValue.ReadOnly = true;
100        IScope globalScope = ExecutionContext.Scope;
101        while (globalScope.Parent != null)
102          globalScope = globalScope.Parent;
103
[7236]104        globalScope.Variables.Add(new Variable(ClonedSymbolicExpressionTreeGrammarParameterName,
105          (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone()));
[6887]106      }
107      return base.Apply();
108    }
109
110    protected override ISymbolicExpressionTree Create(IRandom random) {
[7236]111      return Create(random, ClonedSymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
[6887]112    }
113
[7012]114    public override ISymbolicExpressionTree CreateTree(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
115      return Create(random, grammar, maxTreeLength, maxTreeDepth);
116    }
117
[6887]118    /// <summary>
119    /// Create a symbolic expression tree using 'RampedHalfAndHalf' strategy.
120    /// Half the trees are created with the 'Grow' method, and the other half are created with the 'Full' method.
121    /// </summary>
122    /// <param name="random">Random generator</param>
123    /// <param name="grammar">Available tree grammar</param>
[7236]124    /// <param name="maxTreeLength">Maximum tree length (this parameter is ignored)</param>
[6887]125    /// <param name="maxTreeDepth">Maximum tree depth</param>
126    /// <returns></returns>
[7012]127    public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
[6887]128      var tree = new SymbolicExpressionTree();
129      var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
130      if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
131      rootNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar));
132
133      var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
[7236]134      if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
[6887]135      startNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar));
136
137      rootNode.AddSubtree(startNode);
138
139      double p = random.NextDouble();
140
141      if (p < 0.5)
[7236]142        GrowTreeCreator.Create(random, startNode, maxTreeDepth - 2);
[6887]143      else
[7236]144        FullTreeCreator.Create(random, startNode, maxTreeDepth - 2);
[6887]145
146      tree.Root = rootNode;
147      return tree;
148    }
149  }
[7034]150}
Note: See TracBrowser for help on using the repository browser.