Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/SolutionToPopulationAnalyzer.cs @ 10635

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

#1886 fixed alglib reference and updated year and version information

File size: 10.4 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
22
23using System;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
33  [Item("SolutionToPopulationAnalyzer", "An operator that analyzes the diversity of solutions compared to the population.")]
34  [StorableClass]
35  public class SolutionToPopulationAnalyzer : InitializableOperator, IStatefulItem {
36    private const string ResultsParameterName = "Results";
37    private const string GenerationsParameterName = "Generations";
38
39    #region Parameter properties
40    public IValueLookupParameter<BoolValue> MaximizationParameter {
41      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
42    }
43    public ILookupParameter<ResultCollection> ResultsParameter {
44      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
45    }
46    public ILookupParameter<IntValue> GenerationsParameter {
47      get { return (ILookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
48    }
49    public ILookupParameter<DoubleValue> QualityParameter {
50      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
51    }
52    public ILookupParameter<IItem> SolutionParameter {
53      get { return (ILookupParameter<IItem>)Parameters["Solution"]; }
54    }
55    public ILookupParameter<ItemCollection<IItem>> OperatorsParameter {
56      get { return (ILookupParameter<ItemCollection<IItem>>)Parameters["Operators"]; }
57    }
58    public IValueParameter<ISingleObjectiveSolutionSimilarityCalculator> SimilarityCalculatorParameter {
59      get { return (IValueParameter<ISingleObjectiveSolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
60    }
61    public IValueParameter<StringValue> ChartPostfixParameter {
62      get { return (IValueParameter<StringValue>)Parameters["ChartPostfix"]; }
63    }
64    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
65      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
66    }
67    public ILookupParameter<DoubleValue> WorstKnownQualityParameter {
68      get { return (ILookupParameter<DoubleValue>)Parameters["WorstKnownQuality"]; }
69    }
70    #endregion
71
72    #region Properties
73    public ResultCollection Results {
74      get { return ResultsParameter.ActualValue; }
75    }
76    [Storable]
77    private ScatterPlotHelper populationDiversityPlot, populationQualityPlot, qualityPlot;
78    [Storable]
79    private int cnt = 0;
80    [Storable]
81    private int lastGeneration = 0;
82    [Storable]
83    private bool scalingFinished = false;
84    #endregion
85
86    [StorableConstructor]
87    private SolutionToPopulationAnalyzer(bool deserializing) : base(deserializing) { }
88    private SolutionToPopulationAnalyzer(SolutionToPopulationAnalyzer original, Cloner cloner)
89      : base(original, cloner) {
90      cnt = original.cnt;
91      lastGeneration = original.lastGeneration;
92      populationDiversityPlot = (ScatterPlotHelper)original.populationDiversityPlot.Clone(cloner);
93      populationQualityPlot = (ScatterPlotHelper)original.populationQualityPlot.Clone(cloner);
94      qualityPlot = (ScatterPlotHelper)original.qualityPlot.Clone(cloner);
95      scalingFinished = original.scalingFinished;
96    }
97
98    public SolutionToPopulationAnalyzer()
99      : base() {
100      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
101      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
102      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the child solution."));
103      QualityParameter.ActualName = "TSPTourLength";
104      Parameters.Add(new LookupParameter<IItem>("Solution"));
105      SolutionParameter.ActualName = "TSPTour";
106      Parameters.Add(new ValueParameter<ISingleObjectiveSolutionSimilarityCalculator>("SimilarityCalculator"));
107      Parameters.Add(new ValueParameter<StringValue>("ChartPostfix", new StringValue(string.Empty)));
108      Parameters.Add(new LookupParameter<ItemCollection<IItem>>("Operators", "The operators and items that the problem provides to the algorithms."));
109      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
110      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
111      Parameters.Add(new LookupParameter<DoubleValue>("WorstKnownQuality", "The quality of the worst known solution of this problem."));
112
113      populationDiversityPlot = new ScatterPlotHelper(false, true, false, true);
114      populationQualityPlot = new ScatterPlotHelper(false, true, true, true);
115      qualityPlot = new ScatterPlotHelper(false, true, true, true);
116    }
117
118    public override IDeepCloneable Clone(Cloner cloner) {
119      return new SolutionToPopulationAnalyzer(this, cloner);
120    }
121
122    protected override void InitializeAction() {
123      if (SimilarityCalculatorParameter.Value == null) {
124        SimilarityCalculatorParameter.Value = OperatorsParameter.ActualValue.OfType<ISingleObjectiveSolutionSimilarityCalculator>().FirstOrDefault();
125      }
126
127      populationDiversityPlot.InitializePlot(Results, "Solution to Population Diversity " + ChartPostfixParameter.Value.Value, "Solution Index", "Diversity");
128      populationQualityPlot.InitializePlot(Results, "Solution Quality Difference to Population " + ChartPostfixParameter.Value.Value, "Solution Index", "Quality Difference");
129      qualityPlot.InitializePlot(Results, "Solution Quality " + ChartPostfixParameter.Value.Value, "Solution Index", "Quality");
130
131      Reset();
132    }
133
134    public override IOperation Apply() {
135      Initialize();
136
137      string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
138      double quality = QualityParameter.ActualValue.Value;
139      IItem solution = SolutionParameter.ActualValue;
140      string qualityVariableName = QualityParameter.ActualName;
141      string solutionVariableName = SolutionParameter.ActualName;
142      ISingleObjectiveSolutionSimilarityCalculator simCalc = SimilarityCalculatorParameter.Value;
143      Scope artificialSolutionScope = new Scope();
144      artificialSolutionScope.Variables.Add(new Variable(solutionVariableName, solution));
145
146
147      IScope oldPop = ReverseScopeTreeLookup("Remaining");
148      if (oldPop == null)
149        throw new Exception("Couldn't find the remaining scope");
150
151      if (GenerationsParameter.ActualValue.Value != 0) {
152        if (GenerationsParameter.ActualValue.Value > lastGeneration) {
153          Reset();
154        }
155
156        double oldPopQuality = 0.0;
157        double solToPopDiversity = 0.0;
158        int popSize = 0;
159        foreach (IScope oldSolScope in oldPop.SubScopes) {
160          double curQuality = ((DoubleValue)oldSolScope.Variables[qualityVariableName].Value).Value;
161          IItem curSol = oldSolScope.Variables[solutionVariableName].Value;
162          oldPopQuality += curQuality;
163          solToPopDiversity += simCalc.CalculateSolutionSimilarity(artificialSolutionScope, oldSolScope);
164          popSize++;
165        }
166
167        if (WorstKnownQualityParameter.ActualValue != null && !scalingFinished) {
168          scalingFinished = true;
169          double bkQuality = BestKnownQualityParameter.ActualValue.Value;
170          double wkQuality = WorstKnownQualityParameter.ActualValue.Value;
171
172          if (MaximizationParameter.ActualValue.Value) {
173            if (populationQualityPlot.Max == double.MinValue) {
174              populationQualityPlot.Max = bkQuality - wkQuality;
175              qualityPlot.Max = bkQuality;
176              populationQualityPlot.Min = 0;
177              qualityPlot.Min = wkQuality;
178            }
179          } else {
180            if (populationQualityPlot.Min == double.MaxValue) {
181              populationQualityPlot.Max = wkQuality - bkQuality;
182              qualityPlot.Max = wkQuality;
183              populationQualityPlot.Min = 0;
184              qualityPlot.Min = bkQuality;
185            }
186          }
187        }
188
189        Point2D<double> popQualityPoint;
190        if (MaximizationParameter.ActualValue.Value) {
191          popQualityPoint = new Point2D<double>(cnt, quality - (oldPopQuality / popSize));
192        } else {
193          popQualityPoint = new Point2D<double>(cnt, (oldPopQuality / popSize) - quality);
194        }
195
196        Point2D<double> solQuality = new Point2D<double>(cnt, quality);
197        Point2D<double> diversityPoint = new Point2D<double>(cnt++, solToPopDiversity / popSize);
198
199        populationDiversityPlot.AddPoint(curGenStr, diversityPoint);
200        populationQualityPlot.AddPoint(curGenStr, popQualityPoint);
201        qualityPlot.AddPoint(curGenStr, solQuality);
202      }
203
204      return base.Apply();
205    }
206
207    private void Reset() {
208      cnt = 0;
209      lastGeneration = GenerationsParameter.ActualValue.Value;
210    }
211
212    public override void ClearState() {
213      populationQualityPlot.CleanUp();
214      populationDiversityPlot.CleanUp();
215      qualityPlot.CleanUp();
216      scalingFinished = false;
217    }
218
219    private IScope ReverseScopeTreeLookup(string scopeName) {
220      var currentScope = ExecutionContext.Scope;
221      while (currentScope != null) {
222        var scopes = currentScope.SubScopes.Where(x => x.Name == scopeName);
223        if (scopes.Count() > 0)
224          return scopes.First();
225
226        currentScope = currentScope.Parent;
227      }
228      return null;
229    }
230  }
231}
Note: See TracBrowser for help on using the repository browser.