Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Analysis/3.3/BestScopeSolutionAnalyzer.cs @ 4410

Last change on this file since 4410 was 3901, checked in by abeham, 14 years ago

#1032

  • Added check for maximization or minimization problem when sorting qualities
File size: 5.2 KB
Line 
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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Analysis {
32  /// <summary>
33  /// An operator that extracts (clones) the scope containing the best quality.
34  /// </summary>
35  [Item("BestScopeSolutionAnalyzer", "An operator that extracts the scope containing the best quality.")]
36  [StorableClass]
37  public class BestScopeSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
38    public LookupParameter<BoolValue> MaximizationParameter {
39      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
40    }
41    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
42      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
43    }
44    public ILookupParameter<IScope> BestSolutionParameter {
45      get { return (ILookupParameter<IScope>)Parameters["BestSolution"]; }
46    }
47    public ILookupParameter<IScope> BestKnownSolutionParameter {
48      get { return (ILookupParameter<IScope>)Parameters["BestKnownSolution"]; }
49    }
50    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
51      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
52    }
53    public IValueLookupParameter<ResultCollection> ResultsParameter {
54      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
55    }
56
57    public BestScopeSolutionAnalyzer()
58      : base() {
59      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
60      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the solutions."));
61      Parameters.Add(new LookupParameter<IScope>("BestSolution", "The best solution."));
62      Parameters.Add(new LookupParameter<IScope>("BestKnownSolution", "The best known solution."));
63      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution."));
64      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the solution should be stored."));
65    }
66
67    public override IOperation Apply() {
68      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
69      ResultCollection results = ResultsParameter.ActualValue;
70      bool max = MaximizationParameter.ActualValue.Value;
71      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
72
73      int i = -1;
74      if (!max)
75        i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
76      else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
77     
78      IEnumerable<IScope> scopes = new IScope[] { ExecutionContext.Scope };
79      for (int j = 0; j < QualityParameter.Depth; j++)
80        scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
81      IScope currentBestScope = scopes.ToList()[i];
82
83      if (bestKnownQuality == null ||
84          max && qualities[i].Value > bestKnownQuality.Value
85          || !max && qualities[i].Value < bestKnownQuality.Value) {
86        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
87        BestKnownSolutionParameter.ActualValue = (IScope)currentBestScope.Clone();
88      }
89
90      IScope solution = BestSolutionParameter.ActualValue;
91      if (solution == null) {
92        solution = (IScope)currentBestScope.Clone();
93        BestSolutionParameter.ActualValue = solution;
94        results.Add(new Result("Best Solution", solution));
95      } else {
96        string qualityName = QualityParameter.TranslatedName;
97        if (solution.Variables.ContainsKey(qualityName)) {
98          double bestSoFarQuality = (solution.Variables[qualityName].Value as DoubleValue).Value;
99          if (max && qualities[i].Value > bestSoFarQuality
100            || !max && qualities[i].Value < bestSoFarQuality) {
101            solution = (IScope)currentBestScope.Clone();
102            BestSolutionParameter.ActualValue = solution;
103            results["Best Solution"].Value = solution;
104          }
105        }
106      }
107
108      return base.Apply();
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.