[3223] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3223] | 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 |
|
---|
[4722] | 22 | using HeuristicLab.Common;
|
---|
[3223] | 23 | using HeuristicLab.Core;
|
---|
[12422] | 24 | using HeuristicLab.Data;
|
---|
[3223] | 25 | using HeuristicLab.Parameters;
|
---|
[14927] | 26 | using HeuristicLab.Persistence;
|
---|
[3223] | 27 |
|
---|
[5499] | 28 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
[3223] | 29 | /// <summary>
|
---|
| 30 | /// A base class for operators creating symbolic expression trees.
|
---|
| 31 | /// </summary>
|
---|
| 32 | [Item("SymbolicExpressionTreeCreator", "A base class for operators creating symbolic expression trees.")]
|
---|
[14927] | 33 | [StorableType("de2325eb-560e-419b-8556-c1b5d98012cd")]
|
---|
[3534] | 34 | public abstract class SymbolicExpressionTreeCreator : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeCreator {
|
---|
[12422] | 35 | private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
|
---|
| 36 | private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
|
---|
| 37 |
|
---|
[11496] | 38 | private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
|
---|
| 39 | private const string ClonedSymbolicExpressionTreeGrammarParameterName = "ClonedSymbolicExpressionTreeGrammar";
|
---|
| 40 |
|
---|
[3338] | 41 | #region Parameter Properties
|
---|
[12422] | 42 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
|
---|
| 43 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
|
---|
[3534] | 44 | }
|
---|
[12422] | 45 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
|
---|
| 46 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
|
---|
| 47 | }
|
---|
[11496] | 48 | public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
|
---|
| 49 | get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
|
---|
[3534] | 50 | }
|
---|
[3338] | 51 |
|
---|
[11496] | 52 | public ILookupParameter<ISymbolicExpressionGrammar> ClonedSymbolicExpressionTreeGrammarParameter {
|
---|
| 53 | get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[ClonedSymbolicExpressionTreeGrammarParameterName]; }
|
---|
| 54 | }
|
---|
[3338] | 55 | #endregion
|
---|
[11496] | 56 |
|
---|
[4722] | 57 | [StorableConstructor]
|
---|
[15018] | 58 | protected SymbolicExpressionTreeCreator(StorableConstructorFlag deserializing) : base(deserializing) { }
|
---|
[4722] | 59 | protected SymbolicExpressionTreeCreator(SymbolicExpressionTreeCreator original, Cloner cloner) : base(original, cloner) { }
|
---|
[3223] | 60 | protected SymbolicExpressionTreeCreator()
|
---|
| 61 | : base() {
|
---|
[12422] | 62 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
|
---|
| 63 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
|
---|
| 64 | Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
|
---|
| 65 | Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName, "An immutable clone of the concrete grammar that is actually used to create and manipulate trees."));
|
---|
[3223] | 66 | }
|
---|
| 67 |
|
---|
[11496] | 68 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 69 | private void AfterDeserialization() {
|
---|
| 70 | if (!Parameters.ContainsKey(ClonedSymbolicExpressionTreeGrammarParameterName))
|
---|
| 71 | Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName, "An immutable clone of the concrete grammar that is actually used to create and manipulate trees."));
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[10291] | 74 | public override IOperation InstrumentedApply() {
|
---|
[11496] | 75 | if (ClonedSymbolicExpressionTreeGrammarParameter.ActualValue == null) {
|
---|
| 76 | SymbolicExpressionTreeGrammarParameter.ActualValue.ReadOnly = true;
|
---|
| 77 | IScope globalScope = ExecutionContext.Scope;
|
---|
| 78 | while (globalScope.Parent != null)
|
---|
| 79 | globalScope = globalScope.Parent;
|
---|
| 80 |
|
---|
| 81 | globalScope.Variables.Add(new Variable(ClonedSymbolicExpressionTreeGrammarParameterName,
|
---|
| 82 | (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone()));
|
---|
| 83 | }
|
---|
[12422] | 84 | SymbolicExpressionTreeParameter.ActualValue = Create(RandomParameter.ActualValue);
|
---|
[10291] | 85 | return base.InstrumentedApply();
|
---|
[3223] | 86 | }
|
---|
| 87 |
|
---|
[5510] | 88 | protected abstract ISymbolicExpressionTree Create(IRandom random);
|
---|
[7012] | 89 | public abstract ISymbolicExpressionTree CreateTree(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth);
|
---|
[3223] | 90 | }
|
---|
| 91 | }
|
---|