[6887] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11208] | 3 | * Copyright (C) 2002-2014 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 System;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[7012] | 29 | using HeuristicLab.PluginInfrastructure;
|
---|
[6887] | 30 |
|
---|
| 31 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
[7012] | 32 | [NonDiscoverableType]
|
---|
[6887] | 33 | [StorableClass]
|
---|
| 34 | [Item("GrowTreeCreator", "An operator that creates new symbolic expression trees using the 'Grow' method")]
|
---|
| 35 | public class GrowTreeCreator : SymbolicExpressionTreeCreator,
|
---|
| 36 | ISymbolicExpressionTreeSizeConstraintOperator,
|
---|
| 37 | ISymbolicExpressionTreeGrammarBasedOperator {
|
---|
| 38 | private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
|
---|
| 39 | private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
|
---|
| 40 |
|
---|
| 41 | #region Parameter Properties
|
---|
| 42 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
|
---|
| 43 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
|
---|
| 47 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | #endregion
|
---|
| 51 | #region Properties
|
---|
| 52 | public IntValue MaximumSymbolicExpressionTreeDepth {
|
---|
| 53 | get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[7076] | 56 | public IntValue MaximumSymbolicExpressionTreeLength {
|
---|
[7012] | 57 | get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
|
---|
| 58 | }
|
---|
[6887] | 59 | #endregion
|
---|
| 60 |
|
---|
| 61 | [StorableConstructor]
|
---|
| 62 | protected GrowTreeCreator(bool deserializing) : base(deserializing) { }
|
---|
| 63 | protected GrowTreeCreator(GrowTreeCreator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 64 |
|
---|
| 65 | public GrowTreeCreator()
|
---|
| 66 | : base() {
|
---|
[7236] | 67 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName,
|
---|
| 68 | "The maximal length (number of nodes) of the symbolic expression tree (this parameter is ignored)."));
|
---|
| 69 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName,
|
---|
| 70 | "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
|
---|
[6887] | 71 | }
|
---|
| 72 |
|
---|
| 73 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 74 | return new GrowTreeCreator(this, cloner);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 |
|
---|
| 78 | protected override ISymbolicExpressionTree Create(IRandom random) {
|
---|
[11499] | 79 | return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue,
|
---|
[7236] | 80 | MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
|
---|
[6887] | 81 | }
|
---|
| 82 |
|
---|
[7012] | 83 | public override ISymbolicExpressionTree CreateTree(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
|
---|
| 84 | return Create(random, grammar, maxTreeLength, maxTreeDepth);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[6887] | 87 | /// <summary>
|
---|
| 88 | /// Create a symbolic expression tree using the 'Grow' method.
|
---|
| 89 | /// All symbols are allowed for nodes, so the resulting trees can be of any shape and size.
|
---|
| 90 | /// </summary>
|
---|
| 91 | /// <param name="random">Random generator</param>
|
---|
| 92 | /// <param name="grammar">Available tree grammar</param>
|
---|
| 93 | /// <param name="maxTreeDepth">Maximum tree depth</param>
|
---|
[7012] | 94 | /// <param name="maxTreeLength">Maximum tree length. This parameter is not used.</param>
|
---|
[6887] | 95 | /// <returns></returns>
|
---|
[7012] | 96 | public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
|
---|
[6887] | 97 | var tree = new SymbolicExpressionTree();
|
---|
| 98 | var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
|
---|
| 99 | if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
|
---|
[11499] | 100 | rootNode.SetGrammar(grammar.CreateExpressionTreeGrammar());
|
---|
[6887] | 101 |
|
---|
[11499] | 102 |
|
---|
[6887] | 103 | var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
|
---|
[7236] | 104 | if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
|
---|
[11499] | 105 | startNode.SetGrammar(grammar.CreateExpressionTreeGrammar());
|
---|
[6887] | 106 |
|
---|
| 107 | rootNode.AddSubtree(startNode);
|
---|
| 108 |
|
---|
[7236] | 109 | Create(random, startNode, maxTreeDepth - 2);
|
---|
[6887] | 110 | tree.Root = rootNode;
|
---|
| 111 | return tree;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[7236] | 114 | public static void Create(IRandom random, ISymbolicExpressionTreeNode seedNode, int maxDepth) {
|
---|
[6887] | 115 | // make sure it is possible to create a trees smaller than maxDepth
|
---|
| 116 | if (seedNode.Grammar.GetMinimumExpressionDepth(seedNode.Symbol) > maxDepth)
|
---|
| 117 | throw new ArgumentException("Cannot create trees of depth " + maxDepth + " or smaller because of grammar constraints.", "maxDepth");
|
---|
| 118 |
|
---|
| 119 | var arity = SampleArity(random, seedNode);
|
---|
[6888] | 120 | // throw an exception if the seedNode happens to be a terminal, since in this case we cannot grow a tree
|
---|
[6944] | 121 | if (arity <= 0)
|
---|
| 122 | throw new ArgumentException("Cannot grow tree. Seed node shouldn't have arity zero.");
|
---|
[6887] | 123 |
|
---|
[7236] | 124 | var allowedSymbols = seedNode.Grammar.AllowedSymbols
|
---|
| 125 | .Where(s => s.InitialFrequency > 0.0)
|
---|
| 126 | .ToList();
|
---|
[6887] | 127 |
|
---|
[7236] | 128 | for (var i = 0; i < arity; i++) {
|
---|
| 129 | var possibleSymbols = allowedSymbols
|
---|
| 130 | .Where(s => seedNode.Grammar.IsAllowedChildSymbol(seedNode.Symbol, s, i))
|
---|
| 131 | .ToList();
|
---|
[7961] | 132 | var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList();
|
---|
| 133 | var selectedSymbol = possibleSymbols.SelectRandom(weights, random);
|
---|
[6887] | 134 | var tree = selectedSymbol.CreateTreeNode();
|
---|
| 135 | if (tree.HasLocalParameters) tree.ResetLocalParameters(random);
|
---|
| 136 | seedNode.AddSubtree(tree);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[6888] | 139 | // Only iterate over the non-terminal nodes (those which have arity > 0)
|
---|
| 140 | // Start from depth 2 since the first two levels are formed by the rootNode and the seedNode
|
---|
[7236] | 141 | foreach (var subTree in seedNode.Subtrees)
|
---|
| 142 | if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) > 0)
|
---|
| 143 | RecursiveCreate(random, subTree, 2, maxDepth);
|
---|
[6887] | 144 | }
|
---|
| 145 |
|
---|
[7236] | 146 | private static void RecursiveCreate(IRandom random, ISymbolicExpressionTreeNode root, int currentDepth, int maxDepth) {
|
---|
[6887] | 147 | var arity = SampleArity(random, root);
|
---|
[6888] | 148 | if (arity <= 0)
|
---|
| 149 | throw new ArgumentException("Cannot grow node of arity zero. Expected a function node.");
|
---|
[6887] | 150 |
|
---|
[7961] | 151 | var allowedSymbols = root.Grammar.AllowedSymbols.Where(s => s.InitialFrequency > 0.0).ToList();
|
---|
[7108] | 152 |
|
---|
[7236] | 153 | for (var i = 0; i < arity; i++) {
|
---|
| 154 | var possibleSymbols = allowedSymbols
|
---|
| 155 | .Where(s => root.Grammar.IsAllowedChildSymbol(root.Symbol, s, i) &&
|
---|
| 156 | root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth)
|
---|
| 157 | .ToList();
|
---|
[7961] | 158 |
|
---|
[7108] | 159 | if (!possibleSymbols.Any())
|
---|
| 160 | throw new InvalidOperationException("No symbols are available for the tree.");
|
---|
[7961] | 161 | var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList();
|
---|
| 162 | var selectedSymbol = possibleSymbols.SelectRandom(weights, random);
|
---|
[6887] | 163 | var tree = selectedSymbol.CreateTreeNode();
|
---|
| 164 | if (tree.HasLocalParameters) tree.ResetLocalParameters(random);
|
---|
| 165 | root.AddSubtree(tree);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[7236] | 168 | foreach (var subTree in root.Subtrees)
|
---|
| 169 | if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) != 0)
|
---|
| 170 | RecursiveCreate(random, subTree, currentDepth + 1, maxDepth);
|
---|
[6887] | 171 | }
|
---|
| 172 |
|
---|
| 173 | private static int SampleArity(IRandom random, ISymbolicExpressionTreeNode node) {
|
---|
| 174 | var minArity = node.Grammar.GetMinimumSubtreeCount(node.Symbol);
|
---|
| 175 | var maxArity = node.Grammar.GetMaximumSubtreeCount(node.Symbol);
|
---|
| 176 |
|
---|
| 177 | return random.Next(minArity, maxArity + 1);
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
[7961] | 180 | }
|
---|