Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.TestFunctions/3.3/Analyzers/BestSingleObjectiveTestFunctionSolutionAnalyzer.cs @ 16949

Last change on this file since 16949 was 16949, checked in by abeham, 5 years ago

#2521: Adapted test function problems to new real vector problem

  • Made encoding readonly in symbolic expression tree problem
File size: 8.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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;
23using System.Linq;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.RealVectorEncoding;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Problems.TestFunctions {
35  // BackwardsCompatibility3.3
36  #region Backwards compatible code, remove with 3.4
37  /// <summary>
38  /// An operator for analyzing the best solution for a SingleObjectiveTestFunction problem.
39  /// </summary>
40  [Item("BestSingleObjectiveTestFunctionSolutionAnalyzer", "An operator for analyzing the best solution for a SingleObjectiveTestFunction problem.")]
41  [StorableType("A0F04F9F-DD27-44D8-A1F6-B289F1F40DE2")]
42  [NonDiscoverableType]
43  [Obsolete("Use the Analyze method of the test function problem class")]
44  public class BestSingleObjectiveTestFunctionSolutionAnalyzer : SingleSuccessorOperator, IBestSingleObjectiveTestFunctionSolutionAnalyzer {
45    public virtual bool EnabledByDefault {
46      get { return true; }
47    }
48
49    public LookupParameter<BoolValue> MaximizationParameter {
50      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
51    }
52    public IScopeTreeLookupParameter<RealVector> RealVectorParameter {
53      get { return (IScopeTreeLookupParameter<RealVector>)Parameters["RealVectors"]; }
54    }
55    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
56      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
57    }
58    public ILookupParameter<SingleObjectiveTestFunctionSolution> BestSolutionParameter {
59      get { return (ILookupParameter<SingleObjectiveTestFunctionSolution>)Parameters["BestSolution"]; }
60    }
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    }
67    public IValueLookupParameter<ResultCollection> ResultsParameter {
68      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
69    }
70    public IValueLookupParameter<ISingleObjectiveTestFunction> TestFunctionParameter {
71      get { return (IValueLookupParameter<ISingleObjectiveTestFunction>)Parameters["TestFunction"]; }
72    }
73    public IValueLookupParameter<DoubleMatrix> BoundsParameter {
74      get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
75    }
76
77    [StorableConstructor]
78    protected BestSingleObjectiveTestFunctionSolutionAnalyzer(StorableConstructorFlag _) : base(_) { }
79    protected BestSingleObjectiveTestFunctionSolutionAnalyzer(BestSingleObjectiveTestFunctionSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
80    public BestSingleObjectiveTestFunctionSolutionAnalyzer()
81      : base() {
82      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
83      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RealVectors", "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."));
85      Parameters.Add(new LookupParameter<SingleObjectiveTestFunctionSolution>("BestSolution", "The best SingleObjectiveTestFunction solution."));
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."));
88      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the SingleObjectiveTestFunction solution should be stored."));
89      Parameters.Add(new ValueLookupParameter<ISingleObjectiveTestFunction>("TestFunction", "The evaluator with which the solution is evaluated."));
90      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The bounds of the function."));
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      TestFunctionParameter.Hidden = true;
100      BoundsParameter.Hidden = true;
101    }
102
103    public override IDeepCloneable Clone(Cloner cloner) {
104      return new BestSingleObjectiveTestFunctionSolutionAnalyzer(this, cloner);
105    }
106
107    public override IOperation Apply() {
108      ItemArray<RealVector> realVectors = RealVectorParameter.ActualValue;
109      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
110      bool max = MaximizationParameter.ActualValue.Value;
111      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
112      SingleObjectiveTestFunctionSolution solution = BestSolutionParameter.ActualValue;
113
114      int i = -1;
115      if (!max) i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
116      else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
117
118      if (bestKnownQuality == null ||
119          max && qualities[i].Value > bestKnownQuality.Value
120          || !max && qualities[i].Value < bestKnownQuality.Value) {
121        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
122        BestKnownSolutionParameter.ActualValue = (RealVector)realVectors[i].Clone();
123        if (solution != null)
124          solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
125      }
126
127      if (solution == null) {
128        ResultCollection results = ResultsParameter.ActualValue;
129        solution = new SingleObjectiveTestFunctionSolution((RealVector)realVectors[i].Clone(),
130                                                           (DoubleValue)qualities[i].Clone(),
131                                                           TestFunctionParameter.ActualValue);
132        solution.Population = realVectors[i].Length == 2
133          ? new ItemArray<RealVector>(realVectors.Select(x => x.Clone()).Cast<RealVector>())
134          : null;
135        solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
136        solution.Bounds = BoundsParameter.ActualValue;
137        BestSolutionParameter.ActualValue = solution;
138        results.Add(new Result("Best Solution", solution));
139      } else {
140        if (max && qualities[i].Value > solution.BestQuality.Value
141          || !max && qualities[i].Value < solution.BestQuality.Value) {
142          solution.BestRealVector = (RealVector)realVectors[i].Clone();
143          solution.BestQuality = (DoubleValue)qualities[i].Clone();
144        }
145        solution.Population = realVectors[i].Length == 2
146          ? new ItemArray<RealVector>(realVectors.Select(x => x.Clone()).Cast<RealVector>())
147          : null;
148      }
149
150      return base.Apply();
151    }
152  }
153  #endregion
154}
Note: See TracBrowser for help on using the repository browser.