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