[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.Persistence.Default.CompositeSerializers.Storable;
|
---|
[7012] | 25 | using HeuristicLab.PluginInfrastructure;
|
---|
[6887] | 26 |
|
---|
| 27 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
[7012] | 28 | [NonDiscoverableType]
|
---|
[6887] | 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")]
|
---|
[12422] | 31 | public class RampedHalfAndHalfTreeCreator : SymbolicExpressionTreeCreator {
|
---|
[6887] | 32 | [StorableConstructor]
|
---|
| 33 | protected RampedHalfAndHalfTreeCreator(bool deserializing) : base(deserializing) { }
|
---|
| 34 | protected RampedHalfAndHalfTreeCreator(RampedHalfAndHalfTreeCreator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 35 |
|
---|
[12422] | 36 | public RampedHalfAndHalfTreeCreator() : base() { }
|
---|
[6887] | 37 |
|
---|
| 38 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 39 | return new RampedHalfAndHalfTreeCreator(this, cloner);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | protected override ISymbolicExpressionTree Create(IRandom random) {
|
---|
[12422] | 43 | return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue,
|
---|
| 44 | MaximumSymbolicExpressionTreeLengthParameter.ActualValue.Value, MaximumSymbolicExpressionTreeDepthParameter.ActualValue.Value);
|
---|
[6887] | 45 | }
|
---|
| 46 |
|
---|
[7012] | 47 | public override ISymbolicExpressionTree CreateTree(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
|
---|
| 48 | return Create(random, grammar, maxTreeLength, maxTreeDepth);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[6887] | 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>
|
---|
[7236] | 57 | /// <param name="maxTreeLength">Maximum tree length (this parameter is ignored)</param>
|
---|
[6887] | 58 | /// <param name="maxTreeDepth">Maximum tree depth</param>
|
---|
| 59 | /// <returns></returns>
|
---|
[7012] | 60 | public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
|
---|
[6887] | 61 | var tree = new SymbolicExpressionTree();
|
---|
| 62 | var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
|
---|
| 63 | if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
|
---|
[11496] | 64 | rootNode.SetGrammar(grammar.CreateExpressionTreeGrammar());
|
---|
[6887] | 65 |
|
---|
| 66 | var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
|
---|
[7236] | 67 | if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
|
---|
[11496] | 68 | startNode.SetGrammar(grammar.CreateExpressionTreeGrammar());
|
---|
[6887] | 69 |
|
---|
| 70 | rootNode.AddSubtree(startNode);
|
---|
| 71 |
|
---|
| 72 | double p = random.NextDouble();
|
---|
| 73 |
|
---|
| 74 | if (p < 0.5)
|
---|
[7236] | 75 | GrowTreeCreator.Create(random, startNode, maxTreeDepth - 2);
|
---|
[6887] | 76 | else
|
---|
[7236] | 77 | FullTreeCreator.Create(random, startNode, maxTreeDepth - 2);
|
---|
[6887] | 78 |
|
---|
| 79 | tree.Root = rootNode;
|
---|
| 80 | return tree;
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
[7034] | 83 | }
|
---|