Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Problems.TestFunctions/3.3/Analyzers/BestSingleObjectiveTestFunctionSolutionAnalyzer.cs @ 9283

Last change on this file since 9283 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 7.4 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.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
89    /// <summary>
90    /// This method can simply be removed when the plugin version is > 3.3
91    /// </summary>
92    [StorableHook(HookType.AfterDeserialization)]
93    private void AfterDeserialization() {
94      // BackwardsCompatibility3.3
95      // Bounds are introduced in 3.3.0.3894
96      if (!Parameters.ContainsKey("Bounds"))
97        Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The bounds of the function."));
98    }
99
100    public override IDeepCloneable Clone(Cloner cloner) {
101      return new BestSingleObjectiveTestFunctionSolutionAnalyzer(this, cloner);
102    }
103
104    public override IOperation Apply() {
105      ItemArray<RealVector> realVectors = RealVectorParameter.ActualValue;
106      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
107      bool max = MaximizationParameter.ActualValue.Value;
108      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
109      SingleObjectiveTestFunctionSolution solution = BestSolutionParameter.ActualValue;
110
111      int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
112
113      if (bestKnownQuality == null ||
114          max && qualities[i].Value > bestKnownQuality.Value
115          || !max && qualities[i].Value < bestKnownQuality.Value) {
116        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
117        BestKnownSolutionParameter.ActualValue = (RealVector)realVectors[i].Clone();
118        if (solution != null)
119          solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
120      }
121
122      if (solution == null) {
123        ResultCollection results = ResultsParameter.ActualValue;
124        solution = new SingleObjectiveTestFunctionSolution(realVectors[i], qualities[i], EvaluatorParameter.ActualValue);
125        solution.Population = realVectors;
126        solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
127        solution.Bounds = BoundsParameter.ActualValue;
128        BestSolutionParameter.ActualValue = solution;
129        results.Add(new Result("Best Solution", solution));
130      } else {
131        if (max && qualities[i].Value > solution.BestQuality.Value
132          || !max && qualities[i].Value < solution.BestQuality.Value) {
133          solution.BestRealVector = realVectors[i];
134          solution.BestQuality = qualities[i];
135        }
136        solution.Population = realVectors;
137      }
138
139      return base.Apply();
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.