Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1772: Fixed serialization of genetic fragments, added interface ISymbolicExpressionTreeNodeSimilarityComparer

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