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 |
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Parameters;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using HeuristicLab.PluginInfrastructure;
|
---|
28 |
|
---|
29 | namespace 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 | 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 {
|
---|
52 | return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName];
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public ILookupParameter<ISymbolicExpressionGrammar> ClonedSymbolicExpressionTreeGrammarParameter {
|
---|
57 | get {
|
---|
58 | return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[ClonedSymbolicExpressionTreeGrammarParameterName];
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | #endregion
|
---|
63 | #region Properties
|
---|
64 | public IntValue MaximumSymbolicExpressionTreeDepth {
|
---|
65 | get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
|
---|
66 | }
|
---|
67 |
|
---|
68 | public IntValue MaximumSymbolicExpressionTreeLength {
|
---|
69 | get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
|
---|
70 | }
|
---|
71 |
|
---|
72 | public ISymbolicExpressionGrammar ClonedSymbolicExpressionTreeGrammar {
|
---|
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() {
|
---|
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."));
|
---|
91 | }
|
---|
92 |
|
---|
93 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
94 | return new RampedHalfAndHalfTreeCreator(this, cloner);
|
---|
95 | }
|
---|
96 |
|
---|
97 | public override IOperation InstrumentedApply() {
|
---|
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 |
|
---|
104 | globalScope.Variables.Add(new Variable(ClonedSymbolicExpressionTreeGrammarParameterName,
|
---|
105 | (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone()));
|
---|
106 | }
|
---|
107 | return base.InstrumentedApply();
|
---|
108 | }
|
---|
109 |
|
---|
110 | protected override ISymbolicExpressionTree Create(IRandom random) {
|
---|
111 | return Create(random, ClonedSymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
|
---|
112 | }
|
---|
113 |
|
---|
114 | public override ISymbolicExpressionTree CreateTree(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
|
---|
115 | return Create(random, grammar, maxTreeLength, maxTreeDepth);
|
---|
116 | }
|
---|
117 |
|
---|
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>
|
---|
124 | /// <param name="maxTreeLength">Maximum tree length (this parameter is ignored)</param>
|
---|
125 | /// <param name="maxTreeDepth">Maximum tree depth</param>
|
---|
126 | /// <returns></returns>
|
---|
127 | public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
|
---|
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();
|
---|
134 | if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
|
---|
135 | startNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar));
|
---|
136 |
|
---|
137 | rootNode.AddSubtree(startNode);
|
---|
138 |
|
---|
139 | double p = random.NextDouble();
|
---|
140 |
|
---|
141 | if (p < 0.5)
|
---|
142 | GrowTreeCreator.Create(random, startNode, maxTreeDepth - 2);
|
---|
143 | else
|
---|
144 | FullTreeCreator.Create(random, startNode, maxTreeDepth - 2);
|
---|
145 |
|
---|
146 | tree.Root = rootNode;
|
---|
147 | return tree;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|