[4623] | 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 |
|
---|
[4639] | 22 | using System.Collections.Generic;
|
---|
[4623] | 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 | }
|
---|
[4631] | 47 | public LookupParameter<T> BestKnownSolutionParameter {
|
---|
| 48 | get { return (LookupParameter<T>)Parameters["BestKnownSolution"]; }
|
---|
| 49 | }
|
---|
[4623] | 50 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
| 51 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
| 52 | }
|
---|
[4631] | 53 | public ValueParameter<BoolValue> StoreAlleleFrequenciesHistoryParameter {
|
---|
| 54 | get { return (ValueParameter<BoolValue>)Parameters["StoreAlleleFrequenciesHistory"]; }
|
---|
[4623] | 55 | }
|
---|
[4631] | 56 | public ValueParameter<IntValue> UpdateIntervalParameter {
|
---|
| 57 | get { return (ValueParameter<IntValue>)Parameters["UpdateInterval"]; }
|
---|
[4623] | 58 | }
|
---|
[4631] | 59 | public LookupParameter<IntValue> UpdateCounterParameter {
|
---|
| 60 | get { return (LookupParameter<IntValue>)Parameters["UpdateCounter"]; }
|
---|
| 61 | }
|
---|
[4623] | 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."));
|
---|
[4631] | 68 | Parameters.Add(new LookupParameter<T>("BestKnownSolution", "The best known solution."));
|
---|
[4623] | 69 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the allele frequency analysis results should be stored."));
|
---|
[4631] | 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)));
|
---|
[4641] | 72 | Parameters.Add(new LookupParameter<IntValue>("UpdateCounter", "The value which counts how many times the operator was called since the last update.", "AlleleFrequencyAnalyzerUpdateCounter"));
|
---|
[4623] | 73 | }
|
---|
| 74 |
|
---|
[4641] | 75 | #region AlleleFrequencyIdEqualityComparer
|
---|
| 76 | private class AlleleFrequencyIdEqualityComparer : IEqualityComparer<AlleleFrequency> {
|
---|
[4639] | 77 | public bool Equals(AlleleFrequency x, AlleleFrequency y) {
|
---|
| 78 | return x.Id == y.Id;
|
---|
| 79 | }
|
---|
| 80 | public int GetHashCode(AlleleFrequency obj) {
|
---|
[4641] | 81 | return obj.Id.GetHashCode();
|
---|
[4639] | 82 | }
|
---|
| 83 | }
|
---|
[4641] | 84 | #endregion
|
---|
[4639] | 85 |
|
---|
[4623] | 86 | public override IOperation Apply() {
|
---|
[4631] | 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++;
|
---|
[4623] | 93 |
|
---|
[4631] | 94 | if (updateCounter.Value == updateInterval) {
|
---|
| 95 | updateCounter.Value = 0;
|
---|
[4623] | 96 |
|
---|
[4631] | 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;
|
---|
[4623] | 102 |
|
---|
[4631] | 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;
|
---|
[4623] | 107 |
|
---|
[4631] | 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);
|
---|
[4623] | 113 |
|
---|
[4631] | 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 |
|
---|
[4639] | 124 | // calculate dummy allele frequencies of alleles of best known solution which did not occur
|
---|
[4641] | 125 | var bestKnownFrequencies = bestKnownAlleles.Select(x => new AlleleFrequency(x.Id, 0, x.Impact, 0, true, false)).Except(frequencies, new AlleleFrequencyIdEqualityComparer());
|
---|
[4639] | 126 |
|
---|
[4631] | 127 | // fetch results collection
|
---|
| 128 | ResultCollection results;
|
---|
| 129 | if (!ResultsParameter.ActualValue.ContainsKey("Allele Frequency Analysis Results")) {
|
---|
| 130 | results = new ResultCollection();
|
---|
| 131 | ResultsParameter.ActualValue.Add(new Result("Allele Frequency Analysis Results", results));
|
---|
| 132 | } else {
|
---|
| 133 | results = (ResultCollection)ResultsParameter.ActualValue["Allele Frequency Analysis Results"].Value;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | // store allele frequencies
|
---|
[4639] | 137 | AlleleFrequencyCollection frequenciesCollection = new AlleleFrequencyCollection(bestKnownFrequencies.Concat(frequencies));
|
---|
[4631] | 138 | if (!results.ContainsKey("Allele Frequencies"))
|
---|
[4639] | 139 | results.Add(new Result("Allele Frequencies", frequenciesCollection));
|
---|
[4631] | 140 | else
|
---|
[4639] | 141 | results["Allele Frequencies"].Value = frequenciesCollection;
|
---|
[4631] | 142 |
|
---|
| 143 | // store allele frequencies history
|
---|
| 144 | if (storeHistory) {
|
---|
| 145 | if (!results.ContainsKey("Allele Frequencies History")) {
|
---|
[4641] | 146 | AlleleFrequencyCollectionHistory history = new AlleleFrequencyCollectionHistory();
|
---|
[4639] | 147 | history.Add(frequenciesCollection);
|
---|
[4631] | 148 | results.Add(new Result("Allele Frequencies History", history));
|
---|
| 149 | } else {
|
---|
[4641] | 150 | ((AlleleFrequencyCollectionHistory)results["Allele Frequencies History"].Value).Add(frequenciesCollection);
|
---|
[4631] | 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | // store alleles data table
|
---|
| 155 | DataTable allelesTable;
|
---|
| 156 | if (!results.ContainsKey("Alleles")) {
|
---|
| 157 | allelesTable = new DataTable("Alleles");
|
---|
| 158 | results.Add(new Result("Alleles", allelesTable));
|
---|
| 159 | allelesTable.Rows.Add(new DataRow("Unique Alleles"));
|
---|
| 160 | } else {
|
---|
| 161 | allelesTable = (DataTable)results["Alleles"].Value;
|
---|
| 162 | }
|
---|
[4639] | 163 | allelesTable.Rows["Unique Alleles"].Values.Add(frequenciesCollection.Count);
|
---|
[4631] | 164 | }
|
---|
[4623] | 165 | return base.Apply();
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | protected abstract Allele[] CalculateAlleles(T solution);
|
---|
| 169 | }
|
---|
| 170 | }
|
---|