Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: Worked on ResultParameter for Problem and Algorithms

  • Add ResultParameter to TSP, BinaryVectorProblem, and HillClimber
  • Refactor ResultParameter to allow presetting the ResultCollection instead of having to discover it (e.g. for use in BasicAlgorithms)
  • Unify Results property among EngineAlgorithm and BasicAlgorithm
    • There is now only a single instance which is storable
File size: 3.5 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    [Storable] public IResultParameter<BinaryVector> BestSolutionParameter { get; private set; }
36
37    public int Length {
38      get { return Encoding.Length; }
39      set { Encoding.Length = value; }
40    }
41
42    [StorableConstructor]
43    protected BinaryVectorProblem(StorableConstructorFlag _) : base(_) { }
44    [StorableHook(HookType.AfterDeserialization)]
45    private void AfterDeserialization() {
46      RegisterEventHandlers();
47    }
48
49    protected BinaryVectorProblem(BinaryVectorProblem original, Cloner cloner)
50      : base(original, cloner) {
51      BestSolutionParameter = cloner.Clone(original.BestSolutionParameter);
52      RegisterEventHandlers();
53    }
54
55    protected BinaryVectorProblem() : this(new BinaryVectorEncoding() { Length = 10 }) { }
56    protected BinaryVectorProblem(BinaryVectorEncoding encoding) : base(encoding) {
57      EncodingParameter.ReadOnly = true;
58      BestSolutionParameter = new ResultParameter<BinaryVector>("Best Solution", "The best solution.");
59      Parameters.Add(BestSolutionParameter);
60
61      Operators.Add(new HammingSimilarityCalculator());
62      Operators.Add(new QualitySimilarityCalculator());
63      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
64
65      Parameterize();
66      RegisterEventHandlers();
67    }
68
69    public override void Analyze(BinaryVector[] vectors, double[] qualities, ResultCollection results, IRandom random) {
70      base.Analyze(vectors, qualities, results, random);
71      var best = GetBestSolution(vectors, qualities);
72      BestSolutionParameter.ActualValue = (BinaryVector)best.Item1.Clone();
73    }
74
75    protected override void OnEncodingChanged() {
76      base.OnEncodingChanged();
77      Parameterize();
78    }
79
80    private void Parameterize() {
81      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
82        similarityCalculator.SolutionVariableName = Encoding.Name;
83        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
84      }
85    }
86
87    private void RegisterEventHandlers() {
88      Encoding.LengthParameter.Value.ValueChanged += LengthParameter_ValueChanged;
89    }
90
91    protected virtual void LengthParameter_ValueChanged(object sender, EventArgs e) { }
92  }
93}
Note: See TracBrowser for help on using the repository browser.