Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521:

  • Moving solution creator parameter from problems to algorithms (breaking wiring in some HeuristicOptimizationProblems)
  • Disallowing evaluator or encoding changes in encoding-specific base problems (to avoid confusion in derived problems whether this needs to be handled or not)
  • Added private set to ReferenceParameter property (serialization)
File size: 4.4 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.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31
32namespace HeuristicLab.Encodings.BinaryVectorEncoding {
33  [StorableType("b64caac0-a23a-401a-bb7e-ffa3e22b80ea")]
34  public abstract class BinaryVectorMultiObjectiveProblem : MultiObjectiveProblem<BinaryVectorEncoding, BinaryVector> {
35    [Storable] protected IResultParameter<ParetoFrontScatterPlot<BinaryVector>> BestResultParameter { get; private set; }
36    [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
37
38    public int Dimension {
39      get { return DimensionRefParameter.Value.Value; }
40      protected set { DimensionRefParameter.Value.Value = value; }
41    }
42
43    [StorableConstructor]
44    protected BinaryVectorMultiObjectiveProblem(StorableConstructorFlag _) : base(_) { }
45    [StorableHook(HookType.AfterDeserialization)]
46    private void AfterDeserialization() {
47      RegisterEventHandlers();
48    }
49
50    protected BinaryVectorMultiObjectiveProblem(BinaryVectorMultiObjectiveProblem original, Cloner cloner)
51      : base(original, cloner) {
52      BestResultParameter = cloner.Clone(original.BestResultParameter);
53      DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
54      RegisterEventHandlers();
55    }
56
57    protected BinaryVectorMultiObjectiveProblem() : this(new BinaryVectorEncoding() { Length = 10 }) { }
58    protected BinaryVectorMultiObjectiveProblem(BinaryVectorEncoding encoding) : base(encoding) {
59      EncodingParameter.ReadOnly = true;
60      EvaluatorParameter.ReadOnly = true;
61      Parameters.Add(BestResultParameter = new ResultParameter<ParetoFrontScatterPlot<BinaryVector>>("Best Pareto Front", "The best Pareto front found."));
62      Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the binary vector problem.", Encoding.LengthParameter));
63
64      Operators.Add(new HammingSimilarityCalculator());
65      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
66
67      Parameterize();
68      RegisterEventHandlers();
69    }
70
71    public override void Analyze(BinaryVector[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
72      base.Analyze(individuals, qualities, results, random);
73
74      var fronts = DominationCalculator.CalculateAllParetoFrontsIndices(individuals, qualities, Maximization);
75      var plot = new ParetoFrontScatterPlot<BinaryVector>(fronts, individuals, qualities, Objectives, BestKnownFront);
76
77      BestResultParameter.ActualValue = plot;
78    }
79
80    protected override sealed void OnEvaluatorChanged() {
81      throw new InvalidOperationException("Evaluator may not change!");
82    }
83
84    protected override sealed void OnEncodingChanged() {
85      throw new InvalidOperationException("Encoding may not change!");
86    }
87
88    protected override void ParameterizeOperators() {
89      base.ParameterizeOperators();
90      Parameterize();
91    }
92
93    private void Parameterize() {
94      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
95        similarityCalculator.SolutionVariableName = Encoding.Name;
96        similarityCalculator.QualityVariableName = Evaluator.QualitiesParameter.ActualName;
97      }
98    }
99
100    private void RegisterEventHandlers() {
101      IntValueParameterChangeHandler.Create(DimensionRefParameter, DimensionOnChanged);
102    }
103
104    protected virtual void DimensionOnChanged() { }
105  }
106}
Note: See TracBrowser for help on using the repository browser.