[567] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.ComponentModel;
|
---|
| 4 | using System.Drawing;
|
---|
| 5 | using System.Data;
|
---|
| 6 | using System.Linq;
|
---|
| 7 | using System.Text;
|
---|
| 8 | using System.Windows.Forms;
|
---|
| 9 | using HeuristicLab.Core;
|
---|
| 10 | using HeuristicLab.Charting;
|
---|
| 11 | using HeuristicLab.Functions;
|
---|
| 12 | using HeuristicLab.Logging;
|
---|
| 13 | using HeuristicLab.DataAnalysis;
|
---|
| 14 | using HeuristicLab.Charting.Data;
|
---|
| 15 |
|
---|
| 16 | namespace HeuristicLab.CEDMA.Charting {
|
---|
| 17 | public partial class ModelView : ViewBase {
|
---|
| 18 | private Dataset dataset;
|
---|
| 19 | private IFunctionTree model;
|
---|
| 20 | private int targetVariable;
|
---|
| 21 |
|
---|
| 22 | public ModelView(Dataset dataset, IFunctionTree model, int targetVariable) {
|
---|
| 23 | InitializeComponent();
|
---|
| 24 | this.dataset = dataset;
|
---|
| 25 | this.model = model;
|
---|
| 26 | this.targetVariable = targetVariable;
|
---|
| 27 |
|
---|
| 28 | splitContainer.Panel1.Controls.Add((Control)model.CreateView());
|
---|
| 29 | splitContainer.Panel1.Controls[0].Dock = DockStyle.Fill;
|
---|
| 30 | lowerSplitContainer.Panel1.Controls.Add(CreateLineChart());
|
---|
| 31 | lowerSplitContainer.Panel1.Controls[0].Dock = DockStyle.Fill;
|
---|
| 32 | lowerSplitContainer.Panel2.Controls.Add(CreateScatterPlot());
|
---|
| 33 | lowerSplitContainer.Panel2.Controls[0].Dock = DockStyle.Fill;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | private Control CreateScatterPlot() {
|
---|
| 37 | double minX = double.PositiveInfinity;
|
---|
| 38 | double maxX = double.NegativeInfinity;
|
---|
| 39 |
|
---|
| 40 | Datachart chart = new Datachart(0, 0, 1, 1);
|
---|
| 41 | chart.Title = "Scatter plot";
|
---|
| 42 | chart.UpdateEnabled = false;
|
---|
| 43 | Pen pen = new Pen(Color.FromArgb(80, Color.Blue));
|
---|
| 44 | chart.AddDataRow(DataRowType.Points, pen, pen.Brush);
|
---|
| 45 | IEvaluator eval = model.CreateEvaluator();
|
---|
| 46 | eval.ResetEvaluator(model, dataset);
|
---|
| 47 | for(int i = 0; i < dataset.Rows; i += 10) {
|
---|
| 48 | double predicted = eval.Evaluate(i);
|
---|
| 49 | double original = dataset.GetValue(i, targetVariable);
|
---|
| 50 | if(double.IsInfinity(predicted) || predicted == double.MaxValue || predicted == double.MinValue) predicted = double.NaN;
|
---|
| 51 | if(double.IsInfinity(original) || original == double.MaxValue || original == double.MinValue) original = double.NaN;
|
---|
| 52 | if(!double.IsNaN(predicted) && !double.IsNaN(original)) {
|
---|
| 53 | chart.AddDataPoint(0, original, predicted);
|
---|
| 54 | if(original < minX) minX = original;
|
---|
| 55 | if(original > maxX) maxX = original;
|
---|
| 56 | if(predicted < minX) minX = predicted;
|
---|
| 57 | if(predicted > maxX) maxX = predicted;
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 | chart.UpdateEnabled = true;
|
---|
| 61 | chart.Group.Add(new Axis(chart, (maxX - minX) / 2 + minX, (maxX - minX) / 2+minX, AxisType.Both));
|
---|
| 62 | DataChartControl control = new DataChartControl();
|
---|
| 63 | control.Chart = chart;
|
---|
| 64 | if(minX < maxX) {
|
---|
| 65 | double xExcess = (maxX - minX) * 0.1;
|
---|
| 66 | chart.ZoomIn(minX - xExcess, minX-xExcess, maxX + xExcess, maxX + xExcess);
|
---|
| 67 | control.ScaleOnResize = false;
|
---|
| 68 | }
|
---|
| 69 | return control;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | private Control CreateLineChart() {
|
---|
| 73 | double minY = double.PositiveInfinity;
|
---|
| 74 | double maxY = double.NegativeInfinity;
|
---|
| 75 |
|
---|
| 76 | Datachart chart = new Datachart(0, 0, 1, 1);
|
---|
| 77 | chart.Title = "Linechart";
|
---|
| 78 | chart.UpdateEnabled = false;
|
---|
| 79 | Pen redPen = new Pen(Color.FromArgb(180, Color.Red));
|
---|
| 80 | Pen bluePen = new Pen(Color.FromArgb(180, Color.Blue));
|
---|
| 81 | chart.AddDataRow(DataRowType.Lines, bluePen, bluePen.Brush);
|
---|
| 82 | chart.AddDataRow(DataRowType.Lines, redPen, redPen.Brush);
|
---|
| 83 | IEvaluator eval = model.CreateEvaluator();
|
---|
| 84 | eval.ResetEvaluator(model, dataset);
|
---|
| 85 | int n = 0;
|
---|
| 86 | for(int i = 0; i < dataset.Rows; i += 10) {
|
---|
| 87 | double predicted = eval.Evaluate(i);
|
---|
| 88 | double original = dataset.GetValue(i, targetVariable);
|
---|
| 89 | if(double.IsInfinity(predicted) || predicted == double.MaxValue || predicted == double.MinValue) predicted = double.NaN;
|
---|
| 90 | if(double.IsInfinity(original) || original == double.MaxValue || original == double.MinValue) original = double.NaN;
|
---|
| 91 | if(!double.IsNaN(predicted) && !double.IsNaN(original)) {
|
---|
| 92 | chart.AddDataPoint(0, i, predicted);
|
---|
| 93 | chart.AddDataPoint(1, i, original);
|
---|
| 94 | n++;
|
---|
| 95 | if(original < minY) minY = original;
|
---|
| 96 | if(original > maxY) maxY = original;
|
---|
| 97 | if(predicted < minY) minY = predicted;
|
---|
| 98 | if(predicted > maxY) maxY = predicted;
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | chart.UpdateEnabled = true;
|
---|
| 102 | chart.Group.Add(new Axis(chart, 0, minY, AxisType.Both));
|
---|
| 103 | DataChartControl control = new DataChartControl();
|
---|
| 104 | control.Chart = chart;
|
---|
| 105 | if(minY < maxY) {
|
---|
| 106 | double xExcess = dataset.Rows * 0.05;
|
---|
| 107 | double yExcess = (maxY - minY) * 0.1;
|
---|
| 108 | chart.ZoomIn(0.0 - xExcess, minY - yExcess, (double)dataset.Rows + xExcess, maxY + yExcess);
|
---|
| 109 | control.ScaleOnResize = false;
|
---|
| 110 | }
|
---|
| 111 | return control;
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | }
|
---|