Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScopedAlgorithms/HeuristicLab.Problems.TestFunctions/3.3/Analyzers/BestSingleObjectiveTestFunctionSolutionAnalyzer.cs @ 15301

Last change on this file since 15301 was 13403, checked in by abeham, 9 years ago

#2521:

  • Adapted single-objective test function problem to new problem infrastructure
  • Added additional interfaces to RealVectorEncoding
  • Fixed IParticleUpdater interface (must implement IStochasticOperator if it contains a Random parameter)
File size: 7.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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  public class BestSingleObjectiveTestFunctionSolutionAnalyzer : SingleSuccessorOperator, IBestSingleObjectiveTestFunctionSolutionAnalyzer {
39    public virtual bool EnabledByDefault {
40      get { return true; }
41    }
42
43    public LookupParameter<BoolValue> MaximizationParameter {
44      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
45    }
46    public IScopeTreeLookupParameter<RealVector> RealVectorsParameter {
47      get { return (IScopeTreeLookupParameter<RealVector>)Parameters["RealVectors"]; }
48    }
49    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
50      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
51    }
52    public ILookupParameter<SingleObjectiveTestFunctionSolution> BestSolutionParameter {
53      get { return (ILookupParameter<SingleObjectiveTestFunctionSolution>)Parameters["BestSolution"]; }
54    }
55    public ILookupParameter<RealVector> BestKnownSolutionParameter {
56      get { return (ILookupParameter<RealVector>)Parameters["BestKnownSolution"]; }
57    }
58    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
59      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
60    }
61    public IValueLookupParameter<ResultCollection> ResultsParameter {
62      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
63    }
64    public IValueLookupParameter<ISingleObjectiveTestFunction> TestFunctionParameter {
65      get { return (IValueLookupParameter<ISingleObjectiveTestFunction>)Parameters["TestFunction"]; }
66    }
67    public IValueLookupParameter<DoubleMatrix> BoundsParameter {
68      get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
69    }
70
71    [StorableConstructor]
72    protected BestSingleObjectiveTestFunctionSolutionAnalyzer(bool deserializing) : base(deserializing) { }
73    protected BestSingleObjectiveTestFunctionSolutionAnalyzer(BestSingleObjectiveTestFunctionSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
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>("RealVectors", "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<ISingleObjectiveTestFunction>("TestFunction", "The evaluator with which the solution is evaluated."));
84      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The bounds of the function."));
85
86      MaximizationParameter.Hidden = true;
87      RealVectorsParameter.Hidden = true;
88      QualityParameter.Hidden = true;
89      BestSolutionParameter.Hidden = true;
90      BestKnownSolutionParameter.Hidden = true;
91      BestKnownQualityParameter.Hidden = true;
92      ResultsParameter.Hidden = true;
93      TestFunctionParameter.Hidden = true;
94      BoundsParameter.Hidden = true;
95    }
96
97    public override IDeepCloneable Clone(Cloner cloner) {
98      return new BestSingleObjectiveTestFunctionSolutionAnalyzer(this, cloner);
99    }
100
101    public override IOperation Apply() {
102      ItemArray<RealVector> realVectors = RealVectorsParameter.ActualValue;
103      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
104      bool max = MaximizationParameter.ActualValue.Value;
105      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
106      SingleObjectiveTestFunctionSolution solution = BestSolutionParameter.ActualValue;
107
108      int i = -1;
109      if (!max) i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
110      else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
111
112      if (bestKnownQuality == null ||
113          max && qualities[i].Value > bestKnownQuality.Value
114          || !max && qualities[i].Value < bestKnownQuality.Value) {
115        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
116        BestKnownSolutionParameter.ActualValue = (RealVector)realVectors[i].Clone();
117        if (solution != null)
118          solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
119      }
120
121      if (solution == null) {
122        ResultCollection results = ResultsParameter.ActualValue;
123        solution = new SingleObjectiveTestFunctionSolution((RealVector)realVectors[i].Clone(),
124                                                           (DoubleValue)qualities[i].Clone(),
125                                                           TestFunctionParameter.ActualValue);
126        solution.Population = realVectors[i].Length == 2
127          ? new ItemArray<RealVector>(realVectors.Select(x => x.Clone()).Cast<RealVector>())
128          : null;
129        solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
130        solution.Bounds = BoundsParameter.ActualValue;
131        BestSolutionParameter.ActualValue = solution;
132        results.Add(new Result("Best Solution", solution));
133      } else {
134        if (max && qualities[i].Value > solution.BestQuality.Value
135          || !max && qualities[i].Value < solution.BestQuality.Value) {
136          solution.BestRealVector = (RealVector)realVectors[i].Clone();
137          solution.BestQuality = (DoubleValue)qualities[i].Clone();
138        }
139        solution.Population = realVectors[i].Length == 2
140          ? new ItemArray<RealVector>(realVectors.Select(x => x.Clone()).Cast<RealVector>())
141          : null;
142      }
143
144      return base.Apply();
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.