Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/ScatterPlotHelper.cs @ 10635

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

#1886 fixed alglib reference and updated year and version information

File size: 6.3 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Optimization;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
30  [Item("ScatterPlotHelper", "Helper class for creating scatter plots.")]
31  [StorableClass]
32  public class ScatterPlotHelper : Item {
33
34    [Storable]
35    private ResultCollection resultsCol;
36    [Storable]
37    private string chartName;
38    [Storable]
39    private string xAxisTitle;
40    [Storable]
41    private string yAxisTitle;
42    [Storable]
43    public bool StoreHistory { get; set; }
44    [Storable]
45    public bool CreateDataTable { get; set; }
46    [Storable]
47    public DataTableHelper dataTableHelper, minMaxDataTableHelper;
48    [Storable]
49    private string lastRowName;
50    [Storable]
51    private bool trackMinMaxValues;
52    [Storable]
53    private bool compressData;
54    [Storable]
55    private double min;
56    public double Min {
57      get { return min; }
58      set { min = value; }
59    }
60    [Storable]
61    private double max;
62    public double Max {
63      get { return max; }
64      set { max = value; }
65    }
66
67    [Storable]
68    private ResultCollection Results;
69
70    [Storable]
71    private ScatterPlot plot;
72    public ScatterPlot Plot {
73      get { return plot; }
74      private set { plot = value; }
75    }
76
77    [StorableConstructor]
78    private ScatterPlotHelper(bool deserializing) : base(deserializing) { }
79    private ScatterPlotHelper(ScatterPlotHelper original, Cloner cloner)
80      : base(original, cloner) {
81      StoreHistory = original.StoreHistory;
82      CreateDataTable = original.CreateDataTable;
83      min = original.min;
84      max = original.max;
85      compressData = original.compressData;
86
87      chartName = original.chartName;
88      chartName = original.chartName;
89      xAxisTitle = original.xAxisTitle;
90      yAxisTitle = original.yAxisTitle;
91    }
92
93    public ScatterPlotHelper()
94      : base() {
95      StoreHistory = true;
96      CreateDataTable = false;
97      min = double.MaxValue;
98      max = double.MinValue;
99      compressData = false;
100    }
101
102    public ScatterPlotHelper(bool storeHistory, bool createDataTable, bool trackMinMax = false, bool compress = false)
103      : base() {
104      StoreHistory = storeHistory;
105      CreateDataTable = createDataTable;
106      trackMinMaxValues = trackMinMax;
107      compressData = compress;
108      min = double.MaxValue;
109      max = double.MinValue;
110    }
111
112    public override IDeepCloneable Clone(Cloner cloner) {
113      return new ScatterPlotHelper(this, cloner);
114    }
115
116    public void InitializePlot(ResultCollection results, string chartName, string xAxisTitle, string yAxisTitle) {
117      Results = results;
118      if (CreateDataTable) {
119        if (dataTableHelper == null) {
120          dataTableHelper = new DataTableHelper();
121          minMaxDataTableHelper = new DataTableHelper();
122        }
123        dataTableHelper.InitializeChart(results, chartName + " Chart", new string[] { yAxisTitle });
124
125      }
126
127      if (!results.ContainsKey(chartName)) {
128        this.chartName = chartName;
129        this.resultsCol = results;
130        this.xAxisTitle = xAxisTitle;
131        this.yAxisTitle = yAxisTitle;
132
133        Initialize();
134        lastRowName = null;
135
136        results.Add(new Result(chartName, Plot));
137        if (StoreHistory)
138          results.Add(new Result(chartName + " History", new ScatterPlotHistory()));
139      }
140    }
141
142    public void AddPoint(string rowName, Point2D<double> point) {
143      if (!Plot.Rows.ContainsKey(rowName)) {
144        if (StoreHistory)
145          ((ScatterPlotHistory)resultsCol[chartName + " History"].Value).Add(Plot);
146
147        if (CreateDataTable && lastRowName != null && lastRowName != string.Empty) {
148          double avg = Plot.Rows[lastRowName].Points.Average(x => x.Y);
149          dataTableHelper.AddPoint(avg);
150        }
151
152        Initialize();
153        resultsCol[chartName].Value = Plot;
154
155        var points = new List<Point2D<double>>();
156        points.Add(point);
157        ScatterPlotDataRow row = new ScatterPlotDataRow(rowName, null, points);
158        row.VisualProperties.PointStyle = ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Circle;
159        row.VisualProperties.PointSize = 5;
160        Plot.Rows.Add(row);
161        lastRowName = rowName;
162      } else {
163        Plot.Rows[rowName].Points.Add(point);
164      }
165    }
166
167    private void Initialize() {
168      Plot = new ScatterPlot(chartName, null);
169      Plot.VisualProperties.XAxisTitle = xAxisTitle;
170      Plot.VisualProperties.YAxisTitle = yAxisTitle;
171    }
172
173    public void CleanUp() {
174      if (CreateDataTable && trackMinMaxValues) {
175        minMaxDataTableHelper.InitializeChart(Results, chartName + " Scaled Chart", new string[] { yAxisTitle });
176        dataTableHelper.GetFirstDataRow().ForEach(x => minMaxDataTableHelper.AddPoint((x - min) / (max - min)));
177
178        dataTableHelper.CleanUp(); //just use the scaled chart and remove the original
179
180        if (compressData) {
181          minMaxDataTableHelper.CleanUpAndCompressData();
182        }
183      }
184
185      if (CreateDataTable && compressData && !trackMinMaxValues) {
186        dataTableHelper.CleanUpAndCompressData();
187      }
188
189      if (!StoreHistory && CreateDataTable) {
190        resultsCol[chartName].Value = null;
191        resultsCol.Remove(chartName);
192        plot = null;
193      }
194      min = double.MaxValue;
195      max = double.MinValue;
196    }
197  }
198}
Note: See TracBrowser for help on using the repository browser.