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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HEAL.Attic;
|
---|
25 | using HeuristicLab.Analysis;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 |
|
---|
32 | namespace 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 | public IResultDefinition<ParetoFrontScatterPlot<BinaryVector>> BestResult { get { return BestResultParameter; } }
|
---|
37 | [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
|
---|
38 | public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;
|
---|
39 |
|
---|
40 | public int Dimension {
|
---|
41 | get { return DimensionRefParameter.Value.Value; }
|
---|
42 | set { DimensionRefParameter.Value.Value = value; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | [StorableConstructor]
|
---|
46 | protected BinaryVectorMultiObjectiveProblem(StorableConstructorFlag _) : base(_) { }
|
---|
47 | [StorableHook(HookType.AfterDeserialization)]
|
---|
48 | private void AfterDeserialization() {
|
---|
49 | RegisterEventHandlers();
|
---|
50 | }
|
---|
51 |
|
---|
52 | protected BinaryVectorMultiObjectiveProblem(BinaryVectorMultiObjectiveProblem original, Cloner cloner)
|
---|
53 | : base(original, cloner) {
|
---|
54 | BestResultParameter = cloner.Clone(original.BestResultParameter);
|
---|
55 | DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
|
---|
56 | RegisterEventHandlers();
|
---|
57 | }
|
---|
58 |
|
---|
59 | protected BinaryVectorMultiObjectiveProblem() : this(new BinaryVectorEncoding() { Length = 10 }) { }
|
---|
60 | protected BinaryVectorMultiObjectiveProblem(BinaryVectorEncoding encoding) : base(encoding) {
|
---|
61 | EncodingParameter.ReadOnly = true;
|
---|
62 | Parameters.Add(BestResultParameter = new ResultParameter<ParetoFrontScatterPlot<BinaryVector>>("Best Pareto Front", "The best Pareto front found."));
|
---|
63 | Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the binary vector problem.", Encoding.LengthParameter));
|
---|
64 |
|
---|
65 | Operators.Add(new HammingSimilarityCalculator());
|
---|
66 | Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
|
---|
67 |
|
---|
68 | Parameterize();
|
---|
69 | RegisterEventHandlers();
|
---|
70 | }
|
---|
71 |
|
---|
72 | public override void Analyze(BinaryVector[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
|
---|
73 | base.Analyze(individuals, qualities, results, random);
|
---|
74 |
|
---|
75 | var fronts = DominationCalculator.CalculateAllParetoFrontsIndices(individuals, qualities, Maximization);
|
---|
76 | var plot = new ParetoFrontScatterPlot<BinaryVector>(fronts, individuals, qualities, Objectives, BestKnownFront);
|
---|
77 |
|
---|
78 | BestResultParameter.ActualValue = plot;
|
---|
79 | }
|
---|
80 |
|
---|
81 | protected override void OnEncodingChanged() {
|
---|
82 | base.OnEncodingChanged();
|
---|
83 | Parameterize();
|
---|
84 | }
|
---|
85 |
|
---|
86 | private void Parameterize() {
|
---|
87 | foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
|
---|
88 | similarityCalculator.SolutionVariableName = Encoding.Name;
|
---|
89 | similarityCalculator.QualityVariableName = Evaluator.QualitiesParameter.ActualName;
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | private void RegisterEventHandlers() {
|
---|
94 | DimensionRefParameter.Value.ValueChanged += DimensionParameter_Value_ValueChanged;
|
---|
95 | }
|
---|
96 |
|
---|
97 | private void DimensionParameter_Value_ValueChanged(object sender, EventArgs e) {
|
---|
98 | DimensionOnChanged();
|
---|
99 | }
|
---|
100 |
|
---|
101 | protected virtual void DimensionOnChanged() { }
|
---|
102 | }
|
---|
103 | }
|
---|