[4623] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 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 |
|
---|
[8283] | 22 | using System;
|
---|
[4639] | 23 | using System.Collections.Generic;
|
---|
[4623] | 24 | using System.Linq;
|
---|
[4722] | 25 | using HeuristicLab.Common;
|
---|
[4623] | 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 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 | [StorableClass]
|
---|
[12005] | 39 | public abstract class AlleleFrequencyAnalyzer<T> : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator where T : class, IItem {
|
---|
[7172] | 40 | public virtual bool EnabledByDefault {
|
---|
| 41 | get { return false; }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[4623] | 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 | }
|
---|
[4631] | 53 | public LookupParameter<T> BestKnownSolutionParameter {
|
---|
| 54 | get { return (LookupParameter<T>)Parameters["BestKnownSolution"]; }
|
---|
| 55 | }
|
---|
[4623] | 56 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
| 57 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
| 58 | }
|
---|
[4704] | 59 | public ValueParameter<BoolValue> StoreHistoryParameter {
|
---|
| 60 | get { return (ValueParameter<BoolValue>)Parameters["StoreHistory"]; }
|
---|
[4623] | 61 | }
|
---|
[4631] | 62 | public ValueParameter<IntValue> UpdateIntervalParameter {
|
---|
| 63 | get { return (ValueParameter<IntValue>)Parameters["UpdateInterval"]; }
|
---|
[4623] | 64 | }
|
---|
[4631] | 65 | public LookupParameter<IntValue> UpdateCounterParameter {
|
---|
| 66 | get { return (LookupParameter<IntValue>)Parameters["UpdateCounter"]; }
|
---|
| 67 | }
|
---|
[4623] | 68 |
|
---|
[4704] | 69 | [StorableConstructor]
|
---|
| 70 | protected AlleleFrequencyAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
[4722] | 71 | protected AlleleFrequencyAnalyzer(AlleleFrequencyAnalyzer<T> original, Cloner cloner) : base(original, cloner) { }
|
---|
[4623] | 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."));
|
---|
[4631] | 77 | Parameters.Add(new LookupParameter<T>("BestKnownSolution", "The best known solution."));
|
---|
[4623] | 78 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the allele frequency analysis results should be stored."));
|
---|
[4704] | 79 | Parameters.Add(new ValueParameter<BoolValue>("StoreHistory", "True if the history of the allele frequency analysis should be stored.", new BoolValue(false)));
|
---|
[4631] | 80 | Parameters.Add(new ValueParameter<IntValue>("UpdateInterval", "The interval in which the allele frequency analysis should be applied.", new IntValue(1)));
|
---|
[4641] | 81 | Parameters.Add(new LookupParameter<IntValue>("UpdateCounter", "The value which counts how many times the operator was called since the last update.", "AlleleFrequencyAnalyzerUpdateCounter"));
|
---|
[6051] | 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;
|
---|
[4623] | 89 | }
|
---|
| 90 |
|
---|
[8283] | 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 | }
|
---|
[4641] | 100 | private class AlleleFrequencyIdEqualityComparer : IEqualityComparer<AlleleFrequency> {
|
---|
[4639] | 101 | public bool Equals(AlleleFrequency x, AlleleFrequency y) {
|
---|
| 102 | return x.Id == y.Id;
|
---|
| 103 | }
|
---|
| 104 | public int GetHashCode(AlleleFrequency obj) {
|
---|
[4641] | 105 | return obj.Id.GetHashCode();
|
---|
[4639] | 106 | }
|
---|
| 107 | }
|
---|
[4641] | 108 | #endregion
|
---|
[4639] | 109 |
|
---|
[4623] | 110 | public override IOperation Apply() {
|
---|
[4631] | 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++;
|
---|
[4623] | 117 |
|
---|
[4631] | 118 | if (updateCounter.Value == updateInterval) {
|
---|
| 119 | updateCounter.Value = 0;
|
---|
[4623] | 120 |
|
---|
[4631] | 121 | bool max = MaximizationParameter.ActualValue.Value;
|
---|
| 122 | ItemArray<T> solutions = SolutionParameter.ActualValue;
|
---|
[8283] | 123 | double[] qualities = QualityParameter.ActualValue.Select(x => x.Value).ToArray();
|
---|
[4631] | 124 | T bestKnownSolution = BestKnownSolutionParameter.ActualValue;
|
---|
[4704] | 125 | bool storeHistory = StoreHistoryParameter.Value.Value;
|
---|
[4623] | 126 |
|
---|
[4631] | 127 | // calculate index of current best solution
|
---|
| 128 | int bestIndex = -1;
|
---|
[4849] | 129 | if (!max) {
|
---|
| 130 | bestIndex = qualities
|
---|
[8283] | 131 | .Select((x, i) => new { Index = i, Value = x })
|
---|
[4849] | 132 | .OrderBy(x => x.Value)
|
---|
[8283] | 133 | .First().Index;
|
---|
[4849] | 134 | } else {
|
---|
| 135 | bestIndex = qualities
|
---|
[8283] | 136 | .Select((x, i) => new { Index = i, Value = x })
|
---|
[4849] | 137 | .OrderByDescending(x => x.Value)
|
---|
[8283] | 138 | .First().Index;
|
---|
[4849] | 139 | }
|
---|
[4623] | 140 |
|
---|
[4631] | 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);
|
---|
[4623] | 146 |
|
---|
[4631] | 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),
|
---|
[8283] | 153 | x.Average(a => a.Quality),
|
---|
[4631] | 154 | bestKnownAlleles == null ? false : bestKnownAlleles.Any(a => a.Id == x.Key),
|
---|
| 155 | bestAlleles.Any(a => a.Id == x.Key)));
|
---|
| 156 |
|
---|
[4639] | 157 | // calculate dummy allele frequencies of alleles of best known solution which did not occur
|
---|
[4645] | 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 | }
|
---|
[4639] | 162 |
|
---|
[4631] | 163 | // fetch results collection
|
---|
| 164 | ResultCollection results;
|
---|
[4991] | 165 | if (!ResultsParameter.ActualValue.ContainsKey(Name + " Results")) {
|
---|
[4631] | 166 | results = new ResultCollection();
|
---|
[4991] | 167 | ResultsParameter.ActualValue.Add(new Result(Name + " Results", results));
|
---|
[4631] | 168 | } else {
|
---|
[4991] | 169 | results = (ResultCollection)ResultsParameter.ActualValue[Name + " Results"].Value;
|
---|
[4631] | 170 | }
|
---|
| 171 |
|
---|
| 172 | // store allele frequencies
|
---|
[4645] | 173 | AlleleFrequencyCollection frequenciesCollection = new AlleleFrequencyCollection(frequencies);
|
---|
[4631] | 174 | if (!results.ContainsKey("Allele Frequencies"))
|
---|
[4639] | 175 | results.Add(new Result("Allele Frequencies", frequenciesCollection));
|
---|
[4631] | 176 | else
|
---|
[4639] | 177 | results["Allele Frequencies"].Value = frequenciesCollection;
|
---|
[4631] | 178 |
|
---|
| 179 | // store allele frequencies history
|
---|
| 180 | if (storeHistory) {
|
---|
| 181 | if (!results.ContainsKey("Allele Frequencies History")) {
|
---|
[4641] | 182 | AlleleFrequencyCollectionHistory history = new AlleleFrequencyCollectionHistory();
|
---|
[4639] | 183 | history.Add(frequenciesCollection);
|
---|
[4631] | 184 | results.Add(new Result("Allele Frequencies History", history));
|
---|
| 185 | } else {
|
---|
[4641] | 186 | ((AlleleFrequencyCollectionHistory)results["Allele Frequencies History"].Value).Add(frequenciesCollection);
|
---|
[4631] | 187 | }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | // store alleles data table
|
---|
| 191 | DataTable allelesTable;
|
---|
| 192 | if (!results.ContainsKey("Alleles")) {
|
---|
| 193 | allelesTable = new DataTable("Alleles");
|
---|
[4870] | 194 | allelesTable.VisualProperties.XAxisTitle = "Iteration";
|
---|
| 195 | allelesTable.VisualProperties.YAxisTitle = "Number of Alleles";
|
---|
| 196 | allelesTable.VisualProperties.SecondYAxisTitle = "Number of Alleles";
|
---|
[4777] | 197 |
|
---|
[4631] | 198 | allelesTable.Rows.Add(new DataRow("Unique Alleles"));
|
---|
[4778] | 199 | allelesTable.Rows["Unique Alleles"].VisualProperties.StartIndexZero = true;
|
---|
[4777] | 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;
|
---|
[4870] | 216 |
|
---|
| 217 | results.Add(new Result("Alleles", allelesTable));
|
---|
[4631] | 218 | } else {
|
---|
| 219 | allelesTable = (DataTable)results["Alleles"].Value;
|
---|
| 220 | }
|
---|
[4645] | 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;
|
---|
[4639] | 228 | allelesTable.Rows["Unique Alleles"].Values.Add(frequenciesCollection.Count);
|
---|
[4645] | 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);
|
---|
[4716] | 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;
|
---|
[8289] | 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 | }
|
---|
[4631] | 306 | }
|
---|
[4623] | 307 | return base.Apply();
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | protected abstract Allele[] CalculateAlleles(T solution);
|
---|
| 311 | }
|
---|
| 312 | }
|
---|