Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Analysis/3.3/BestScopeSolutionAnalyzer.cs @ 11301

Last change on this file since 11301 was 11170, checked in by ascheibe, 10 years ago

#2115 updated copyright year in stable branch

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