Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/SelectionPressureAnalyzer.cs @ 9384

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

#1886 added calculation of selection intensity

File size: 8.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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
22
23using System;
24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
34  [Item("SelectionPressureAnalyzer", "An operator that analyzes the current selection pressure.")]
35  [StorableClass]
36  public class SelectionPressureAnalyzer : InitializableOperator, IStatefulItem {
37    private const string ResultsParameterName = "Results";
38    private const string GenerationsParameterName = "Generations";
39    [Storable]
40    public string SolutionQualityName { get; set; }
41
42    #region Parameter properties
43    public IValueLookupParameter<BoolValue> MaximizationParameter {
44      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
45    }
46    public ILookupParameter<ResultCollection> ResultsParameter {
47      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
48    }
49    public ILookupParameter<IntValue> GenerationsParameter {
50      get { return (ILookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
51    }
52    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
53      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
54    }
55    public ILookupParameter<DoubleValue> WorstKnownQualityParameter {
56      get { return (ILookupParameter<DoubleValue>)Parameters["WorstKnownQuality"]; }
57    }
58    public ILookupParameter<ItemArray<DoubleValue>> ParentsQualityParameter {
59      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["ParentsQuality"]; }
60    }
61    #endregion
62
63    #region Properties
64    public ResultCollection Results {
65      get { return ResultsParameter.ActualValue; }
66    }
67    [Storable]
68    private ScatterPlotHelper selPressurePlot, selIntensityPlot;
69    [Storable]
70    private int cnt = 0;
71    [Storable]
72    private int lastGeneration = 0;
73    #endregion
74
75    [StorableConstructor]
76    private SelectionPressureAnalyzer(bool deserializing) : base(deserializing) { }
77    private SelectionPressureAnalyzer(SelectionPressureAnalyzer original, Cloner cloner)
78      : base(original, cloner) {
79      cnt = original.cnt;
80      lastGeneration = original.lastGeneration;
81      selPressurePlot = (ScatterPlotHelper)original.selPressurePlot.Clone(cloner);
82      selIntensityPlot = (ScatterPlotHelper)original.selIntensityPlot.Clone(cloner);
83      SolutionQualityName = original.SolutionQualityName;
84    }
85
86    public SelectionPressureAnalyzer()
87      : base() {
88      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
89      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
90      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
91      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
92      Parameters.Add(new LookupParameter<DoubleValue>("WorstKnownQuality", "The quality of the worst known solution of this problem."));
93      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ParentsQuality", "The quality of the parent solutions."));
94      SolutionQualityName = "TSPTourLength";
95
96      selPressurePlot = new ScatterPlotHelper(false, true, true, true);
97      selIntensityPlot = new ScatterPlotHelper(false, true, true, true);
98    }
99
100    public override IDeepCloneable Clone(Cloner cloner) {
101      return new SelectionPressureAnalyzer(this, cloner);
102    }
103
104    protected override void InitializeAction() {
105      selPressurePlot.InitializePlot(Results, "Selection Pressure", "Solution Index", "Quality Difference");
106      selIntensityPlot.InitializePlot(Results, "Selection Intensity", "Solution Index", "Scaled Quality Difference");
107      ParentsQualityParameter.ActualName = SolutionQualityName;
108
109      Reset();
110    }
111
112    public override IOperation Apply() {
113      Initialize();
114
115      string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
116
117      IScope oldPop = ReverseScopeTreeLookup("Remaining");
118      if (oldPop == null)
119        throw new Exception("Couldn't find the remaining scope");
120
121      if (GenerationsParameter.ActualValue.Value != 0) {
122        if (GenerationsParameter.ActualValue.Value > lastGeneration) {
123          Reset();
124        }
125
126        double oldPopQuality = 0.0;
127        List<double> qValues = new List<double>();
128        int popSize = 0;
129        foreach (IScope oldSolScope in oldPop.SubScopes) {
130          double curQuality = ((DoubleValue)oldSolScope.Variables[SolutionQualityName].Value).Value;
131          oldPopQuality += curQuality;
132          qValues.Add(curQuality);
133          popSize++;
134        }
135
136        double quality = ParentsQualityParameter.ActualValue.Average(x => x.Value);
137
138        if (GenerationsParameter.ActualValue.Value == 1) {
139          double bkQuality = BestKnownQualityParameter.ActualValue.Value;
140          double wkQuality = WorstKnownQualityParameter.ActualValue.Value;
141
142          if (MaximizationParameter.ActualValue.Value) {
143            if (selPressurePlot.Max == double.MinValue) {
144              selPressurePlot.Max = bkQuality - wkQuality;
145              selPressurePlot.Min = 0;
146              selIntensityPlot.Max = bkQuality - wkQuality;
147              selIntensityPlot.Min = 0;
148            }
149          } else {
150            if (selPressurePlot.Min == double.MaxValue) {
151              selPressurePlot.Max = wkQuality - bkQuality;
152              selPressurePlot.Min = 0;
153              selIntensityPlot.Max = wkQuality - bkQuality;
154              selIntensityPlot.Min = 0;
155            }
156          }
157        }
158
159        Point2D<double> popQualityPoint, selectionIntensityPoint;
160        if (MaximizationParameter.ActualValue.Value) {
161          popQualityPoint = new Point2D<double>(cnt, quality - (oldPopQuality / popSize));
162          selectionIntensityPoint = new Point2D<double>(cnt, (quality - (oldPopQuality / popSize) / qValues.StandardDeviation()));
163        } else {
164          popQualityPoint = new Point2D<double>(cnt, (oldPopQuality / popSize) - quality);
165          selectionIntensityPoint = new Point2D<double>(cnt, ((oldPopQuality / popSize) - quality) / qValues.StandardDeviation());
166        }
167
168        selPressurePlot.AddPoint(curGenStr, popQualityPoint);
169        selIntensityPlot.AddPoint(curGenStr, selectionIntensityPoint);
170      }
171
172      return base.Apply();
173    }
174
175    private void Reset() {
176      cnt = 0;
177      lastGeneration = GenerationsParameter.ActualValue.Value;
178    }
179
180    public override void ClearState() {
181      selPressurePlot.CleanUp();
182      selIntensityPlot.CleanUp();
183    }
184
185    private IScope ReverseScopeTreeLookup(string scopeName) {
186      var currentScope = ExecutionContext.Scope;
187      while (currentScope != null) {
188        var scopes = currentScope.SubScopes.Where(x => x.Name == scopeName);
189        if (scopes.Count() > 0)
190          return scopes.First();
191
192        currentScope = currentScope.Parent;
193      }
194      return null;
195    }
196  }
197}
Note: See TracBrowser for help on using the repository browser.