Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5899 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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