Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis.GenealogyAnalysis/Operators/BeforeManipulatorOperator.cs @ 18067

Last change on this file since 18067 was 15771, checked in by bburlacu, 6 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

File size: 3.0 KB
RevLine 
[10278]1#region License Information
2/* HeuristicLab
[12951]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[10278]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
[11257]22using System.Linq;
[10278]23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
[15771]28namespace HeuristicLab.Problems.ProgramSynthesis {
[10278]29  [StorableClass]
30  [Item("AfterCrossoverOperator", "Performs an action after the crossover operator is applied.")]
[12951]31  public class BeforeManipulatorOperator<T> : EvolutionTrackingOperator<T>, IManipulatorOperator<T> where T : class, IItem {
[10285]32    private const string ChildParameterName = "Child";
33
[10830]34    public ILookupParameter<T> ChildParameter {
35      get { return (ILookupParameter<T>)Parameters[ChildParameterName]; }
36    }
37
[10300]38    protected BeforeManipulatorOperator(BeforeManipulatorOperator<T> original, Cloner cloner)
[10278]39      : base(original, cloner) {
40    }
[11233]41
[10278]42    public override IDeepCloneable Clone(Cloner cloner) {
[10300]43      return new BeforeManipulatorOperator<T>(this, cloner);
[10278]44    }
[11233]45
[11227]46    [StorableConstructor]
47    protected BeforeManipulatorOperator(bool deserializing) : base(deserializing) { }
[10278]48
[10285]49    public BeforeManipulatorOperator() {
[10830]50      Parameters.Add(new LookupParameter<T>(ChildParameterName));
[10285]51    }
[10278]52
53    public override IOperation Apply() {
[13527]54      // since mutation always takes place after crossover, the vertex for the current child is already in the graph
[11858]55      var vChild = GenealogyGraph.GetByContent(ChildParameter.ActualValue);
56      var vClone = (IGenealogyGraphNode<T>)vChild.Clone();
57      vClone.Rank = vChild.Rank - 0.5;
58      GenealogyGraph.AddVertex(vClone);
[10837]59
[11858]60      var arcs = vChild.InArcs.ToList();
[11253]61      foreach (var arc in arcs) {
[11233]62        var p = arc.Source;
63        GenealogyGraph.RemoveArc(arc);
[11858]64        GenealogyGraph.AddArc(new GenealogyGraphArc(p, vClone));
[10890]65      }
[11387]66      // by convention, the arc connecting child with parent (non-root parent in case of crossover)
67      // holds fragment data when applicable. the following line of code preserves this data
68      // the graph arcs are now connecting parent (rank n) --> clone (rank n+0.5) --> child (rank n+1)
[11858]69      vClone.InArcs.Last().Data = arcs.Last().Data;
70      GenealogyGraph.AddArc(new GenealogyGraphArc(vClone, vChild));
[10837]71
[10278]72      return base.Apply();
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.