[3223] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14312] | 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;
|
---|
[12891] | 24 | using HeuristicLab.Data;
|
---|
[3223] | 25 | using HeuristicLab.Parameters;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 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.")]
|
---|
| 33 | [StorableClass]
|
---|
[3534] | 34 | public abstract class SymbolicExpressionTreeCreator : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeCreator {
|
---|
[12891] | 35 | private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
|
---|
| 36 | private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
|
---|
| 37 |
|
---|
[11499] | 38 | private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
|
---|
| 39 | private const string ClonedSymbolicExpressionTreeGrammarParameterName = "ClonedSymbolicExpressionTreeGrammar";
|
---|
| 40 |
|
---|
[3338] | 41 | #region Parameter Properties
|
---|
[12891] | 42 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
|
---|
| 43 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
|
---|
[3534] | 44 | }
|
---|
[12891] | 45 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
|
---|
| 46 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
|
---|
| 47 | }
|
---|
[11499] | 48 | public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
|
---|
| 49 | get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
|
---|
[3534] | 50 | }
|
---|
[3338] | 51 |
|
---|
[11499] | 52 | public ILookupParameter<ISymbolicExpressionGrammar> ClonedSymbolicExpressionTreeGrammarParameter {
|
---|
| 53 | get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[ClonedSymbolicExpressionTreeGrammarParameterName]; }
|
---|
| 54 | }
|
---|
[3338] | 55 | #endregion
|
---|
[11499] | 56 |
|
---|
[4722] | 57 | [StorableConstructor]
|
---|
| 58 | protected SymbolicExpressionTreeCreator(bool deserializing) : base(deserializing) { }
|
---|
| 59 | protected SymbolicExpressionTreeCreator(SymbolicExpressionTreeCreator original, Cloner cloner) : base(original, cloner) { }
|
---|
[3223] | 60 | protected SymbolicExpressionTreeCreator()
|
---|
| 61 | : base() {
|
---|
[12891] | 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 |
|
---|
[11499] | 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 |
|
---|
[10278] | 74 | public override IOperation InstrumentedApply() {
|
---|
[11499] | 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 | }
|
---|
[12891] | 84 | SymbolicExpressionTreeParameter.ActualValue = Create(RandomParameter.ActualValue);
|
---|
[10278] | 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 | }
|
---|