Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVectorProblem.cs

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

#2521: worked on refactoring

  • add results to problem base classes
  • fix external evaluation problem
  • Add result descriptions
File size: 5.3 KB
RevLine 
[16949]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
[17695]24using System;
[16949]25using System.Linq;
26using HEAL.Attic;
27using HeuristicLab.Analysis;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
[17544]30using HeuristicLab.Data;
[16949]31using HeuristicLab.Optimization;
32using HeuristicLab.Optimization.Operators;
[17544]33using HeuristicLab.Parameters;
[16949]34
35namespace HeuristicLab.Encodings.RealVectorEncoding {
36  [StorableType("7860a955-af16-459a-9cd8-51667e06d38e")]
37  public abstract class RealVectorProblem : SingleObjectiveProblem<RealVectorEncoding, RealVector> {
[17544]38    [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
39    [Storable] protected ReferenceParameter<DoubleMatrix> BoundsRefParameter { get; private set; }
[17747]40    [Storable] public IResult<ISingleObjectiveSolutionContext<RealVector>> BestSolutionResult { get; private set; }
[17544]41
42    public int Dimension {
43      get { return DimensionRefParameter.Value.Value; }
44      set { DimensionRefParameter.Value.Value = value; }
[16949]45    }
46
[17544]47    public DoubleMatrix Bounds {
48      get { return BoundsRefParameter.Value; }
[17587]49      set { BoundsRefParameter.Value = value; }
[17544]50    }
51
[17747]52    protected ISingleObjectiveSolutionContext<RealVector> BestSolution {
53      get => BestSolutionResult.Value;
54      set => BestSolutionResult.Value = value;
55    }
56
[16949]57    [StorableConstructor]
58    protected RealVectorProblem(StorableConstructorFlag _) : base(_) { }
59    [StorableHook(HookType.AfterDeserialization)]
60    private void AfterDeserialization() {
61      RegisterEventHandlers();
62    }
63
64    protected RealVectorProblem(RealVectorProblem original, Cloner cloner)
65      : base(original, cloner) {
[17544]66      DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
67      BoundsRefParameter = cloner.Clone(original.BoundsRefParameter);
[17747]68      BestSolutionResult = cloner.Clone(original.BestSolutionResult);
[16949]69      RegisterEventHandlers();
70    }
71
72    protected RealVectorProblem() : this(new RealVectorEncoding() { Length = 10 }) { }
73    protected RealVectorProblem(RealVectorEncoding encoding) : base(encoding) {
74      EncodingParameter.ReadOnly = true;
[17695]75      EvaluatorParameter.ReadOnly = true;
[17544]76      Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the real vector problem.", Encoding.LengthParameter));
77      Parameters.Add(BoundsRefParameter = new ReferenceParameter<DoubleMatrix>("Bounds", "The bounding box of the values.", Encoding.BoundsParameter));
[17747]78      Results.Add(BestSolutionResult = new Result<ISingleObjectiveSolutionContext<RealVector>>("Best Solution", "The best solution found so far."));
[16949]79
80      Operators.Add(new HammingSimilarityCalculator());
[17620]81
82      // TODO: These should be added in the SingleObjectiveProblem base class (if they were accessible from there)
[16949]83      Operators.Add(new QualitySimilarityCalculator());
84      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
85
86      Parameterize();
87      RegisterEventHandlers();
88    }
89
[17745]90    public override void Analyze(ISingleObjectiveSolutionContext<RealVector>[] solutionContexts, IRandom random) {
91      base.Analyze(solutionContexts, random);
[17747]92      var best = GetBest(solutionContexts);
93      if (BestSolution == null || IsBetter(best, BestSolution))
94        BestSolution = best.Clone() as SingleObjectiveSolutionContext<RealVector>;
[16949]95    }
96
[17695]97    protected override sealed void OnEvaluatorChanged() {
98      throw new InvalidOperationException("Evaluator may not change!");
99    }
100
101    protected override sealed void OnEncodingChanged() {
102      throw new InvalidOperationException("Encoding may not change!");
103    }
104
[17620]105    protected override void ParameterizeOperators() {
106      base.ParameterizeOperators();
[16949]107      Parameterize();
108    }
109
110    private void Parameterize() {
[17620]111      // TODO: this is done in base class as well (but operators are added at this level of the hierarchy)
[16949]112      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
113        similarityCalculator.SolutionVariableName = Encoding.Name;
114        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
115      }
116    }
117
118    private void RegisterEventHandlers() {
[17587]119      IntValueParameterChangeHandler.Create(DimensionRefParameter, DimensionOnChanged);
120      DoubleMatrixParameterChangeHandler.Create(BoundsRefParameter, BoundsOnChanged);
[16949]121    }
122
[17544]123    protected virtual void DimensionOnChanged() { }
124
125    protected virtual void BoundsOnChanged() { }
[16949]126  }
127}
Note: See TracBrowser for help on using the repository browser.