source: branches/histogram/HeuristicLab.Problems.TestFunctions/3.3/Analyzers/BestSingleObjectiveTestFunctionSolutionAnalyzer.cs @ 6055

Last change on this file since 6055 was 6055, checked in by abeham, 8 years ago

#1465

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