Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/TracingSymbolicExpressionTreeCreator.cs @ 7479

Last change on this file since 7479 was 7479, checked in by bburlacu, 12 years ago

#1772: Implemented an initial set of features: individual ancestry, genealogy tracking via customized genetic operators and data structures.

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Parameters;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
28  /// <summary>
29  /// A base class for operators creating symbolic expression trees.
30  /// </summary>
31  [Item("TracingSymbolicExpressionTreeCreator", "A base class for operators creating symbolic expression trees.")]
32  [StorableClass]
33  public abstract class TracingSymbolicExpressionTreeCreator : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeCreator {
34    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
35    private const string GlobalTreeCacheParameterName = "GlobalTreeCache";
36
37
38    #region Parameter Properties
39    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
40      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
41    }
42    #endregion
43
44    #region Properties
45    public ISymbolicExpressionTree SymbolicExpressionTree {
46      get { return SymbolicExpressionTreeParameter.ActualValue; }
47      set { SymbolicExpressionTreeParameter.ActualValue = value; }
48    }
49
50    #endregion
51    [StorableConstructor]
52    protected TracingSymbolicExpressionTreeCreator(bool deserializing) : base(deserializing) { }
53    protected TracingSymbolicExpressionTreeCreator(TracingSymbolicExpressionTreeCreator original, Cloner cloner) : base(original, cloner) { }
54    protected TracingSymbolicExpressionTreeCreator()
55      : base() {
56      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree that should be created."));
57      Parameters.Add(new LookupParameter<ItemList<ISymbolicExpressionTree>>(GlobalTreeCacheParameterName, "The global cache for storing the trees"));
58      //globalScope.Variables.Add(new Variable(GlobalOperatorCacheParameterName, new ItemList<ISymbolicExpressionTree>()));
59    }
60
61    public override IOperation Apply() {
62      SymbolicExpressionTree = Create(Random);
63      // add tree to the global tree cache
64      AddTreeToGlobalCache();
65
66      return base.Apply();
67    }
68
69    private void AddTreeToGlobalCache() {
70      var globalScope = ExecutionContext.Scope;
71      while (globalScope.Parent != null) globalScope = globalScope.Parent;
72
73      Variable globalTreeCache;
74      if (!globalScope.Variables.ContainsKey(GlobalTreeCacheParameterName)) {
75        globalTreeCache = new Variable(GlobalTreeCacheParameterName, new ItemList<ISymbolicExpressionTree>());
76        globalScope.Variables.Add(globalTreeCache);
77      } else
78        globalTreeCache = (Variable)globalScope.Variables[GlobalTreeCacheParameterName];
79      var trees = (ItemList<ISymbolicExpressionTree>)globalTreeCache.Value;
80      trees.Add(SymbolicExpressionTree);
81    }
82
83    protected abstract ISymbolicExpressionTree Create(IRandom random);
84    public abstract ISymbolicExpressionTree CreateTree(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth);
85  }
86}
Note: See TracBrowser for help on using the repository browser.