Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVectorProblem.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.2 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.RealVectorEncoding {
35  [StorableType("7860a955-af16-459a-9cd8-51667e06d38e")]
36  public abstract class RealVectorProblem : SingleObjectiveProblem<RealVectorEncoding, RealVector> {
37    [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
38    [Storable] protected ReferenceParameter<DoubleMatrix> BoundsRefParameter { get; private set; }
39
40    public int Dimension {
41      get { return DimensionRefParameter.Value.Value; }
42      set { DimensionRefParameter.Value.Value = value; }
43    }
44
45    public DoubleMatrix Bounds {
46      get { return BoundsRefParameter.Value; }
47      set { BoundsRefParameter.Value = value; }
48    }
49
50    [StorableConstructor]
51    protected RealVectorProblem(StorableConstructorFlag _) : base(_) { }
52    [StorableHook(HookType.AfterDeserialization)]
53    private void AfterDeserialization() {
54      RegisterEventHandlers();
55    }
56
57    protected RealVectorProblem(RealVectorProblem original, Cloner cloner)
58      : base(original, cloner) {
59      DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
60      BoundsRefParameter = cloner.Clone(original.BoundsRefParameter);
61      RegisterEventHandlers();
62    }
63
64    protected RealVectorProblem() : this(new RealVectorEncoding() { Length = 10 }) { }
65    protected RealVectorProblem(RealVectorEncoding encoding) : base(encoding) {
66      EncodingParameter.ReadOnly = true;
67      Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the real vector problem.", Encoding.LengthParameter));
68      Parameters.Add(BoundsRefParameter = new ReferenceParameter<DoubleMatrix>("Bounds", "The bounding box of the values.", Encoding.BoundsParameter));
69
70      Operators.Add(new HammingSimilarityCalculator());
71      Operators.Add(new QualitySimilarityCalculator());
72      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
73
74      Parameterize();
75      RegisterEventHandlers();
76    }
77
78    public override void Analyze(RealVector[] vectors, double[] qualities, ResultCollection results, IRandom random) {
79      base.Analyze(vectors, qualities, results, random);
80      var best = GetBestSolution(vectors, qualities);
81
82      results.AddOrUpdateResult("Best Solution", (IItem)best.Item1.Clone());
83    }
84
85    protected override void OnEncodingChanged() {
86      base.OnEncodingChanged();
87      Parameterize();
88    }
89
90    private void Parameterize() {
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      DoubleMatrixParameterChangeHandler.Create(BoundsRefParameter, BoundsOnChanged);
100    }
101
102    protected virtual void DimensionOnChanged() { }
103
104    protected virtual void BoundsOnChanged() { }
105  }
106}
Note: See TracBrowser for help on using the repository browser.