[6887] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6887] | 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;
|
---|
[7012] | 27 | using HeuristicLab.PluginInfrastructure;
|
---|
[6887] | 28 |
|
---|
| 29 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
[7012] | 30 | [NonDiscoverableType]
|
---|
[6887] | 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 |
|
---|
| 39 | #region Parameter Properties
|
---|
| 40 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
|
---|
| 41 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
|
---|
| 45 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | #endregion
|
---|
| 49 | #region Properties
|
---|
| 50 | public IntValue MaximumSymbolicExpressionTreeDepth {
|
---|
| 51 | get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[7012] | 54 | public IntValue MaximumSymbolicExpressionTreeLength {
|
---|
| 55 | get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
|
---|
| 56 | }
|
---|
[6887] | 57 | #endregion
|
---|
| 58 |
|
---|
| 59 | [StorableConstructor]
|
---|
| 60 | protected RampedHalfAndHalfTreeCreator(bool deserializing) : base(deserializing) { }
|
---|
| 61 | protected RampedHalfAndHalfTreeCreator(RampedHalfAndHalfTreeCreator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 62 |
|
---|
| 63 | public RampedHalfAndHalfTreeCreator()
|
---|
| 64 | : base() {
|
---|
[7236] | 65 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName,
|
---|
| 66 | "The maximal length (number of nodes) of the symbolic expression tree (this parameter is ignored)."));
|
---|
| 67 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName,
|
---|
| 68 | "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
|
---|
[6887] | 69 | }
|
---|
| 70 |
|
---|
| 71 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 72 | return new RampedHalfAndHalfTreeCreator(this, cloner);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | protected override ISymbolicExpressionTree Create(IRandom random) {
|
---|
[11496] | 76 | return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
|
---|
[6887] | 77 | }
|
---|
| 78 |
|
---|
[7012] | 79 | public override ISymbolicExpressionTree CreateTree(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
|
---|
| 80 | return Create(random, grammar, maxTreeLength, maxTreeDepth);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[6887] | 83 | /// <summary>
|
---|
| 84 | /// Create a symbolic expression tree using 'RampedHalfAndHalf' strategy.
|
---|
| 85 | /// Half the trees are created with the 'Grow' method, and the other half are created with the 'Full' method.
|
---|
| 86 | /// </summary>
|
---|
| 87 | /// <param name="random">Random generator</param>
|
---|
| 88 | /// <param name="grammar">Available tree grammar</param>
|
---|
[7236] | 89 | /// <param name="maxTreeLength">Maximum tree length (this parameter is ignored)</param>
|
---|
[6887] | 90 | /// <param name="maxTreeDepth">Maximum tree depth</param>
|
---|
| 91 | /// <returns></returns>
|
---|
[7012] | 92 | public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
|
---|
[6887] | 93 | var tree = new SymbolicExpressionTree();
|
---|
| 94 | var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
|
---|
| 95 | if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
|
---|
[11496] | 96 | rootNode.SetGrammar(grammar.CreateExpressionTreeGrammar());
|
---|
[6887] | 97 |
|
---|
| 98 | var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
|
---|
[7236] | 99 | if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
|
---|
[11496] | 100 | startNode.SetGrammar(grammar.CreateExpressionTreeGrammar());
|
---|
[6887] | 101 |
|
---|
| 102 | rootNode.AddSubtree(startNode);
|
---|
| 103 |
|
---|
| 104 | double p = random.NextDouble();
|
---|
| 105 |
|
---|
| 106 | if (p < 0.5)
|
---|
[7236] | 107 | GrowTreeCreator.Create(random, startNode, maxTreeDepth - 2);
|
---|
[6887] | 108 | else
|
---|
[7236] | 109 | FullTreeCreator.Create(random, startNode, maxTreeDepth - 2);
|
---|
[6887] | 110 |
|
---|
| 111 | tree.Root = rootNode;
|
---|
| 112 | return tree;
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
[7034] | 115 | }
|
---|