1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Analysis {
|
---|
32 | /// <summary>
|
---|
33 | /// An operator for analyzing the frequency of alleles.
|
---|
34 | /// </summary>
|
---|
35 | [Item("AlleleFrequencyAnalyzer", "An operator for analyzing the frequency of alleles.")]
|
---|
36 | [StorableClass]
|
---|
37 | public abstract class AlleleFrequencyAnalyzer<T> : SingleSuccessorOperator, IAnalyzer where T : class, IItem {
|
---|
38 | public LookupParameter<BoolValue> MaximizationParameter {
|
---|
39 | get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
40 | }
|
---|
41 | public ScopeTreeLookupParameter<T> SolutionParameter {
|
---|
42 | get { return (ScopeTreeLookupParameter<T>)Parameters["Solution"]; }
|
---|
43 | }
|
---|
44 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
45 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
46 | }
|
---|
47 | public LookupParameter<T> BestKnownSolutionParameter {
|
---|
48 | get { return (LookupParameter<T>)Parameters["BestKnownSolution"]; }
|
---|
49 | }
|
---|
50 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
51 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
52 | }
|
---|
53 | public ValueParameter<BoolValue> StoreAlleleFrequenciesHistoryParameter {
|
---|
54 | get { return (ValueParameter<BoolValue>)Parameters["StoreAlleleFrequenciesHistory"]; }
|
---|
55 | }
|
---|
56 | public ValueParameter<IntValue> UpdateIntervalParameter {
|
---|
57 | get { return (ValueParameter<IntValue>)Parameters["UpdateInterval"]; }
|
---|
58 | }
|
---|
59 | public LookupParameter<IntValue> UpdateCounterParameter {
|
---|
60 | get { return (LookupParameter<IntValue>)Parameters["UpdateCounter"]; }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public AlleleFrequencyAnalyzer()
|
---|
64 | : base() {
|
---|
65 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
|
---|
66 | Parameters.Add(new ScopeTreeLookupParameter<T>("Solution", "The solutions whose alleles should be analyzed."));
|
---|
67 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the solutions which should be analyzed."));
|
---|
68 | Parameters.Add(new LookupParameter<T>("BestKnownSolution", "The best known solution."));
|
---|
69 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the allele frequency analysis results should be stored."));
|
---|
70 | Parameters.Add(new ValueParameter<BoolValue>("StoreAlleleFrequenciesHistory", "True if the history of all allele frequencies should be stored.", new BoolValue(false)));
|
---|
71 | Parameters.Add(new ValueParameter<IntValue>("UpdateInterval", "The interval in which the allele frequency analysis should be applied.", new IntValue(1)));
|
---|
72 | Parameters.Add(new LookupParameter<IntValue>("UpdateCounter", "The value which counts how many times the operator was called since the last update.", "AlleleFrequencyAnalyzerUpdateCounter"));
|
---|
73 | }
|
---|
74 |
|
---|
75 | #region AlleleFrequencyIdEqualityComparer
|
---|
76 | private class AlleleFrequencyIdEqualityComparer : IEqualityComparer<AlleleFrequency> {
|
---|
77 | public bool Equals(AlleleFrequency x, AlleleFrequency y) {
|
---|
78 | return x.Id == y.Id;
|
---|
79 | }
|
---|
80 | public int GetHashCode(AlleleFrequency obj) {
|
---|
81 | return obj.Id.GetHashCode();
|
---|
82 | }
|
---|
83 | }
|
---|
84 | #endregion
|
---|
85 |
|
---|
86 | public override IOperation Apply() {
|
---|
87 | int updateInterval = UpdateIntervalParameter.Value.Value;
|
---|
88 | IntValue updateCounter = UpdateCounterParameter.ActualValue;
|
---|
89 | if (updateCounter == null) {
|
---|
90 | updateCounter = new IntValue(updateInterval);
|
---|
91 | UpdateCounterParameter.ActualValue = updateCounter;
|
---|
92 | } else updateCounter.Value++;
|
---|
93 |
|
---|
94 | if (updateCounter.Value == updateInterval) {
|
---|
95 | updateCounter.Value = 0;
|
---|
96 |
|
---|
97 | bool max = MaximizationParameter.ActualValue.Value;
|
---|
98 | ItemArray<T> solutions = SolutionParameter.ActualValue;
|
---|
99 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
100 | T bestKnownSolution = BestKnownSolutionParameter.ActualValue;
|
---|
101 | bool storeHistory = StoreAlleleFrequenciesHistoryParameter.Value.Value;
|
---|
102 |
|
---|
103 | // calculate index of current best solution
|
---|
104 | int bestIndex = -1;
|
---|
105 | if (!max) bestIndex = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
|
---|
106 | else bestIndex = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
|
---|
107 |
|
---|
108 | // calculate allels of current best and (if available) best known solution
|
---|
109 | Allele[] bestAlleles = CalculateAlleles(solutions[bestIndex]);
|
---|
110 | Allele[] bestKnownAlleles = null;
|
---|
111 | if (bestKnownSolution != null)
|
---|
112 | bestKnownAlleles = CalculateAlleles(bestKnownSolution);
|
---|
113 |
|
---|
114 | // calculate allele frequencies
|
---|
115 | var frequencies = solutions.SelectMany((s, index) => CalculateAlleles(s).Select(a => new { Allele = a, Quality = qualities[index] })).
|
---|
116 | GroupBy(x => x.Allele.Id).
|
---|
117 | Select(x => new AlleleFrequency(x.Key,
|
---|
118 | x.Count() / ((double)solutions.Length),
|
---|
119 | x.Average(a => a.Allele.Impact),
|
---|
120 | x.Average(a => a.Quality.Value),
|
---|
121 | bestKnownAlleles == null ? false : bestKnownAlleles.Any(a => a.Id == x.Key),
|
---|
122 | bestAlleles.Any(a => a.Id == x.Key)));
|
---|
123 |
|
---|
124 | // calculate dummy allele frequencies of alleles of best known solution which did not occur
|
---|
125 | if (bestKnownAlleles != null) {
|
---|
126 | var bestKnownFrequencies = bestKnownAlleles.Select(x => new AlleleFrequency(x.Id, 0, x.Impact, 0, true, false)).Except(frequencies, new AlleleFrequencyIdEqualityComparer());
|
---|
127 | frequencies = frequencies.Concat(bestKnownFrequencies);
|
---|
128 | }
|
---|
129 |
|
---|
130 | // fetch results collection
|
---|
131 | ResultCollection results;
|
---|
132 | if (!ResultsParameter.ActualValue.ContainsKey("Allele Frequency Analysis Results")) {
|
---|
133 | results = new ResultCollection();
|
---|
134 | ResultsParameter.ActualValue.Add(new Result("Allele Frequency Analysis Results", results));
|
---|
135 | } else {
|
---|
136 | results = (ResultCollection)ResultsParameter.ActualValue["Allele Frequency Analysis Results"].Value;
|
---|
137 | }
|
---|
138 |
|
---|
139 | // store allele frequencies
|
---|
140 | AlleleFrequencyCollection frequenciesCollection = new AlleleFrequencyCollection(frequencies);
|
---|
141 | if (!results.ContainsKey("Allele Frequencies"))
|
---|
142 | results.Add(new Result("Allele Frequencies", frequenciesCollection));
|
---|
143 | else
|
---|
144 | results["Allele Frequencies"].Value = frequenciesCollection;
|
---|
145 |
|
---|
146 | // store allele frequencies history
|
---|
147 | if (storeHistory) {
|
---|
148 | if (!results.ContainsKey("Allele Frequencies History")) {
|
---|
149 | AlleleFrequencyCollectionHistory history = new AlleleFrequencyCollectionHistory();
|
---|
150 | history.Add(frequenciesCollection);
|
---|
151 | results.Add(new Result("Allele Frequencies History", history));
|
---|
152 | } else {
|
---|
153 | ((AlleleFrequencyCollectionHistory)results["Allele Frequencies History"].Value).Add(frequenciesCollection);
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | // store alleles data table
|
---|
158 | DataTable allelesTable;
|
---|
159 | if (!results.ContainsKey("Alleles")) {
|
---|
160 | allelesTable = new DataTable("Alleles");
|
---|
161 | results.Add(new Result("Alleles", allelesTable));
|
---|
162 | allelesTable.Rows.Add(new DataRow("Unique Alleles"));
|
---|
163 | DataRowVisualProperties visualProperties = new DataRowVisualProperties();
|
---|
164 | visualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Line;
|
---|
165 | visualProperties.SecondYAxis = true;
|
---|
166 | visualProperties.StartIndexZero = true;
|
---|
167 | allelesTable.Rows.Add(new DataRow("Unique Alleles of Best Known Solution", null, visualProperties));
|
---|
168 | allelesTable.Rows.Add(new DataRow("Fixed Alleles", null, visualProperties));
|
---|
169 | allelesTable.Rows.Add(new DataRow("Fixed Alleles of Best Known Solution", null, visualProperties));
|
---|
170 | allelesTable.Rows.Add(new DataRow("Lost Alleles of Best Known Solution", null, visualProperties));
|
---|
171 | } else {
|
---|
172 | allelesTable = (DataTable)results["Alleles"].Value;
|
---|
173 | }
|
---|
174 |
|
---|
175 | int fixedAllelesCount = frequenciesCollection.Where(x => x.Frequency == 1).Count();
|
---|
176 | var relevantAlleles = frequenciesCollection.Where(x => x.ContainedInBestKnownSolution);
|
---|
177 | int relevantAllelesCount = relevantAlleles.Count();
|
---|
178 | int fixedRelevantAllelesCount = relevantAlleles.Where(x => x.Frequency == 1).Count();
|
---|
179 | int lostRelevantAllelesCount = relevantAlleles.Where(x => x.Frequency == 0).Count();
|
---|
180 | int uniqueRelevantAllelesCount = relevantAllelesCount - lostRelevantAllelesCount;
|
---|
181 | allelesTable.Rows["Unique Alleles"].Values.Add(frequenciesCollection.Count);
|
---|
182 | allelesTable.Rows["Unique Alleles of Best Known Solution"].Values.Add(uniqueRelevantAllelesCount);
|
---|
183 | allelesTable.Rows["Fixed Alleles"].Values.Add(fixedAllelesCount);
|
---|
184 | allelesTable.Rows["Fixed Alleles of Best Known Solution"].Values.Add(fixedRelevantAllelesCount);
|
---|
185 | allelesTable.Rows["Lost Alleles of Best Known Solution"].Values.Add(lostRelevantAllelesCount);
|
---|
186 | }
|
---|
187 | return base.Apply();
|
---|
188 | }
|
---|
189 |
|
---|
190 | protected abstract Allele[] CalculateAlleles(T solution);
|
---|
191 | }
|
---|
192 | }
|
---|