[4623] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4623] | 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 |
|
---|
[4639] | 22 | using System.Collections.Generic;
|
---|
[4623] | 23 | using System.Linq;
|
---|
[4722] | 24 | using HeuristicLab.Common;
|
---|
[4623] | 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Analysis {
|
---|
| 33 | /// <summary>
|
---|
| 34 | /// An operator for analyzing the frequency of alleles.
|
---|
| 35 | /// </summary>
|
---|
| 36 | [Item("AlleleFrequencyAnalyzer", "An operator for analyzing the frequency of alleles.")]
|
---|
| 37 | [StorableClass]
|
---|
| 38 | public abstract class AlleleFrequencyAnalyzer<T> : SingleSuccessorOperator, IAnalyzer where T : class, IItem {
|
---|
| 39 | public LookupParameter<BoolValue> MaximizationParameter {
|
---|
| 40 | get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 41 | }
|
---|
| 42 | public ScopeTreeLookupParameter<T> SolutionParameter {
|
---|
| 43 | get { return (ScopeTreeLookupParameter<T>)Parameters["Solution"]; }
|
---|
| 44 | }
|
---|
| 45 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 46 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 47 | }
|
---|
[4631] | 48 | public LookupParameter<T> BestKnownSolutionParameter {
|
---|
| 49 | get { return (LookupParameter<T>)Parameters["BestKnownSolution"]; }
|
---|
| 50 | }
|
---|
[4623] | 51 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
| 52 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
| 53 | }
|
---|
[4704] | 54 | public ValueParameter<BoolValue> StoreHistoryParameter {
|
---|
| 55 | get { return (ValueParameter<BoolValue>)Parameters["StoreHistory"]; }
|
---|
[4623] | 56 | }
|
---|
[4631] | 57 | public ValueParameter<IntValue> UpdateIntervalParameter {
|
---|
| 58 | get { return (ValueParameter<IntValue>)Parameters["UpdateInterval"]; }
|
---|
[4623] | 59 | }
|
---|
[4631] | 60 | public LookupParameter<IntValue> UpdateCounterParameter {
|
---|
| 61 | get { return (LookupParameter<IntValue>)Parameters["UpdateCounter"]; }
|
---|
| 62 | }
|
---|
[4623] | 63 |
|
---|
[4704] | 64 | [StorableConstructor]
|
---|
| 65 | protected AlleleFrequencyAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
[4722] | 66 | protected AlleleFrequencyAnalyzer(AlleleFrequencyAnalyzer<T> original, Cloner cloner) : base(original, cloner) { }
|
---|
[4623] | 67 | public AlleleFrequencyAnalyzer()
|
---|
| 68 | : base() {
|
---|
| 69 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
|
---|
| 70 | Parameters.Add(new ScopeTreeLookupParameter<T>("Solution", "The solutions whose alleles should be analyzed."));
|
---|
| 71 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the solutions which should be analyzed."));
|
---|
[4631] | 72 | Parameters.Add(new LookupParameter<T>("BestKnownSolution", "The best known solution."));
|
---|
[4623] | 73 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the allele frequency analysis results should be stored."));
|
---|
[4704] | 74 | Parameters.Add(new ValueParameter<BoolValue>("StoreHistory", "True if the history of the allele frequency analysis should be stored.", new BoolValue(false)));
|
---|
[4631] | 75 | Parameters.Add(new ValueParameter<IntValue>("UpdateInterval", "The interval in which the allele frequency analysis should be applied.", new IntValue(1)));
|
---|
[4641] | 76 | Parameters.Add(new LookupParameter<IntValue>("UpdateCounter", "The value which counts how many times the operator was called since the last update.", "AlleleFrequencyAnalyzerUpdateCounter"));
|
---|
[6051] | 77 |
|
---|
| 78 | MaximizationParameter.Hidden = true;
|
---|
| 79 | SolutionParameter.Hidden = true;
|
---|
| 80 | QualityParameter.Hidden = true;
|
---|
| 81 | BestKnownSolutionParameter.Hidden = true;
|
---|
| 82 | ResultsParameter.Hidden = true;
|
---|
| 83 | UpdateCounterParameter.Hidden = true;
|
---|
[4623] | 84 | }
|
---|
| 85 |
|
---|
[4641] | 86 | #region AlleleFrequencyIdEqualityComparer
|
---|
| 87 | private class AlleleFrequencyIdEqualityComparer : IEqualityComparer<AlleleFrequency> {
|
---|
[4639] | 88 | public bool Equals(AlleleFrequency x, AlleleFrequency y) {
|
---|
| 89 | return x.Id == y.Id;
|
---|
| 90 | }
|
---|
| 91 | public int GetHashCode(AlleleFrequency obj) {
|
---|
[4641] | 92 | return obj.Id.GetHashCode();
|
---|
[4639] | 93 | }
|
---|
| 94 | }
|
---|
[4641] | 95 | #endregion
|
---|
[4639] | 96 |
|
---|
[4623] | 97 | public override IOperation Apply() {
|
---|
[4631] | 98 | int updateInterval = UpdateIntervalParameter.Value.Value;
|
---|
| 99 | IntValue updateCounter = UpdateCounterParameter.ActualValue;
|
---|
| 100 | if (updateCounter == null) {
|
---|
| 101 | updateCounter = new IntValue(updateInterval);
|
---|
| 102 | UpdateCounterParameter.ActualValue = updateCounter;
|
---|
| 103 | } else updateCounter.Value++;
|
---|
[4623] | 104 |
|
---|
[4631] | 105 | if (updateCounter.Value == updateInterval) {
|
---|
| 106 | updateCounter.Value = 0;
|
---|
[4623] | 107 |
|
---|
[4631] | 108 | bool max = MaximizationParameter.ActualValue.Value;
|
---|
| 109 | ItemArray<T> solutions = SolutionParameter.ActualValue;
|
---|
| 110 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
| 111 | T bestKnownSolution = BestKnownSolutionParameter.ActualValue;
|
---|
[4704] | 112 | bool storeHistory = StoreHistoryParameter.Value.Value;
|
---|
[4623] | 113 |
|
---|
[4631] | 114 | // calculate index of current best solution
|
---|
| 115 | int bestIndex = -1;
|
---|
[4849] | 116 | if (!max) {
|
---|
| 117 | bestIndex = qualities
|
---|
| 118 | .Select((x, index) => new { index, x.Value })
|
---|
| 119 | .OrderBy(x => x.Value)
|
---|
| 120 | .First().index;
|
---|
| 121 | } else {
|
---|
| 122 | bestIndex = qualities
|
---|
| 123 | .Select((x, index) => new { index, x.Value })
|
---|
| 124 | .OrderByDescending(x => x.Value)
|
---|
| 125 | .First().index;
|
---|
| 126 | }
|
---|
[4623] | 127 |
|
---|
[4631] | 128 | // calculate allels of current best and (if available) best known solution
|
---|
| 129 | Allele[] bestAlleles = CalculateAlleles(solutions[bestIndex]);
|
---|
| 130 | Allele[] bestKnownAlleles = null;
|
---|
| 131 | if (bestKnownSolution != null)
|
---|
| 132 | bestKnownAlleles = CalculateAlleles(bestKnownSolution);
|
---|
[4623] | 133 |
|
---|
[4631] | 134 | // calculate allele frequencies
|
---|
| 135 | var frequencies = solutions.SelectMany((s, index) => CalculateAlleles(s).Select(a => new { Allele = a, Quality = qualities[index] })).
|
---|
| 136 | GroupBy(x => x.Allele.Id).
|
---|
| 137 | Select(x => new AlleleFrequency(x.Key,
|
---|
| 138 | x.Count() / ((double)solutions.Length),
|
---|
| 139 | x.Average(a => a.Allele.Impact),
|
---|
| 140 | x.Average(a => a.Quality.Value),
|
---|
| 141 | bestKnownAlleles == null ? false : bestKnownAlleles.Any(a => a.Id == x.Key),
|
---|
| 142 | bestAlleles.Any(a => a.Id == x.Key)));
|
---|
| 143 |
|
---|
[4639] | 144 | // calculate dummy allele frequencies of alleles of best known solution which did not occur
|
---|
[4645] | 145 | if (bestKnownAlleles != null) {
|
---|
| 146 | var bestKnownFrequencies = bestKnownAlleles.Select(x => new AlleleFrequency(x.Id, 0, x.Impact, 0, true, false)).Except(frequencies, new AlleleFrequencyIdEqualityComparer());
|
---|
| 147 | frequencies = frequencies.Concat(bestKnownFrequencies);
|
---|
| 148 | }
|
---|
[4639] | 149 |
|
---|
[4631] | 150 | // fetch results collection
|
---|
| 151 | ResultCollection results;
|
---|
[4991] | 152 | if (!ResultsParameter.ActualValue.ContainsKey(Name + " Results")) {
|
---|
[4631] | 153 | results = new ResultCollection();
|
---|
[4991] | 154 | ResultsParameter.ActualValue.Add(new Result(Name + " Results", results));
|
---|
[4631] | 155 | } else {
|
---|
[4991] | 156 | results = (ResultCollection)ResultsParameter.ActualValue[Name + " Results"].Value;
|
---|
[4631] | 157 | }
|
---|
| 158 |
|
---|
| 159 | // store allele frequencies
|
---|
[4645] | 160 | AlleleFrequencyCollection frequenciesCollection = new AlleleFrequencyCollection(frequencies);
|
---|
[4631] | 161 | if (!results.ContainsKey("Allele Frequencies"))
|
---|
[4639] | 162 | results.Add(new Result("Allele Frequencies", frequenciesCollection));
|
---|
[4631] | 163 | else
|
---|
[4639] | 164 | results["Allele Frequencies"].Value = frequenciesCollection;
|
---|
[4631] | 165 |
|
---|
| 166 | // store allele frequencies history
|
---|
| 167 | if (storeHistory) {
|
---|
| 168 | if (!results.ContainsKey("Allele Frequencies History")) {
|
---|
[4641] | 169 | AlleleFrequencyCollectionHistory history = new AlleleFrequencyCollectionHistory();
|
---|
[4639] | 170 | history.Add(frequenciesCollection);
|
---|
[4631] | 171 | results.Add(new Result("Allele Frequencies History", history));
|
---|
| 172 | } else {
|
---|
[4641] | 173 | ((AlleleFrequencyCollectionHistory)results["Allele Frequencies History"].Value).Add(frequenciesCollection);
|
---|
[4631] | 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | // store alleles data table
|
---|
| 178 | DataTable allelesTable;
|
---|
| 179 | if (!results.ContainsKey("Alleles")) {
|
---|
| 180 | allelesTable = new DataTable("Alleles");
|
---|
[4870] | 181 | allelesTable.VisualProperties.XAxisTitle = "Iteration";
|
---|
| 182 | allelesTable.VisualProperties.YAxisTitle = "Number of Alleles";
|
---|
| 183 | allelesTable.VisualProperties.SecondYAxisTitle = "Number of Alleles";
|
---|
[4777] | 184 |
|
---|
[4631] | 185 | allelesTable.Rows.Add(new DataRow("Unique Alleles"));
|
---|
[4778] | 186 | allelesTable.Rows["Unique Alleles"].VisualProperties.StartIndexZero = true;
|
---|
[4777] | 187 |
|
---|
| 188 | allelesTable.Rows.Add(new DataRow("Unique Alleles of Best Known Solution", null));
|
---|
| 189 | allelesTable.Rows["Unique Alleles of Best Known Solution"].VisualProperties.SecondYAxis = true;
|
---|
| 190 | allelesTable.Rows["Unique Alleles of Best Known Solution"].VisualProperties.StartIndexZero = true;
|
---|
| 191 |
|
---|
| 192 | allelesTable.Rows.Add(new DataRow("Fixed Alleles", null));
|
---|
| 193 | allelesTable.Rows["Fixed Alleles"].VisualProperties.SecondYAxis = true;
|
---|
| 194 | allelesTable.Rows["Fixed Alleles"].VisualProperties.StartIndexZero = true;
|
---|
| 195 |
|
---|
| 196 | allelesTable.Rows.Add(new DataRow("Fixed Alleles of Best Known Solution", null));
|
---|
| 197 | allelesTable.Rows["Fixed Alleles of Best Known Solution"].VisualProperties.SecondYAxis = true;
|
---|
| 198 | allelesTable.Rows["Fixed Alleles of Best Known Solution"].VisualProperties.StartIndexZero = true;
|
---|
| 199 |
|
---|
| 200 | allelesTable.Rows.Add(new DataRow("Lost Alleles of Best Known Solution", null));
|
---|
| 201 | allelesTable.Rows["Lost Alleles of Best Known Solution"].VisualProperties.SecondYAxis = true;
|
---|
| 202 | allelesTable.Rows["Lost Alleles of Best Known Solution"].VisualProperties.StartIndexZero = true;
|
---|
[4870] | 203 |
|
---|
| 204 | results.Add(new Result("Alleles", allelesTable));
|
---|
[4631] | 205 | } else {
|
---|
| 206 | allelesTable = (DataTable)results["Alleles"].Value;
|
---|
| 207 | }
|
---|
[4645] | 208 |
|
---|
| 209 | int fixedAllelesCount = frequenciesCollection.Where(x => x.Frequency == 1).Count();
|
---|
| 210 | var relevantAlleles = frequenciesCollection.Where(x => x.ContainedInBestKnownSolution);
|
---|
| 211 | int relevantAllelesCount = relevantAlleles.Count();
|
---|
| 212 | int fixedRelevantAllelesCount = relevantAlleles.Where(x => x.Frequency == 1).Count();
|
---|
| 213 | int lostRelevantAllelesCount = relevantAlleles.Where(x => x.Frequency == 0).Count();
|
---|
| 214 | int uniqueRelevantAllelesCount = relevantAllelesCount - lostRelevantAllelesCount;
|
---|
[4639] | 215 | allelesTable.Rows["Unique Alleles"].Values.Add(frequenciesCollection.Count);
|
---|
[4645] | 216 | allelesTable.Rows["Unique Alleles of Best Known Solution"].Values.Add(uniqueRelevantAllelesCount);
|
---|
| 217 | allelesTable.Rows["Fixed Alleles"].Values.Add(fixedAllelesCount);
|
---|
| 218 | allelesTable.Rows["Fixed Alleles of Best Known Solution"].Values.Add(fixedRelevantAllelesCount);
|
---|
| 219 | allelesTable.Rows["Lost Alleles of Best Known Solution"].Values.Add(lostRelevantAllelesCount);
|
---|
[4716] | 220 |
|
---|
| 221 | // store alleles values
|
---|
| 222 | if (!results.ContainsKey("Unique Alleles"))
|
---|
| 223 | results.Add(new Result("Unique Alleles", new DoubleValue(frequenciesCollection.Count)));
|
---|
| 224 | else
|
---|
| 225 | ((DoubleValue)results["Unique Alleles"].Value).Value = frequenciesCollection.Count;
|
---|
| 226 |
|
---|
| 227 | if (!results.ContainsKey("Unique Alleles of Best Known Solution"))
|
---|
| 228 | results.Add(new Result("Unique Alleles of Best Known Solution", new DoubleValue(uniqueRelevantAllelesCount)));
|
---|
| 229 | else
|
---|
| 230 | ((DoubleValue)results["Unique Alleles of Best Known Solution"].Value).Value = uniqueRelevantAllelesCount;
|
---|
| 231 |
|
---|
| 232 | if (!results.ContainsKey("Fixed Alleles"))
|
---|
| 233 | results.Add(new Result("Fixed Alleles", new DoubleValue(fixedAllelesCount)));
|
---|
| 234 | else
|
---|
| 235 | ((DoubleValue)results["Fixed Alleles"].Value).Value = fixedAllelesCount;
|
---|
| 236 |
|
---|
| 237 | if (!results.ContainsKey("Fixed Alleles of Best Known Solution"))
|
---|
| 238 | results.Add(new Result("Fixed Alleles of Best Known Solution", new DoubleValue(fixedRelevantAllelesCount)));
|
---|
| 239 | else
|
---|
| 240 | ((DoubleValue)results["Fixed Alleles of Best Known Solution"].Value).Value = fixedRelevantAllelesCount;
|
---|
| 241 |
|
---|
| 242 | if (!results.ContainsKey("Lost Alleles of Best Known Solution"))
|
---|
| 243 | results.Add(new Result("Lost Alleles of Best Known Solution", new DoubleValue(lostRelevantAllelesCount)));
|
---|
| 244 | else
|
---|
| 245 | ((DoubleValue)results["Lost Alleles of Best Known Solution"].Value).Value = lostRelevantAllelesCount;
|
---|
[4631] | 246 | }
|
---|
[4623] | 247 | return base.Apply();
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | protected abstract Allele[] CalculateAlleles(T solution);
|
---|
| 251 | }
|
---|
| 252 | }
|
---|