#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;
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 : TracingSymbolicExpressionTreeOperator, ISymbolicExpressionTreeCrossover {
private const string ParentsParameterName = "Parents";
private const string ChildParameterName = "Child";
#region Parameter Properties
public ILookupParameter> ParentsParameter {
get { return (ScopeTreeLookupParameter)Parameters[ParentsParameterName]; }
}
public ILookupParameter ChildParameter {
get { return (ILookupParameter)Parameters[ChildParameterName]; }
}
#endregion
#region Properties
public ItemArray Parents {
get { return ParentsParameter.ActualValue; }
}
public ISymbolicExpressionTree Child {
get { return ChildParameter.ActualValue; }
set { ChildParameter.ActualValue = value; }
}
#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."));
}
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.");
AddTracingVariablesToGlobalScope();
// save the nodes of parent0 in a list so we can track what modifications are made by crossover
var nodes0 = Parents[0].IterateNodesBreadth().ToList();
// perform crossover
Child = Crossover(Random, Parents[0], Parents[1]);
// create another list of parent0's nodes, so we can compare it with the first list to see what changed
var nodes1 = Child.IterateNodesBreadth().ToList();
// compare the two node lists and check the difference (comparing node references so we avoid false functional identity).
int i = 0, min = Math.Min(nodes0.Count, nodes1.Count);
while (i != min && ReferenceEquals(nodes0[i], nodes1[i])) ++i;
// map child to its corresponding parents from the previous generation
GlobalTraceMap[Child] = new ItemList(from p in Parents select GlobalCloneMap[p]);
var fragment0 = new Fragment(i == min ? null : nodes0[i]); // fragment replaced in Parent0
var fragment1 = new Fragment(i == min ? null : nodes1[i]); // fragment received from Parent1
GlobalFragmentMap[Child] = fragment1; // map child to the index of its fragment
// transaction.FragmentIn = the fragment received from the other parent
// transaction.FragmentOut = the fragment that got replaced
GlobalGeneticExchangeMap.Add(new GeneticExchange { FragmentIn = fragment1, FragmentOut = fragment0 });
return base.Apply();
}
public abstract ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1);
}
}