Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Problems.TestFunctions/3.3/Analyzers/BestSingleObjectiveTestFunctionSolutionAnalyzer.cs @ 4688

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

Finished cloning refactoring of HeuristicLab.Problems.TestFunctions (#922)

File size: 7.4 KB
RevLine 
[3647]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;
[4688]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) { }
[4688]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)]
[4688]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
[4688]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
111      int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
[3787]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();
[3894]118        if (solution != null)
119          solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
[3787]120      }
121
[3647]122      if (solution == null) {
[3894]123        ResultCollection results = ResultsParameter.ActualValue;
124        solution = new SingleObjectiveTestFunctionSolution(realVectors[i], qualities[i], EvaluatorParameter.ActualValue);
[3661]125        solution.Population = realVectors;
[3781]126        solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
[3894]127        solution.Bounds = BoundsParameter.ActualValue;
[3647]128        BestSolutionParameter.ActualValue = solution;
[3787]129        results.Add(new Result("Best Solution", solution));
[3647]130      } else {
[3781]131        if (max && qualities[i].Value > solution.BestQuality.Value
132          || !max && qualities[i].Value < solution.BestQuality.Value) {
[3661]133          solution.BestRealVector = realVectors[i];
134          solution.BestQuality = qualities[i];
135        }
136        solution.Population = realVectors;
[3647]137      }
138
139      return base.Apply();
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.