Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorProblem.cs @ 17229

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

#2521: Refactored ParetoFrontScatterPlot (moved from TestFunctions.MultiObjective to Analysis)

  • Introduced generic type that may work with all solution encodings
File size: 3.2 KB
Line 
1
2#region License Information
3/* HeuristicLab
4 * Copyright (C) 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#endregion
22
23using System;
24using System.Linq;
25using HEAL.Attic;
26using HeuristicLab.Analysis;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Optimization;
30using HeuristicLab.Optimization.Operators;
31
32namespace HeuristicLab.Encodings.BinaryVectorEncoding {
33  [StorableType("2F6FEB34-BD19-47AF-9484-7F48565C0C43")]
34  public abstract class BinaryVectorProblem : SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> {
35    public int Length {
36      get { return Encoding.Length; }
37      set { Encoding.Length = value; }
38    }
39
40    [StorableConstructor]
41    protected BinaryVectorProblem(StorableConstructorFlag _) : base(_) { }
42    [StorableHook(HookType.AfterDeserialization)]
43    private void AfterDeserialization() {
44      RegisterEventHandlers();
45    }
46
47    protected BinaryVectorProblem(BinaryVectorProblem original, Cloner cloner)
48      : base(original, cloner) {
49      RegisterEventHandlers();
50    }
51
52    protected BinaryVectorProblem() : this(new BinaryVectorEncoding() { Length = 10 }) { }
53    protected BinaryVectorProblem(BinaryVectorEncoding encoding) : base(encoding) {
54      EncodingParameter.ReadOnly = true;
55
56      Operators.Add(new HammingSimilarityCalculator());
57      Operators.Add(new QualitySimilarityCalculator());
58      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
59
60      Parameterize();
61      RegisterEventHandlers();
62    }
63
64    public override void Analyze(BinaryVector[] vectors, double[] qualities, ResultCollection results, IRandom random) {
65      base.Analyze(vectors, qualities, results, random);
66      var best = GetBestSolution(vectors, qualities);
67
68      results.AddOrUpdateResult("Best Solution", (IItem)best.Item1.Clone());
69    }
70
71    protected override void OnEncodingChanged() {
72      base.OnEncodingChanged();
73      Parameterize();
74    }
75
76    private void Parameterize() {
77      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
78        similarityCalculator.SolutionVariableName = Encoding.Name;
79        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
80      }
81    }
82
83    private void RegisterEventHandlers() {
84      Encoding.LengthParameter.Value.ValueChanged += LengthParameter_ValueChanged;
85    }
86
87    protected virtual void LengthParameter_ValueChanged(object sender, EventArgs e) { }
88  }
89}
Note: See TracBrowser for help on using the repository browser.