Free cookie consent management tool by TermsFeed Policy Generator

source: branches/plugins/HeuristicLab.CEDMA.Charting/3.2/ModelView.cs @ 2515

Last change on this file since 2515 was 703, checked in by gkronber, 16 years ago

fixed compilation errors in CEDMA plugins introduced with r702 (#328)

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