Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/GenealogyAnalyzer.cs @ 10278

Last change on this file since 10278 was 10278, checked in by bburlacu, 10 years ago

#1772: Implemented generic genealogy analyzer (should work with any encoding provided the proper wiring is performed in the problem class), and before/after operators for creation, mutation and crossover.

File size: 9.8 KB
Line 
1using HeuristicLab.Common;
2using HeuristicLab.Core;
3using HeuristicLab.Data;
4using HeuristicLab.Operators;
5using HeuristicLab.Optimization;
6using HeuristicLab.Parameters;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8
9namespace HeuristicLab.EvolutionTracking {
10  [StorableClass]
11  [Item("GenealogyAnalyzer", "An analyzer which performs the necessary instrumentation to record the evolution of a genetic algorithm.")]
12  public class GenealogyAnalyzer<TGraph, TVertex, TContent> : SingleSuccessorOperator, IAnalyzer
13    where TGraph : class, IGenealogyGraph<TVertex, TContent>
14    where TVertex : class, IGenealogyGraphNode<TContent>, new()
15    where TContent : class, IItem {
16
17    private AfterCrossoverOperator<TGraph, TVertex, TContent> afterCrossoverOperator;
18    private AfterManipulatorOperator<TGraph, TVertex, TContent> afterManipulatorOperator;
19    private BeforeManipulatorOperator<TGraph, TVertex, TContent> beforeManipulatorOperator;
20    //    private AfterSolutionCreatorOperator<TGraph, TVertex, TContent> afterSolutionCreatorOperator;
21    public string CrossoverParentsParameterName { get; set; }
22    public string CrossoverChildParameterName { get; set; }
23    public string ManipulatorChildParameterName { get; set; }
24
25    private string solutionCreatorIndividualParameterName;
26    public string SolutionCreatorIndividualParameterName {
27      get { return solutionCreatorIndividualParameterName; }
28      set {
29        if (solutionCreatorIndividualParameterName != null && Parameters.ContainsKey(solutionCreatorIndividualParameterName)) {
30          Parameters.Remove(solutionCreatorIndividualParameterName);
31        }
32        solutionCreatorIndividualParameterName = value;
33        if (!Parameters.ContainsKey(solutionCreatorIndividualParameterName)) {
34          Parameters.Add(new ScopeTreeLookupParameter<TContent>(solutionCreatorIndividualParameterName));
35        }
36      }
37    }
38
39    private const string GenerationsParameterName = "Generations";
40    private const string ResultsParameterName = "Results";
41    private const string PopulationGraphParameterName = "PopulationGraph";
42
43    private const string CrossoverParameterName = "Crossover";
44    private const string ManipulatorParameterName = "Mutator";
45    private const string SolutionCreatorParameterName = "SolutionCreator";
46
47    private const string EnableCrossoverTrackingParameterName = "EnableCrossoverTracking";
48    private const string EnableManipulatorTrackingParameterName = "EnableManipulatorTracking";
49    private const string EnableSolutionCreatorTrackingParameterName = "EnableSolutionCreatorTracking"; // should always be enabled. maybe superfluous
50
51    #region parameter properties
52    public ILookupParameter<ResultCollection> ResultsParameter {
53      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
54    }
55    public IScopeTreeLookupParameter<TContent> PopulationParameter {
56      get { return (IScopeTreeLookupParameter<TContent>)Parameters[solutionCreatorIndividualParameterName]; }
57    }
58    public ILookupParameter<IntValue> GenerationsParameter {
59      get { return (ILookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
60    }
61    public IValueParameter<BoolValue> EnableCrossoverTrackingParameter {
62      get { return (IValueParameter<BoolValue>)Parameters[EnableCrossoverTrackingParameterName]; }
63    }
64    public IValueParameter<BoolValue> EnableManipulatorTrackingParameter {
65      get { return (IValueParameter<BoolValue>)Parameters[EnableManipulatorTrackingParameterName]; }
66    }
67    public IValueParameter<BoolValue> EnableSolutionCreatorTrackingParameter {
68      get { return (IValueParameter<BoolValue>)Parameters[EnableSolutionCreatorTrackingParameterName]; }
69    }
70    public ILookupParameter<ICrossover> CrossoverParameter {
71      get { return (ILookupParameter<ICrossover>)Parameters[CrossoverParameterName]; }
72    }
73    public ILookupParameter<IManipulator> ManipulatorParameter {
74      get { return (ILookupParameter<IManipulator>)Parameters[ManipulatorParameterName]; }
75    }
76    public ILookupParameter<ISolutionCreator> SolutionCreatorParameter {
77      get { return (ILookupParameter<ISolutionCreator>)Parameters[SolutionCreatorParameterName]; }
78    }
79    public IntValue Generations {
80      get { return GenerationsParameter.ActualValue; }
81    }
82    #endregion
83
84    #region properties
85    public ICrossover Crossover {
86      get { return CrossoverParameter.ActualValue; }
87    }
88    public IManipulator Manipulator {
89      get { return ManipulatorParameter.ActualValue; }
90    }
91    public ISolutionCreator SolutionCreator {
92      get { return SolutionCreatorParameter.ActualValue; }
93    }
94    public BoolValue EnableCrossoverTracking {
95      get { return EnableCrossoverTrackingParameter.Value; }
96    }
97    public BoolValue EnableManipulatorTracking {
98      get { return EnableManipulatorTrackingParameter.Value; }
99    }
100    public BoolValue EnableSolutionCreatorTracking {
101      get { return EnableSolutionCreatorTrackingParameter.Value; }
102    }
103    public ResultCollection Results {
104      get { return ResultsParameter.ActualValue; }
105    }
106    public IGenealogyGraph<TVertex, TContent> GenealogyGraph {
107      get {
108        IResult result;
109        if (!Results.ContainsKey(PopulationGraphParameterName)) {
110          result = new Result(PopulationGraphParameterName, new GenealogyGraph<TVertex, TContent>());
111          Results.Add(result);
112        } else {
113          result = Results[PopulationGraphParameterName];
114        }
115        var graph = (GenealogyGraph<TVertex, TContent>)result.Value;
116        return graph;
117      }
118    }
119    #endregion
120
121    public GenealogyAnalyzer() {
122      // the instrumented operators
123      Parameters.Add(new LookupParameter<ICrossover>(CrossoverParameterName, "The crossover operator."));
124      Parameters.Add(new LookupParameter<IManipulator>(ManipulatorParameterName, "The manipulator operator."));
125      Parameters.Add(new LookupParameter<ISolutionCreator>(SolutionCreatorParameterName, "The solution creator operator."));
126      // the analyzer parameters
127      Parameters.Add(new ValueParameter<BoolValue>(EnableCrossoverTrackingParameterName, new BoolValue(true)));
128      Parameters.Add(new ValueParameter<BoolValue>(EnableManipulatorTrackingParameterName, new BoolValue(true)));
129      Parameters.Add(new ValueParameter<BoolValue>(EnableSolutionCreatorTrackingParameterName, new BoolValue(true)));
130      // parameters required by the analyzer to do its work
131      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations so far."));
132      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName));
133    }
134    public override IDeepCloneable Clone(Cloner cloner) {
135      return new GenealogyAnalyzer<TGraph, TVertex, TContent>(this, cloner);
136    }
137    protected GenealogyAnalyzer(GenealogyAnalyzer<TGraph, TVertex, TContent> original, Cloner cloner)
138      : base(original, cloner) {
139    }
140
141    public bool EnabledByDefault {
142      get { return false; }
143    }
144
145    public override IOperation Apply() {
146      // wire the before- and after operators
147      //      if (EnableSolutionCreatorTracking.Value) {
148      //        if (afterSolutionCreatorOperator == null) {
149      //          afterSolutionCreatorOperator = new AfterSolutionCreatorOperator<TGraph, TVertex, TContent>();
150      //          afterSolutionCreatorOperator.NewIndividualParameterName = SolutionCreatorIndividualParameterName;
151      //        }
152      //        var instrumentedSolutionCreator = (InstrumentedOperator)SolutionCreator;
153      //        instrumentedSolutionCreator.AfterExecutionOperators.Add(afterSolutionCreatorOperator);
154      //      }
155
156      if (Generations.Value == 0) {
157        foreach (var individual in PopulationParameter.ActualValue) {
158          var vertex = new TVertex {
159            Content = individual,
160            Rank = Generations.Value
161          };
162          GenealogyGraph.AddVertex(vertex);
163        }
164      } else {
165        // add missing individuals in the current generation
166        foreach (var individual in PopulationParameter.ActualValue) {
167          if (!GenealogyGraph.Contains(individual)) continue;
168          // it is an elite which was already added to the graph in the previous generation
169          var vertex = new TVertex {
170            Content = individual,
171            Rank = Generations.Value
172          };
173          GenealogyGraph.AddVertex(vertex);
174        }
175      }
176
177      if (EnableCrossoverTracking.Value) {
178        if (afterCrossoverOperator == null) {
179          afterCrossoverOperator = new AfterCrossoverOperator<TGraph, TVertex, TContent>();
180          afterCrossoverOperator.ParentsParameterName = CrossoverParentsParameterName;
181          afterCrossoverOperator.ChildParameterName = CrossoverChildParameterName;
182        }
183        var instrumentedCrossover = (InstrumentedOperator)Crossover;
184        instrumentedCrossover.AfterExecutionOperators.Add(afterCrossoverOperator);
185      }
186
187      if (EnableManipulatorTracking.Value) {
188        if (beforeManipulatorOperator == null) {
189          beforeManipulatorOperator = new BeforeManipulatorOperator<TGraph, TVertex, TContent>();
190          beforeManipulatorOperator.ChildParameterName = ManipulatorChildParameterName;
191        }
192        if (afterManipulatorOperator == null) {
193          afterManipulatorOperator = new AfterManipulatorOperator<TGraph, TVertex, TContent>();
194          afterManipulatorOperator.ChildParameterName = ManipulatorChildParameterName;
195        }
196        var instrumentedManipulator = (InstrumentedOperator)Manipulator;
197        instrumentedManipulator.BeforeExecutionOperators.Add(beforeManipulatorOperator);
198        instrumentedManipulator.AfterExecutionOperators.Add(afterManipulatorOperator);
199      }
200
201      return base.Apply();
202    }
203  }
204}
Note: See TracBrowser for help on using the repository browser.