[8055] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8055] | 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 |
|
---|
| 22 | using System;
|
---|
[13534] | 23 | using System.Collections.Generic;
|
---|
[8055] | 24 | using System.Linq;
|
---|
| 25 | using System.Windows.Forms;
|
---|
[13534] | 26 | using HeuristicLab.Common.Resources;
|
---|
[8055] | 27 | using HeuristicLab.Core.Views;
|
---|
[13534] | 28 | using HeuristicLab.Data;
|
---|
[8055] | 29 | using HeuristicLab.MainForm;
|
---|
[13540] | 30 | using HeuristicLab.Optimization;
|
---|
| 31 | using HeuristicLab.PluginInfrastructure;
|
---|
[8055] | 32 |
|
---|
| 33 | namespace HeuristicLab.Clients.OKB.RunCreation {
|
---|
| 34 | [View("OKBProblem View")]
|
---|
| 35 | [Content(typeof(SingleObjectiveOKBProblem), true)]
|
---|
| 36 | [Content(typeof(MultiObjectiveOKBProblem), true)]
|
---|
| 37 | public sealed partial class OKBProblemView : NamedItemView {
|
---|
| 38 | public new OKBProblem Content {
|
---|
| 39 | get { return (OKBProblem)base.Content; }
|
---|
| 40 | set { base.Content = value; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public OKBProblemView() {
|
---|
| 44 | InitializeComponent();
|
---|
[13540] | 45 | refreshButton.Text = string.Empty;
|
---|
| 46 | refreshButton.Image = VSImageLibrary.Refresh;
|
---|
| 47 | cloneProblemButton.Text = string.Empty;
|
---|
| 48 | cloneProblemButton.Image = VSImageLibrary.Clone;
|
---|
[13534] | 49 | downloadCharacteristicsButton.Text = string.Empty;
|
---|
| 50 | downloadCharacteristicsButton.Image = VSImageLibrary.Refresh;
|
---|
| 51 | uploadCharacteristicsButton.Text = string.Empty;
|
---|
| 52 | uploadCharacteristicsButton.Image = VSImageLibrary.PublishToWeb;
|
---|
[8055] | 53 | }
|
---|
| 54 |
|
---|
| 55 | protected override void OnInitialized(System.EventArgs e) {
|
---|
| 56 | base.OnInitialized(e);
|
---|
| 57 | RunCreationClient.Instance.Refreshing += new EventHandler(RunCreationClient_Refreshing);
|
---|
| 58 | RunCreationClient.Instance.Refreshed += new EventHandler(RunCreationClient_Refreshed);
|
---|
| 59 | PopulateComboBox();
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | protected override void DeregisterContentEvents() {
|
---|
| 63 | Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
|
---|
| 64 | base.DeregisterContentEvents();
|
---|
| 65 | }
|
---|
| 66 | protected override void RegisterContentEvents() {
|
---|
| 67 | base.RegisterContentEvents();
|
---|
| 68 | Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | protected override void OnContentChanged() {
|
---|
| 72 | base.OnContentChanged();
|
---|
| 73 | if (Content == null) {
|
---|
| 74 | problemComboBox.SelectedIndex = -1;
|
---|
| 75 | parameterCollectionView.Content = null;
|
---|
| 76 | } else {
|
---|
| 77 | problemComboBox.SelectedItem = RunCreationClient.Instance.Problems.FirstOrDefault(x => x.Id == Content.ProblemId);
|
---|
| 78 | parameterCollectionView.Content = Content.Parameters;
|
---|
| 79 | }
|
---|
[13540] | 80 | UpdateCharacteristicCalculators();
|
---|
[8055] | 81 | }
|
---|
| 82 |
|
---|
| 83 | protected override void SetEnabledStateOfControls() {
|
---|
| 84 | base.SetEnabledStateOfControls();
|
---|
| 85 | problemComboBox.Enabled = (Content != null) && !ReadOnly && !Locked && (problemComboBox.Items.Count > 0);
|
---|
| 86 | cloneProblemButton.Enabled = (Content != null) && (Content.ProblemId != -1) && !ReadOnly && !Locked;
|
---|
| 87 | refreshButton.Enabled = (Content != null) && !ReadOnly && !Locked;
|
---|
| 88 | parameterCollectionView.Enabled = Content != null;
|
---|
[13534] | 89 | characteristicsMatrixView.Enabled = Content != null;
|
---|
| 90 | downloadCharacteristicsButton.Enabled = Content != null && Content.ProblemId != -1 && !Locked;
|
---|
| 91 | uploadCharacteristicsButton.Enabled = Content != null && Content.ProblemId != -1 && !Locked && !ReadOnly
|
---|
| 92 | && characteristicsMatrixView.Content != null && characteristicsMatrixView.Content.Rows > 0;
|
---|
[13550] | 93 | calculateButton.Enabled = Content != null && Content.ProblemId != -1 && !Locked && !ReadOnly && calculatorListView.CheckedItems.Count > 0;
|
---|
[8055] | 94 | }
|
---|
| 95 |
|
---|
| 96 | protected override void OnClosed(FormClosedEventArgs e) {
|
---|
| 97 | RunCreationClient.Instance.Refreshing -= new EventHandler(RunCreationClient_Refreshing);
|
---|
| 98 | RunCreationClient.Instance.Refreshed -= new EventHandler(RunCreationClient_Refreshed);
|
---|
| 99 | base.OnClosed(e);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[13540] | 102 | private void UpdateCharacteristicCalculators() {
|
---|
| 103 | calculatorListView.Items.Clear();
|
---|
| 104 | calculatorListView.Groups.Clear();
|
---|
| 105 | if (Content == null || Content.ProblemId == -1) return;
|
---|
| 106 | var problem = Content.CloneProblem();
|
---|
| 107 | var calculators = ApplicationManager.Manager.GetInstances<ICharacteristicCalculator>().Where(x => x.CanCalculate(problem)).ToList();
|
---|
| 108 | try {
|
---|
| 109 | calculatorListView.BeginUpdate();
|
---|
| 110 | foreach (var calc in calculators) {
|
---|
| 111 | var group = calculatorListView.Groups.Add(calc.Name, calc.Name);
|
---|
| 112 | group.Tag = calc;
|
---|
| 113 | foreach (var c in calc.Characteristics) {
|
---|
| 114 | var item = calculatorListView.Items.Add(c, c);
|
---|
| 115 | item.Group = group;
|
---|
[13550] | 116 | item.Checked = true;
|
---|
[13540] | 117 | }
|
---|
| 118 | }
|
---|
| 119 | } finally { calculatorListView.EndUpdate(); }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[8055] | 122 | private void RunCreationClient_Refreshing(object sender, EventArgs e) {
|
---|
| 123 | if (InvokeRequired) {
|
---|
| 124 | Invoke(new EventHandler(RunCreationClient_Refreshing), sender, e);
|
---|
| 125 | } else {
|
---|
| 126 | Cursor = Cursors.AppStarting;
|
---|
| 127 | problemComboBox.Enabled = cloneProblemButton.Enabled = refreshButton.Enabled = parameterCollectionView.Enabled = false;
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 | private void RunCreationClient_Refreshed(object sender, EventArgs e) {
|
---|
| 131 | if (InvokeRequired) {
|
---|
| 132 | Invoke(new EventHandler(RunCreationClient_Refreshed), sender, e);
|
---|
| 133 | } else {
|
---|
| 134 | PopulateComboBox();
|
---|
| 135 | SetEnabledStateOfControls();
|
---|
| 136 | Cursor = Cursors.Default;
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | #region Content Events
|
---|
| 141 | private void Content_ProblemChanged(object sender, EventArgs e) {
|
---|
| 142 | if (InvokeRequired)
|
---|
| 143 | Invoke(new EventHandler(Content_ProblemChanged), sender, e);
|
---|
[13534] | 144 | else {
|
---|
[8055] | 145 | OnContentChanged();
|
---|
[13534] | 146 | SetEnabledStateOfControls();
|
---|
| 147 | }
|
---|
[8055] | 148 | }
|
---|
| 149 | #endregion
|
---|
| 150 |
|
---|
| 151 | #region Control Events
|
---|
| 152 | private void cloneProblemButton_Click(object sender, EventArgs e) {
|
---|
| 153 | MainFormManager.MainForm.ShowContent(Content.CloneProblem());
|
---|
| 154 | }
|
---|
| 155 | private void refreshButton_Click(object sender, System.EventArgs e) {
|
---|
| 156 | RunCreationClient.Instance.Refresh();
|
---|
| 157 | }
|
---|
| 158 | private void problemComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
|
---|
| 159 | Problem problem = problemComboBox.SelectedValue as Problem;
|
---|
| 160 | if ((problem != null) && (Content != null)) {
|
---|
| 161 | Content.Load(problem.Id);
|
---|
| 162 | if (Content.ProblemId != problem.Id) // reset selected item if load was not successful
|
---|
| 163 | problemComboBox.SelectedItem = RunCreationClient.Instance.Problems.FirstOrDefault(x => x.Id == Content.ProblemId);
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
[13534] | 166 | private void downloadCharacteristicsButton_Click(object sender, EventArgs e) {
|
---|
| 167 | var values = RunCreationClient.GetCharacteristicValues(Content.ProblemId).ToList();
|
---|
| 168 | var content = new StringMatrix(values.Count, 3);
|
---|
| 169 | for (var i = 0; i < values.Count; i++) {
|
---|
| 170 | content[i, 0] = values[i].Name;
|
---|
[13552] | 171 | content[i, 1] = values[i].GetValue();
|
---|
[13534] | 172 | content[i, 2] = values[i].GetType().Name;
|
---|
| 173 | }
|
---|
| 174 | characteristicsMatrixView.Content = content;
|
---|
| 175 | SetEnabledStateOfControls();
|
---|
| 176 | }
|
---|
| 177 | private void uploadCharacteristicsButton_Click(object sender, EventArgs e) {
|
---|
| 178 | var matrix = characteristicsMatrixView.Content as StringMatrix;
|
---|
| 179 | if (matrix == null) return;
|
---|
| 180 | var values = new List<Value>(matrix.Rows);
|
---|
| 181 | for (var i = 0; i < matrix.Rows; i++) {
|
---|
| 182 | var name = matrix[i, 0];
|
---|
| 183 | var strValue = matrix[i, 1];
|
---|
| 184 | var type = matrix[i, 2];
|
---|
| 185 | values.Add(Value.Create(name, strValue, type));
|
---|
| 186 | }
|
---|
| 187 | try {
|
---|
| 188 | RunCreationClient.SetCharacteristicValues(Content.ProblemId, values);
|
---|
[13550] | 189 | } catch (Exception ex) { ErrorHandling.ShowErrorDialog(ex); }
|
---|
[13534] | 190 | }
|
---|
[13540] | 191 | private void calculateButton_Click(object sender, EventArgs e) {
|
---|
[13550] | 192 | var characteristics = calculatorListView.CheckedItems.OfType<ListViewItem>().GroupBy(x => x.Group).ToList();
|
---|
| 193 | if (characteristics.Count == 0) return;
|
---|
[13540] | 194 | var problem = Content.CloneProblem();
|
---|
[13550] | 195 | var results = new Dictionary<string, Value>();
|
---|
[13540] | 196 | foreach (var c in characteristics) {
|
---|
| 197 | var calc = (ICharacteristicCalculator)c.Key.Tag;
|
---|
| 198 | foreach (var result in calc.Calculate(problem, c.Select(x => x.Text).ToArray()))
|
---|
[13550] | 199 | results[result.Key] = RunCreationClient.Instance.ConvertToValue(result.Value, result.Key);
|
---|
[13540] | 200 | }
|
---|
[13550] | 201 | var matrix = (characteristicsMatrixView.Content as StringMatrix) ?? (new StringMatrix(results.Count, 3));
|
---|
[13540] | 202 | for (var i = 0; i < matrix.Rows; i++) {
|
---|
[13550] | 203 | Value r;
|
---|
[13540] | 204 | if (results.TryGetValue(matrix[i, 0], out r)) {
|
---|
[13550] | 205 | matrix[i, 1] = r.GetValue();
|
---|
| 206 | matrix[i, 2] = r.GetType().Name;
|
---|
[13540] | 207 | results.Remove(matrix[i, 0]);
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 | if (results.Count == 0) return;
|
---|
| 211 | var resultsList = results.ToList();
|
---|
| 212 | var counter = resultsList.Count - 1;
|
---|
| 213 | for (var i = 0; i < matrix.Rows; i++) {
|
---|
| 214 | if (string.IsNullOrEmpty(matrix[i, 0])) {
|
---|
| 215 | matrix[i, 0] = resultsList[counter].Key;
|
---|
[13550] | 216 | matrix[i, 1] = resultsList[counter].Value.GetValue();
|
---|
| 217 | matrix[i, 2] = resultsList[counter].Value.GetType().Name;
|
---|
[13540] | 218 | resultsList.RemoveAt(counter);
|
---|
| 219 | counter--;
|
---|
| 220 | if (counter < 0) return;
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 | if (counter >= 0) {
|
---|
| 224 | ((IStringConvertibleMatrix)matrix).Rows += counter + 1;
|
---|
| 225 | for (var i = matrix.Rows - 1; counter >= 0; i--) {
|
---|
| 226 | matrix[i, 0] = resultsList[0].Key;
|
---|
[13550] | 227 | matrix[i, 1] = resultsList[0].Value.GetValue();
|
---|
| 228 | matrix[i, 2] = resultsList[0].Value.GetType().Name;
|
---|
| 229 | resultsList.RemoveAt(0);
|
---|
[13540] | 230 | counter--;
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
[13550] | 233 | SetEnabledStateOfControls();
|
---|
[13540] | 234 | }
|
---|
[13550] | 235 | private void calculatorListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
|
---|
| 236 | SetEnabledStateOfControls();
|
---|
| 237 | }
|
---|
[8055] | 238 | #endregion
|
---|
| 239 |
|
---|
| 240 | #region Helpers
|
---|
| 241 | private void PopulateComboBox() {
|
---|
| 242 | problemComboBox.DataSource = null;
|
---|
| 243 | problemComboBox.DataSource = RunCreationClient.Instance.Problems.ToList();
|
---|
| 244 | problemComboBox.DisplayMember = "Name";
|
---|
| 245 | }
|
---|
| 246 | #endregion
|
---|
[13550] | 247 |
|
---|
[8055] | 248 | }
|
---|
| 249 | }
|
---|