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

Last change on this file since 17577 was 17577, checked in by abeham, 2 years ago

#2521: remove scope-based contexts, add helper methods to ScopeUtil, adapt operators

File size: 4.3 KB
Line 
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
22using System.Linq;
23using HEAL.Attic;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Optimization.Operators;
30using HeuristicLab.Parameters;
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 => BestResultParameter;
37    [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
38
39    public int Dimension {
40      get { return DimensionRefParameter.Value.Value; }
41      protected set { DimensionRefParameter.Value.Value = value; }
42    }
43
44    [StorableConstructor]
45    protected BinaryVectorProblem(StorableConstructorFlag _) : base(_) { }
46    [StorableHook(HookType.AfterDeserialization)]
47    private void AfterDeserialization() {
48      RegisterEventHandlers();
49    }
50
51    protected BinaryVectorProblem(BinaryVectorProblem original, Cloner cloner)
52      : base(original, cloner) {
53      BestResultParameter = cloner.Clone(original.BestResultParameter);
54      DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
55      RegisterEventHandlers();
56    }
57
58    protected BinaryVectorProblem() : this(new BinaryVectorEncoding() { Length = 10 }) { }
59    protected BinaryVectorProblem(BinaryVectorEncoding encoding) : base(encoding) {
60      EncodingParameter.ReadOnly = true;
61      Parameters.Add(BestResultParameter = new ResultParameter<ISingleObjectiveSolutionContext<BinaryVector>>("Best Solution", "The best solution."));
62      Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the binary vector problem.", Encoding.LengthParameter));
63
64      Operators.Add(new HammingSimilarityCalculator());
65      Operators.Add(new QualitySimilarityCalculator());
66      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
67
68      Parameterize();
69      RegisterEventHandlers();
70    }
71
72    public override void Analyze(ISingleObjectiveSolutionContext<BinaryVector>[] solutionContexts, ResultCollection results, IRandom random) {
73      base.Analyze(solutionContexts, results, random);
74      var best = GetBest(solutionContexts);
75      var currentBest = BestResultParameter.ActualValue;
76      if (currentBest == null || IsBetter(best.EvaluationResult.Quality, currentBest.EvaluationResult.Quality))
77        BestResultParameter.ActualValue = new SingleObjectiveSolutionContext<BinaryVector>(
78          (BinaryVector)best.EncodedSolution.Clone(), (ISingleObjectiveEvaluationResult)best.EvaluationResult.Clone());
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      IntValueParameterChangeHandler.Create(DimensionRefParameter, DimensionOnChanged);
90    }
91
92    protected virtual void DimensionOnChanged() { }
93  }
94}
Note: See TracBrowser for help on using the repository browser.