Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/IntegerVectorMultiObjectiveProblem.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: 5.0 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Linq;
26using HEAL.Attic;
27using HeuristicLab.Analysis;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HeuristicLab.Optimization;
32using HeuristicLab.Parameters;
33
34namespace HeuristicLab.Encodings.IntegerVectorEncoding {
35  [StorableType("11916b0f-4c34-4ece-acae-e28d11211b43")]
36  public abstract class IntegerVectorMultiObjectiveProblem : MultiObjectiveProblem<IntegerVectorEncoding, IntegerVector> {
37    [Storable] protected IResultParameter<ParetoFrontScatterPlot<IntegerVector>> BestResultParameter { get; private set; }
38    [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
39    [Storable] protected ReferenceParameter<IntMatrix> BoundsRefParameter { get; private set; }
40
41    public int Dimension {
42      get { return DimensionRefParameter.Value.Value; }
43      set { DimensionRefParameter.Value.Value = value; }
44    }
45
46    public IntMatrix Bounds {
47      get { return BoundsRefParameter.Value; }
48      set { BoundsRefParameter.Value = value; }
49    }
50
51    [StorableConstructor]
52    protected IntegerVectorMultiObjectiveProblem(StorableConstructorFlag _) : base(_) { }
53    [StorableHook(HookType.AfterDeserialization)]
54    private void AfterDeserialization() {
55      RegisterEventHandlers();
56    }
57
58    protected IntegerVectorMultiObjectiveProblem(IntegerVectorMultiObjectiveProblem original, Cloner cloner)
59      : base(original, cloner) {
60      BestResultParameter = cloner.Clone(original.BestResultParameter);
61      DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
62      BoundsRefParameter = cloner.Clone(original.BoundsRefParameter);
63      RegisterEventHandlers();
64    }
65
66    protected IntegerVectorMultiObjectiveProblem() : this(new IntegerVectorEncoding() { Length = 10 }) { }
67    protected IntegerVectorMultiObjectiveProblem(IntegerVectorEncoding encoding) : base(encoding) {
68      EncodingParameter.ReadOnly = true;
69      EvaluatorParameter.ReadOnly = true;
70      Parameters.Add(BestResultParameter = new ResultParameter<ParetoFrontScatterPlot<IntegerVector>>("Best Pareto Front", "The best Pareto front found."));
71      Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the integer vector problem.", Encoding.LengthParameter));
72      Parameters.Add(BoundsRefParameter = new ReferenceParameter<IntMatrix>("Bounds", "The bounds of the integer vector problem.", Encoding.BoundsParameter));
73
74      Operators.Add(new HammingSimilarityCalculator());
75      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
76
77      Parameterize();
78      RegisterEventHandlers();
79    }
80
81    public override void Analyze(IntegerVector[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
82      base.Analyze(individuals, qualities, results, random);
83
84      var fronts = DominationCalculator.CalculateAllParetoFrontsIndices(individuals, qualities, Maximization);
85      var plot = new ParetoFrontScatterPlot<IntegerVector>(fronts, individuals, qualities, Objectives, BestKnownFront);
86
87      BestResultParameter.ActualValue = plot;
88    }
89
90    protected override sealed void OnEvaluatorChanged() {
91      throw new InvalidOperationException("Evaluator may not change!");
92    }
93
94    protected override sealed void OnEncodingChanged() {
95      throw new InvalidOperationException("Encoding may not change!");
96    }
97
98    protected override void ParameterizeOperators() {
99      base.ParameterizeOperators();
100      Parameterize();
101    }
102
103    private void Parameterize() {
104      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
105        similarityCalculator.SolutionVariableName = Encoding.Name;
106        similarityCalculator.QualityVariableName = Evaluator.QualitiesParameter.ActualName;
107      }
108    }
109
110    private void RegisterEventHandlers() {
111      IntValueParameterChangeHandler.Create(DimensionRefParameter, DimensionOnChanged);
112      IntMatrixParameterChangeHandler.Create(BoundsRefParameter, BoundsOnChanged);
113    }
114
115    protected virtual void DimensionOnChanged() { }
116
117    protected virtual void BoundsOnChanged() { }
118  }
119}
Note: See TracBrowser for help on using the repository browser.