Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5698 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 7.6 KB
RevLine 
[3647]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3647]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;
[4722]23using HeuristicLab.Common;
[4068]24using HeuristicLab.Core;
[3647]25using HeuristicLab.Data;
[4068]26using HeuristicLab.Encodings.RealVectorEncoding;
[3647]27using HeuristicLab.Operators;
[4068]28using HeuristicLab.Optimization;
[3647]29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
[3797]32namespace HeuristicLab.Problems.TestFunctions {
[3647]33  /// <summary>
34  /// An operator for analyzing the best solution for a SingleObjectiveTestFunction problem.
35  /// </summary>
[3661]36  [Item("BestSingleObjectiveTestFunctionSolutionAnalyzer", "An operator for analyzing the best solution for a SingleObjectiveTestFunction problem.")]
[3647]37  [StorableClass]
[3661]38  class BestSingleObjectiveTestFunctionSolutionAnalyzer : SingleSuccessorOperator, IBestSingleObjectiveTestFunctionSolutionAnalyzer, IAnalyzer {
[3787]39    public LookupParameter<BoolValue> MaximizationParameter {
40      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
41    }
[3661]42    public ScopeTreeLookupParameter<RealVector> RealVectorParameter {
43      get { return (ScopeTreeLookupParameter<RealVector>)Parameters["RealVector"]; }
[3647]44    }
45    ILookupParameter IBestSingleObjectiveTestFunctionSolutionAnalyzer.RealVectorParameter {
46      get { return RealVectorParameter; }
47    }
[3661]48    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
49      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
[3647]50    }
51    ILookupParameter IBestSingleObjectiveTestFunctionSolutionAnalyzer.QualityParameter {
52      get { return QualityParameter; }
53    }
54    public ILookupParameter<SingleObjectiveTestFunctionSolution> BestSolutionParameter {
55      get { return (ILookupParameter<SingleObjectiveTestFunctionSolution>)Parameters["BestSolution"]; }
56    }
[3781]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    }
[3647]63    public IValueLookupParameter<ResultCollection> ResultsParameter {
64      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
65    }
[3661]66    public IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator> EvaluatorParameter {
67      get { return (IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>)Parameters["Evaluator"]; }
68    }
[3894]69    public ILookupParameter<DoubleMatrix> BoundsParameter {
70      get { return (ILookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
71    }
[3647]72
[4086]73    [StorableConstructor]
[3894]74    protected BestSingleObjectiveTestFunctionSolutionAnalyzer(bool deserializing) : base(deserializing) { }
[4722]75    protected BestSingleObjectiveTestFunctionSolutionAnalyzer(BestSingleObjectiveTestFunctionSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
[3661]76    public BestSingleObjectiveTestFunctionSolutionAnalyzer()
[3647]77      : base() {
[3787]78      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
[3659]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."));
[3647]81      Parameters.Add(new LookupParameter<SingleObjectiveTestFunctionSolution>("BestSolution", "The best SingleObjectiveTestFunction solution."));
[3781]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."));
[3647]84      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the SingleObjectiveTestFunction solution should be stored."));
[3661]85      Parameters.Add(new ValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>("Evaluator", "The evaluator with which the solution is evaluated."));
[3894]86      Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The bounds of the function."));
[3647]87    }
88
[3894]89    /// <summary>
90    /// This method can simply be removed when the plugin version is > 3.3
91    /// </summary>
92    [StorableHook(HookType.AfterDeserialization)]
[4722]93    private void AfterDeserialization() {
[4098]94      // BackwardsCompatibility3.3
[3894]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
[4722]100    public override IDeepCloneable Clone(Cloner cloner) {
101      return new BestSingleObjectiveTestFunctionSolutionAnalyzer(this, cloner);
102    }
103
[3647]104    public override IOperation Apply() {
[3661]105      ItemArray<RealVector> realVectors = RealVectorParameter.ActualValue;
[3647]106      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
[3787]107      bool max = MaximizationParameter.ActualValue.Value;
108      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
[3894]109      SingleObjectiveTestFunctionSolution solution = BestSolutionParameter.ActualValue;
[3647]110
[5204]111      int i = -1;
112      if (!max) i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
113      else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
[3787]114
115      if (bestKnownQuality == null ||
116          max && qualities[i].Value > bestKnownQuality.Value
117          || !max && qualities[i].Value < bestKnownQuality.Value) {
118        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
119        BestKnownSolutionParameter.ActualValue = (RealVector)realVectors[i].Clone();
[3894]120        if (solution != null)
121          solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
[3787]122      }
123
[3647]124      if (solution == null) {
[3894]125        ResultCollection results = ResultsParameter.ActualValue;
126        solution = new SingleObjectiveTestFunctionSolution(realVectors[i], qualities[i], EvaluatorParameter.ActualValue);
[5204]127        solution.Population = realVectors[i].Length == 2 ? realVectors : null;
[3781]128        solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
[3894]129        solution.Bounds = BoundsParameter.ActualValue;
[3647]130        BestSolutionParameter.ActualValue = solution;
[3787]131        results.Add(new Result("Best Solution", solution));
[3647]132      } else {
[3781]133        if (max && qualities[i].Value > solution.BestQuality.Value
134          || !max && qualities[i].Value < solution.BestQuality.Value) {
[3661]135          solution.BestRealVector = realVectors[i];
136          solution.BestQuality = qualities[i];
137        }
[5204]138        solution.Population = realVectors[i].Length == 2 ? realVectors : null;
[3647]139      }
140
141      return base.Apply();
142    }
143  }
144}
Note: See TracBrowser for help on using the repository browser.