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