#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 System; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using CloneMapType = HeuristicLab.Core.ItemDictionary; using TraceMapType = HeuristicLab.Core.ItemDictionary>; namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { /// /// A base class for operators that perform a crossover of symbolic expression trees. /// [Item("SymbolicExpressionTreeCrossover", "A base class for operators that perform a crossover of symbolic expression trees.")] [StorableClass] public abstract class TracingSymbolicExpressionTreeCrossover : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeCrossover { private const string ParentsParameterName = "Parents"; private const string ChildParameterName = "Child"; private const string GlobalTraceMapParameterName = "GlobalTraceMap"; private const string GlobalCloneMapParameterName = "GlobalCloneMap"; #region Parameter Properties public ILookupParameter> ParentsParameter { get { return (ScopeTreeLookupParameter)Parameters[ParentsParameterName]; } } public ILookupParameter ChildParameter { get { return (ILookupParameter)Parameters[ChildParameterName]; } } public LookupParameter GlobalCloneMapParameter { get { return (LookupParameter)Parameters[GlobalCloneMapParameterName]; } } public LookupParameter GlobalTraceMapParameter { get { return (LookupParameter)Parameters[GlobalTraceMapParameterName]; } } #endregion #region Properties public ItemArray Parents { get { return ParentsParameter.ActualValue; } } public ISymbolicExpressionTree Child { get { return ChildParameter.ActualValue; } set { ChildParameter.ActualValue = value; } } public CloneMapType GlobalCloneMap { get { return GlobalCloneMapParameter.ActualValue; } } public TraceMapType GlobalTraceMap { get { return GlobalTraceMapParameter.ActualValue; } } #endregion [StorableConstructor] protected TracingSymbolicExpressionTreeCrossover(bool deserializing) : base(deserializing) { } protected TracingSymbolicExpressionTreeCrossover(TracingSymbolicExpressionTreeCrossover original, Cloner cloner) : base(original, cloner) { } protected TracingSymbolicExpressionTreeCrossover() : base() { Parameters.Add(new ScopeTreeLookupParameter(ParentsParameterName, "The parent symbolic expression trees which should be crossed.")); Parameters.Add(new LookupParameter(ChildParameterName, "The child symbolic expression tree resulting from the crossover.")); Parameters.Add(new LookupParameter(GlobalCloneMapParameterName, "A global map keeping track of trees and their clones (made during selection).")); Parameters.Add(new LookupParameter(GlobalTraceMapParameterName, "A global cache containing tracing info.")); } public sealed override IOperation Apply() { if (Parents.Length != 2) throw new ArgumentException("Number of parents must be exactly two for symbolic expression tree crossover operators."); // add global trace cache if not already present in global scope if (GlobalTraceMap == null) { var gScope = ExecutionContext.Scope; while (gScope.Parent != null) gScope = gScope.Parent; gScope.Variables.Add(new Variable(GlobalTraceMapParameterName, new TraceMapType())); } var originalParents = new ItemList(Parents.Select(x => GlobalCloneMap[x])); ISymbolicExpressionTree result = Crossover(Random, Parents[0], Parents[1]); Child = result; GlobalTraceMap.Add(Child, originalParents); return base.Apply(); } public abstract ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1); } }