Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/IntegerVectorProblem.cs @ 17587

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

#2521: refactoring in progress

File size: 4.6 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2019 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
22#endregion
23
24using System.Linq;
25using HEAL.Attic;
26using HeuristicLab.Analysis;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Optimization;
31using HeuristicLab.Optimization.Operators;
32using HeuristicLab.Parameters;
33
34namespace HeuristicLab.Encodings.IntegerVectorEncoding {
35  [StorableType("c6081457-a3de-45ce-9f47-e0eb1c851bd2")]
36  public abstract class IntegerVectorProblem : SingleObjectiveProblem<IntegerVectorEncoding, IntegerVector> {
37    [Storable] protected IResultParameter<IntegerVector> BestResultParameter { get; private set; }
38    public IResultDefinition<IntegerVector> BestResult { get => BestResultParameter; }
39    [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
40    [Storable] protected ReferenceParameter<IntMatrix> BoundsRefParameter { get; private set; }
41
42    public int Dimension {
43      get { return DimensionRefParameter.Value.Value; }
44      protected set { DimensionRefParameter.Value.Value = value; }
45    }
46
47    public IntMatrix Bounds {
48      get { return BoundsRefParameter.Value; }
49      protected set { BoundsRefParameter.Value = value; }
50    }
51
52    [StorableConstructor]
53    protected IntegerVectorProblem(StorableConstructorFlag _) : base(_) { }
54    [StorableHook(HookType.AfterDeserialization)]
55    private void AfterDeserialization() {
56      RegisterEventHandlers();
57    }
58
59    protected IntegerVectorProblem(IntegerVectorProblem original, Cloner cloner)
60      : base(original, cloner) {
61      BestResultParameter = cloner.Clone(original.BestResultParameter);
62      DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
63      BoundsRefParameter = cloner.Clone(original.BoundsRefParameter);
64      RegisterEventHandlers();
65    }
66
67    protected IntegerVectorProblem() : this(new IntegerVectorEncoding() { Length = 10 }) { }
68    protected IntegerVectorProblem(IntegerVectorEncoding encoding) : base(encoding) {
69      EncodingParameter.ReadOnly = true;
70      Parameters.Add(BestResultParameter = new ResultParameter<IntegerVector>("Best Solution", "The best solution."));
71      Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the integer vector problem.", Encoding.LengthParameter));
72      Parameters.Add(BoundsRefParameter = new ReferenceParameter<IntMatrix>("Bounds", "The bounding box and step sizes of the values.", Encoding.BoundsParameter));
73
74      Operators.Add(new HammingSimilarityCalculator());
75      Operators.Add(new QualitySimilarityCalculator());
76      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
77
78      Parameterize();
79      RegisterEventHandlers();
80    }
81
82    public override void Analyze(IntegerVector[] vectors, double[] qualities, ResultCollection results, IRandom random) {
83      base.Analyze(vectors, qualities, results, random);
84      var best = GetBestSolution(vectors, qualities);
85
86      results.AddOrUpdateResult("Best Solution", (IItem)best.Item1.Clone());
87    }
88
89    protected override void OnEncodingChanged() {
90      base.OnEncodingChanged();
91      Parameterize();
92    }
93
94    private void Parameterize() {
95      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
96        similarityCalculator.SolutionVariableName = Encoding.Name;
97        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
98      }
99    }
100
101    private void RegisterEventHandlers() {
102      IntValueParameterChangeHandler.Create(DimensionRefParameter, DimensionOnChanged);
103      IntMatrixParameterChangeHandler.Create(BoundsRefParameter, BoundsOnChanged);
104    }
105
106    protected virtual void DimensionOnChanged() { }
107
108    protected virtual void BoundsOnChanged() { }
109  }
110}
Note: See TracBrowser for help on using the repository browser.