Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2824 was 2824, checked in by gkronber, 14 years ago

Quick fix: ported CEDMA server form to HL.MainForm to be able to open and edit algorithms. #758 (CEDMA server should be based on new HL.MainForm)

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