[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 |
|
---|
| 22 | using System.Linq;
|
---|
[4722] | 23 | using HeuristicLab.Common;
|
---|
[4068] | 24 | using HeuristicLab.Core;
|
---|
[3647] | 25 | using HeuristicLab.Data;
|
---|
[4068] | 26 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
[3647] | 27 | using HeuristicLab.Operators;
|
---|
[4068] | 28 | using HeuristicLab.Optimization;
|
---|
[3647] | 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
[3797] | 32 | namespace 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."));
|
---|
[6051] | 87 |
|
---|
| 88 | MaximizationParameter.Hidden = true;
|
---|
| 89 | RealVectorParameter.Hidden = true;
|
---|
| 90 | QualityParameter.Hidden = true;
|
---|
| 91 | BestSolutionParameter.Hidden = true;
|
---|
| 92 | BestKnownSolutionParameter.Hidden = true;
|
---|
| 93 | BestKnownQualityParameter.Hidden = true;
|
---|
| 94 | ResultsParameter.Hidden = true;
|
---|
| 95 | EvaluatorParameter.Hidden = true;
|
---|
| 96 | BoundsParameter.Hidden = true;
|
---|
[3647] | 97 | }
|
---|
| 98 |
|
---|
[3894] | 99 | /// <summary>
|
---|
| 100 | /// This method can simply be removed when the plugin version is > 3.3
|
---|
| 101 | /// </summary>
|
---|
| 102 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[4722] | 103 | private void AfterDeserialization() {
|
---|
[4098] | 104 | // BackwardsCompatibility3.3
|
---|
[3894] | 105 | // Bounds are introduced in 3.3.0.3894
|
---|
| 106 | if (!Parameters.ContainsKey("Bounds"))
|
---|
| 107 | Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The bounds of the function."));
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[4722] | 110 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 111 | return new BestSingleObjectiveTestFunctionSolutionAnalyzer(this, cloner);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[3647] | 114 | public override IOperation Apply() {
|
---|
[3661] | 115 | ItemArray<RealVector> realVectors = RealVectorParameter.ActualValue;
|
---|
[3647] | 116 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
[3787] | 117 | bool max = MaximizationParameter.ActualValue.Value;
|
---|
| 118 | DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
|
---|
[3894] | 119 | SingleObjectiveTestFunctionSolution solution = BestSolutionParameter.ActualValue;
|
---|
[3647] | 120 |
|
---|
[5204] | 121 | int i = -1;
|
---|
| 122 | if (!max) i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
|
---|
| 123 | else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
|
---|
[3787] | 124 |
|
---|
| 125 | if (bestKnownQuality == null ||
|
---|
| 126 | max && qualities[i].Value > bestKnownQuality.Value
|
---|
| 127 | || !max && qualities[i].Value < bestKnownQuality.Value) {
|
---|
| 128 | BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
|
---|
| 129 | BestKnownSolutionParameter.ActualValue = (RealVector)realVectors[i].Clone();
|
---|
[3894] | 130 | if (solution != null)
|
---|
| 131 | solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
|
---|
[3787] | 132 | }
|
---|
| 133 |
|
---|
[3647] | 134 | if (solution == null) {
|
---|
[3894] | 135 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
| 136 | solution = new SingleObjectiveTestFunctionSolution(realVectors[i], qualities[i], EvaluatorParameter.ActualValue);
|
---|
[5204] | 137 | solution.Population = realVectors[i].Length == 2 ? realVectors : null;
|
---|
[3781] | 138 | solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
|
---|
[3894] | 139 | solution.Bounds = BoundsParameter.ActualValue;
|
---|
[3647] | 140 | BestSolutionParameter.ActualValue = solution;
|
---|
[3787] | 141 | results.Add(new Result("Best Solution", solution));
|
---|
[3647] | 142 | } else {
|
---|
[3781] | 143 | if (max && qualities[i].Value > solution.BestQuality.Value
|
---|
| 144 | || !max && qualities[i].Value < solution.BestQuality.Value) {
|
---|
[3661] | 145 | solution.BestRealVector = realVectors[i];
|
---|
| 146 | solution.BestQuality = qualities[i];
|
---|
| 147 | }
|
---|
[5204] | 148 | solution.Population = realVectors[i].Length == 2 ? realVectors : null;
|
---|
[3647] | 149 | }
|
---|
| 150 |
|
---|
| 151 | return base.Apply();
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | }
|
---|