Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521 WIP refactoring:

  1. Introduce nicer type IResultDefinition for API users to avoid complex IParameter interface (hide ActualValue)
  2. Change result parameter to contexts (need quality and solution): only implemented for BinaryVectorProblem
File size: 3.8 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] protected IResultParameter<ISingleObjectiveSolutionContext<BinaryVector>> BestResultParameter { get; private set; }
36    public IResultDefinition<ISingleObjectiveSolutionContext<BinaryVector>> BestResult { get { return BestResultParameter; } }
37
38    public int Length {
39      get { return Encoding.Length; }
40      set { Encoding.Length = value; }
41    }
42
43    [StorableConstructor]
44    protected BinaryVectorProblem(StorableConstructorFlag _) : base(_) { }
45    [StorableHook(HookType.AfterDeserialization)]
46    private void AfterDeserialization() {
47      RegisterEventHandlers();
48    }
49
50    protected BinaryVectorProblem(BinaryVectorProblem original, Cloner cloner)
51      : base(original, cloner) {
52      BestResultParameter = cloner.Clone(original.BestResultParameter);
53      RegisterEventHandlers();
54    }
55
56    protected BinaryVectorProblem() : this(new BinaryVectorEncoding() { Length = 10 }) { }
57    protected BinaryVectorProblem(BinaryVectorEncoding encoding) : base(encoding) {
58      EncodingParameter.ReadOnly = true;
59      Parameters.Add(BestResultParameter = new ResultParameter<ISingleObjectiveSolutionContext<BinaryVector>>("Best Solution", "The best solution."));
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(ISingleObjectiveSolutionContext<BinaryVector>[] solutionContexts, ResultCollection results, IRandom random) {
70      var best = GetBest(solutionContexts);
71      var currentBest = BestResultParameter.ActualValue;
72      if (currentBest == null || IsBetter(best.EvaluationResult.Quality, currentBest.EvaluationResult.Quality))
73        BestResultParameter.ActualValue = (ISingleObjectiveSolutionContext<BinaryVector>)best.Clone();
74    }
75
76    protected override void OnEncodingChanged() {
77      base.OnEncodingChanged();
78      Parameterize();
79    }
80
81    private void Parameterize() {
82      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
83        similarityCalculator.SolutionVariableName = Encoding.Name;
84        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
85      }
86    }
87
88    private void RegisterEventHandlers() {
89      Encoding.LengthParameter.Value.ValueChanged += LengthParameter_ValueChanged;
90    }
91
92    protected virtual void LengthParameter_ValueChanged(object sender, EventArgs e) { }
93  }
94}
Note: See TracBrowser for help on using the repository browser.