Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12012 was 12012, checked in by ascheibe, 9 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 8.5 KB
RevLine 
[3647]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 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]
[11970]38  public class BestSingleObjectiveTestFunctionSolutionAnalyzer : SingleSuccessorOperator, IBestSingleObjectiveTestFunctionSolutionAnalyzer {
[7172]39    public virtual bool EnabledByDefault {
40      get { return true; }
41    }
42
[3787]43    public LookupParameter<BoolValue> MaximizationParameter {
44      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
45    }
[3661]46    public ScopeTreeLookupParameter<RealVector> RealVectorParameter {
47      get { return (ScopeTreeLookupParameter<RealVector>)Parameters["RealVector"]; }
[3647]48    }
49    ILookupParameter IBestSingleObjectiveTestFunctionSolutionAnalyzer.RealVectorParameter {
50      get { return RealVectorParameter; }
51    }
[3661]52    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
53      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
[3647]54    }
55    ILookupParameter IBestSingleObjectiveTestFunctionSolutionAnalyzer.QualityParameter {
56      get { return QualityParameter; }
57    }
58    public ILookupParameter<SingleObjectiveTestFunctionSolution> BestSolutionParameter {
59      get { return (ILookupParameter<SingleObjectiveTestFunctionSolution>)Parameters["BestSolution"]; }
60    }
[3781]61    public ILookupParameter<RealVector> BestKnownSolutionParameter {
62      get { return (ILookupParameter<RealVector>)Parameters["BestKnownSolution"]; }
63    }
64    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
65      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
66    }
[3647]67    public IValueLookupParameter<ResultCollection> ResultsParameter {
68      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
69    }
[3661]70    public IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator> EvaluatorParameter {
71      get { return (IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>)Parameters["Evaluator"]; }
72    }
[3894]73    public ILookupParameter<DoubleMatrix> BoundsParameter {
74      get { return (ILookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
75    }
[3647]76
[4086]77    [StorableConstructor]
[3894]78    protected BestSingleObjectiveTestFunctionSolutionAnalyzer(bool deserializing) : base(deserializing) { }
[4722]79    protected BestSingleObjectiveTestFunctionSolutionAnalyzer(BestSingleObjectiveTestFunctionSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
[3661]80    public BestSingleObjectiveTestFunctionSolutionAnalyzer()
[3647]81      : base() {
[3787]82      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
[3659]83      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RealVector", "The SingleObjectiveTestFunction solutions from which the best solution should be visualized."));
84      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the SingleObjectiveTestFunction solutions which should be visualized."));
[3647]85      Parameters.Add(new LookupParameter<SingleObjectiveTestFunctionSolution>("BestSolution", "The best SingleObjectiveTestFunction solution."));
[3781]86      Parameters.Add(new LookupParameter<RealVector>("BestKnownSolution", "The best known solution."));
87      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution."));
[3647]88      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the SingleObjectiveTestFunction solution should be stored."));
[3661]89      Parameters.Add(new ValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>("Evaluator", "The evaluator with which the solution is evaluated."));
[3894]90      Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The bounds of the function."));
[6051]91
92      MaximizationParameter.Hidden = true;
93      RealVectorParameter.Hidden = true;
94      QualityParameter.Hidden = true;
95      BestSolutionParameter.Hidden = true;
96      BestKnownSolutionParameter.Hidden = true;
97      BestKnownQualityParameter.Hidden = true;
98      ResultsParameter.Hidden = true;
99      EvaluatorParameter.Hidden = true;
100      BoundsParameter.Hidden = true;
[3647]101    }
102
[3894]103    /// <summary>
104    /// This method can simply be removed when the plugin version is > 3.3
105    /// </summary>
106    [StorableHook(HookType.AfterDeserialization)]
[4722]107    private void AfterDeserialization() {
[4098]108      // BackwardsCompatibility3.3
[3894]109      // Bounds are introduced in 3.3.0.3894
110      if (!Parameters.ContainsKey("Bounds"))
111        Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The bounds of the function."));
112    }
113
[4722]114    public override IDeepCloneable Clone(Cloner cloner) {
115      return new BestSingleObjectiveTestFunctionSolutionAnalyzer(this, cloner);
116    }
117
[3647]118    public override IOperation Apply() {
[3661]119      ItemArray<RealVector> realVectors = RealVectorParameter.ActualValue;
[3647]120      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
[3787]121      bool max = MaximizationParameter.ActualValue.Value;
122      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
[3894]123      SingleObjectiveTestFunctionSolution solution = BestSolutionParameter.ActualValue;
[3647]124
[5204]125      int i = -1;
126      if (!max) i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
127      else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
[3787]128
129      if (bestKnownQuality == null ||
130          max && qualities[i].Value > bestKnownQuality.Value
131          || !max && qualities[i].Value < bestKnownQuality.Value) {
132        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
133        BestKnownSolutionParameter.ActualValue = (RealVector)realVectors[i].Clone();
[3894]134        if (solution != null)
135          solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
[3787]136      }
137
[3647]138      if (solution == null) {
[3894]139        ResultCollection results = ResultsParameter.ActualValue;
[10794]140        solution = new SingleObjectiveTestFunctionSolution((RealVector)realVectors[i].Clone(),
141                                                           (DoubleValue)qualities[i].Clone(),
142                                                           EvaluatorParameter.ActualValue);
143        solution.Population = realVectors[i].Length == 2
144          ? new ItemArray<RealVector>(realVectors.Select(x => x.Clone()).Cast<RealVector>())
145          : null;
[3781]146        solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
[3894]147        solution.Bounds = BoundsParameter.ActualValue;
[3647]148        BestSolutionParameter.ActualValue = solution;
[3787]149        results.Add(new Result("Best Solution", solution));
[3647]150      } else {
[3781]151        if (max && qualities[i].Value > solution.BestQuality.Value
152          || !max && qualities[i].Value < solution.BestQuality.Value) {
[10794]153          solution.BestRealVector = (RealVector)realVectors[i].Clone();
154          solution.BestQuality = (DoubleValue)qualities[i].Clone();
[3661]155        }
[10794]156        solution.Population = realVectors[i].Length == 2
157          ? new ItemArray<RealVector>(realVectors.Select(x => x.Clone()).Cast<RealVector>())
158          : null;
[3647]159      }
160
161      return base.Apply();
162    }
163  }
164}
Note: See TracBrowser for help on using the repository browser.