Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/TracingSymbolicExpressionTreeOperator.cs @ 10355

Last change on this file since 10355 was 9835, checked in by bburlacu, 11 years ago

#1772: Merged remaining trunk changes into the EvolutionaryTracking branch.

File size: 7.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 System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.PluginInfrastructure;
28using CloneMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItem>;
29using GeneticExchangeMapType = HeuristicLab.Core.ItemList<HeuristicLab.Core.IItem>;
30using TraceMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItemList<HeuristicLab.Core.IItem>>;
31
32namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
33  /// <summary>
34  /// A base class for operators for symbolic expression trees.
35  /// </summary>
36  [Item("SymbolicExpressionTreeOperator", "A base class for operators for symbolic expression trees.")]
37  [StorableClass]
38  public abstract class TracingSymbolicExpressionTreeOperator : SymbolicExpressionTreeOperator, ITracingSymbolicExpressionTreeOperator {
39    private const string GlobalTraceMapParameterName = "GlobalTraceMap";
40    private const string GlobalCloneMapParameterName = "GlobalCloneMap";
41    private const string GlobalFragmentMapParameterName = "GlobalFragmentMap";
42    private const string GlobalGeneticExchangeMapParameterName = "GlobalGeneticExchangeMap";
43    private const string SymbolicExpressionTreeNodeComparerParameterName = "SymbolicExpressionTreeNodeComparer";
44    private const string SymbolicExpressionTreeNodeComparerParameterDescription = "The comparison operator used to check if two symbolic expression tree nodes are equal or similar.";
45
46    #region Parameter properties
47    public LookupParameter<CloneMapType> GlobalCloneMapParameter {
48      get { return (LookupParameter<CloneMapType>)Parameters[GlobalCloneMapParameterName]; }
49    }
50    public LookupParameter<TraceMapType> GlobalTraceMapParameter {
51      get { return (LookupParameter<TraceMapType>)Parameters[GlobalTraceMapParameterName]; }
52    }
53    public LookupParameter<CloneMapType> GlobalFragmentMapParameter {
54      get { return (LookupParameter<CloneMapType>)Parameters[GlobalFragmentMapParameterName]; }
55    }
56    public LookupParameter<GeneticExchangeMapType> GlobalGeneticExchangeMapParameter {
57      get { return (LookupParameter<GeneticExchangeMapType>)Parameters[GlobalGeneticExchangeMapParameterName]; }
58    }
59    public ValueParameter<ISymbolicExpressionTreeNodeSimilarityComparer> SymbolicExpressionTreeNodeComparerParameter {
60      get { return (ValueParameter<ISymbolicExpressionTreeNodeSimilarityComparer>)Parameters[SymbolicExpressionTreeNodeComparerParameterName]; }
61    }
62    #endregion
63
64    #region Properties
65    public CloneMapType GlobalCloneMap {
66      get { return GlobalCloneMapParameter.ActualValue; }
67    }
68    public TraceMapType GlobalTraceMap {
69      get { return GlobalTraceMapParameter.ActualValue; }
70    }
71    public CloneMapType GlobalFragmentMap {
72      get { return GlobalFragmentMapParameter.ActualValue; }
73    }
74    public GeneticExchangeMapType GlobalGeneticExchangeMap {
75      get { return GlobalGeneticExchangeMapParameter.ActualValue; }
76    }
77    public ISymbolicExpressionTreeNodeSimilarityComparer SymbolicExpressionTreeNodeComparer {
78      get { return (ISymbolicExpressionTreeNodeSimilarityComparer)SymbolicExpressionTreeNodeComparerParameter.ActualValue; }
79    }
80    #endregion
81
82    [StorableHook(HookType.AfterDeserialization)]
83    private void AfterDeserialization() {
84      if (!Parameters.ContainsKey(GlobalCloneMapParameterName))
85        Parameters.Add(new LookupParameter<CloneMapType>(GlobalCloneMapParameterName, "A global map keeping track of trees and their clones (made during selection)."));
86      if (!Parameters.ContainsKey(GlobalFragmentMapParameterName))
87        Parameters.Add(new LookupParameter<CloneMapType>(GlobalFragmentMapParameterName, "A global map keeping track of tree fragments received via crossover."));
88      if (!Parameters.ContainsKey(GlobalTraceMapParameterName))
89        Parameters.Add(new LookupParameter<TraceMapType>(GlobalTraceMapParameterName, "A global cache containing tracing info."));
90      if (Parameters.ContainsKey("GlobalTransactionMap"))
91        Parameters.Remove("GlobalTransactionMap");
92      if (!Parameters.ContainsKey(GlobalGeneticExchangeMapParameterName))
93        Parameters.Add(new LookupParameter<GeneticExchangeMapType>(GlobalGeneticExchangeMapParameterName, "A global list of genetic exchange transactions."));
94      if (!Parameters.ContainsKey(SymbolicExpressionTreeNodeComparerParameterName))
95        Parameters.Add(new ValueParameter<ISymbolicExpressionTreeNodeSimilarityComparer>(SymbolicExpressionTreeNodeComparerParameterName, SymbolicExpressionTreeNodeComparerParameterDescription));
96      if (SymbolicExpressionTreeNodeComparer == null) {
97        SymbolicExpressionTreeNodeComparerParameter.ActualValue = ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeNodeSimilarityComparer>().First();
98      }
99    }
100
101
102    [StorableConstructor]
103    protected TracingSymbolicExpressionTreeOperator(bool deserializing) : base(deserializing) { }
104    protected TracingSymbolicExpressionTreeOperator(TracingSymbolicExpressionTreeOperator original, Cloner cloner)
105      : base(original, cloner) {
106    }
107    protected TracingSymbolicExpressionTreeOperator()
108      : base() {
109      Parameters.Add(new LookupParameter<CloneMapType>(GlobalCloneMapParameterName, "A global map keeping track of trees and their clones (made during selection)."));
110      Parameters.Add(new LookupParameter<CloneMapType>(GlobalFragmentMapParameterName, "A global map keeping track of tree fragments received via crossover."));
111      Parameters.Add(new LookupParameter<TraceMapType>(GlobalTraceMapParameterName, "A global cache containing tracing info."));
112      Parameters.Add(new LookupParameter<GeneticExchangeMapType>(GlobalGeneticExchangeMapParameterName, "A global list of genetic exchange transactions."));
113      Parameters.Add(new ValueParameter<ISymbolicExpressionTreeNodeSimilarityComparer>(SymbolicExpressionTreeNodeComparerParameterName, SymbolicExpressionTreeNodeComparerParameterDescription));
114    }
115
116    protected void AddTracingVariablesToGlobalScope() {
117      // add global trace cache if not already present in global scope
118      var gScope = ExecutionContext.Scope;
119      while (gScope.Parent != null) gScope = gScope.Parent;
120      if (GlobalTraceMap == null) gScope.Variables.Add(new Variable(GlobalTraceMapParameterName, new TraceMapType()));
121      if (GlobalFragmentMap == null) gScope.Variables.Add(new Variable(GlobalFragmentMapParameterName, new CloneMapType()));
122      if (GlobalGeneticExchangeMap == null) gScope.Variables.Add(new Variable(GlobalGeneticExchangeMapParameterName, new GeneticExchangeMapType()));
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.