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