Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorMultiObjectiveProblem.cs @ 17522

Last change on this file since 17522 was 17522, checked in by abeham, 4 years ago

#2521 WIP refactoring:

  1. Introduce nicer type IResultDefinition for API users to avoid complex IParameter interface (hide ActualValue)
  2. Change result parameter to contexts (need quality and solution): only implemented for BinaryVectorProblem
File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 System;
23using System.Linq;
24using HEAL.Attic;
25using HeuristicLab.Analysis;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Optimization;
29
30namespace HeuristicLab.Encodings.BinaryVectorEncoding {
31  [StorableType("b64caac0-a23a-401a-bb7e-ffa3e22b80ea")]
32  public abstract class BinaryVectorMultiObjectiveProblem : MultiObjectiveProblem<BinaryVectorEncoding, BinaryVector> {
33    [Storable] protected IResultParameter<ParetoFrontScatterPlot<BinaryVector>> BestResultParameter { get; private set; }
34    public IResultDefinition<ParetoFrontScatterPlot<BinaryVector>> BestResult { get { return BestResultParameter; } }
35
36    public int Length {
37      get { return Encoding.Length; }
38      set { Encoding.Length = value; }
39    }
40
41    [StorableConstructor]
42    protected BinaryVectorMultiObjectiveProblem(StorableConstructorFlag _) : base(_) { }
43    [StorableHook(HookType.AfterDeserialization)]
44    private void AfterDeserialization() {
45      RegisterEventHandlers();
46    }
47
48    protected BinaryVectorMultiObjectiveProblem(BinaryVectorMultiObjectiveProblem original, Cloner cloner)
49      : base(original, cloner) {
50      BestResultParameter = cloner.Clone(original.BestResultParameter);
51      RegisterEventHandlers();
52    }
53
54    protected BinaryVectorMultiObjectiveProblem() : this(new BinaryVectorEncoding() { Length = 10 }) { }
55    protected BinaryVectorMultiObjectiveProblem(BinaryVectorEncoding encoding) : base(encoding) {
56      EncodingParameter.ReadOnly = true;
57      Parameters.Add(BestResultParameter = new ResultParameter<ParetoFrontScatterPlot<BinaryVector>>("Best Pareto Front", "The best Pareto front found."));
58
59      Operators.Add(new HammingSimilarityCalculator());
60      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
61
62      Parameterize();
63      RegisterEventHandlers();
64    }
65
66    public override void Analyze(BinaryVector[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
67      base.Analyze(individuals, qualities, results, random);
68
69      var fronts = DominationCalculator.CalculateAllParetoFrontsIndices(individuals, qualities, Maximization);
70      var plot = new ParetoFrontScatterPlot<BinaryVector>(fronts, individuals, qualities, Objectives, BestKnownFront);
71     
72      BestResultParameter.ActualValue = plot;
73    }
74
75    protected override void OnEncodingChanged() {
76      base.OnEncodingChanged();
77      Parameterize();
78    }
79
80    private void Parameterize() {
81      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
82        similarityCalculator.SolutionVariableName = Encoding.Name;
83        similarityCalculator.QualityVariableName = Evaluator.QualitiesParameter.ActualName;
84      }
85    }
86
87    private void RegisterEventHandlers() {
88      Encoding.LengthParameter.Value.ValueChanged += LengthParameter_ValueChanged;
89    }
90
91    protected virtual void LengthParameter_ValueChanged(object sender, EventArgs e) { }
92  }
93}
Note: See TracBrowser for help on using the repository browser.