Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Problems.TestFunctions/3.3/Analyzers/BestSingleObjectiveTestFunctionSolutionAnalyzer.cs @ 15301

Last change on this file since 15301 was 13403, checked in by abeham, 9 years ago

#2521:

  • Adapted single-objective test function problem to new problem infrastructure
  • Added additional interfaces to RealVectorEncoding
  • Fixed IParticleUpdater interface (must implement IStochasticOperator if it contains a Random parameter)
File size: 7.7 KB
RevLine 
[3647]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3647]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;
[4722]23using HeuristicLab.Common;
[4068]24using HeuristicLab.Core;
[3647]25using HeuristicLab.Data;
[4068]26using HeuristicLab.Encodings.RealVectorEncoding;
[3647]27using HeuristicLab.Operators;
[4068]28using HeuristicLab.Optimization;
[3647]29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
[3797]32namespace HeuristicLab.Problems.TestFunctions {
[3647]33  /// <summary>
34  /// An operator for analyzing the best solution for a SingleObjectiveTestFunction problem.
35  /// </summary>
[3661]36  [Item("BestSingleObjectiveTestFunctionSolutionAnalyzer", "An operator for analyzing the best solution for a SingleObjectiveTestFunction problem.")]
[3647]37  [StorableClass]
[11970]38  public class BestSingleObjectiveTestFunctionSolutionAnalyzer : SingleSuccessorOperator, IBestSingleObjectiveTestFunctionSolutionAnalyzer {
[7172]39    public virtual bool EnabledByDefault {
40      get { return true; }
41    }
42
[3787]43    public LookupParameter<BoolValue> MaximizationParameter {
44      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
45    }
[13403]46    public IScopeTreeLookupParameter<RealVector> RealVectorsParameter {
47      get { return (IScopeTreeLookupParameter<RealVector>)Parameters["RealVectors"]; }
[3647]48    }
[13403]49    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
50      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
[3647]51    }
52    public ILookupParameter<SingleObjectiveTestFunctionSolution> BestSolutionParameter {
53      get { return (ILookupParameter<SingleObjectiveTestFunctionSolution>)Parameters["BestSolution"]; }
54    }
[3781]55    public ILookupParameter<RealVector> BestKnownSolutionParameter {
56      get { return (ILookupParameter<RealVector>)Parameters["BestKnownSolution"]; }
57    }
58    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
59      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
60    }
[3647]61    public IValueLookupParameter<ResultCollection> ResultsParameter {
62      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
63    }
[13403]64    public IValueLookupParameter<ISingleObjectiveTestFunction> TestFunctionParameter {
65      get { return (IValueLookupParameter<ISingleObjectiveTestFunction>)Parameters["TestFunction"]; }
[3661]66    }
[13403]67    public IValueLookupParameter<DoubleMatrix> BoundsParameter {
68      get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
[3894]69    }
[3647]70
[4086]71    [StorableConstructor]
[3894]72    protected BestSingleObjectiveTestFunctionSolutionAnalyzer(bool deserializing) : base(deserializing) { }
[4722]73    protected BestSingleObjectiveTestFunctionSolutionAnalyzer(BestSingleObjectiveTestFunctionSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
[3661]74    public BestSingleObjectiveTestFunctionSolutionAnalyzer()
[3647]75      : base() {
[3787]76      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
[13403]77      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RealVectors", "The SingleObjectiveTestFunction solutions from which the best solution should be visualized."));
[3659]78      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the SingleObjectiveTestFunction solutions which should be visualized."));
[3647]79      Parameters.Add(new LookupParameter<SingleObjectiveTestFunctionSolution>("BestSolution", "The best SingleObjectiveTestFunction solution."));
[3781]80      Parameters.Add(new LookupParameter<RealVector>("BestKnownSolution", "The best known solution."));
81      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution."));
[3647]82      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the SingleObjectiveTestFunction solution should be stored."));
[13403]83      Parameters.Add(new ValueLookupParameter<ISingleObjectiveTestFunction>("TestFunction", "The evaluator with which the solution is evaluated."));
84      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The bounds of the function."));
[6051]85
86      MaximizationParameter.Hidden = true;
[13403]87      RealVectorsParameter.Hidden = true;
[6051]88      QualityParameter.Hidden = true;
89      BestSolutionParameter.Hidden = true;
90      BestKnownSolutionParameter.Hidden = true;
91      BestKnownQualityParameter.Hidden = true;
92      ResultsParameter.Hidden = true;
[13403]93      TestFunctionParameter.Hidden = true;
[6051]94      BoundsParameter.Hidden = true;
[3647]95    }
96
[4722]97    public override IDeepCloneable Clone(Cloner cloner) {
98      return new BestSingleObjectiveTestFunctionSolutionAnalyzer(this, cloner);
99    }
100
[3647]101    public override IOperation Apply() {
[13403]102      ItemArray<RealVector> realVectors = RealVectorsParameter.ActualValue;
[3647]103      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
[3787]104      bool max = MaximizationParameter.ActualValue.Value;
105      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
[3894]106      SingleObjectiveTestFunctionSolution solution = BestSolutionParameter.ActualValue;
[3647]107
[5204]108      int i = -1;
109      if (!max) i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
110      else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
[3787]111
112      if (bestKnownQuality == null ||
113          max && qualities[i].Value > bestKnownQuality.Value
114          || !max && qualities[i].Value < bestKnownQuality.Value) {
115        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
116        BestKnownSolutionParameter.ActualValue = (RealVector)realVectors[i].Clone();
[3894]117        if (solution != null)
118          solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
[3787]119      }
120
[3647]121      if (solution == null) {
[3894]122        ResultCollection results = ResultsParameter.ActualValue;
[10794]123        solution = new SingleObjectiveTestFunctionSolution((RealVector)realVectors[i].Clone(),
124                                                           (DoubleValue)qualities[i].Clone(),
[13403]125                                                           TestFunctionParameter.ActualValue);
[10794]126        solution.Population = realVectors[i].Length == 2
127          ? new ItemArray<RealVector>(realVectors.Select(x => x.Clone()).Cast<RealVector>())
128          : null;
[3781]129        solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
[3894]130        solution.Bounds = BoundsParameter.ActualValue;
[3647]131        BestSolutionParameter.ActualValue = solution;
[3787]132        results.Add(new Result("Best Solution", solution));
[3647]133      } else {
[3781]134        if (max && qualities[i].Value > solution.BestQuality.Value
135          || !max && qualities[i].Value < solution.BestQuality.Value) {
[10794]136          solution.BestRealVector = (RealVector)realVectors[i].Clone();
137          solution.BestQuality = (DoubleValue)qualities[i].Clone();
[3661]138        }
[10794]139        solution.Population = realVectors[i].Length == 2
140          ? new ItemArray<RealVector>(realVectors.Select(x => x.Clone()).Cast<RealVector>())
141          : null;
[3647]142      }
143
144      return base.Apply();
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.