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("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 | #endregion
|
---|
65 |
|
---|
66 | #region Properties
|
---|
67 | public ResultCollection Results {
|
---|
68 | get { return ResultsParameter.ActualValue; }
|
---|
69 | }
|
---|
70 | [Storable]
|
---|
71 | private ScatterPlotHelper populationDiversityPlot, populationQualityPlot, qualityPlot;
|
---|
72 | [Storable]
|
---|
73 | private int cnt = 0;
|
---|
74 | [Storable]
|
---|
75 | private int lastGeneration = 0;
|
---|
76 | #endregion
|
---|
77 |
|
---|
78 | [StorableConstructor]
|
---|
79 | private SolutionToPopulationAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
80 | private SolutionToPopulationAnalyzer(SolutionToPopulationAnalyzer original, Cloner cloner)
|
---|
81 | : base(original, cloner) {
|
---|
82 | cnt = original.cnt;
|
---|
83 | lastGeneration = original.lastGeneration;
|
---|
84 | populationDiversityPlot = (ScatterPlotHelper)populationDiversityPlot.Clone(cloner);
|
---|
85 | populationQualityPlot = (ScatterPlotHelper)populationQualityPlot.Clone(cloner);
|
---|
86 | qualityPlot = (ScatterPlotHelper)qualityPlot.Clone(cloner);
|
---|
87 | }
|
---|
88 |
|
---|
89 | public SolutionToPopulationAnalyzer()
|
---|
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 LookupParameter<DoubleValue>("Quality", "The evaluated quality of the child solution."));
|
---|
94 | QualityParameter.ActualName = "TSPTourLength";
|
---|
95 | Parameters.Add(new LookupParameter<IItem>("Solution"));
|
---|
96 | SolutionParameter.ActualName = "TSPTour";
|
---|
97 | Parameters.Add(new ValueParameter<ISingleObjectiveSolutionSimilarityCalculator>("SimilarityCalculator"));
|
---|
98 | Parameters.Add(new ValueParameter<StringValue>("ChartPostfix", new StringValue(string.Empty)));
|
---|
99 | Parameters.Add(new LookupParameter<ItemCollection<IItem>>("Operators", "The operators and items that the problem provides to the algorithms."));
|
---|
100 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
|
---|
101 |
|
---|
102 | populationDiversityPlot = new ScatterPlotHelper(false, true);
|
---|
103 | populationQualityPlot = new ScatterPlotHelper(false, true, true);
|
---|
104 | qualityPlot = new ScatterPlotHelper(false, true, true);
|
---|
105 | }
|
---|
106 |
|
---|
107 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
108 | return new SolutionToPopulationAnalyzer(this, cloner);
|
---|
109 | }
|
---|
110 |
|
---|
111 | protected override void InitializeAction() {
|
---|
112 | if (SimilarityCalculatorParameter.Value == null) {
|
---|
113 | SimilarityCalculatorParameter.Value = OperatorsParameter.ActualValue.OfType<ISingleObjectiveSolutionSimilarityCalculator>().FirstOrDefault();
|
---|
114 | }
|
---|
115 |
|
---|
116 | populationDiversityPlot.InitializePlot(Results, "Solution to Population Diversity " + ChartPostfixParameter.Value.Value, "Solution Index", "Diversity");
|
---|
117 | populationQualityPlot.InitializePlot(Results, "Solution Quality Difference to Population " + ChartPostfixParameter.Value.Value, "Solution Index", "Quality Difference");
|
---|
118 | qualityPlot.InitializePlot(Results, "Solution Quality " + ChartPostfixParameter.Value.Value, "Solution Index", "Quality");
|
---|
119 |
|
---|
120 | Reset();
|
---|
121 | }
|
---|
122 |
|
---|
123 | public override IOperation Apply() {
|
---|
124 | Initialize();
|
---|
125 |
|
---|
126 | string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
|
---|
127 | double quality = QualityParameter.ActualValue.Value;
|
---|
128 | IItem solution = SolutionParameter.ActualValue;
|
---|
129 | string qualityVariableName = QualityParameter.ActualName;
|
---|
130 | string solutionVariableName = SolutionParameter.ActualName;
|
---|
131 | ISingleObjectiveSolutionSimilarityCalculator simCalc = SimilarityCalculatorParameter.Value;
|
---|
132 | Scope artificialSolutionScope = new Scope();
|
---|
133 | artificialSolutionScope.Variables.Add(new Variable(solutionVariableName, solution));
|
---|
134 |
|
---|
135 |
|
---|
136 | IScope oldPop = ReverseScopeTreeLookup("Remaining");
|
---|
137 | if (oldPop == null)
|
---|
138 | throw new Exception("Couldn't find the remaining scope");
|
---|
139 |
|
---|
140 | if (GenerationsParameter.ActualValue.Value != 0) {
|
---|
141 | if (GenerationsParameter.ActualValue.Value > lastGeneration) {
|
---|
142 | Reset();
|
---|
143 | }
|
---|
144 |
|
---|
145 | double oldPopQuality = 0.0;
|
---|
146 | double solToPopDiversity = 0.0;
|
---|
147 | int popSize = 0;
|
---|
148 | foreach (IScope oldSolScope in oldPop.SubScopes) {
|
---|
149 | double curQuality = ((DoubleValue)oldSolScope.Variables[qualityVariableName].Value).Value;
|
---|
150 | IItem curSol = oldSolScope.Variables[solutionVariableName].Value;
|
---|
151 | oldPopQuality += curQuality;
|
---|
152 | solToPopDiversity += simCalc.CalculateSolutionSimilarity(artificialSolutionScope, oldSolScope);
|
---|
153 | popSize++;
|
---|
154 | }
|
---|
155 |
|
---|
156 | Point2D<double> popQualityPoint;
|
---|
157 | if (MaximizationParameter.ActualValue.Value) {
|
---|
158 | popQualityPoint = new Point2D<double>(cnt, quality - (oldPopQuality / popSize));
|
---|
159 | } else {
|
---|
160 | popQualityPoint = new Point2D<double>(cnt, (oldPopQuality / popSize) - quality);
|
---|
161 | }
|
---|
162 |
|
---|
163 | Point2D<double> solQuality = new Point2D<double>(cnt, quality);
|
---|
164 | Point2D<double> diversityPoint = new Point2D<double>(cnt++, solToPopDiversity / popSize);
|
---|
165 |
|
---|
166 | populationDiversityPlot.AddPoint(curGenStr, diversityPoint);
|
---|
167 | populationQualityPlot.AddPoint(curGenStr, popQualityPoint);
|
---|
168 | qualityPlot.AddPoint(curGenStr, solQuality);
|
---|
169 | }
|
---|
170 |
|
---|
171 | return base.Apply();
|
---|
172 | }
|
---|
173 |
|
---|
174 | private void Reset() {
|
---|
175 | cnt = 0;
|
---|
176 | lastGeneration = GenerationsParameter.ActualValue.Value;
|
---|
177 | }
|
---|
178 |
|
---|
179 | public override void ClearState() {
|
---|
180 | populationQualityPlot.CleanUp();
|
---|
181 | populationDiversityPlot.CleanUp();
|
---|
182 | qualityPlot.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 | }
|
---|