1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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.Data;
|
---|
25 | using HeuristicLab.Parameters;
|
---|
26 | using HEAL.Attic;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
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 | [StorableType("88CC0F00-5674-4598-A3C4-756B3B7E80CB")]
|
---|
34 | public abstract class SymbolicExpressionTreeCreator : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeCreator {
|
---|
35 | private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
|
---|
36 | private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
|
---|
37 |
|
---|
38 | private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
|
---|
39 | private const string ClonedSymbolicExpressionTreeGrammarParameterName = "ClonedSymbolicExpressionTreeGrammar";
|
---|
40 |
|
---|
41 | #region Parameter Properties
|
---|
42 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
|
---|
43 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
|
---|
44 | }
|
---|
45 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
|
---|
46 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
|
---|
47 | }
|
---|
48 | public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
|
---|
49 | get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public ILookupParameter<ISymbolicExpressionGrammar> ClonedSymbolicExpressionTreeGrammarParameter {
|
---|
53 | get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[ClonedSymbolicExpressionTreeGrammarParameterName]; }
|
---|
54 | }
|
---|
55 | #endregion
|
---|
56 |
|
---|
57 | [StorableConstructor]
|
---|
58 | protected SymbolicExpressionTreeCreator(StorableConstructorFlag _) : base(_) { }
|
---|
59 | protected SymbolicExpressionTreeCreator(SymbolicExpressionTreeCreator original, Cloner cloner) : base(original, cloner) { }
|
---|
60 | protected SymbolicExpressionTreeCreator()
|
---|
61 | : base() {
|
---|
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."));
|
---|
66 | }
|
---|
67 |
|
---|
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 |
|
---|
74 | public override IOperation InstrumentedApply() {
|
---|
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 | }
|
---|
84 | SymbolicExpressionTreeParameter.ActualValue = Create(RandomParameter.ActualValue);
|
---|
85 | return base.InstrumentedApply();
|
---|
86 | }
|
---|
87 |
|
---|
88 | protected abstract ISymbolicExpressionTree Create(IRandom random);
|
---|
89 | public abstract ISymbolicExpressionTree CreateTree(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth);
|
---|
90 | }
|
---|
91 | }
|
---|