#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.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
using HeuristicLab.EvolutionTracking;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
public class SymbolicDataAnalysisExpressionBeforeManipulatorOperator : BeforeManipulatorOperator {
public SymbolicDataAnalysisExpressionBeforeManipulatorOperator() { }
protected SymbolicDataAnalysisExpressionBeforeManipulatorOperator(SymbolicDataAnalysisExpressionBeforeManipulatorOperator original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new SymbolicDataAnalysisExpressionBeforeManipulatorOperator(this, cloner);
}
[StorableConstructor]
protected SymbolicDataAnalysisExpressionBeforeManipulatorOperator(bool deserializing) : base(deserializing) { }
public override IOperation Apply() {
// the call below does the following things:
// 1) creates a clone of the individual to be manipulated, and adds it in the genealogy graph with rank = currentRank - 0.5.
// the cloning will also clone the vertex data object, therefore reference comparison of node lists will not work
// 2) replaces the parent of the current individual with the clone, and alters graph connections accordingly
// so that vChild vertex will represent the child, and vClone will represent the cloned parent
var result = base.Apply();
var vChild = GenealogyGraph.GetByContent(ChildParameter.ActualValue);
var vClone = vChild.Parents.Last();
var fragment = (IFragment)vClone.InArcs.Last().Data;
if (fragment != null) {
// this step must be performed because the fragment root actually references the original child (not the clone).
// when mutation occurs the fragment root would point to the mutated subtree in the child instead of the original one in the clone
fragment.Root = vClone.Data.IterateNodesPrefix().ElementAt(fragment.Index1);
}
return result;
}
}
}