Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/SymbolicExpressionTreeCreator.cs @ 15529

Last change on this file since 15529 was 14711, checked in by gkronber, 8 years ago

#2520

  • renamed StorableClass -> StorableType
  • changed persistence to use GUIDs instead of type names
File size: 5.3 KB
RevLine 
[13368]1#region License Information
[3223]2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 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]22using HeuristicLab.Common;
[3223]23using HeuristicLab.Core;
[12422]24using HeuristicLab.Data;
[3223]25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
[5499]28namespace 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.")]
[14711]33  [StorableType("9DF00734-B692-4211-BFD2-57A93F1C47FC")]
[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]
58    protected SymbolicExpressionTreeCreator(bool deserializing) : base(deserializing) { }
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}
Note: See TracBrowser for help on using the repository browser.