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