Free cookie consent management tool by TermsFeed Policy Generator

source: branches/crossvalidation-2434/HeuristicLab.Clients.OKB.Views/3.3/RunCreation/Views/OKBProblemView.cs @ 14029

Last change on this file since 14029 was 14029, checked in by gkronber, 8 years ago

#2434: merged trunk changes r12934:14026 from trunk to branch

File size: 12.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using HeuristicLab.Common.Resources;
23using HeuristicLab.Core;
24using HeuristicLab.Core.Views;
25using HeuristicLab.Data;
26using HeuristicLab.MainForm;
27using HeuristicLab.Optimization;
28using HeuristicLab.PluginInfrastructure;
29using System;
30using System.Collections.Generic;
31using System.Drawing;
32using System.Linq;
33using System.Windows.Forms;
34
35namespace HeuristicLab.Clients.OKB.RunCreation {
36  [View("OKBProblem View")]
37  [Content(typeof(SingleObjectiveOKBProblem), true)]
38  [Content(typeof(MultiObjectiveOKBProblem), true)]
39  public sealed partial class OKBProblemView : NamedItemView {
40    private readonly CheckedItemList<ICharacteristicCalculator> calculatorList;
41
42    public new OKBProblem Content {
43      get { return (OKBProblem)base.Content; }
44      set { base.Content = value; }
45    }
46
47    public OKBProblemView() {
48      InitializeComponent();
49      var calculatorListView = new CheckedItemListView<ICharacteristicCalculator>() {
50        Anchor = AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Top,
51        Location = new Point(flaSplitContainer.Padding.Left, calculateButton.Location.Y + calculateButton.Height + calculateButton.Padding.Bottom + 3),
52      };
53      calculatorListView.Size = new Size(flaSplitContainer.Panel1.Size.Width - flaSplitContainer.Panel1.Padding.Horizontal,
54          flaSplitContainer.Panel1.Height - calculatorListView.Location.Y - flaSplitContainer.Panel1.Padding.Bottom);
55      calculatorList = new CheckedItemList<ICharacteristicCalculator>();
56      calculatorList.ItemsAdded += CalculatorListOnChanged;
57      calculatorList.ItemsRemoved += CalculatorListOnChanged;
58      calculatorList.ItemsReplaced += CalculatorListOnChanged;
59      calculatorList.CollectionReset += CalculatorListOnChanged;
60      calculatorList.CheckedItemsChanged += CalculatorListOnChanged;
61
62      calculatorListView.Content = calculatorList.AsReadOnly();
63
64      flaSplitContainer.Panel1.Controls.Add(calculatorListView);
65      calculateButton.Text = string.Empty;
66      calculateButton.Image = VSImageLibrary.Play;
67      refreshButton.Text = string.Empty;
68      refreshButton.Image = VSImageLibrary.Refresh;
69      cloneProblemButton.Text = string.Empty;
70      cloneProblemButton.Image = VSImageLibrary.Clone;
71      downloadCharacteristicsButton.Text = string.Empty;
72      downloadCharacteristicsButton.Image = VSImageLibrary.Refresh;
73      uploadCharacteristicsButton.Text = string.Empty;
74      uploadCharacteristicsButton.Image = VSImageLibrary.PublishToWeb;
75      refreshSolutionsButton.Text = string.Empty;
76      refreshSolutionsButton.Image = VSImageLibrary.Refresh;
77      uploadSolutionsButton.Text = string.Empty;
78      uploadSolutionsButton.Image = VSImageLibrary.PublishToWeb;
79    }
80
81    private void CalculatorListOnChanged(object sender, EventArgs e) {
82      SetEnabledStateOfControls();
83    }
84
85    protected override void OnInitialized(System.EventArgs e) {
86      base.OnInitialized(e);
87      RunCreationClient.Instance.Refreshing += new EventHandler(RunCreationClient_Refreshing);
88      RunCreationClient.Instance.Refreshed += new EventHandler(RunCreationClient_Refreshed);
89      PopulateComboBox();
90    }
91
92    protected override void DeregisterContentEvents() {
93      Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
94      base.DeregisterContentEvents();
95    }
96    protected override void RegisterContentEvents() {
97      base.RegisterContentEvents();
98      Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
99      Content.Solutions.ItemsAdded += SolutionsOnChanged;
100      Content.Solutions.ItemsReplaced += SolutionsOnChanged;
101      Content.Solutions.ItemsRemoved += SolutionsOnChanged;
102      Content.Solutions.CollectionReset += SolutionsOnChanged;
103    }
104
105    private void SolutionsOnChanged(object sender, EventArgs e) {
106      if (InvokeRequired) { Invoke((Action<object, EventArgs>)SolutionsOnChanged, sender, e); return; }
107      SetEnabledStateOfControls();
108    }
109
110    protected override void OnContentChanged() {
111      base.OnContentChanged();
112      if (Content == null) {
113        problemComboBox.SelectedIndex = -1;
114        parameterCollectionView.Content = null;
115        solutionsViewHost.Content = null;
116      } else {
117        problemComboBox.SelectedItem = RunCreationClient.Instance.Problems.FirstOrDefault(x => x.Id == Content.ProblemId);
118        parameterCollectionView.Content = Content.Parameters;
119        solutionsViewHost.Content = Content.Solutions;
120      }
121      UpdateCharacteristicCalculators();
122    }
123
124    protected override void SetEnabledStateOfControls() {
125      base.SetEnabledStateOfControls();
126      problemComboBox.Enabled = (Content != null) && !ReadOnly && !Locked && (problemComboBox.Items.Count > 0);
127      cloneProblemButton.Enabled = (Content != null) && (Content.ProblemId != -1) && !ReadOnly && !Locked;
128      refreshButton.Enabled = (Content != null) && !ReadOnly && !Locked;
129      parameterCollectionView.Enabled = Content != null;
130      characteristicsMatrixView.Enabled = Content != null;
131      downloadCharacteristicsButton.Enabled = Content != null && Content.ProblemId != -1 && !Locked;
132      uploadCharacteristicsButton.Enabled = Content != null && Content.ProblemId != -1 && !Locked && !ReadOnly
133        && characteristicsMatrixView.Content != null && characteristicsMatrixView.Content.Rows > 0;
134      calculateButton.Enabled = Content != null && Content.ProblemId != -1 && !Locked && !ReadOnly && calculatorList.CheckedItems.Any();
135      refreshSolutionsButton.Enabled = Content != null && !ReadOnly && !Locked && Content.ProblemId != -1;
136      uploadSolutionsButton.Enabled = Content != null && !ReadOnly && !Locked && Content.ProblemId != -1 && Content.Solutions.Any(x => x.SolutionId == -1);
137    }
138
139    protected override void OnClosed(FormClosedEventArgs e) {
140      RunCreationClient.Instance.Refreshing -= new EventHandler(RunCreationClient_Refreshing);
141      RunCreationClient.Instance.Refreshed -= new EventHandler(RunCreationClient_Refreshed);
142      base.OnClosed(e);
143    }
144
145    private void UpdateCharacteristicCalculators() {
146      calculatorList.Clear();
147      if (Content == null || Content.ProblemId == -1) return;
148      var problem = Content.CloneProblem();
149      var calculators = ApplicationManager.Manager.GetInstances<ICharacteristicCalculator>().ToList();
150      foreach (var calc in calculators) {
151        calc.Problem = problem;
152        if (!calc.CanCalculate()) continue;
153        calculatorList.Add(calc, true);
154      }
155    }
156
157    private void RunCreationClient_Refreshing(object sender, EventArgs e) {
158      if (InvokeRequired) {
159        Invoke(new EventHandler(RunCreationClient_Refreshing), sender, e);
160      } else {
161        Cursor = Cursors.AppStarting;
162        problemComboBox.Enabled = cloneProblemButton.Enabled = refreshButton.Enabled = parameterCollectionView.Enabled = false;
163      }
164    }
165    private void RunCreationClient_Refreshed(object sender, EventArgs e) {
166      if (InvokeRequired) {
167        Invoke(new EventHandler(RunCreationClient_Refreshed), sender, e);
168      } else {
169        PopulateComboBox();
170        SetEnabledStateOfControls();
171        Cursor = Cursors.Default;
172      }
173    }
174
175    #region Content Events
176    private void Content_ProblemChanged(object sender, EventArgs e) {
177      if (InvokeRequired)
178        Invoke(new EventHandler(Content_ProblemChanged), sender, e);
179      else {
180        OnContentChanged();
181        SetEnabledStateOfControls();
182      }
183    }
184    #endregion
185
186    #region Control Events
187    private void cloneProblemButton_Click(object sender, EventArgs e) {
188      MainFormManager.MainForm.ShowContent(Content.CloneProblem());
189    }
190    private void refreshButton_Click(object sender, System.EventArgs e) {
191      RunCreationClient.Instance.Refresh();
192    }
193    private void problemComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
194      Problem problem = problemComboBox.SelectedValue as Problem;
195      if ((problem != null) && (Content != null)) {
196        Content.Load(problem.Id);
197        if (Content.ProblemId != problem.Id)  // reset selected item if load was not successful
198          problemComboBox.SelectedItem = RunCreationClient.Instance.Problems.FirstOrDefault(x => x.Id == Content.ProblemId);
199      }
200    }
201    private void downloadCharacteristicsButton_Click(object sender, EventArgs e) {
202      var values = RunCreationClient.Instance.GetCharacteristicValues(Content.ProblemId).ToList();
203      var content = new StringMatrix(values.Count, 3);
204      for (var i = 0; i < values.Count; i++) {
205        content[i, 0] = values[i].Name;
206        content[i, 1] = values[i].GetValue();
207        content[i, 2] = values[i].GetType().Name;
208      }
209      characteristicsMatrixView.Content = content;
210      SetEnabledStateOfControls();
211    }
212    private void uploadCharacteristicsButton_Click(object sender, EventArgs e) {
213      var matrix = characteristicsMatrixView.Content as StringMatrix;
214      if (matrix == null) return;
215      var values = new List<Value>(matrix.Rows);
216      for (var i = 0; i < matrix.Rows; i++) {
217        var name = matrix[i, 0];
218        var strValue = matrix[i, 1];
219        var type = matrix[i, 2];
220        values.Add(Value.Create(name, strValue, type));
221      }
222      try {
223        RunCreationClient.Instance.SetCharacteristicValues(Content.ProblemId, values);
224      } catch (Exception ex) { ErrorHandling.ShowErrorDialog(ex); }
225    }
226    private void calculateButton_Click(object sender, EventArgs e) {
227      var calculators = calculatorList.CheckedItems.Select(x => x.Value).Where(x => x.CanCalculate()).ToList();
228      if (calculators.Count == 0) return;
229
230      var results = new Dictionary<string, Value>();
231      foreach (var calc in calculators) {
232        foreach (var result in calc.Calculate())
233          results[result.Name] = RunCreationClient.Instance.ConvertToValue(result.Value, result.Name);
234      }
235      var matrix = (characteristicsMatrixView.Content as StringMatrix) ?? (new StringMatrix(results.Count, 3));
236      try {
237        for (var i = 0; i < matrix.Rows; i++) {
238          Value r;
239          if (results.TryGetValue(matrix[i, 0], out r)) {
240            matrix[i, 1] = r.GetValue();
241            matrix[i, 2] = r.GetType().Name;
242            results.Remove(matrix[i, 0]);
243          }
244        }
245        if (results.Count == 0) return;
246        var resultsList = results.ToList();
247        var counter = resultsList.Count - 1;
248        for (var i = 0; i < matrix.Rows; i++) {
249          if (string.IsNullOrEmpty(matrix[i, 0])) {
250            matrix[i, 0] = resultsList[counter].Key;
251            matrix[i, 1] = resultsList[counter].Value.GetValue();
252            matrix[i, 2] = resultsList[counter].Value.GetType().Name;
253            resultsList.RemoveAt(counter);
254            counter--;
255            if (counter < 0) return;
256          }
257        }
258        if (counter >= 0) {
259          ((IStringConvertibleMatrix)matrix).Rows += counter + 1;
260          for (var i = matrix.Rows - 1; counter >= 0; i--) {
261            matrix[i, 0] = resultsList[0].Key;
262            matrix[i, 1] = resultsList[0].Value.GetValue();
263            matrix[i, 2] = resultsList[0].Value.GetType().Name;
264            resultsList.RemoveAt(0);
265            counter--;
266          }
267        }
268      } finally {
269        characteristicsMatrixView.Content = matrix;
270        SetEnabledStateOfControls();
271      }
272    }
273    #endregion
274
275    #region Helpers
276    private void PopulateComboBox() {
277      problemComboBox.DataSource = null;
278      problemComboBox.DataSource = RunCreationClient.Instance.Problems.ToList();
279      problemComboBox.DisplayMember = "Name";
280    }
281    #endregion
282
283    private void refreshSolutionsButton_Click(object sender, EventArgs e) {
284      Content.RefreshSolutions();
285    }
286
287    private void uploadSolutionsButton_Click(object sender, EventArgs e) {
288      foreach (var solution in Content.Solutions.Where(x => x.SolutionId == -1))
289        solution.Upload();
290      SetEnabledStateOfControls();
291    }
292
293  }
294}
Note: See TracBrowser for help on using the repository browser.