Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TestFunctions/3.3/Analyzers/BestSingleObjectiveTestFunctionSolutionAnalyzer.cs @ 4086

Last change on this file since 4086 was 4086, checked in by abeham, 14 years ago

#1040

  • Added a very very basic analyzer for listing the qualities on the pareto front
File size: 7.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.RealVectorEncoding;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.TestFunctions {
32  /// <summary>
33  /// An operator for analyzing the best solution for a SingleObjectiveTestFunction problem.
34  /// </summary>
35  [Item("BestSingleObjectiveTestFunctionSolutionAnalyzer", "An operator for analyzing the best solution for a SingleObjectiveTestFunction problem.")]
36  [StorableClass]
37  class BestSingleObjectiveTestFunctionSolutionAnalyzer : SingleSuccessorOperator, IBestSingleObjectiveTestFunctionSolutionAnalyzer, IAnalyzer {
38    public LookupParameter<BoolValue> MaximizationParameter {
39      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
40    }
41    public ScopeTreeLookupParameter<RealVector> RealVectorParameter {
42      get { return (ScopeTreeLookupParameter<RealVector>)Parameters["RealVector"]; }
43    }
44    ILookupParameter IBestSingleObjectiveTestFunctionSolutionAnalyzer.RealVectorParameter {
45      get { return RealVectorParameter; }
46    }
47    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
48      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
49    }
50    ILookupParameter IBestSingleObjectiveTestFunctionSolutionAnalyzer.QualityParameter {
51      get { return QualityParameter; }
52    }
53    public ILookupParameter<SingleObjectiveTestFunctionSolution> BestSolutionParameter {
54      get { return (ILookupParameter<SingleObjectiveTestFunctionSolution>)Parameters["BestSolution"]; }
55    }
56    public ILookupParameter<RealVector> BestKnownSolutionParameter {
57      get { return (ILookupParameter<RealVector>)Parameters["BestKnownSolution"]; }
58    }
59    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
60      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
61    }
62    public IValueLookupParameter<ResultCollection> ResultsParameter {
63      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
64    }
65    public IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator> EvaluatorParameter {
66      get { return (IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>)Parameters["Evaluator"]; }
67    }
68    public ILookupParameter<DoubleMatrix> BoundsParameter {
69      get { return (ILookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
70    }
71
72    [StorableConstructor]
73    protected BestSingleObjectiveTestFunctionSolutionAnalyzer(bool deserializing) : base(deserializing) { }
74    public BestSingleObjectiveTestFunctionSolutionAnalyzer()
75      : base() {
76      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
77      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RealVector", "The SingleObjectiveTestFunction solutions from which the best solution should be visualized."));
78      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the SingleObjectiveTestFunction solutions which should be visualized."));
79      Parameters.Add(new LookupParameter<SingleObjectiveTestFunctionSolution>("BestSolution", "The best SingleObjectiveTestFunction solution."));
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."));
82      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the SingleObjectiveTestFunction solution should be stored."));
83      Parameters.Add(new ValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>("Evaluator", "The evaluator with which the solution is evaluated."));
84      Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The bounds of the function."));
85    }
86
87    /// <summary>
88    /// This method can simply be removed when the plugin version is > 3.3
89    /// </summary>
90    [StorableHook(HookType.AfterDeserialization)]
91    private void CompatibilityMethod() {
92      // Bounds are introduced in 3.3.0.3894
93      if (!Parameters.ContainsKey("Bounds"))
94        Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The bounds of the function."));
95    }
96
97    public override IOperation Apply() {
98      ItemArray<RealVector> realVectors = RealVectorParameter.ActualValue;
99      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
100      bool max = MaximizationParameter.ActualValue.Value;
101      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
102      SingleObjectiveTestFunctionSolution solution = BestSolutionParameter.ActualValue;
103
104      int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
105
106      if (bestKnownQuality == null ||
107          max && qualities[i].Value > bestKnownQuality.Value
108          || !max && qualities[i].Value < bestKnownQuality.Value) {
109        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
110        BestKnownSolutionParameter.ActualValue = (RealVector)realVectors[i].Clone();
111        if (solution != null)
112          solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
113      }
114
115      if (solution == null) {
116        ResultCollection results = ResultsParameter.ActualValue;
117        solution = new SingleObjectiveTestFunctionSolution(realVectors[i], qualities[i], EvaluatorParameter.ActualValue);
118        solution.Population = realVectors;
119        solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
120        solution.Bounds = BoundsParameter.ActualValue;
121        BestSolutionParameter.ActualValue = solution;
122        results.Add(new Result("Best Solution", solution));
123      } else {
124        if (max && qualities[i].Value > solution.BestQuality.Value
125          || !max && qualities[i].Value < solution.BestQuality.Value) {
126          solution.BestRealVector = realVectors[i];
127          solution.BestQuality = qualities[i];
128        }
129        solution.Population = realVectors;
130      }
131
132      return base.Apply();
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.