Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVectorMultiObjectiveProblem.cs @ 17544

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

#2521: worked on refactoring, worked a lot on binary encoding / problems

File size: 4.7 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.RealVectorEncoding {
35  [StorableType("135697c1-1b2b-46b6-a518-1c6efae09475")]
36  public abstract class RealVectorMultiObjectiveProblem : MultiObjectiveProblem<RealVectorEncoding, RealVector> {
37    [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
38    public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;
39    [Storable] protected ReferenceParameter<DoubleMatrix> BoundsRefParameter { get; private set; }
40    public IValueParameter<DoubleMatrix> BoundsParameter => BoundsRefParameter;
41
42    public int Dimension {
43      get { return DimensionRefParameter.Value.Value; }
44      set { DimensionRefParameter.Value.Value = value; }
45    }
46
47    public DoubleMatrix Bounds {
48      get { return BoundsRefParameter.Value; }
49      set { BoundsParameter.Value = value; }
50    }
51
52    [StorableConstructor]
53    protected RealVectorMultiObjectiveProblem(StorableConstructorFlag _) : base(_) { }
54    [StorableHook(HookType.AfterDeserialization)]
55    private void AfterDeserialization() {
56      RegisterEventHandlers();
57    }
58
59    protected RealVectorMultiObjectiveProblem(RealVectorMultiObjectiveProblem original, Cloner cloner)
60      : base(original, cloner) {
61      DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
62      BoundsRefParameter = cloner.Clone(original.BoundsRefParameter);
63      RegisterEventHandlers();
64    }
65
66    protected RealVectorMultiObjectiveProblem() : this(new RealVectorEncoding() { Length = 10 }) { }
67    protected RealVectorMultiObjectiveProblem(RealVectorEncoding encoding) : base(encoding) {
68      EncodingParameter.ReadOnly = true;
69      Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the real vector problem.", Encoding.LengthParameter));
70      Parameters.Add(BoundsRefParameter = new ReferenceParameter<DoubleMatrix>("Bounds", "The bounding box of the values.", Encoding.BoundsParameter));
71
72
73      Operators.Add(new HammingSimilarityCalculator());
74      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
75
76      Parameterize();
77      RegisterEventHandlers();
78    }
79
80    public override void Analyze(RealVector[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
81      base.Analyze(individuals, qualities, results, random);
82
83      var fronts = DominationCalculator.CalculateAllParetoFrontsIndices(individuals, qualities, Maximization);
84      var plot = new ParetoFrontScatterPlot<RealVector>(fronts, individuals, qualities, Objectives, BestKnownFront);
85      results.AddOrUpdateResult("Pareto Front Scatter Plot", plot);
86    }
87
88    protected override void OnEncodingChanged() {
89      base.OnEncodingChanged();
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      DimensionRefParameter.Value.ValueChanged += DimensionParameter_Value_ValueChanged;
102      BoundsRefParameter.ValueChanged += BoundsParameter_ValueChanged;
103    }
104
105    private void DimensionParameter_Value_ValueChanged(object sender, EventArgs e) {
106      DimensionOnChanged();
107    }
108
109    private void BoundsParameter_ValueChanged(object sender, EventArgs e) {
110      BoundsOnChanged();
111    }
112
113    protected virtual void DimensionOnChanged() { }
114
115    protected virtual void BoundsOnChanged() { }
116  }
117}
Note: See TracBrowser for help on using the repository browser.