1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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.Persistence.Default.CompositeSerializers.Storable;
|
---|
25 | using HeuristicLab.PluginInfrastructure;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
28 | [NonDiscoverableType]
|
---|
29 | [StorableClass]
|
---|
30 | [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")]
|
---|
31 | public class RampedHalfAndHalfTreeCreator : SymbolicExpressionTreeCreator {
|
---|
32 | [StorableConstructor]
|
---|
33 | protected RampedHalfAndHalfTreeCreator(bool deserializing) : base(deserializing) { }
|
---|
34 | protected RampedHalfAndHalfTreeCreator(RampedHalfAndHalfTreeCreator original, Cloner cloner) : base(original, cloner) { }
|
---|
35 |
|
---|
36 | public RampedHalfAndHalfTreeCreator() : base() { }
|
---|
37 |
|
---|
38 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
39 | return new RampedHalfAndHalfTreeCreator(this, cloner);
|
---|
40 | }
|
---|
41 |
|
---|
42 | protected override ISymbolicExpressionTree Create(IRandom random) {
|
---|
43 | return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue,
|
---|
44 | MaximumSymbolicExpressionTreeLengthParameter.ActualValue.Value, MaximumSymbolicExpressionTreeDepthParameter.ActualValue.Value);
|
---|
45 | }
|
---|
46 |
|
---|
47 | public override ISymbolicExpressionTree CreateTree(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
|
---|
48 | return Create(random, grammar, maxTreeLength, maxTreeDepth);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /// <summary>
|
---|
52 | /// Create a symbolic expression tree using 'RampedHalfAndHalf' strategy.
|
---|
53 | /// Half the trees are created with the 'Grow' method, and the other half are created with the 'Full' method.
|
---|
54 | /// </summary>
|
---|
55 | /// <param name="random">Random generator</param>
|
---|
56 | /// <param name="grammar">Available tree grammar</param>
|
---|
57 | /// <param name="maxTreeLength">Maximum tree length (this parameter is ignored)</param>
|
---|
58 | /// <param name="maxTreeDepth">Maximum tree depth</param>
|
---|
59 | /// <returns></returns>
|
---|
60 | public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
|
---|
61 | var tree = new SymbolicExpressionTree();
|
---|
62 | var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
|
---|
63 | if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
|
---|
64 | rootNode.SetGrammar(grammar.CreateExpressionTreeGrammar());
|
---|
65 |
|
---|
66 | var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
|
---|
67 | if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
|
---|
68 | startNode.SetGrammar(grammar.CreateExpressionTreeGrammar());
|
---|
69 |
|
---|
70 | rootNode.AddSubtree(startNode);
|
---|
71 |
|
---|
72 | double p = random.NextDouble();
|
---|
73 |
|
---|
74 | if (p < 0.5)
|
---|
75 | GrowTreeCreator.Create(random, startNode, maxTreeDepth - 2);
|
---|
76 | else
|
---|
77 | FullTreeCreator.Create(random, startNode, maxTreeDepth - 2);
|
---|
78 |
|
---|
79 | tree.Root = rootNode;
|
---|
80 | return tree;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|