Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.CEDMA.Charting/ModelView.cs @ 16124

Last change on this file since 16124 was 577, checked in by gkronber, 16 years ago

evaluate all samples for the model line chart

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