Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Problems.TestFunctions/3.3/Analyzers/BestSingleObjectiveTestFunctionSolutionAnalyzer.cs @ 15018

Last change on this file since 15018 was 15018, checked in by gkronber, 7 years ago

#2520 introduced StorableConstructorFlag type for StorableConstructors

File size: 8.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.RealVectorEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence;
31
32namespace HeuristicLab.Problems.TestFunctions {
33  /// <summary>
34  /// An operator for analyzing the best solution for a SingleObjectiveTestFunction problem.
35  /// </summary>
36  [Item("BestSingleObjectiveTestFunctionSolutionAnalyzer", "An operator for analyzing the best solution for a SingleObjectiveTestFunction problem.")]
37  [StorableType("f5d81231-809d-474c-812d-f897ab11e16f")]
38  public class BestSingleObjectiveTestFunctionSolutionAnalyzer : SingleSuccessorOperator, IBestSingleObjectiveTestFunctionSolutionAnalyzer {
39    public virtual bool EnabledByDefault {
40      get { return true; }
41    }
42
43    public LookupParameter<BoolValue> MaximizationParameter {
44      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
45    }
46    public ScopeTreeLookupParameter<RealVector> RealVectorParameter {
47      get { return (ScopeTreeLookupParameter<RealVector>)Parameters["RealVector"]; }
48    }
49    ILookupParameter IBestSingleObjectiveTestFunctionSolutionAnalyzer.RealVectorParameter {
50      get { return RealVectorParameter; }
51    }
52    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
53      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
54    }
55    ILookupParameter IBestSingleObjectiveTestFunctionSolutionAnalyzer.QualityParameter {
56      get { return QualityParameter; }
57    }
58    public ILookupParameter<SingleObjectiveTestFunctionSolution> BestSolutionParameter {
59      get { return (ILookupParameter<SingleObjectiveTestFunctionSolution>)Parameters["BestSolution"]; }
60    }
61    public ILookupParameter<RealVector> BestKnownSolutionParameter {
62      get { return (ILookupParameter<RealVector>)Parameters["BestKnownSolution"]; }
63    }
64    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
65      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
66    }
67    public IValueLookupParameter<ResultCollection> ResultsParameter {
68      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
69    }
70    public IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator> EvaluatorParameter {
71      get { return (IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>)Parameters["Evaluator"]; }
72    }
73    public ILookupParameter<DoubleMatrix> BoundsParameter {
74      get { return (ILookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
75    }
76
77    [StorableConstructor]
78    protected BestSingleObjectiveTestFunctionSolutionAnalyzer(StorableConstructorFlag deserializing) : base(deserializing) { }
79    protected BestSingleObjectiveTestFunctionSolutionAnalyzer(BestSingleObjectiveTestFunctionSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
80    public BestSingleObjectiveTestFunctionSolutionAnalyzer()
81      : base() {
82      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
83      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RealVector", "The SingleObjectiveTestFunction solutions from which the best solution should be visualized."));
84      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the SingleObjectiveTestFunction solutions which should be visualized."));
85      Parameters.Add(new LookupParameter<SingleObjectiveTestFunctionSolution>("BestSolution", "The best SingleObjectiveTestFunction solution."));
86      Parameters.Add(new LookupParameter<RealVector>("BestKnownSolution", "The best known solution."));
87      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution."));
88      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the SingleObjectiveTestFunction solution should be stored."));
89      Parameters.Add(new ValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>("Evaluator", "The evaluator with which the solution is evaluated."));
90      Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The bounds of the function."));
91
92      MaximizationParameter.Hidden = true;
93      RealVectorParameter.Hidden = true;
94      QualityParameter.Hidden = true;
95      BestSolutionParameter.Hidden = true;
96      BestKnownSolutionParameter.Hidden = true;
97      BestKnownQualityParameter.Hidden = true;
98      ResultsParameter.Hidden = true;
99      EvaluatorParameter.Hidden = true;
100      BoundsParameter.Hidden = true;
101    }
102
103    /// <summary>
104    /// This method can simply be removed when the plugin version is > 3.3
105    /// </summary>
106    [StorableHook(HookType.AfterDeserialization)]
107    private void AfterDeserialization() {
108      // BackwardsCompatibility3.3
109      // Bounds are introduced in 3.3.0.3894
110      if (!Parameters.ContainsKey("Bounds"))
111        Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The bounds of the function."));
112    }
113
114    public override IDeepCloneable Clone(Cloner cloner) {
115      return new BestSingleObjectiveTestFunctionSolutionAnalyzer(this, cloner);
116    }
117
118    public override IOperation Apply() {
119      ItemArray<RealVector> realVectors = RealVectorParameter.ActualValue;
120      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
121      bool max = MaximizationParameter.ActualValue.Value;
122      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
123      SingleObjectiveTestFunctionSolution solution = BestSolutionParameter.ActualValue;
124
125      int i = -1;
126      if (!max) i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
127      else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
128
129      if (bestKnownQuality == null ||
130          max && qualities[i].Value > bestKnownQuality.Value
131          || !max && qualities[i].Value < bestKnownQuality.Value) {
132        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
133        BestKnownSolutionParameter.ActualValue = (RealVector)realVectors[i].Clone();
134        if (solution != null)
135          solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
136      }
137
138      if (solution == null) {
139        ResultCollection results = ResultsParameter.ActualValue;
140        solution = new SingleObjectiveTestFunctionSolution((RealVector)realVectors[i].Clone(),
141                                                           (DoubleValue)qualities[i].Clone(),
142                                                           EvaluatorParameter.ActualValue);
143        solution.Population = realVectors[i].Length == 2
144          ? new ItemArray<RealVector>(realVectors.Select(x => x.Clone()).Cast<RealVector>())
145          : null;
146        solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
147        solution.Bounds = BoundsParameter.ActualValue;
148        BestSolutionParameter.ActualValue = solution;
149        results.Add(new Result("Best Solution", solution));
150      } else {
151        if (max && qualities[i].Value > solution.BestQuality.Value
152          || !max && qualities[i].Value < solution.BestQuality.Value) {
153          solution.BestRealVector = (RealVector)realVectors[i].Clone();
154          solution.BestQuality = (DoubleValue)qualities[i].Clone();
155        }
156        solution.Population = realVectors[i].Length == 2
157          ? new ItemArray<RealVector>(realVectors.Select(x => x.Clone()).Cast<RealVector>())
158          : null;
159      }
160
161      return base.Apply();
162    }
163  }
164}
Note: See TracBrowser for help on using the repository browser.