Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Operators/3.3/BeforeMutatorOperator.cs @ 8239

Last change on this file since 8239 was 8239, checked in by jkarder, 12 years ago

#1886: added BeforeMutatorOperator to update the trace map accordingly before mutation

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Encodings.PermutationEncoding;
25using HeuristicLab.Operators;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Analysis.AlgorithmBehavior.Operators {
30  using CloneMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItem>;
31  using TraceMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItemList<HeuristicLab.Core.IItem>>;
32
33  /// <summary>
34  /// An operator that tracks parents after crossover.
35  /// </summary>
36  [Item("BeforeMutatorOperator", "An operator that tracks parents before mutation.")]
37  [StorableClass]
38  public class BeforeMutatorOperator : SingleSuccessorOperator {
39    private const string TargetName = "TSPTour";
40
41    #region Parameter properties
42    public ILookupParameter<Permutation> IndividualParameter {
43      get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
44    }
45    public ILookupParameter<CloneMapType> GlobalCloneMapParameter {
46      get { return (ILookupParameter<CloneMapType>)Parameters[CreateTrackingDatastructures.GlobalCloneMapParameterName]; }
47    }
48    public ILookupParameter<TraceMapType> GlobalTraceMapParameter {
49      get { return (ILookupParameter<TraceMapType>)Parameters[CreateTrackingDatastructures.GlobalTraceMapParameterName]; }
50    }
51    #endregion
52
53    #region Properties
54    public Permutation Individual {
55      get { return IndividualParameter.ActualValue; }
56    }
57    public CloneMapType GlobalCloneMap {
58      get { return GlobalCloneMapParameter.ActualValue; }
59    }
60    public TraceMapType GlobalTraceMap {
61      get { return GlobalTraceMapParameter.ActualValue; }
62    }
63    #endregion
64
65    [StorableConstructor]
66    private BeforeMutatorOperator(bool deserializing) : base(deserializing) { }
67    private BeforeMutatorOperator(BeforeMutatorOperator original, Cloner cloner) : base(original, cloner) { }
68    public BeforeMutatorOperator()
69      : base() {
70      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The individual to be mutated."));
71      Parameters.Add(new LookupParameter<CloneMapType>(CreateTrackingDatastructures.GlobalCloneMapParameterName, "A global map keeping track of trees and their clones (made during selection)."));
72      Parameters.Add(new LookupParameter<TraceMapType>(CreateTrackingDatastructures.GlobalTraceMapParameterName, "A global cache containing tracing info."));
73      IndividualParameter.ActualName = TargetName;
74    }
75
76    public override IDeepCloneable Clone(Cloner cloner) {
77      return new BeforeMutatorOperator(this, cloner);
78    }
79
80    public override IOperation Apply() {
81      if (GlobalTraceMap.ContainsKey(Individual)) { // crossover executed
82        var clone = (IItem)Individual.Clone();
83        GlobalTraceMap[clone] = GlobalTraceMap[Individual];
84        GlobalTraceMap[Individual] = new ItemList<IItem> { clone };
85      } else {
86        var original = GlobalCloneMap[Individual];
87        GlobalTraceMap[Individual] = new ItemList<IItem> { original };
88      }
89      return base.Apply();
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.