Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Analysis/3.3/AlleleFrequencyAnalysis/AlleleFrequencyAnalyzer.cs @ 8283

Last change on this file since 8283 was 8283, checked in by swagner, 12 years ago

Worked on AlleleFrequencyAnalyzer (#1893)

File size: 16.1 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace 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  [StorableClass]
39  public abstract class AlleleFrequencyAnalyzer<T> : SingleSuccessorOperator, IAnalyzer 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(bool deserializing) : base(deserializing) { }
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        // calculate scatter plot of contained relevant alleles and relative quality
173        double avgContainedReleventAlleles = 0;
174        if (bestKnownAlleles != null) {
175          double qualityRange = Math.Abs(qualities.Max() - qualities.Min());
176          var points = solutions.Select((s, index) => new Point2D<double>(CalculateAlleles(s).Intersect(bestKnownAlleles, new AlleleIdEqualityComparer()).Count(),
177                                                                          Math.Abs(qualities[index] - qualities[bestIndex]) / qualityRange));
178          avgContainedReleventAlleles = points.Select(x => x.X).Average();
179          var plot = new ScatterPlot("Contained Alleles of Best Known Solution and Relative Solution Qualtiy", null);
180          plot.VisualProperties.XAxisTitle = "Contained Alleles of Best Known Solution";
181          plot.VisualProperties.YAxisTitle = "Relative Solution Quality";
182          plot.VisualProperties.XAxisMinimumAuto = false;
183          plot.VisualProperties.XAxisMinimumFixedValue = 0.0;
184          plot.VisualProperties.XAxisMaximumAuto = false;
185          plot.VisualProperties.XAxisMaximumFixedValue = bestKnownAlleles.Length;
186          plot.VisualProperties.YAxisMinimumAuto = false;
187          plot.VisualProperties.YAxisMinimumFixedValue = 0.0;
188          plot.VisualProperties.YAxisMaximumAuto = false;
189          plot.VisualProperties.YAxisMaximumFixedValue = 1.0;
190          var row = new ScatterPlotDataRow("Solutions of Current Generation", null, points);
191          row.VisualProperties.PointStyle = ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Circle;
192          row.VisualProperties.PointSize = 5;
193          plot.Rows.Add(row);
194
195          if (!results.ContainsKey("Scatter Plot"))
196            results.Add(new Result("Scatter Plot", plot));
197          else
198            results["Scatter Plot"].Value = plot;
199          if (storeHistory) {
200            if (!results.ContainsKey("Scatter Plot History")) {
201              results.Add(new Result("Scatter Plot History", new ScatterPlotHistory()));
202            }
203            ((ScatterPlotHistory)results["Scatter Plot History"].Value).Add(plot);
204          }
205        }
206
207        // store allele frequencies
208        AlleleFrequencyCollection frequenciesCollection = new AlleleFrequencyCollection(frequencies);
209        if (!results.ContainsKey("Allele Frequencies"))
210          results.Add(new Result("Allele Frequencies", frequenciesCollection));
211        else
212          results["Allele Frequencies"].Value = frequenciesCollection;
213
214        // store allele frequencies history
215        if (storeHistory) {
216          if (!results.ContainsKey("Allele Frequencies History")) {
217            AlleleFrequencyCollectionHistory history = new AlleleFrequencyCollectionHistory();
218            history.Add(frequenciesCollection);
219            results.Add(new Result("Allele Frequencies History", history));
220          } else {
221            ((AlleleFrequencyCollectionHistory)results["Allele Frequencies History"].Value).Add(frequenciesCollection);
222          }
223        }
224
225        // store alleles data table
226        DataTable allelesTable;
227        if (!results.ContainsKey("Alleles")) {
228          allelesTable = new DataTable("Alleles");
229          allelesTable.VisualProperties.XAxisTitle = "Iteration";
230          allelesTable.VisualProperties.YAxisTitle = "Number of Alleles";
231          allelesTable.VisualProperties.SecondYAxisTitle = "Number of Alleles";
232
233          allelesTable.Rows.Add(new DataRow("Unique Alleles"));
234          allelesTable.Rows["Unique Alleles"].VisualProperties.StartIndexZero = true;
235
236          allelesTable.Rows.Add(new DataRow("Unique Alleles of Best Known Solution", null));
237          allelesTable.Rows["Unique Alleles of Best Known Solution"].VisualProperties.SecondYAxis = true;
238          allelesTable.Rows["Unique Alleles of Best Known Solution"].VisualProperties.StartIndexZero = true;
239
240          allelesTable.Rows.Add(new DataRow("Fixed Alleles", null));
241          allelesTable.Rows["Fixed Alleles"].VisualProperties.SecondYAxis = true;
242          allelesTable.Rows["Fixed Alleles"].VisualProperties.StartIndexZero = true;
243
244          allelesTable.Rows.Add(new DataRow("Fixed Alleles of Best Known Solution", null));
245          allelesTable.Rows["Fixed Alleles of Best Known Solution"].VisualProperties.SecondYAxis = true;
246          allelesTable.Rows["Fixed Alleles of Best Known Solution"].VisualProperties.StartIndexZero = true;
247
248          allelesTable.Rows.Add(new DataRow("Lost Alleles of Best Known Solution", null));
249          allelesTable.Rows["Lost Alleles of Best Known Solution"].VisualProperties.SecondYAxis = true;
250          allelesTable.Rows["Lost Alleles of Best Known Solution"].VisualProperties.StartIndexZero = true;
251
252          allelesTable.Rows.Add(new DataRow("Average Contained Alleles of Best Known Solution", null));
253          allelesTable.Rows["Average Contained Alleles of Best Known Solution"].VisualProperties.SecondYAxis = true;
254          allelesTable.Rows["Average Contained Alleles of Best Known Solution"].VisualProperties.StartIndexZero = true;
255
256          results.Add(new Result("Alleles", allelesTable));
257        } else {
258          allelesTable = (DataTable)results["Alleles"].Value;
259        }
260
261        int fixedAllelesCount = frequenciesCollection.Where(x => x.Frequency == 1).Count();
262        var relevantAlleles = frequenciesCollection.Where(x => x.ContainedInBestKnownSolution);
263        int relevantAllelesCount = relevantAlleles.Count();
264        int fixedRelevantAllelesCount = relevantAlleles.Where(x => x.Frequency == 1).Count();
265        int lostRelevantAllelesCount = relevantAlleles.Where(x => x.Frequency == 0).Count();
266        int uniqueRelevantAllelesCount = relevantAllelesCount - lostRelevantAllelesCount;
267        allelesTable.Rows["Unique Alleles"].Values.Add(frequenciesCollection.Count);
268        allelesTable.Rows["Unique Alleles of Best Known Solution"].Values.Add(uniqueRelevantAllelesCount);
269        allelesTable.Rows["Fixed Alleles"].Values.Add(fixedAllelesCount);
270        allelesTable.Rows["Fixed Alleles of Best Known Solution"].Values.Add(fixedRelevantAllelesCount);
271        allelesTable.Rows["Lost Alleles of Best Known Solution"].Values.Add(lostRelevantAllelesCount);
272        allelesTable.Rows["Average Contained Alleles of Best Known Solution"].Values.Add(avgContainedReleventAlleles);
273
274        // store alleles values
275        if (!results.ContainsKey("Unique Alleles"))
276          results.Add(new Result("Unique Alleles", new DoubleValue(frequenciesCollection.Count)));
277        else
278          ((DoubleValue)results["Unique Alleles"].Value).Value = frequenciesCollection.Count;
279
280        if (!results.ContainsKey("Unique Alleles of Best Known Solution"))
281          results.Add(new Result("Unique Alleles of Best Known Solution", new DoubleValue(uniqueRelevantAllelesCount)));
282        else
283          ((DoubleValue)results["Unique Alleles of Best Known Solution"].Value).Value = uniqueRelevantAllelesCount;
284
285        if (!results.ContainsKey("Fixed Alleles"))
286          results.Add(new Result("Fixed Alleles", new DoubleValue(fixedAllelesCount)));
287        else
288          ((DoubleValue)results["Fixed Alleles"].Value).Value = fixedAllelesCount;
289
290        if (!results.ContainsKey("Fixed Alleles of Best Known Solution"))
291          results.Add(new Result("Fixed Alleles of Best Known Solution", new DoubleValue(fixedRelevantAllelesCount)));
292        else
293          ((DoubleValue)results["Fixed Alleles of Best Known Solution"].Value).Value = fixedRelevantAllelesCount;
294
295        if (!results.ContainsKey("Lost Alleles of Best Known Solution"))
296          results.Add(new Result("Lost Alleles of Best Known Solution", new DoubleValue(lostRelevantAllelesCount)));
297        else
298          ((DoubleValue)results["Lost Alleles of Best Known Solution"].Value).Value = lostRelevantAllelesCount;
299      }
300      return base.Apply();
301    }
302
303    protected abstract Allele[] CalculateAlleles(T solution);
304  }
305}
Note: See TracBrowser for help on using the repository browser.