Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/DataTableHelper.cs @ 10007

Last change on this file since 10007 was 9789, checked in by ascheibe, 11 years ago

#1886 fixed alglib reference and updated year and version information

File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Linq;
23using HeuristicLab.Collections;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
31  [Item("DataTableHelper", "Helper class for creating datatables.")]
32  [StorableClass]
33  public class DataTableHelper : Item {
34
35    [Storable]
36    private ResultCollection resultsCol;
37    [Storable]
38    private string chartName;
39    [Storable]
40    private string[] dataRowNames;
41    [Storable]
42    DataTable dt;
43
44    [StorableConstructor]
45    private DataTableHelper(bool deserializing) : base(deserializing) { }
46    private DataTableHelper(DataTableHelper original, Cloner cloner)
47      : base(original, cloner) { }
48    public DataTableHelper() : base() { }
49
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new DataTableHelper(this, cloner);
52    }
53
54    public void InitializeChart(ResultCollection results, string chartName, string[] dataRowNames) {
55      if (!results.ContainsKey(chartName)) {
56        this.resultsCol = results;
57        this.chartName = chartName;
58        this.dataRowNames = dataRowNames;
59
60        dt = new DataTable(chartName);
61
62        foreach (string dataRowName in dataRowNames) {
63          DataRow dtRow = new DataRow(dataRowName);
64          dt.Rows.Add(dtRow);
65        }
66
67        results.Add(new Result(chartName, dt));
68      }
69    }
70
71    public void AddPoint(string dataRow, double point) {
72      dt.Rows[dataRow].Values.Add(point);
73    }
74
75    public void AddPoint(double point) {
76      // assume that there is only 1 row and therefore the name doesn't have to be provided
77      dt.Rows[dataRowNames[0]].Values.Add(point);
78    }
79
80    public ObservableList<double> GetFirstDataRow() {
81      return dt.Rows[dataRowNames[0]].Values;
82    }
83
84    public void CleanUp() {
85      //remove chart
86      resultsCol[chartName].Value = null;
87      resultsCol.Remove(chartName);
88      dt = null;
89    }
90
91    public void CleanUpAndCompressData() {
92      string[] columnNames = new string[] { "Count", "Minimum", "Maximum", "Average", "Median", "Standard Deviation", "Variance", "25th Percentile", "75th Percentile", "Gradient", "Relative Error", "Avg. of Upper 25 %", " Avg. of Lower 25 %", "Avg. of First 25 %", "Avg. of Last 25 %" };
93
94      foreach (string rowName in dataRowNames) {
95        DataRow curDataRow = dt.Rows[rowName];
96        var values = curDataRow.Values.AsEnumerable();
97
98        double cnt = values.Count();
99        double min = values.Min();
100        double max = values.Max();
101        double avg = values.Average();
102        double median = values.Median();
103        double stdDev = values.StandardDeviation();
104        double variance = values.Variance();
105        double percentile25 = values.Percentile(0.25);
106        double percentile75 = values.Percentile(0.75);
107        double k, d, r;
108        LinearLeastSquaresFitting.Calculate(values.ToArray(), out k, out d);
109        r = LinearLeastSquaresFitting.CalculateError(values.ToArray(), k, d);
110        double lowerAvg = values.OrderBy(x => x).Take((int)(values.Count() * 0.25)).Average();
111        double upperAvg = values.OrderByDescending(x => x).Take((int)(values.Count() * 0.25)).Average();
112        double firstAvg = values.Take((int)(values.Count() * 0.25)).Average();
113        double lastAvg = values.Skip((int)(values.Count() * 0.75)).Average();
114
115        resultsCol.Add(new Result(chartName + " " + rowName + " " + columnNames[0], new DoubleValue(cnt)));
116        resultsCol.Add(new Result(chartName + " " + rowName + " " + columnNames[1], new DoubleValue(min)));
117        resultsCol.Add(new Result(chartName + " " + rowName + " " + columnNames[2], new DoubleValue(max)));
118        resultsCol.Add(new Result(chartName + " " + rowName + " " + columnNames[3], new DoubleValue(avg)));
119        resultsCol.Add(new Result(chartName + " " + rowName + " " + columnNames[4], new DoubleValue(median)));
120        resultsCol.Add(new Result(chartName + " " + rowName + " " + columnNames[5], new DoubleValue(stdDev)));
121        resultsCol.Add(new Result(chartName + " " + rowName + " " + columnNames[6], new DoubleValue(variance)));
122        resultsCol.Add(new Result(chartName + " " + rowName + " " + columnNames[7], new DoubleValue(percentile25)));
123        resultsCol.Add(new Result(chartName + " " + rowName + " " + columnNames[8], new DoubleValue(percentile75)));
124        resultsCol.Add(new Result(chartName + " " + rowName + " " + columnNames[9], new DoubleValue(k)));
125        resultsCol.Add(new Result(chartName + " " + rowName + " " + columnNames[10], new DoubleValue(r)));
126        resultsCol.Add(new Result(chartName + " " + rowName + " " + columnNames[11], new DoubleValue(upperAvg)));
127        resultsCol.Add(new Result(chartName + " " + rowName + " " + columnNames[12], new DoubleValue(lowerAvg)));
128        resultsCol.Add(new Result(chartName + " " + rowName + " " + columnNames[13], new DoubleValue(firstAvg)));
129        resultsCol.Add(new Result(chartName + " " + rowName + " " + columnNames[14], new DoubleValue(lastAvg)));
130      }
131
132      CleanUp();
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.