Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Server/3.3/DispatcherView.cs @ 2815

Last change on this file since 2815 was 2471, checked in by gkronber, 15 years ago

Fixed #786 (Allowed algorithms are not set correctly for all target variables)

File size: 10.6 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 System.Diagnostics;
11
12namespace HeuristicLab.CEDMA.Server {
13  public partial class DispatcherView : ViewBase {
14    private SimpleDispatcher dispatcher;
15    private ProblemSpecification selectedSpec;
16    public DispatcherView(SimpleDispatcher dispatcher)
17      : base() {
18      this.dispatcher = dispatcher;
19      InitializeComponent();
20      dispatcher.Changed += (sender, args) => UpdateControls();
21      UpdateControls();
22      this.inputVariableList.CheckOnClick = true;
23    }
24
25    protected override void UpdateControls() {
26      if (InvokeRequired) {
27        Invoke((Action)UpdateControls);
28      } else {
29        base.UpdateControls();
30        algorithmsListBox.Items.Clear();
31        targetVariableList.Items.Clear();
32        inputVariableList.Items.Clear();
33
34        foreach (string targetVar in dispatcher.TargetVariables) {
35          targetVariableList.Items.Add(targetVar, false);
36        }
37
38        foreach (string inputVar in dispatcher.Variables) {
39          inputVariableList.Items.Add(inputVar, false);
40        }
41
42        targetVariableList.ClearSelected();
43        inputVariableList.Enabled = false;
44      }
45    }
46
47    private void targetVariableList_ItemCheck(object sender, ItemCheckEventArgs e) {
48      if (e.NewValue == CheckState.Checked) {
49        dispatcher.EnableTargetVariable((string)targetVariableList.Items[e.Index]);
50      } else if (e.NewValue == CheckState.Unchecked) {
51        dispatcher.DisableTargetVariable((string)targetVariableList.Items[e.Index]);
52      }
53    }
54
55    private void inputVariableList_ItemCheck(object sender, ItemCheckEventArgs e) {
56      if (e.NewValue == CheckState.Checked) {
57        selectedSpec.AddInputVariable((string)inputVariableList.Items[e.Index]);
58      } else if (e.NewValue == CheckState.Unchecked) {
59        selectedSpec.RemoveInputVariable((string)inputVariableList.Items[e.Index]);
60      }
61    }
62
63    private void targetVariableList_SelectedValueChanged(object sender, EventArgs e) {
64      string selectedTarget = (string)targetVariableList.SelectedItem;
65      selectedSpec = dispatcher.GetProblemSpecification(selectedTarget);
66      UpdateInputVariableList();
67      UpdateAlgorithms();
68    }
69
70    private void UpdateAlgorithms() {
71      learningTaskGroupBox.Enabled = true;
72      algorithmsListBox.Enabled = true;
73      switch (selectedSpec.LearningTask) {
74        case LearningTask.Classification: {
75            classificationRadioButton.Checked = true;
76            break;
77          }
78        case LearningTask.Regression: {
79            regressionRadioButton.Checked = true;
80            break;
81          }
82        case LearningTask.TimeSeries: {
83            timeSeriesRadioButton.Checked = true;
84            break;
85          }
86        default: { break; }
87      }
88      algorithmsListBox.Items.Clear();
89      foreach (HeuristicLab.Modeling.IAlgorithm algo in dispatcher.GetAlgorithms(selectedSpec.LearningTask)) {
90        algorithmsListBox.Items.Add(algo, dispatcher.GetAllowedAlgorithms(selectedSpec.TargetVariable).Contains(algo));
91      }
92      UpdateAlgorithmConfiguration();
93    }
94
95    private void UpdateAlgorithmConfiguration() {
96      partitioningGroupBox.Enabled = true;
97      trainingSamplesStartTextBox.Text = selectedSpec.TrainingSamplesStart.ToString();
98      trainingSamplesEndTextBox.Text = selectedSpec.TrainingSamplesEnd.ToString();
99      validationSamplesStartTextBox.Text = selectedSpec.ValidationSamplesStart.ToString();
100      validationSamplesEndTextBox.Text = selectedSpec.ValidationSamplesEnd.ToString();
101      testSamplesStartTextBox.Text = selectedSpec.TestSamplesStart.ToString();
102      testSamplesEndTextBox.Text = selectedSpec.TestSamplesEnd.ToString();
103      minTimeOffsetTextBox.Text = selectedSpec.MinTimeOffset.ToString();
104      maxTimeOffsetTextBox.Text = selectedSpec.MaxTimeOffset.ToString();
105      autoregressiveCheckBox.Checked = selectedSpec.AutoRegressive;
106    }
107
108    private void UpdateInputVariableList() {
109      inputVariableList.Items.Clear();
110      var activatedInputVariables = selectedSpec.InputVariables;
111      foreach (string inputVar in dispatcher.Variables) {
112        inputVariableList.Items.Add(inputVar, activatedInputVariables.Contains(inputVar));
113      }
114      inputVariableList.Enabled = true;
115    }
116
117    private void setAllButton_Click(object sender, EventArgs e) {
118      foreach (string targetVar in dispatcher.TargetVariables) {
119        ProblemSpecification spec = dispatcher.GetProblemSpecification(targetVar);
120        for (int i = 0; i < inputVariableList.Items.Count; i++) {
121          if (inputVariableList.GetItemChecked(i)) {
122            spec.AddInputVariable((string)inputVariableList.Items[i]);
123          } else {
124            spec.RemoveInputVariable((string)inputVariableList.Items[i]);
125          }
126        }
127      }
128    }
129
130
131    private void setAlgorithmDefault_Click(object sender, EventArgs e) {
132      var newAllowedAlgos = dispatcher.GetAllowedAlgorithms(selectedSpec.TargetVariable).ToList();
133      foreach (string targetVar in dispatcher.TargetVariables) {
134        if (targetVar != selectedSpec.TargetVariable) {
135          ProblemSpecification spec = dispatcher.GetProblemSpecification(targetVar);
136          spec.LearningTask = selectedSpec.LearningTask;
137          spec.MinTimeOffset = selectedSpec.MinTimeOffset;
138          spec.MaxTimeOffset = selectedSpec.MaxTimeOffset;
139          spec.AutoRegressive = selectedSpec.AutoRegressive;
140          var curAllowedAlgos = dispatcher.GetAllowedAlgorithms(targetVar).ToList();
141          foreach (var algo in curAllowedAlgos)
142            dispatcher.DisableAlgorithm(targetVar, algo);
143          foreach (var algo in newAllowedAlgos)
144            dispatcher.EnableAlgorithm(targetVar, algo);
145        }
146      }
147    }
148
149    private void algorithmsListBox_ItemCheck(object sender, ItemCheckEventArgs e) {
150      if (e.NewValue == CheckState.Checked) {
151        dispatcher.EnableAlgorithm(selectedSpec.TargetVariable, (HeuristicLab.Modeling.IAlgorithm)algorithmsListBox.Items[e.Index]);
152      } else if (e.NewValue == CheckState.Unchecked) {
153        dispatcher.DisableAlgorithm(selectedSpec.TargetVariable, (HeuristicLab.Modeling.IAlgorithm)algorithmsListBox.Items[e.Index]);
154      }
155    }
156
157    private void radioButton_CheckedChanged(object sender, EventArgs e) {
158      minTimeOffsetLabel.Enabled = timeSeriesRadioButton.Checked;
159      minTimeOffsetTextBox.Enabled = timeSeriesRadioButton.Checked;
160      maxTimeOffsetLabel.Enabled = timeSeriesRadioButton.Checked;
161      maxTimeOffsetTextBox.Enabled = timeSeriesRadioButton.Checked;
162      autoregressiveCheckBox.Enabled = timeSeriesRadioButton.Checked;
163      autoregressiveLabel.Enabled = timeSeriesRadioButton.Checked;
164      if (timeSeriesRadioButton.Checked) selectedSpec.LearningTask = LearningTask.TimeSeries;
165      else if (classificationRadioButton.Checked) selectedSpec.LearningTask = LearningTask.Classification;
166      else if (regressionRadioButton.Checked) selectedSpec.LearningTask = LearningTask.Regression;
167      var allowedAlgorithms = dispatcher.GetAllowedAlgorithms(selectedSpec.TargetVariable);
168      foreach (var algo in allowedAlgorithms) {
169        dispatcher.DisableAlgorithm(selectedSpec.TargetVariable, algo);
170      }
171      UpdateAlgorithms();
172    }
173
174    private void timeOffsetTextBox_Validating(object sender, CancelEventArgs e) {
175      int min, max;
176      e.Cancel = !int.TryParse(minTimeOffsetTextBox.Text, out min);
177      e.Cancel = !int.TryParse(maxTimeOffsetTextBox.Text, out max);
178      e.Cancel = min > max;
179    }
180
181    private void timeOffsetTextBox_Validated(object sender, EventArgs e) {
182      selectedSpec.MinTimeOffset = int.Parse(minTimeOffsetTextBox.Text);
183      selectedSpec.MaxTimeOffset = int.Parse(maxTimeOffsetTextBox.Text);
184    }
185
186    private void samplesTextBox_Validated(object sender, EventArgs e) {
187      selectedSpec.TrainingSamplesStart = int.Parse(trainingSamplesStartTextBox.Text);
188      selectedSpec.TrainingSamplesEnd = int.Parse(trainingSamplesEndTextBox.Text);
189      selectedSpec.ValidationSamplesStart = int.Parse(validationSamplesStartTextBox.Text);
190      selectedSpec.ValidationSamplesEnd = int.Parse(validationSamplesEndTextBox.Text);
191      selectedSpec.TestSamplesStart = int.Parse(testSamplesStartTextBox.Text);
192      selectedSpec.TestSamplesEnd = int.Parse(testSamplesEndTextBox.Text);
193    }
194
195    private void ColorSamplesTextBoxes(Color color) {
196      trainingSamplesStartTextBox.BackColor = color;
197      trainingSamplesEndTextBox.BackColor = color;
198      validationSamplesStartTextBox.BackColor = color;
199      validationSamplesEndTextBox.BackColor = color;
200      testSamplesStartTextBox.BackColor = color;
201      testSamplesEndTextBox.BackColor = color;
202    }
203
204    private void samplesTextBox_Validating(object sender, CancelEventArgs e) {
205      try {
206        int trainingStart = int.Parse(trainingSamplesStartTextBox.Text);
207        int trainingEnd = int.Parse(trainingSamplesEndTextBox.Text);
208        int validationStart = int.Parse(validationSamplesStartTextBox.Text);
209        int validationEnd = int.Parse(validationSamplesEndTextBox.Text);
210        int testStart = int.Parse(testSamplesStartTextBox.Text);
211        int testEnd = int.Parse(testSamplesEndTextBox.Text);
212        if (trainingStart < 0 || validationStart < 0 || testStart < 0 ||
213          trainingEnd >= selectedSpec.Dataset.Rows || validationEnd >= selectedSpec.Dataset.Rows || testEnd >= selectedSpec.Dataset.Rows ||
214          trainingStart >= trainingEnd ||
215          validationStart >= validationEnd ||
216          testStart >= testEnd ||
217          IsOverlapping(trainingStart, trainingEnd, validationStart, validationEnd) ||
218          IsOverlapping(trainingStart, trainingEnd, testStart, testEnd) ||
219          IsOverlapping(validationStart, validationEnd, testStart, testEnd))
220          ColorSamplesTextBoxes(Color.Red);
221        else
222          ColorSamplesTextBoxes(Color.White);
223      }
224      catch (FormatException) {
225        ColorSamplesTextBoxes(Color.Red);
226      }
227    }
228
229    private void autoregressiveCheckBox_CheckedChanged(object sender, EventArgs e) {
230      selectedSpec.AutoRegressive = autoregressiveCheckBox.Checked;
231    }
232
233    private bool IsOverlapping(int x0, int y0, int x1, int y1) {
234      Trace.Assert(x0 <= y0 && x1 <= y1);
235      int tmp;
236      // make sure that x0,y0 is the left interval
237      if (x1 < x0) {
238        tmp = x1;
239        x1 = x0;
240        x0 = tmp;
241        tmp = y1;
242        y1 = y0;
243        y0 = tmp;
244      }
245      return y0 > x1;
246    }
247
248  }
249}
Note: See TracBrowser for help on using the repository browser.