#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 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].IterateNodesPrefix().ToList();
var nodes1 = Parents[1].IterateNodesPrefix().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 childNodes = Child.IterateNodesPrefix().ToList();
// compare the two node lists and check the difference (comparing node references so we avoid false functional identity).
int i = 0, min0 = Math.Min(nodes0.Count, childNodes.Count);
// find the index of the fragment (with respect to the prefix order of nodes) in Parent0
while (i < min0 && ReferenceEquals(nodes0[i], childNodes[i])) ++i;
var fragmentRoot = (i == min0) ? null : childNodes[i];
int j = 0;
// find the index of the fragment (with respect to the prefix order of nodes) in Parent1
while (j < nodes1.Count && !ReferenceEquals(nodes1[j], fragmentRoot)) ++j; // this works because the swapped subtree is not removed from the non-root parent
if (i < min0 && j < nodes1.Count && !ReferenceEquals(childNodes[i], nodes1[j])) {
throw new Exception("The two array elements should reference the same subtree.");
}
// 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 { Root = i == min0 ? null : nodes0[i], Index = i }; // fragment replaced in Parent0
var fragment1 = new Fragment { Root = fragmentRoot, Index = i, OldIndex = j }; // 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);
}
}