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 |
|
---|
23 | using System;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | namespace 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 | [Storable]
|
---|
74 | private bool scalingFinished = false;
|
---|
75 | #endregion
|
---|
76 |
|
---|
77 | [StorableConstructor]
|
---|
78 | private SelectionPressureAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
79 | private SelectionPressureAnalyzer(SelectionPressureAnalyzer original, Cloner cloner)
|
---|
80 | : base(original, cloner) {
|
---|
81 | cnt = original.cnt;
|
---|
82 | lastGeneration = original.lastGeneration;
|
---|
83 | selPressurePlot = (ScatterPlotHelper)original.selPressurePlot.Clone(cloner);
|
---|
84 | selIntensityPlot = (ScatterPlotHelper)original.selIntensityPlot.Clone(cloner);
|
---|
85 | SolutionQualityName = original.SolutionQualityName;
|
---|
86 | scalingFinished = original.scalingFinished;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public SelectionPressureAnalyzer()
|
---|
90 | : base() {
|
---|
91 | Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
|
---|
92 | Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
|
---|
93 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
|
---|
94 | Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
|
---|
95 | Parameters.Add(new LookupParameter<DoubleValue>("WorstKnownQuality", "The quality of the worst known solution of this problem."));
|
---|
96 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ParentsQuality", "The quality of the parent solutions."));
|
---|
97 | SolutionQualityName = "TSPTourLength";
|
---|
98 |
|
---|
99 | selPressurePlot = new ScatterPlotHelper(false, true, true, true);
|
---|
100 | selIntensityPlot = new ScatterPlotHelper(false, true, true, true);
|
---|
101 | }
|
---|
102 |
|
---|
103 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
104 | return new SelectionPressureAnalyzer(this, cloner);
|
---|
105 | }
|
---|
106 |
|
---|
107 | protected override void InitializeAction() {
|
---|
108 | selPressurePlot.InitializePlot(Results, "Selection Pressure", "Solution Index", "Quality Difference");
|
---|
109 | selIntensityPlot.InitializePlot(Results, "Selection Intensity", "Solution Index", "Scaled Quality Difference");
|
---|
110 | ParentsQualityParameter.ActualName = SolutionQualityName;
|
---|
111 |
|
---|
112 | Reset();
|
---|
113 | }
|
---|
114 |
|
---|
115 | public override IOperation Apply() {
|
---|
116 | Initialize();
|
---|
117 |
|
---|
118 | string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
|
---|
119 |
|
---|
120 | IScope oldPop = ReverseScopeTreeLookup("Remaining");
|
---|
121 | if (oldPop == null)
|
---|
122 | throw new Exception("Couldn't find the remaining scope");
|
---|
123 |
|
---|
124 | if (GenerationsParameter.ActualValue.Value != 0) {
|
---|
125 | if (GenerationsParameter.ActualValue.Value > lastGeneration) {
|
---|
126 | Reset();
|
---|
127 | }
|
---|
128 |
|
---|
129 | double oldPopQuality = 0.0;
|
---|
130 | List<double> qValues = new List<double>();
|
---|
131 | int popSize = 0;
|
---|
132 | foreach (IScope oldSolScope in oldPop.SubScopes) {
|
---|
133 | double curQuality = ((DoubleValue)oldSolScope.Variables[SolutionQualityName].Value).Value;
|
---|
134 | oldPopQuality += curQuality;
|
---|
135 | qValues.Add(curQuality);
|
---|
136 | popSize++;
|
---|
137 | }
|
---|
138 |
|
---|
139 | double quality = ParentsQualityParameter.ActualValue.Average(x => x.Value);
|
---|
140 |
|
---|
141 | if (WorstKnownQualityParameter.ActualValue != null && !scalingFinished) {
|
---|
142 | scalingFinished = true;
|
---|
143 | double bkQuality = BestKnownQualityParameter.ActualValue.Value;
|
---|
144 | double wkQuality = WorstKnownQualityParameter.ActualValue.Value;
|
---|
145 |
|
---|
146 | if (MaximizationParameter.ActualValue.Value) {
|
---|
147 | if (selPressurePlot.Max == double.MinValue) {
|
---|
148 | selPressurePlot.Max = bkQuality - wkQuality;
|
---|
149 | selPressurePlot.Min = 0;
|
---|
150 | selIntensityPlot.Max = bkQuality - wkQuality;
|
---|
151 | selIntensityPlot.Min = 0;
|
---|
152 | }
|
---|
153 | } else {
|
---|
154 | if (selPressurePlot.Min == double.MaxValue) {
|
---|
155 | selPressurePlot.Max = wkQuality - bkQuality;
|
---|
156 | selPressurePlot.Min = 0;
|
---|
157 | selIntensityPlot.Max = wkQuality - bkQuality;
|
---|
158 | selIntensityPlot.Min = 0;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | Point2D<double> popQualityPoint, selectionIntensityPoint;
|
---|
164 | if (MaximizationParameter.ActualValue.Value) {
|
---|
165 | popQualityPoint = new Point2D<double>(cnt, quality - (oldPopQuality / popSize));
|
---|
166 | selectionIntensityPoint = new Point2D<double>(cnt, (quality - (oldPopQuality / popSize) / qValues.StandardDeviation()));
|
---|
167 | } else {
|
---|
168 | popQualityPoint = new Point2D<double>(cnt, (oldPopQuality / popSize) - quality);
|
---|
169 | selectionIntensityPoint = new Point2D<double>(cnt, ((oldPopQuality / popSize) - quality) / qValues.StandardDeviation());
|
---|
170 | }
|
---|
171 |
|
---|
172 | selPressurePlot.AddPoint(curGenStr, popQualityPoint);
|
---|
173 | selIntensityPlot.AddPoint(curGenStr, selectionIntensityPoint);
|
---|
174 | }
|
---|
175 |
|
---|
176 | return base.Apply();
|
---|
177 | }
|
---|
178 |
|
---|
179 | private void Reset() {
|
---|
180 | cnt = 0;
|
---|
181 | lastGeneration = GenerationsParameter.ActualValue.Value;
|
---|
182 | }
|
---|
183 |
|
---|
184 | public override void ClearState() {
|
---|
185 | selPressurePlot.CleanUp();
|
---|
186 | selIntensityPlot.CleanUp();
|
---|
187 | scalingFinished = false;
|
---|
188 | }
|
---|
189 |
|
---|
190 | private IScope ReverseScopeTreeLookup(string scopeName) {
|
---|
191 | var currentScope = ExecutionContext.Scope;
|
---|
192 | while (currentScope != null) {
|
---|
193 | var scopes = currentScope.SubScopes.Where(x => x.Name == scopeName);
|
---|
194 | if (scopes.Count() > 0)
|
---|
195 | return scopes.First();
|
---|
196 |
|
---|
197 | currentScope = currentScope.Parent;
|
---|
198 | }
|
---|
199 | return null;
|
---|
200 | }
|
---|
201 | }
|
---|
202 | }
|
---|