#region License Information
/* HeuristicLab
* Copyright (C) 2002-2014 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.Collections.Generic;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.EvolutionTracking {
[StorableClass]
[Item("AfterCrossoverOperator", "Performs an action after the crossover operator is applied.")]
public class BeforeManipulatorOperator : EvolutionTrackingOperator, IManipulatorOperator where T : class,IItem {
private const string ChildParameterName = "Child";
public ILookupParameter ChildParameter {
get { return (ILookupParameter)Parameters[ChildParameterName]; }
}
protected BeforeManipulatorOperator(BeforeManipulatorOperator original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new BeforeManipulatorOperator(this, cloner);
}
public BeforeManipulatorOperator() {
Parameters.Add(new LookupParameter(ChildParameterName));
}
public override IOperation Apply() {
if (GenealogyGraph.Contains(ChildParameter.ActualValue)) {
// if the graph already contains a vertex representing the child, it means that the child is a product of crossover
// when mutation follows after crossover, some changes need to be made to the graph to maintain consistency
var child = ChildParameter.ActualValue;
var clone = (T)child.Clone();
var vChild = (IGenealogyGraphNode)GenealogyGraph[child];
var vClone = new GenealogyGraphNode { Content = clone, Rank = vChild.Rank - 0.5 };
GenealogyGraph.AddVertex(vClone);
var parents = vChild.Parents; // parents of child become parents of clone
foreach (var p in parents) {
foreach (var a in p.OutArcs.Where(a => a.Target == vChild)) {
a.Target = vClone;
}
vClone.AddReverseArc(p);
}
vClone.InArcs.Last().Data = vChild.InArcs.Last().Data; // fragment of child becomes fragment of clone
vChild.InArcs = new List(); // child will be connected to clone
vChild.AddReverseArc(vClone);
vClone.AddForwardArc(vChild);
// the following step in necessary because some mutation operators change values while the reference stays the same
// so we clone in order to prevent mutations to be "shadowed" in the parent
var parent = vClone.Parents.First();
parent.Content = (T)parent.Content.Clone();
GenealogyGraph[parent.Content] = parent;
} else { // this needs to be checked
var vChild = new GenealogyGraphNode { Content = ChildParameter.ActualValue, Rank = Generations.Value };
GenealogyGraph.AddVertex(vChild);
}
return base.Apply();
}
}
}