Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1090

  • Fixed initialization of operators and made operator list storable
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      // BackwardsCompatibility3.3
93      // Bounds are introduced in 3.3.0.3894
94      if (!Parameters.ContainsKey("Bounds"))
95        Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The bounds of the function."));
96    }
97
98    public override IOperation Apply() {
99      ItemArray<RealVector> realVectors = RealVectorParameter.ActualValue;
100      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
101      bool max = MaximizationParameter.ActualValue.Value;
102      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
103      SingleObjectiveTestFunctionSolution solution = BestSolutionParameter.ActualValue;
104
105      int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
106
107      if (bestKnownQuality == null ||
108          max && qualities[i].Value > bestKnownQuality.Value
109          || !max && qualities[i].Value < bestKnownQuality.Value) {
110        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
111        BestKnownSolutionParameter.ActualValue = (RealVector)realVectors[i].Clone();
112        if (solution != null)
113          solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
114      }
115
116      if (solution == null) {
117        ResultCollection results = ResultsParameter.ActualValue;
118        solution = new SingleObjectiveTestFunctionSolution(realVectors[i], qualities[i], EvaluatorParameter.ActualValue);
119        solution.Population = realVectors;
120        solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
121        solution.Bounds = BoundsParameter.ActualValue;
122        BestSolutionParameter.ActualValue = solution;
123        results.Add(new Result("Best Solution", solution));
124      } else {
125        if (max && qualities[i].Value > solution.BestQuality.Value
126          || !max && qualities[i].Value < solution.BestQuality.Value) {
127          solution.BestRealVector = realVectors[i];
128          solution.BestQuality = qualities[i];
129        }
130        solution.Population = realVectors;
131      }
132
133      return base.Apply();
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.