[8498] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9789] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8498] | 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
[8511] | 23 | using System.Linq;
|
---|
[8498] | 24 | using HeuristicLab.Common;
|
---|
[8501] | 25 | using HeuristicLab.Core;
|
---|
[8498] | 26 | using HeuristicLab.Optimization;
|
---|
[8501] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[8498] | 28 |
|
---|
| 29 | namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
|
---|
[8501] | 30 | [Item("ScatterPlotHelper", "Helper class for creating scatter plots.")]
|
---|
| 31 | [StorableClass]
|
---|
| 32 | public class ScatterPlotHelper : Item {
|
---|
[8498] | 33 |
|
---|
[8505] | 34 | [Storable]
|
---|
[8498] | 35 | private ResultCollection resultsCol;
|
---|
[8505] | 36 | [Storable]
|
---|
[8498] | 37 | private string chartName;
|
---|
[8505] | 38 | [Storable]
|
---|
[8498] | 39 | private string xAxisTitle;
|
---|
[8505] | 40 | [Storable]
|
---|
[8498] | 41 | private string yAxisTitle;
|
---|
[8511] | 42 | [Storable]
|
---|
| 43 | public bool StoreHistory { get; set; }
|
---|
| 44 | [Storable]
|
---|
| 45 | public bool CreateDataTable { get; set; }
|
---|
| 46 | [Storable]
|
---|
[9059] | 47 | public DataTableHelper dataTableHelper, minMaxDataTableHelper;
|
---|
[8511] | 48 | [Storable]
|
---|
| 49 | private string lastRowName;
|
---|
[9059] | 50 | [Storable]
|
---|
| 51 | private bool trackMinMaxValues;
|
---|
| 52 | [Storable]
|
---|
[9252] | 53 | private bool compressData;
|
---|
| 54 | [Storable]
|
---|
[9059] | 55 | private double min;
|
---|
[9185] | 56 | public double Min {
|
---|
| 57 | get { return min; }
|
---|
| 58 | set { min = value; }
|
---|
| 59 | }
|
---|
[9059] | 60 | [Storable]
|
---|
| 61 | private double max;
|
---|
[9185] | 62 | public double Max {
|
---|
| 63 | get { return max; }
|
---|
| 64 | set { max = value; }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[9059] | 67 | [Storable]
|
---|
| 68 | private ResultCollection Results;
|
---|
[8498] | 69 |
|
---|
[8505] | 70 | [Storable]
|
---|
[8498] | 71 | private ScatterPlot plot;
|
---|
| 72 | public ScatterPlot Plot {
|
---|
| 73 | get { return plot; }
|
---|
| 74 | private set { plot = value; }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[8501] | 77 | [StorableConstructor]
|
---|
| 78 | private ScatterPlotHelper(bool deserializing) : base(deserializing) { }
|
---|
| 79 | private ScatterPlotHelper(ScatterPlotHelper original, Cloner cloner)
|
---|
| 80 | : base(original, cloner) {
|
---|
[8511] | 81 | StoreHistory = original.StoreHistory;
|
---|
| 82 | CreateDataTable = original.CreateDataTable;
|
---|
[9185] | 83 | min = original.min;
|
---|
| 84 | max = original.max;
|
---|
[9252] | 85 | compressData = original.compressData;
|
---|
[9185] | 86 |
|
---|
| 87 | chartName = original.chartName;
|
---|
| 88 | chartName = original.chartName;
|
---|
| 89 | xAxisTitle = original.xAxisTitle;
|
---|
| 90 | yAxisTitle = original.yAxisTitle;
|
---|
[8501] | 91 | }
|
---|
| 92 |
|
---|
[8511] | 93 | public ScatterPlotHelper()
|
---|
| 94 | : base() {
|
---|
| 95 | StoreHistory = true;
|
---|
| 96 | CreateDataTable = false;
|
---|
[9059] | 97 | min = double.MaxValue;
|
---|
| 98 | max = double.MinValue;
|
---|
[9252] | 99 | compressData = false;
|
---|
[8511] | 100 | }
|
---|
| 101 |
|
---|
[9252] | 102 | public ScatterPlotHelper(bool storeHistory, bool createDataTable, bool trackMinMax = false, bool compress = false)
|
---|
[8511] | 103 | : base() {
|
---|
| 104 | StoreHistory = storeHistory;
|
---|
| 105 | CreateDataTable = createDataTable;
|
---|
[9059] | 106 | trackMinMaxValues = trackMinMax;
|
---|
[9252] | 107 | compressData = compress;
|
---|
[9059] | 108 | min = double.MaxValue;
|
---|
| 109 | max = double.MinValue;
|
---|
[8511] | 110 | }
|
---|
| 111 |
|
---|
[8501] | 112 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 113 | return new ScatterPlotHelper(this, cloner);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[8498] | 116 | public void InitializePlot(ResultCollection results, string chartName, string xAxisTitle, string yAxisTitle) {
|
---|
[9059] | 117 | Results = results;
|
---|
[8511] | 118 | if (CreateDataTable) {
|
---|
| 119 | if (dataTableHelper == null) {
|
---|
| 120 | dataTableHelper = new DataTableHelper();
|
---|
[9059] | 121 | minMaxDataTableHelper = new DataTableHelper();
|
---|
[8511] | 122 | }
|
---|
[8797] | 123 | dataTableHelper.InitializeChart(results, chartName + " Chart", new string[] { yAxisTitle });
|
---|
[9059] | 124 |
|
---|
[8511] | 125 | }
|
---|
| 126 |
|
---|
[8498] | 127 | if (!results.ContainsKey(chartName)) {
|
---|
| 128 | this.chartName = chartName;
|
---|
| 129 | this.resultsCol = results;
|
---|
| 130 | this.xAxisTitle = xAxisTitle;
|
---|
| 131 | this.yAxisTitle = yAxisTitle;
|
---|
| 132 |
|
---|
| 133 | Initialize();
|
---|
[8515] | 134 | lastRowName = null;
|
---|
[8498] | 135 |
|
---|
| 136 | results.Add(new Result(chartName, Plot));
|
---|
[8511] | 137 | if (StoreHistory)
|
---|
| 138 | results.Add(new Result(chartName + " History", new ScatterPlotHistory()));
|
---|
[8498] | 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | public void AddPoint(string rowName, Point2D<double> point) {
|
---|
| 143 | if (!Plot.Rows.ContainsKey(rowName)) {
|
---|
[8511] | 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 |
|
---|
[8498] | 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);
|
---|
[8511] | 161 | lastRowName = rowName;
|
---|
[8498] | 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 | }
|
---|
[8867] | 172 |
|
---|
| 173 | public void CleanUp() {
|
---|
[9059] | 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)));
|
---|
[9252] | 177 |
|
---|
| 178 | dataTableHelper.CleanUp(); //just use the scaled chart and remove the original
|
---|
| 179 |
|
---|
| 180 | if (compressData) {
|
---|
| 181 | minMaxDataTableHelper.CleanUpAndCompressData();
|
---|
| 182 | }
|
---|
[9059] | 183 | }
|
---|
| 184 |
|
---|
[9252] | 185 | if (CreateDataTable && compressData && !trackMinMaxValues) {
|
---|
| 186 | dataTableHelper.CleanUpAndCompressData();
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[8867] | 189 | if (!StoreHistory && CreateDataTable) {
|
---|
| 190 | resultsCol[chartName].Value = null;
|
---|
| 191 | resultsCol.Remove(chartName);
|
---|
| 192 | plot = null;
|
---|
| 193 | }
|
---|
[9185] | 194 | min = double.MaxValue;
|
---|
| 195 | max = double.MinValue;
|
---|
[8867] | 196 | }
|
---|
[8498] | 197 | }
|
---|
| 198 | }
|
---|