Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/WorstQualityAnalyzer.cs @ 10007

Last change on this file since 10007 was 9789, checked in by ascheibe, 11 years ago

#1886 fixed alglib reference and updated year and version information

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Linq;
23using HeuristicLab.Common;
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.AlgorithmBehavior.Analyzers {
32  [Item("WorstQualityAnalyzer", "An operator that collects the worst quality found.")]
33  [StorableClass]
34  public class WorstQualityAnalyzer : SingleSuccessorOperator, IStatefulItem {
35    private const string GenerationsParameterName = "Generations";
36    private const string ResultsParameterName = "Results";
37
38    public IValueLookupParameter<BoolValue> MaximizationParameter {
39      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
40    }
41    public ILookupParameter<ItemArray<DoubleValue>> ParentsQualityParameter {
42      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["ParentsQuality"]; }
43    }
44    public ILookupParameter<IntValue> GenerationsParameter {
45      get { return (ILookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
46    }
47    public ILookupParameter<DoubleValue> WorstKnownQualityParameter {
48      get { return (ILookupParameter<DoubleValue>)Parameters["WorstKnownQuality"]; }
49    }
50    public ILookupParameter<ResultCollection> ResultsParameter {
51      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
52    }
53    [Storable]
54    private double upperBound = double.MinValue;
55
56    [StorableConstructor]
57    private WorstQualityAnalyzer(bool deserializing) : base(deserializing) { }
58    private WorstQualityAnalyzer(WorstQualityAnalyzer original, Cloner cloner)
59      : base(original, cloner) {
60      upperBound = (double)original.upperBound;
61    }
62
63    public WorstQualityAnalyzer()
64      : base() {
65      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ParentsQuality", "The quality of the parent solutions."));
66      ParentsQualityParameter.ActualName = "TSPTourLength";
67      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
68      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
69      Parameters.Add(new LookupParameter<DoubleValue>("WorstKnownQuality", "The quality of the worst known solution of this problem."));
70      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
71    }
72
73    public override IDeepCloneable Clone(Cloner cloner) {
74      return new WorstQualityAnalyzer(this, cloner);
75    }
76
77    public override IOperation Apply() {
78      var qualityParent1 = ParentsQualityParameter.ActualValue.First().Value;
79      var qualityParent2 = ParentsQualityParameter.ActualValue.Last().Value;
80
81      if (GenerationsParameter.ActualValue.Value == 0) {
82        if (MaximizationParameter.ActualValue.Value) {
83          if (upperBound > qualityParent1) {
84            upperBound = qualityParent1;
85          }
86          if (upperBound > qualityParent2) {
87            upperBound = qualityParent2;
88          }
89        } else {
90          if (upperBound < qualityParent1) {
91            upperBound = qualityParent1;
92          }
93          if (upperBound < qualityParent2) {
94            upperBound = qualityParent2;
95          }
96        }
97      } else {
98        if (WorstKnownQualityParameter.ActualValue == null) {
99          AddVariableToGlobalScope("WorstKnownQuality", "The quality of the worst known solution of this problem.", new DoubleValue(upperBound));
100          ResultsParameter.ActualValue.Add(new Result("WorstQuality", new DoubleValue(upperBound)));
101        }
102      }
103
104      return base.Apply();
105    }
106
107    public override void ClearState() {
108      upperBound = double.MinValue;
109    }
110
111    public override void InitializeState() { }
112
113    private void AddVariableToGlobalScope(string name, string description, DoubleValue value) {
114      var globalScope = ExecutionContext;
115      while (globalScope.Parent != null) {
116        globalScope = globalScope.Parent;
117      }
118
119      globalScope.Scope.Variables.Add(new Variable(name, description, value));
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.