1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Windows.Forms;
|
---|
5 | using HeuristicLab.Core.Views;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
8 | using HeuristicLab.MainForm;
|
---|
9 | using HeuristicLab.Optimization;
|
---|
10 | using HeuristicLab.Optimization.Views;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
|
---|
13 | [View("Sliding Window Best Solutions View")]
|
---|
14 | [Content(typeof(SlidingWindowBestSolutionsCollection), true)]
|
---|
15 | public partial class SlidingWindowBestSolutionsCollectionView : ItemView {
|
---|
16 | public new SlidingWindowBestSolutionsCollection Content {
|
---|
17 | get { return (SlidingWindowBestSolutionsCollection)base.Content; }
|
---|
18 | set { base.Content = value; }
|
---|
19 | }
|
---|
20 | public SlidingWindowBestSolutionsCollectionView() {
|
---|
21 | InitializeComponent();
|
---|
22 |
|
---|
23 | qualityMeasure = (int)QualityMeasures.PEARSON;
|
---|
24 | }
|
---|
25 |
|
---|
26 | enum QualityMeasures { PEARSON, MSE };
|
---|
27 |
|
---|
28 | private int qualityMeasure;
|
---|
29 |
|
---|
30 | protected override void DeregisterContentEvents() {
|
---|
31 | // TODO: Deregister your event handlers here
|
---|
32 | enhancedStringConvertibleMatrixView.DataGridView.CellDoubleClick -= enhancedStringConvertibleMatrixView_cellDoubleClick;
|
---|
33 | base.DeregisterContentEvents();
|
---|
34 | }
|
---|
35 |
|
---|
36 | protected override void RegisterContentEvents() {
|
---|
37 | base.RegisterContentEvents();
|
---|
38 | enhancedStringConvertibleMatrixView.DataGridView.CellDoubleClick += enhancedStringConvertibleMatrixView_cellDoubleClick;
|
---|
39 | // TODO: Register your event handlers here
|
---|
40 | }
|
---|
41 |
|
---|
42 | public void enhancedStringConvertibleMatrixView_cellDoubleClick(object sender, DataGridViewCellEventArgs e) {
|
---|
43 | var dataGridView = (DataGridView)sender;
|
---|
44 | var cell = dataGridView.SelectedCells[0];
|
---|
45 | var tree = Content.BestSolutions[cell.RowIndex];
|
---|
46 | var model = Content.CreateModel(tree, Content.Interpreter);
|
---|
47 | var solution = Content.CreateSolution(model, Content.ProblemData);
|
---|
48 | var result = new Result("Sliding Window Solution", solution);
|
---|
49 | var view = MainFormManager.MainForm.ShowContent(result, typeof(ResultView));
|
---|
50 | if (view != null) {
|
---|
51 | view.ReadOnly = ReadOnly;
|
---|
52 | view.Locked = Locked;
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | #region Event Handlers (Content)
|
---|
57 | // TODO: Put event handlers of the content here
|
---|
58 | #endregion
|
---|
59 |
|
---|
60 | protected override void OnContentChanged() {
|
---|
61 | base.OnContentChanged();
|
---|
62 | if (Content == null) {
|
---|
63 | // TODO: Add code when content has been changed and is null
|
---|
64 | } else {
|
---|
65 | // TODO: Add code when content has been changed and is not null
|
---|
66 | UpdateQualitiesMap();
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | protected override void SetEnabledStateOfControls() {
|
---|
71 | base.SetEnabledStateOfControls();
|
---|
72 | // TODO: Enable or disable controls based on whether the content is null or the view is set readonly
|
---|
73 | }
|
---|
74 |
|
---|
75 | #region Event Handlers (child controls)
|
---|
76 | // TODO: Put event handlers of child controls here.
|
---|
77 | #endregion
|
---|
78 |
|
---|
79 | private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e) {
|
---|
80 | var combobox = (ComboBox)sender;
|
---|
81 | switch ((string)combobox.SelectedItem) {
|
---|
82 | case "Pearson R2": {
|
---|
83 | qualityMeasure = (int)QualityMeasures.PEARSON;
|
---|
84 | break;
|
---|
85 | }
|
---|
86 | case "Mean Squared Error": {
|
---|
87 | qualityMeasure = (int)QualityMeasures.MSE;
|
---|
88 | break;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | UpdateQualitiesMap();
|
---|
92 | }
|
---|
93 |
|
---|
94 | private void UpdateQualitiesMap() {
|
---|
95 | var qualitiesMap = new DoubleMatrix(Content.BestSolutions.Count, Content.SlidingWindowPositions.Count + 1);
|
---|
96 | qualitiesMap.ColumnNames = Content.SlidingWindowPositions.Select(x => x.Start + "-" + x.End).Concat(new List<string> { "Training" });
|
---|
97 | qualitiesMap.RowNames = Enumerable.Range(1, Content.BestSolutions.Count).Select(x => "M" + x);
|
---|
98 | double min = 0, max = 0;
|
---|
99 |
|
---|
100 | for (int i = 0; i < qualitiesMap.Columns - 1; ++i) {
|
---|
101 | var pos = Content.SlidingWindowPositions[i];
|
---|
102 | var rows = Enumerable.Range(pos.Start, pos.End - pos.Start).ToList();
|
---|
103 |
|
---|
104 | for (int j = 0; j < qualitiesMap.Rows; ++j) {
|
---|
105 | var tree = Content.BestSolutions[j];
|
---|
106 | var q = CalculateQuality(tree, rows);
|
---|
107 | if (min > q) min = q;
|
---|
108 | if (max < q) max = q;
|
---|
109 | qualitiesMap[j, i] = q;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | // deal separately with the last column which represents the whole training partition interval
|
---|
113 | for (int j = 0; j < qualitiesMap.Rows; ++j) {
|
---|
114 | var tree = Content.BestSolutions[j];
|
---|
115 | var q = CalculateQuality(tree, Content.ProblemData.TrainingIndices);
|
---|
116 | if (min > q) min = q;
|
---|
117 | if (max < q) max = q;
|
---|
118 | qualitiesMap[j, qualitiesMap.Columns - 1] = q;
|
---|
119 | }
|
---|
120 |
|
---|
121 | enhancedStringConvertibleMatrixView.Minimum = min;
|
---|
122 | enhancedStringConvertibleMatrixView.Maximum = max;
|
---|
123 | enhancedStringConvertibleMatrixView.Content = qualitiesMap;
|
---|
124 | }
|
---|
125 |
|
---|
126 | private string GetTargetVariable(IDataAnalysisProblemData problemData) {
|
---|
127 | var regressionProblemData = problemData as IRegressionProblemData;
|
---|
128 | var classificationProblemData = problemData as IClassificationProblemData;
|
---|
129 | if (regressionProblemData != null) return regressionProblemData.TargetVariable;
|
---|
130 | if (classificationProblemData != null) return classificationProblemData.TargetVariable;
|
---|
131 | throw new NotSupportedException();
|
---|
132 | }
|
---|
133 |
|
---|
134 | private double CalculateQuality(ISymbolicExpressionTree tree, IEnumerable<int> rows) {
|
---|
135 | var estimatedValues = Content.Interpreter.GetSymbolicExpressionTreeValues(tree, Content.ProblemData.Dataset, rows);
|
---|
136 | var originalValues = Content.ProblemData.Dataset.GetDoubleValues(GetTargetVariable(Content.ProblemData), rows);
|
---|
137 | double quality = 0;
|
---|
138 | var errorState = new OnlineCalculatorError();
|
---|
139 | switch ((QualityMeasures)qualityMeasure) {
|
---|
140 | case QualityMeasures.PEARSON:
|
---|
141 | quality = OnlinePearsonsRSquaredCalculator.Calculate(estimatedValues, originalValues, out errorState);
|
---|
142 | break;
|
---|
143 | case QualityMeasures.MSE:
|
---|
144 | quality = OnlineMeanSquaredErrorCalculator.Calculate(estimatedValues, originalValues, out errorState);
|
---|
145 | break;
|
---|
146 | }
|
---|
147 | return errorState == OnlineCalculatorError.None ? quality : double.NaN;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|