Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521:

  • Fixed orienteering problem
  • Corrected ParameterizeOperators in all encoding-specific problem base classes
  • Added new interfaces and wiring code to IntegerVectorEncoding
File size: 4.4 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 ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
36    [Storable] public IResult<ISingleObjectiveSolutionContext<BinaryVector>> BestSolutionResult { get; private set; }
37
38    private ISingleObjectiveSolutionContext<BinaryVector> BestSolution {
39      get => BestSolutionResult.Value;
40      set => BestSolutionResult.Value = value;
41    }
42
43    public int Dimension {
44      get { return DimensionRefParameter.Value.Value; }
45      protected set { DimensionRefParameter.Value.Value = value; }
46    }
47
48    [StorableConstructor]
49    protected BinaryVectorProblem(StorableConstructorFlag _) : base(_) { }
50    [StorableHook(HookType.AfterDeserialization)]
51    private void AfterDeserialization() {
52      RegisterEventHandlers();
53    }
54
55    protected BinaryVectorProblem(BinaryVectorProblem original, Cloner cloner)
56      : base(original, cloner) {
57      DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
58      BestSolutionResult = cloner.Clone(original.BestSolutionResult);
59      RegisterEventHandlers();
60    }
61
62    protected BinaryVectorProblem() : this(new BinaryVectorEncoding() { Length = 10 }) { }
63    protected BinaryVectorProblem(BinaryVectorEncoding encoding) : base(encoding) {
64      EncodingParameter.ReadOnly = true;
65      Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the binary vector problem.", Encoding.LengthParameter));
66      Results.Add(BestSolutionResult = new Result<ISingleObjectiveSolutionContext<BinaryVector>>("Best Solution"));
67
68      Operators.Add(new HammingSimilarityCalculator());
69      // TODO: These should be added in the SingleObjectiveProblem base class (if they were accessible from there)
70      Operators.Add(new QualitySimilarityCalculator());
71      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
72
73      Parameterize();
74      RegisterEventHandlers();
75    }
76
77    public override void Analyze(ISingleObjectiveSolutionContext<BinaryVector>[] solutionContexts, ResultCollection results, IRandom random) {
78      base.Analyze(solutionContexts, results, random);
79      var best = GetBest(solutionContexts);
80      if (BestSolution == null || IsBetter(best, BestSolution))
81        BestSolution = best.Clone() as SingleObjectiveSolutionContext<BinaryVector>;
82    }
83
84    protected override void ParameterizeOperators() {
85      base.ParameterizeOperators();
86      Parameterize();
87    }
88
89    private void Parameterize() {
90      // TODO: this is done in base class as well (but operators are added at this level of the hierarchy)
91      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
92        similarityCalculator.SolutionVariableName = Encoding.Name;
93        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
94      }
95    }
96
97    private void RegisterEventHandlers() {
98      IntValueParameterChangeHandler.Create(DimensionRefParameter, DimensionOnChanged);
99    }
100
101    protected virtual void DimensionOnChanged() { }
102  }
103}
Note: See TracBrowser for help on using the repository browser.