#region License Information /* HeuristicLab * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using CloneMapType = HeuristicLab.Core.ItemDictionary; using GeneticExchangeMapType = HeuristicLab.Core.ItemList; using TraceMapType = HeuristicLab.Core.ItemDictionary>; namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { /// /// A base class for operators for symbolic expression trees. /// [Item("SymbolicExpressionTreeOperator", "A base class for operators for symbolic expression trees.")] [StorableClass] public abstract class TracingSymbolicExpressionTreeOperator : SymbolicExpressionTreeOperator, ITracingSymbolicExpressionTreeOperator { private const string GlobalTraceMapParameterName = "GlobalTraceMap"; private const string GlobalCloneMapParameterName = "GlobalCloneMap"; private const string GlobalFragmentMapParameterName = "GlobalFragmentMap"; private const string GlobalGeneticExchangeMapParameterName = "GlobalGeneticExchangeMap"; private const string SymbolicExpressionTreeNodeComparerParameterName = "SymbolicExpressionTreeNodeComparer"; private const string SymbolicExpressionTreeNodeComparerParameterDescription = "The comparison operator used to check if two symbolic expression tree nodes are equal or similar."; #region Parameter properties public LookupParameter GlobalCloneMapParameter { get { return (LookupParameter)Parameters[GlobalCloneMapParameterName]; } } public LookupParameter GlobalTraceMapParameter { get { return (LookupParameter)Parameters[GlobalTraceMapParameterName]; } } public LookupParameter GlobalFragmentMapParameter { get { return (LookupParameter)Parameters[GlobalFragmentMapParameterName]; } } public LookupParameter GlobalGeneticExchangeMapParameter { get { return (LookupParameter)Parameters[GlobalGeneticExchangeMapParameterName]; } } public ValueParameter SymbolicExpressionTreeNodeComparerParameter { get { return (ValueParameter)Parameters[SymbolicExpressionTreeNodeComparerParameterName]; } } #endregion [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { if (Parameters.ContainsKey("GlobalTransactionMap")) Parameters.Remove("GlobalTransactionMap"); if (!Parameters.ContainsKey(GlobalGeneticExchangeMapParameterName)) Parameters.Add(new LookupParameter(GlobalGeneticExchangeMapParameterName, "A global list of genetic exchange transactions.")); if (!Parameters.ContainsKey(SymbolicExpressionTreeNodeComparerParameterName)) Parameters.Add(new ValueParameter(SymbolicExpressionTreeNodeComparerParameterName, SymbolicExpressionTreeNodeComparerParameterDescription)); } #region Properties public CloneMapType GlobalCloneMap { get { return GlobalCloneMapParameter.ActualValue; } } public TraceMapType GlobalTraceMap { get { return GlobalTraceMapParameter.ActualValue; } } public CloneMapType GlobalFragmentMap { get { return GlobalFragmentMapParameter.ActualValue; } } public GeneticExchangeMapType GlobalGeneticExchangeMap { get { return GlobalGeneticExchangeMapParameter.ActualValue; } } public ISymbolicExpressionTreeNodeComparer SymbolicExpressionTreeNodeComparer { get { return (ISymbolicExpressionTreeNodeComparer)SymbolicExpressionTreeNodeComparerParameter.ActualValue; } } #endregion [StorableConstructor] protected TracingSymbolicExpressionTreeOperator(bool deserializing) : base(deserializing) { } protected TracingSymbolicExpressionTreeOperator(TracingSymbolicExpressionTreeOperator original, Cloner cloner) : base(original, cloner) { } protected TracingSymbolicExpressionTreeOperator() : base() { Parameters.Add(new LookupParameter(GlobalCloneMapParameterName, "A global map keeping track of trees and their clones (made during selection).")); Parameters.Add(new LookupParameter(GlobalFragmentMapParameterName, "A global map keeping track of tree fragments received via crossover.")); Parameters.Add(new LookupParameter(GlobalTraceMapParameterName, "A global cache containing tracing info.")); Parameters.Add(new LookupParameter(GlobalGeneticExchangeMapParameterName, "A global list of genetic exchange transactions.")); Parameters.Add(new ValueParameter(SymbolicExpressionTreeNodeComparerParameterName, SymbolicExpressionTreeNodeComparerParameterDescription)); } protected void AddTracingVariablesToGlobalScope() { // add global trace cache if not already present in global scope var gScope = ExecutionContext.Scope; while (gScope.Parent != null) gScope = gScope.Parent; if (GlobalTraceMap == null) gScope.Variables.Add(new Variable(GlobalTraceMapParameterName, new TraceMapType())); if (GlobalFragmentMap == null) gScope.Variables.Add(new Variable(GlobalFragmentMapParameterName, new CloneMapType())); if (GlobalGeneticExchangeMap == null) gScope.Variables.Add(new Variable(GlobalGeneticExchangeMapParameterName, new GeneticExchangeMapType())); } } }