[6887] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 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("FullTreeCreator", "An operator that creates new symbolic expression trees using the 'Full' method")]
|
---|
| 35 | public class FullTreeCreator : SymbolicExpressionTreeCreator,
|
---|
| 36 | ISymbolicExpressionTreeSizeConstraintOperator,
|
---|
| 37 | ISymbolicExpressionTreeGrammarBasedOperator {
|
---|
| 38 | private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
|
---|
| 39 | private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
|
---|
| 40 | private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
|
---|
| 41 | private const string ClonedSymbolicExpressionTreeGrammarParameterName = "ClonedSymbolicExpressionTreeGrammar";
|
---|
| 42 |
|
---|
| 43 | #region Parameter Properties
|
---|
| 44 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
|
---|
| 45 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
|
---|
| 49 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
|
---|
[6888] | 53 | get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
|
---|
[6887] | 54 | }
|
---|
| 55 |
|
---|
| 56 | public ILookupParameter<ISymbolicExpressionGrammar> ClonedSymbolicExpressionTreeGrammarParameter {
|
---|
[6888] | 57 | get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[ClonedSymbolicExpressionTreeGrammarParameterName]; }
|
---|
[6887] | 58 | }
|
---|
| 59 |
|
---|
| 60 | #endregion
|
---|
| 61 | #region Properties
|
---|
| 62 | public IntValue MaximumSymbolicExpressionTreeDepth {
|
---|
| 63 | get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
|
---|
| 64 | }
|
---|
[7076] | 65 |
|
---|
[7012] | 66 | public IntValue MaximumSymbolicExpressionTreeLength {
|
---|
| 67 | get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
|
---|
| 68 | }
|
---|
[6887] | 69 |
|
---|
[7236] | 70 | public ISymbolicExpressionGrammar ClonedSymbolicExpressionTreeGrammar {
|
---|
[6887] | 71 | get { return ClonedSymbolicExpressionTreeGrammarParameter.ActualValue; }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | #endregion
|
---|
| 75 |
|
---|
| 76 | [StorableConstructor]
|
---|
| 77 | protected FullTreeCreator(bool deserializing) : base(deserializing) { }
|
---|
| 78 | protected FullTreeCreator(FullTreeCreator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 79 |
|
---|
| 80 | public FullTreeCreator()
|
---|
| 81 | : base() {
|
---|
[7236] | 82 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName,
|
---|
| 83 | "The maximal length (number of nodes) of the symbolic expression tree (this parameter is ignored)."));
|
---|
| 84 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName,
|
---|
| 85 | "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
|
---|
| 86 | Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName,
|
---|
| 87 | "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
|
---|
| 88 | Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName,
|
---|
| 89 | "An immutable clone of the concrete grammar that is actually used to create and manipulate trees."));
|
---|
[6887] | 90 | }
|
---|
| 91 |
|
---|
| 92 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 93 | return new FullTreeCreator(this, cloner);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | public override IOperation Apply() {
|
---|
| 97 | if (ClonedSymbolicExpressionTreeGrammarParameter.ActualValue == null) {
|
---|
| 98 | SymbolicExpressionTreeGrammarParameter.ActualValue.ReadOnly = true;
|
---|
| 99 | IScope globalScope = ExecutionContext.Scope;
|
---|
| 100 | while (globalScope.Parent != null)
|
---|
| 101 | globalScope = globalScope.Parent;
|
---|
| 102 |
|
---|
[7236] | 103 | globalScope.Variables.Add(new Variable(ClonedSymbolicExpressionTreeGrammarParameterName,
|
---|
| 104 | (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone()));
|
---|
[6887] | 105 | }
|
---|
| 106 | return base.Apply();
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | protected override ISymbolicExpressionTree Create(IRandom random) {
|
---|
[7236] | 110 | return Create(random, ClonedSymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
|
---|
[6887] | 111 | }
|
---|
| 112 |
|
---|
[7012] | 113 | public override ISymbolicExpressionTree CreateTree(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
|
---|
[7076] | 114 | return Create(random, grammar, maxTreeLength, maxTreeDepth);
|
---|
[7012] | 115 | }
|
---|
| 116 |
|
---|
[6887] | 117 | /// <summary>
|
---|
| 118 | /// Create a symbolic expression tree using the 'Full' method.
|
---|
| 119 | /// Function symbols are used for all nodes situated on a level above the maximum tree depth.
|
---|
| 120 | /// Nodes on the last tree level will have Terminal symbols.
|
---|
| 121 | /// </summary>
|
---|
| 122 | /// <param name="random">Random generator</param>
|
---|
| 123 | /// <param name="grammar">Available tree grammar</param>
|
---|
| 124 | /// <param name="maxTreeDepth">Maximum tree depth</param>
|
---|
[7012] | 125 | /// <param name="maxTreeLength">Maximum tree length. This parameter is not used.</param>
|
---|
[6887] | 126 | /// <returns></returns>
|
---|
[7012] | 127 | public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
|
---|
[6887] | 128 | var tree = new SymbolicExpressionTree();
|
---|
| 129 | var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
|
---|
| 130 | if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
|
---|
| 131 | rootNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar));
|
---|
| 132 |
|
---|
| 133 | var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
|
---|
[7236] | 134 | if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
|
---|
[6887] | 135 | startNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar));
|
---|
| 136 |
|
---|
| 137 | rootNode.AddSubtree(startNode);
|
---|
| 138 |
|
---|
[7236] | 139 | Create(random, startNode, maxTreeDepth - 2);
|
---|
[6887] | 140 | tree.Root = rootNode;
|
---|
| 141 | return tree;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[7236] | 144 | public static void Create(IRandom random, ISymbolicExpressionTreeNode seedNode, int maxDepth) {
|
---|
[6887] | 145 | // make sure it is possible to create a trees smaller than maxDepth
|
---|
| 146 | if (seedNode.Grammar.GetMinimumExpressionDepth(seedNode.Symbol) > maxDepth)
|
---|
| 147 | throw new ArgumentException("Cannot create trees of depth " + maxDepth + " or smaller because of grammar constraints.", "maxDepth");
|
---|
| 148 |
|
---|
| 149 |
|
---|
| 150 | int arity = seedNode.Grammar.GetMaximumSubtreeCount(seedNode.Symbol);
|
---|
[6888] | 151 | // Throw an exception if the seedNode happens to be a terminal, since in this case we cannot grow a tree.
|
---|
| 152 | if (arity <= 0)
|
---|
[6944] | 153 | throw new ArgumentException("Cannot grow tree. Seed node shouldn't have arity zero.");
|
---|
[6887] | 154 |
|
---|
[7236] | 155 | var allowedSymbols = seedNode.Grammar.AllowedSymbols
|
---|
| 156 | .Where(s => s.InitialFrequency > 0.0 && seedNode.Grammar.GetMaximumSubtreeCount(s) > 0)
|
---|
| 157 | .ToList();
|
---|
[7108] | 158 |
|
---|
[7236] | 159 | for (var i = 0; i < arity; i++) {
|
---|
| 160 | var possibleSymbols = allowedSymbols
|
---|
| 161 | .Where(s => seedNode.Grammar.IsAllowedChildSymbol(seedNode.Symbol, s, i))
|
---|
| 162 | .ToList();
|
---|
[7961] | 163 | var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList();
|
---|
| 164 | var selectedSymbol = possibleSymbols.SelectRandom(weights, random);
|
---|
[6887] | 165 | var tree = selectedSymbol.CreateTreeNode();
|
---|
| 166 | if (tree.HasLocalParameters) tree.ResetLocalParameters(random);
|
---|
| 167 | seedNode.AddSubtree(tree);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[6888] | 170 | // Only iterate over the non-terminal nodes (those which have arity > 0)
|
---|
| 171 | // Start from depth 2 since the first two levels are formed by the rootNode and the seedNode
|
---|
[7236] | 172 | foreach (var subTree in seedNode.Subtrees)
|
---|
| 173 | if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) > 0)
|
---|
| 174 | RecursiveCreate(random, subTree, 2, maxDepth);
|
---|
[6887] | 175 | }
|
---|
| 176 |
|
---|
[7236] | 177 | private static void RecursiveCreate(IRandom random, ISymbolicExpressionTreeNode root, int currentDepth, int maxDepth) {
|
---|
[6887] | 178 | var arity = root.Grammar.GetMaximumSubtreeCount(root.Symbol);
|
---|
[6888] | 179 | // In the 'Full' grow method, we cannot have terminals on the intermediate tree levels.
|
---|
| 180 | if (arity <= 0)
|
---|
| 181 | throw new ArgumentException("Cannot grow node of arity zero. Expected a function node.");
|
---|
[6887] | 182 |
|
---|
[7236] | 183 | var allowedSymbols = root.Grammar.AllowedSymbols
|
---|
| 184 | .Where(s => s.InitialFrequency > 0.0)
|
---|
| 185 | .ToList();
|
---|
[7108] | 186 |
|
---|
[7236] | 187 | for (var i = 0; i < arity; i++) {
|
---|
| 188 | var possibleSymbols = allowedSymbols
|
---|
| 189 | .Where(s => root.Grammar.IsAllowedChildSymbol(root.Symbol, s, i) &&
|
---|
| 190 | root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth &&
|
---|
| 191 | root.Grammar.GetMaximumExpressionDepth(s) > maxDepth - currentDepth)
|
---|
| 192 | .ToList();
|
---|
[7108] | 193 | if (!possibleSymbols.Any())
|
---|
| 194 | throw new InvalidOperationException("No symbols are available for the tree.");
|
---|
[7961] | 195 | var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList();
|
---|
[7964] | 196 | var selectedSymbol = possibleSymbols.SelectRandom(weights, random);
|
---|
[6887] | 197 | var tree = selectedSymbol.CreateTreeNode();
|
---|
| 198 | if (tree.HasLocalParameters) tree.ResetLocalParameters(random);
|
---|
| 199 | root.AddSubtree(tree);
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[7236] | 202 | foreach (var subTree in root.Subtrees)
|
---|
| 203 | if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) > 0)
|
---|
| 204 | RecursiveCreate(random, subTree, currentDepth + 1, maxDepth);
|
---|
[6887] | 205 | }
|
---|
| 206 | }
|
---|
[7964] | 207 | }
|
---|