1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HEAL.Attic;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Analysis {
|
---|
34 | /// <summary>
|
---|
35 | /// An operator for analyzing the frequency of alleles.
|
---|
36 | /// </summary>
|
---|
37 | [Item("AlleleFrequencyAnalyzer", "An operator for analyzing the frequency of alleles.")]
|
---|
38 | [StorableType("FB4ABEB4-E94E-4B79-9356-8AC48D8F77BE")]
|
---|
39 | public abstract class AlleleFrequencyAnalyzer<T> : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator where T : class, IItem {
|
---|
40 | public virtual bool EnabledByDefault {
|
---|
41 | get { return false; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public LookupParameter<BoolValue> MaximizationParameter {
|
---|
45 | get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
46 | }
|
---|
47 | public ScopeTreeLookupParameter<T> SolutionParameter {
|
---|
48 | get { return (ScopeTreeLookupParameter<T>)Parameters["Solution"]; }
|
---|
49 | }
|
---|
50 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
51 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
52 | }
|
---|
53 | public LookupParameter<T> BestKnownSolutionParameter {
|
---|
54 | get { return (LookupParameter<T>)Parameters["BestKnownSolution"]; }
|
---|
55 | }
|
---|
56 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
57 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
58 | }
|
---|
59 | public ValueParameter<BoolValue> StoreHistoryParameter {
|
---|
60 | get { return (ValueParameter<BoolValue>)Parameters["StoreHistory"]; }
|
---|
61 | }
|
---|
62 | public ValueParameter<IntValue> UpdateIntervalParameter {
|
---|
63 | get { return (ValueParameter<IntValue>)Parameters["UpdateInterval"]; }
|
---|
64 | }
|
---|
65 | public LookupParameter<IntValue> UpdateCounterParameter {
|
---|
66 | get { return (LookupParameter<IntValue>)Parameters["UpdateCounter"]; }
|
---|
67 | }
|
---|
68 |
|
---|
69 | [StorableConstructor]
|
---|
70 | protected AlleleFrequencyAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
71 | protected AlleleFrequencyAnalyzer(AlleleFrequencyAnalyzer<T> original, Cloner cloner) : base(original, cloner) { }
|
---|
72 | public AlleleFrequencyAnalyzer()
|
---|
73 | : base() {
|
---|
74 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
|
---|
75 | Parameters.Add(new ScopeTreeLookupParameter<T>("Solution", "The solutions whose alleles should be analyzed."));
|
---|
76 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the solutions which should be analyzed."));
|
---|
77 | Parameters.Add(new LookupParameter<T>("BestKnownSolution", "The best known solution."));
|
---|
78 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the allele frequency analysis results should be stored."));
|
---|
79 | Parameters.Add(new ValueParameter<BoolValue>("StoreHistory", "True if the history of the allele frequency analysis should be stored.", new BoolValue(false)));
|
---|
80 | Parameters.Add(new ValueParameter<IntValue>("UpdateInterval", "The interval in which the allele frequency analysis should be applied.", new IntValue(1)));
|
---|
81 | Parameters.Add(new LookupParameter<IntValue>("UpdateCounter", "The value which counts how many times the operator was called since the last update.", "AlleleFrequencyAnalyzerUpdateCounter"));
|
---|
82 |
|
---|
83 | MaximizationParameter.Hidden = true;
|
---|
84 | SolutionParameter.Hidden = true;
|
---|
85 | QualityParameter.Hidden = true;
|
---|
86 | BestKnownSolutionParameter.Hidden = true;
|
---|
87 | ResultsParameter.Hidden = true;
|
---|
88 | UpdateCounterParameter.Hidden = true;
|
---|
89 | }
|
---|
90 |
|
---|
91 | #region Equality Comparers
|
---|
92 | private class AlleleIdEqualityComparer : IEqualityComparer<Allele> {
|
---|
93 | public bool Equals(Allele x, Allele y) {
|
---|
94 | return x.Id == y.Id;
|
---|
95 | }
|
---|
96 | public int GetHashCode(Allele obj) {
|
---|
97 | return obj.Id.GetHashCode();
|
---|
98 | }
|
---|
99 | }
|
---|
100 | private class AlleleFrequencyIdEqualityComparer : IEqualityComparer<AlleleFrequency> {
|
---|
101 | public bool Equals(AlleleFrequency x, AlleleFrequency y) {
|
---|
102 | return x.Id == y.Id;
|
---|
103 | }
|
---|
104 | public int GetHashCode(AlleleFrequency obj) {
|
---|
105 | return obj.Id.GetHashCode();
|
---|
106 | }
|
---|
107 | }
|
---|
108 | #endregion
|
---|
109 |
|
---|
110 | public override IOperation Apply() {
|
---|
111 | int updateInterval = UpdateIntervalParameter.Value.Value;
|
---|
112 | IntValue updateCounter = UpdateCounterParameter.ActualValue;
|
---|
113 | if (updateCounter == null) {
|
---|
114 | updateCounter = new IntValue(updateInterval);
|
---|
115 | UpdateCounterParameter.ActualValue = updateCounter;
|
---|
116 | } else updateCounter.Value++;
|
---|
117 |
|
---|
118 | if (updateCounter.Value == updateInterval) {
|
---|
119 | updateCounter.Value = 0;
|
---|
120 |
|
---|
121 | bool max = MaximizationParameter.ActualValue.Value;
|
---|
122 | ItemArray<T> solutions = SolutionParameter.ActualValue;
|
---|
123 | double[] qualities = QualityParameter.ActualValue.Select(x => x.Value).ToArray();
|
---|
124 | T bestKnownSolution = BestKnownSolutionParameter.ActualValue;
|
---|
125 | bool storeHistory = StoreHistoryParameter.Value.Value;
|
---|
126 |
|
---|
127 | // calculate index of current best solution
|
---|
128 | int bestIndex = -1;
|
---|
129 | if (!max) {
|
---|
130 | bestIndex = qualities
|
---|
131 | .Select((x, i) => new { Index = i, Value = x })
|
---|
132 | .OrderBy(x => x.Value)
|
---|
133 | .First().Index;
|
---|
134 | } else {
|
---|
135 | bestIndex = qualities
|
---|
136 | .Select((x, i) => new { Index = i, Value = x })
|
---|
137 | .OrderByDescending(x => x.Value)
|
---|
138 | .First().Index;
|
---|
139 | }
|
---|
140 |
|
---|
141 | // calculate allels of current best and (if available) best known solution
|
---|
142 | Allele[] bestAlleles = CalculateAlleles(solutions[bestIndex]);
|
---|
143 | Allele[] bestKnownAlleles = null;
|
---|
144 | if (bestKnownSolution != null)
|
---|
145 | bestKnownAlleles = CalculateAlleles(bestKnownSolution);
|
---|
146 |
|
---|
147 | // calculate allele frequencies
|
---|
148 | var frequencies = solutions.SelectMany((s, index) => CalculateAlleles(s).Select(a => new { Allele = a, Quality = qualities[index] })).
|
---|
149 | GroupBy(x => x.Allele.Id).
|
---|
150 | Select(x => new AlleleFrequency(x.Key,
|
---|
151 | x.Count() / ((double)solutions.Length),
|
---|
152 | x.Average(a => a.Allele.Impact),
|
---|
153 | x.Average(a => a.Quality),
|
---|
154 | bestKnownAlleles == null ? false : bestKnownAlleles.Any(a => a.Id == x.Key),
|
---|
155 | bestAlleles.Any(a => a.Id == x.Key)));
|
---|
156 |
|
---|
157 | // calculate dummy allele frequencies of alleles of best known solution which did not occur
|
---|
158 | if (bestKnownAlleles != null) {
|
---|
159 | var bestKnownFrequencies = bestKnownAlleles.Select(x => new AlleleFrequency(x.Id, 0, x.Impact, 0, true, false)).Except(frequencies, new AlleleFrequencyIdEqualityComparer());
|
---|
160 | frequencies = frequencies.Concat(bestKnownFrequencies);
|
---|
161 | }
|
---|
162 |
|
---|
163 | // fetch results collection
|
---|
164 | ResultCollection results;
|
---|
165 | if (!ResultsParameter.ActualValue.ContainsKey(Name + " Results")) {
|
---|
166 | results = new ResultCollection();
|
---|
167 | ResultsParameter.ActualValue.Add(new Result(Name + " Results", results));
|
---|
168 | } else {
|
---|
169 | results = (ResultCollection)ResultsParameter.ActualValue[Name + " Results"].Value;
|
---|
170 | }
|
---|
171 |
|
---|
172 | // store allele frequencies
|
---|
173 | AlleleFrequencyCollection frequenciesCollection = new AlleleFrequencyCollection(frequencies);
|
---|
174 | if (!results.ContainsKey("Allele Frequencies"))
|
---|
175 | results.Add(new Result("Allele Frequencies", frequenciesCollection));
|
---|
176 | else
|
---|
177 | results["Allele Frequencies"].Value = frequenciesCollection;
|
---|
178 |
|
---|
179 | // store allele frequencies history
|
---|
180 | if (storeHistory) {
|
---|
181 | if (!results.ContainsKey("Allele Frequencies History")) {
|
---|
182 | AlleleFrequencyCollectionHistory history = new AlleleFrequencyCollectionHistory();
|
---|
183 | history.Add(frequenciesCollection);
|
---|
184 | results.Add(new Result("Allele Frequencies History", history));
|
---|
185 | } else {
|
---|
186 | ((AlleleFrequencyCollectionHistory)results["Allele Frequencies History"].Value).Add(frequenciesCollection);
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | // store alleles data table
|
---|
191 | DataTable allelesTable;
|
---|
192 | if (!results.ContainsKey("Alleles")) {
|
---|
193 | allelesTable = new DataTable("Alleles");
|
---|
194 | allelesTable.VisualProperties.XAxisTitle = "Iteration";
|
---|
195 | allelesTable.VisualProperties.YAxisTitle = "Number of Alleles";
|
---|
196 | allelesTable.VisualProperties.SecondYAxisTitle = "Number of Alleles";
|
---|
197 |
|
---|
198 | allelesTable.Rows.Add(new DataRow("Unique Alleles"));
|
---|
199 | allelesTable.Rows["Unique Alleles"].VisualProperties.StartIndexZero = true;
|
---|
200 |
|
---|
201 | allelesTable.Rows.Add(new DataRow("Unique Alleles of Best Known Solution", null));
|
---|
202 | allelesTable.Rows["Unique Alleles of Best Known Solution"].VisualProperties.SecondYAxis = true;
|
---|
203 | allelesTable.Rows["Unique Alleles of Best Known Solution"].VisualProperties.StartIndexZero = true;
|
---|
204 |
|
---|
205 | allelesTable.Rows.Add(new DataRow("Fixed Alleles", null));
|
---|
206 | allelesTable.Rows["Fixed Alleles"].VisualProperties.SecondYAxis = true;
|
---|
207 | allelesTable.Rows["Fixed Alleles"].VisualProperties.StartIndexZero = true;
|
---|
208 |
|
---|
209 | allelesTable.Rows.Add(new DataRow("Fixed Alleles of Best Known Solution", null));
|
---|
210 | allelesTable.Rows["Fixed Alleles of Best Known Solution"].VisualProperties.SecondYAxis = true;
|
---|
211 | allelesTable.Rows["Fixed Alleles of Best Known Solution"].VisualProperties.StartIndexZero = true;
|
---|
212 |
|
---|
213 | allelesTable.Rows.Add(new DataRow("Lost Alleles of Best Known Solution", null));
|
---|
214 | allelesTable.Rows["Lost Alleles of Best Known Solution"].VisualProperties.SecondYAxis = true;
|
---|
215 | allelesTable.Rows["Lost Alleles of Best Known Solution"].VisualProperties.StartIndexZero = true;
|
---|
216 |
|
---|
217 | results.Add(new Result("Alleles", allelesTable));
|
---|
218 | } else {
|
---|
219 | allelesTable = (DataTable)results["Alleles"].Value;
|
---|
220 | }
|
---|
221 |
|
---|
222 | int fixedAllelesCount = frequenciesCollection.Where(x => x.Frequency == 1).Count();
|
---|
223 | var relevantAlleles = frequenciesCollection.Where(x => x.ContainedInBestKnownSolution);
|
---|
224 | int relevantAllelesCount = relevantAlleles.Count();
|
---|
225 | int fixedRelevantAllelesCount = relevantAlleles.Where(x => x.Frequency == 1).Count();
|
---|
226 | int lostRelevantAllelesCount = relevantAlleles.Where(x => x.Frequency == 0).Count();
|
---|
227 | int uniqueRelevantAllelesCount = relevantAllelesCount - lostRelevantAllelesCount;
|
---|
228 | allelesTable.Rows["Unique Alleles"].Values.Add(frequenciesCollection.Count);
|
---|
229 | allelesTable.Rows["Unique Alleles of Best Known Solution"].Values.Add(uniqueRelevantAllelesCount);
|
---|
230 | allelesTable.Rows["Fixed Alleles"].Values.Add(fixedAllelesCount);
|
---|
231 | allelesTable.Rows["Fixed Alleles of Best Known Solution"].Values.Add(fixedRelevantAllelesCount);
|
---|
232 | allelesTable.Rows["Lost Alleles of Best Known Solution"].Values.Add(lostRelevantAllelesCount);
|
---|
233 |
|
---|
234 | // store alleles values
|
---|
235 | if (!results.ContainsKey("Unique Alleles"))
|
---|
236 | results.Add(new Result("Unique Alleles", new DoubleValue(frequenciesCollection.Count)));
|
---|
237 | else
|
---|
238 | ((DoubleValue)results["Unique Alleles"].Value).Value = frequenciesCollection.Count;
|
---|
239 |
|
---|
240 | if (!results.ContainsKey("Unique Alleles of Best Known Solution"))
|
---|
241 | results.Add(new Result("Unique Alleles of Best Known Solution", new DoubleValue(uniqueRelevantAllelesCount)));
|
---|
242 | else
|
---|
243 | ((DoubleValue)results["Unique Alleles of Best Known Solution"].Value).Value = uniqueRelevantAllelesCount;
|
---|
244 |
|
---|
245 | if (!results.ContainsKey("Fixed Alleles"))
|
---|
246 | results.Add(new Result("Fixed Alleles", new DoubleValue(fixedAllelesCount)));
|
---|
247 | else
|
---|
248 | ((DoubleValue)results["Fixed Alleles"].Value).Value = fixedAllelesCount;
|
---|
249 |
|
---|
250 | if (!results.ContainsKey("Fixed Alleles of Best Known Solution"))
|
---|
251 | results.Add(new Result("Fixed Alleles of Best Known Solution", new DoubleValue(fixedRelevantAllelesCount)));
|
---|
252 | else
|
---|
253 | ((DoubleValue)results["Fixed Alleles of Best Known Solution"].Value).Value = fixedRelevantAllelesCount;
|
---|
254 |
|
---|
255 | if (!results.ContainsKey("Lost Alleles of Best Known Solution"))
|
---|
256 | results.Add(new Result("Lost Alleles of Best Known Solution", new DoubleValue(lostRelevantAllelesCount)));
|
---|
257 | else
|
---|
258 | ((DoubleValue)results["Lost Alleles of Best Known Solution"].Value).Value = lostRelevantAllelesCount;
|
---|
259 |
|
---|
260 | // calculate contained alleles of best known solution and relative quality
|
---|
261 | if (bestKnownAlleles != null) {
|
---|
262 | double qualityRange = Math.Abs(qualities.Max() - qualities.Min());
|
---|
263 | var points = solutions.Select((s, index) => new Point2D<double>(CalculateAlleles(s).Intersect(bestKnownAlleles, new AlleleIdEqualityComparer()).Count(),
|
---|
264 | Math.Abs(qualities[index] - qualities[bestIndex]) / qualityRange));
|
---|
265 | var avgContainedReleventAlleles = points.Select(x => x.X).Average();
|
---|
266 |
|
---|
267 | var plot = new ScatterPlot("Contained Alleles of Best Known Solution and Relative Solution Qualtiy", null);
|
---|
268 | plot.VisualProperties.XAxisTitle = "Contained Alleles of Best Known Solution";
|
---|
269 | plot.VisualProperties.YAxisTitle = "Relative Solution Quality";
|
---|
270 | plot.VisualProperties.XAxisMinimumAuto = false;
|
---|
271 | plot.VisualProperties.XAxisMinimumFixedValue = 0.0;
|
---|
272 | plot.VisualProperties.XAxisMaximumAuto = false;
|
---|
273 | plot.VisualProperties.XAxisMaximumFixedValue = bestKnownAlleles.Length;
|
---|
274 | plot.VisualProperties.YAxisMinimumAuto = false;
|
---|
275 | plot.VisualProperties.YAxisMinimumFixedValue = 0.0;
|
---|
276 | plot.VisualProperties.YAxisMaximumAuto = false;
|
---|
277 | plot.VisualProperties.YAxisMaximumFixedValue = 1.0;
|
---|
278 | var row = new ScatterPlotDataRow("Solutions of Current Generation", null, points);
|
---|
279 | row.VisualProperties.PointStyle = ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Circle;
|
---|
280 | row.VisualProperties.PointSize = 5;
|
---|
281 | plot.Rows.Add(row);
|
---|
282 |
|
---|
283 | if (!results.ContainsKey("Scatter Plot"))
|
---|
284 | results.Add(new Result("Scatter Plot", plot));
|
---|
285 | else
|
---|
286 | results["Scatter Plot"].Value = plot;
|
---|
287 | if (storeHistory) {
|
---|
288 | if (!results.ContainsKey("Scatter Plot History")) {
|
---|
289 | results.Add(new Result("Scatter Plot History", new ScatterPlotHistory()));
|
---|
290 | }
|
---|
291 | ((ScatterPlotHistory)results["Scatter Plot History"].Value).Add(plot);
|
---|
292 | }
|
---|
293 |
|
---|
294 | if (!allelesTable.Rows.ContainsKey("Average Contained Alleles of Best Known Solution")) {
|
---|
295 | allelesTable.Rows.Add(new DataRow("Average Contained Alleles of Best Known Solution", null));
|
---|
296 | allelesTable.Rows["Average Contained Alleles of Best Known Solution"].VisualProperties.SecondYAxis = true;
|
---|
297 | allelesTable.Rows["Average Contained Alleles of Best Known Solution"].VisualProperties.StartIndexZero = true;
|
---|
298 | }
|
---|
299 | allelesTable.Rows["Average Contained Alleles of Best Known Solution"].Values.Add(avgContainedReleventAlleles);
|
---|
300 |
|
---|
301 | if (!results.ContainsKey("Average Contained Alleles of Best Known Solution"))
|
---|
302 | results.Add(new Result("Average Contained Alleles of Best Known Solution", new DoubleValue(avgContainedReleventAlleles)));
|
---|
303 | else
|
---|
304 | ((DoubleValue)results["Average Contained Alleles of Best Known Solution"].Value).Value = avgContainedReleventAlleles;
|
---|
305 | }
|
---|
306 | }
|
---|
307 | return base.Apply();
|
---|
308 | }
|
---|
309 |
|
---|
310 | protected abstract Allele[] CalculateAlleles(T solution);
|
---|
311 | }
|
---|
312 | }
|
---|