Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: Reverse behavior, parameters are not readonly by default, can be readonly if computed automatically

File size: 4.2 KB
RevLine 
[16876]1#region License Information
2/* HeuristicLab
[17226]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[16876]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.Linq;
23using HEAL.Attic;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
[17544]27using HeuristicLab.Data;
[16876]28using HeuristicLab.Optimization;
[17544]29using HeuristicLab.Parameters;
[16876]30
31namespace HeuristicLab.Encodings.BinaryVectorEncoding {
32  [StorableType("b64caac0-a23a-401a-bb7e-ffa3e22b80ea")]
33  public abstract class BinaryVectorMultiObjectiveProblem : MultiObjectiveProblem<BinaryVectorEncoding, BinaryVector> {
[17522]34    [Storable] protected IResultParameter<ParetoFrontScatterPlot<BinaryVector>> BestResultParameter { get; private set; }
35    public IResultDefinition<ParetoFrontScatterPlot<BinaryVector>> BestResult { get { return BestResultParameter; } }
[17544]36    [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
[17522]37
[17544]38    public int Dimension {
[17545]39      get { return Encoding.Length; }
40      set { Encoding.Length = value; }
[16876]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) {
[17522]52      BestResultParameter = cloner.Clone(original.BestResultParameter);
[17544]53      DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
[16876]54      RegisterEventHandlers();
55    }
56
[16948]57    protected BinaryVectorMultiObjectiveProblem() : this(new BinaryVectorEncoding() { Length = 10 }) { }
58    protected BinaryVectorMultiObjectiveProblem(BinaryVectorEncoding encoding) : base(encoding) {
59      EncodingParameter.ReadOnly = true;
[17522]60      Parameters.Add(BestResultParameter = new ResultParameter<ParetoFrontScatterPlot<BinaryVector>>("Best Pareto Front", "The best Pareto front found."));
[17544]61      Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the binary vector problem.", Encoding.LengthParameter));
[16876]62
63      Operators.Add(new HammingSimilarityCalculator());
64      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
65
66      Parameterize();
67      RegisterEventHandlers();
68    }
69
70    public override void Analyze(BinaryVector[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
71      base.Analyze(individuals, qualities, results, random);
[17230]72
73      var fronts = DominationCalculator.CalculateAllParetoFrontsIndices(individuals, qualities, Maximization);
74      var plot = new ParetoFrontScatterPlot<BinaryVector>(fronts, individuals, qualities, Objectives, BestKnownFront);
[17522]75     
76      BestResultParameter.ActualValue = plot;
[16876]77    }
78
79    protected override void OnEncodingChanged() {
80      base.OnEncodingChanged();
81      Parameterize();
82    }
83
84    private void Parameterize() {
85      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
86        similarityCalculator.SolutionVariableName = Encoding.Name;
87        similarityCalculator.QualityVariableName = Evaluator.QualitiesParameter.ActualName;
88      }
89    }
90
91    private void RegisterEventHandlers() {
[17545]92      Encoding.PropertyChanged += (sender, args) => {
93        if (args.PropertyName == nameof(Encoding.Length))
94          DimensionOnChanged();
95      };
[16876]96    }
97
[17544]98    protected virtual void DimensionOnChanged() { }
[16876]99  }
100}
Note: See TracBrowser for help on using the repository browser.