Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionProblem.cs @ 17946

Last change on this file since 17946 was 17745, checked in by mkommend, 4 years ago

#2971: Added first draft of results implementation and problem adaptation.

File size: 8.0 KB
RevLine 
[3150]1#region License Information
2/* HeuristicLab
[17226]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3150]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
[9980]22using System;
23using System.Collections.Generic;
24using System.Linq;
[17320]25using System.Threading;
[16949]26using HEAL.Attic;
[3150]27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Encodings.RealVectorEncoding;
31using HeuristicLab.Optimization;
32using HeuristicLab.Parameters;
[9980]33using HeuristicLab.Problems.Instances;
[3150]34
[3170]35namespace HeuristicLab.Problems.TestFunctions {
[12504]36  [Item("Test Function (single-objective)", "Test function with real valued inputs and a single objective.")]
[16723]37  [StorableType("F0AB7236-2C9B-49DC-9D4F-A3558FD9E992")]
[12504]38  [Creatable(CreatableAttribute.Categories.Problems, Priority = 90)]
[16949]39  public sealed class SingleObjectiveTestFunctionProblem : RealVectorProblem,
[13403]40    IProblemInstanceConsumer<SOTFData> {
[4419]41
[3150]42    #region Parameter Properties
[3781]43    public OptionalValueParameter<RealVector> BestKnownSolutionParameter {
44      get { return (OptionalValueParameter<RealVector>)Parameters["BestKnownSolution"]; }
45    }
[13403]46    public IValueParameter<ISingleObjectiveTestFunction> TestFunctionParameter {
47      get { return (IValueParameter<ISingleObjectiveTestFunction>)Parameters["TestFunction"]; }
48    }
[3150]49    #endregion
50
51    #region Properties
[13403]52    public ISingleObjectiveTestFunction TestFunction {
53      get { return TestFunctionParameter.Value; }
54      set { TestFunctionParameter.Value = value; }
[3150]55    }
56    #endregion
57
58    [StorableConstructor]
[16723]59    private SingleObjectiveTestFunctionProblem(StorableConstructorFlag _) : base(_) { }
[4722]60    private SingleObjectiveTestFunctionProblem(SingleObjectiveTestFunctionProblem original, Cloner cloner)
61      : base(original, cloner) {
[7351]62      RegisterEventHandlers();
[4722]63    }
[3170]64    public SingleObjectiveTestFunctionProblem()
[13403]65      : base(new RealVectorEncoding("Point")) {
[3781]66      Parameters.Add(new OptionalValueParameter<RealVector>("BestKnownSolution", "The best known solution for this test function instance."));
[13403]67      Parameters.Add(new ValueParameter<ISingleObjectiveTestFunction>("TestFunction", "The function that is to be optimized.", new Ackley()));
[17270]68      Maximization = TestFunction.Maximization;
[3150]69
[13403]70      BestKnownQuality = TestFunction.BestKnownQuality;
[17587]71      Bounds = (DoubleMatrix)TestFunction.Bounds.Clone();
[17625]72      Dimension = Math.Min(Math.Max(2, TestFunction.MinimumProblemSize), TestFunction.MaximumProblemSize);
[17587]73      BestKnownSolutionParameter.Value = TestFunction.GetBestKnownSolution(Dimension);
[17625]74
75
76      Operators.AddRange(new IItem[] {
77        new SingleObjectiveTestFunctionImprovementOperator(),
78        new SingleObjectiveTestFunctionPathRelinker(),
79        new SingleObjectiveTestFunctionSimilarityCalculator(),
80        new EuclideanSimilarityCalculator(),
81        new AdditiveMoveEvaluator() });
82
83      Parameterize();
[7351]84      RegisterEventHandlers();
[3150]85    }
86
87    public override IDeepCloneable Clone(Cloner cloner) {
[4722]88      return new SingleObjectiveTestFunctionProblem(this, cloner);
[3150]89    }
90
[13403]91    [StorableHook(HookType.AfterDeserialization)]
92    private void AfterDeserialization() {
93      RegisterEventHandlers();
94    }
95
96    private void RegisterEventHandlers() {
97      TestFunctionParameter.ValueChanged += TestFunctionParameterOnValueChanged;
98    }
99
[17382]100    public override ISingleObjectiveEvaluationResult Evaluate(RealVector individual, IRandom random, CancellationToken cancellationToken) {
101      var quality = TestFunction.Evaluate(individual);
102      return new SingleObjectiveEvaluationResult(quality);
[13403]103    }
104
[17745]105    //TODO: change to new analyze interface
106    public override void Analyze(ISingleObjectiveSolutionContext<RealVector>[] solutionContexts, IRandom random) {
107      base.Analyze(solutionContexts, random);
[16949]108
[17745]109      //TODO: reimplement code below using results directly
[16950]110
[17745]111      //var best = GetBestSolution(realVectors, qualities);
112
113      //DoubleValue bestKnownQuality = BestKnownQualityParameter.Value;
114      //RealVector bestKnownSolution = null;
115      //var bestKnownUpdate = bestKnownQuality == null || IsBetter(best.Item2, bestKnownQuality.Value);
116      //if (bestKnownUpdate) {
117      //  if (bestKnownQuality != null) bestKnownQuality.Value = best.Item2;
118      //  else BestKnownQualityParameter.Value = bestKnownQuality = new DoubleValue(best.Item2);
119      //  BestKnownSolutionParameter.Value = bestKnownSolution = (RealVector)best.Item1.Clone();
120      //}
121
122      //SingleObjectiveTestFunctionSolution solution = null;
123      //if (results.TryGetValue("Best Solution", out var res) && res.Value != null) {
124      //  solution = (SingleObjectiveTestFunctionSolution)res.Value;
125      //  if (IsBetter(best.Item2, solution.BestQuality.Value)) {
126      //    solution.BestRealVector = (RealVector)best.Item1.Clone();
127      //    solution.BestQuality = new DoubleValue(best.Item2);
128      //  }
129      //} else {
130      //  solution = new SingleObjectiveTestFunctionSolution((RealVector)best.Item1.Clone(),
131      //                                                     new DoubleValue(best.Item2),
132      //                                                     TestFunctionParameter.Value) {
133      //    BestKnownRealVector = bestKnownSolution,
134      //    Bounds = BoundsRefParameter.Value
135      //  };
136      //  results.AddOrUpdateResult("Best Solution", solution);
137      //}
138      //if (best.Item1.Length == 2) solution.Population = new ItemArray<RealVector>(realVectors.Select(x => (RealVector)x.Clone()));
139      //if (bestKnownUpdate) solution.BestKnownRealVector = bestKnownSolution;
[16949]140    }
141
[3150]142    #region Events
[17625]143    protected override void ParameterizeOperators() {
144      base.ParameterizeOperators();
[13403]145      Parameterize();
[3150]146    }
[17544]147    protected override void DimensionOnChanged() {
148      base.DimensionOnChanged();
149      if (Dimension < TestFunction.MinimumProblemSize || Dimension > TestFunction.MaximumProblemSize)
150        Dimension = Math.Min(TestFunction.MaximumProblemSize, Math.Max(TestFunction.MinimumProblemSize, Dimension));
151    }
152    protected override void BoundsOnChanged() {
153      base.BoundsOnChanged();
154      Parameterize();
155    }
[13403]156    private void TestFunctionParameterOnValueChanged(object sender, EventArgs eventArgs) {
[17544]157      var problemSizeChange = Dimension < TestFunction.MinimumProblemSize
158                              || Dimension > TestFunction.MaximumProblemSize;
[13403]159      if (problemSizeChange) {
[17544]160        Dimension = Math.Max(TestFunction.MinimumProblemSize, Math.Min(Dimension, TestFunction.MaximumProblemSize));
[3187]161      }
[13403]162      BestKnownQuality = TestFunction.BestKnownQuality;
163      Bounds = (DoubleMatrix)TestFunction.Bounds.Clone();
[17544]164      var bestSolution = TestFunction.GetBestKnownSolution(Dimension);
[13403]165      BestKnownSolutionParameter.Value = bestSolution;
[17270]166      Maximization = TestFunction.Maximization;
[13403]167
168      OnReset();
[3182]169    }
[3150]170    #endregion
171
172    #region Helpers
[13403]173    private void Parameterize() {
174      var operators = new List<IItem>();
[17571]175
[13403]176      foreach (var op in Operators.OfType<ITestFunctionSolutionSimilarityCalculator>()) {
177        operators.Add(op);
178        op.Bounds = Bounds;
[8334]179      }
[13403]180
181      if (operators.Count > 0) Encoding.ConfigureOperators(operators);
[3150]182    }
183    #endregion
[9980]184
185    public void Load(SOTFData data) {
186      Name = data.Name;
187      Description = data.Description;
[13403]188      TestFunction = data.TestFunction;
[9980]189    }
[3150]190  }
191}
Note: See TracBrowser for help on using the repository browser.