Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/TracingSymbolicExpressionTreeManipulator.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: 4.5 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.Data;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
29  /// <summary>
30  /// A base class for operators that manipulate real-valued vectors.
31  /// </summary>
32  [Item("TracingSymbolicExpressionTreeManipulator", "A base class for operators that manipulate symbolic expression trees.")]
33  [StorableClass]
34  public abstract class TracingSymbolicExpressionTreeManipulator : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeManipulator {
35    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
36    private const string GlobalTraceCacheParameterName = "GlobalTraceCache";
37    private const string GlobalTreeMapParameterName = "GlobalTreeMap";
38
39    #region Parameter Properties
40    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
41      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
42    }
43    public LookupParameter<ItemDictionary<ISymbolicExpressionTree, ISymbolicExpressionTree>> GlobalTreeMapParameter {
44      get {
45        return (LookupParameter<ItemDictionary<ISymbolicExpressionTree, ISymbolicExpressionTree>>)Parameters[GlobalTreeMapParameterName];
46      }
47    }
48    #endregion
49
50    #region Properties
51    public ISymbolicExpressionTree SymbolicExpressionTree {
52      get { return SymbolicExpressionTreeParameter.ActualValue; }
53    }
54    public ItemDictionary<ISymbolicExpressionTree, ISymbolicExpressionTree> GlobalTreeMap {
55      get { return GlobalTreeMapParameter.ActualValue; }
56    }
57    #endregion
58
59    [StorableConstructor]
60    protected TracingSymbolicExpressionTreeManipulator(bool deserializing) : base(deserializing) { }
61    protected TracingSymbolicExpressionTreeManipulator(TracingSymbolicExpressionTreeManipulator original, Cloner cloner) : base(original, cloner) { }
62    public TracingSymbolicExpressionTreeManipulator()
63      : base() {
64      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied."));
65      Parameters.Add(new LookupParameter<ItemDictionary<ISymbolicExpressionTree, ISymbolicExpressionTree>>(GlobalTreeMapParameterName, "A global map keeping track of trees and their clones (made during selection)."));
66    }
67
68    public sealed override IOperation Apply() {
69      ISymbolicExpressionTree tree = SymbolicExpressionTreeParameter.ActualValue;
70
71      Manipulate(RandomParameter.ActualValue, tree);
72
73      //AddTraceInfoToGlobalCache();
74
75      return base.Apply();
76    }
77
78    private void AddTraceInfoToGlobalCache() {
79      var globalScope = ExecutionContext.Scope;
80      while (globalScope.Parent != null) globalScope = globalScope.Parent;
81
82      Variable globalTraceCache;
83      if (!globalScope.Variables.ContainsKey(GlobalTraceCacheParameterName)) {
84        globalTraceCache = new Variable(GlobalTraceCacheParameterName, new ItemDictionary<ISymbolicExpressionTree, IItemArray<ISymbolicExpressionTree>>());
85        globalScope.Variables.Add(globalTraceCache);
86      } else
87        globalTraceCache = (Variable)globalScope.Variables[GlobalTraceCacheParameterName];
88
89      var traceCache = (ItemDictionary<ISymbolicExpressionTree, IItemArray<ISymbolicExpressionTree>>)globalTraceCache.Value;
90
91      traceCache.Add(SymbolicExpressionTree, new ItemArray<ISymbolicExpressionTree>(new[] { GlobalTreeMap[SymbolicExpressionTree] }));
92    }
93
94    protected abstract void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree);
95  }
96}
Note: See TracBrowser for help on using the repository browser.