[8955] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2012 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.ComponentModel;
|
---|
| 24 | using System.Windows.Forms;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core.Views;
|
---|
| 27 | using HeuristicLab.MainForm;
|
---|
[12847] | 28 | using HeuristicLab.Optimization;
|
---|
[8955] | 29 | using HeuristicLab.PluginInfrastructure;
|
---|
| 30 |
|
---|
[12847] | 31 | namespace HeuristicLab.OptimizationExpertSystem.Views {
|
---|
| 32 | [View("Expert-system Optimizer View")]
|
---|
[12860] | 33 | [Content(typeof(ExpertSystem), IsDefaultView = true)]
|
---|
| 34 | public partial class ExpertSystemView : NamedItemView {
|
---|
[12847] | 35 | protected TypeSelectorDialog problemTypeSelectorDialog;
|
---|
[8955] | 36 | protected virtual bool SuppressEvents { get; set; }
|
---|
| 37 |
|
---|
[12860] | 38 | public new ExpertSystem Content {
|
---|
| 39 | get { return (ExpertSystem)base.Content; }
|
---|
[8955] | 40 | set { base.Content = value; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[12860] | 43 | public ExpertSystemView() {
|
---|
[8955] | 44 | InitializeComponent();
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | protected override void Dispose(bool disposing) {
|
---|
| 48 | if (disposing) {
|
---|
[12847] | 49 | if (problemTypeSelectorDialog != null) problemTypeSelectorDialog.Dispose();
|
---|
[8955] | 50 | if (components != null) components.Dispose();
|
---|
| 51 | }
|
---|
| 52 | base.Dispose(disposing);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | protected override void DeregisterContentEvents() {
|
---|
| 56 | Content.PropertyChanged -= Content_PropertyChanged;
|
---|
[12847] | 57 | Content.SuggestedInstances.CollectionReset -= SuggestedInstancesOnChanged;
|
---|
| 58 | Content.SuggestedInstances.ItemsAdded -= SuggestedInstancesOnChanged;
|
---|
| 59 | Content.SuggestedInstances.ItemsMoved -= SuggestedInstancesOnChanged;
|
---|
| 60 | Content.SuggestedInstances.ItemsRemoved -= SuggestedInstancesOnChanged;
|
---|
| 61 | Content.SuggestedInstances.ItemsReplaced -= SuggestedInstancesOnChanged;
|
---|
[8955] | 62 | base.DeregisterContentEvents();
|
---|
| 63 | }
|
---|
| 64 | protected override void RegisterContentEvents() {
|
---|
| 65 | base.RegisterContentEvents();
|
---|
| 66 | Content.PropertyChanged += Content_PropertyChanged;
|
---|
[12847] | 67 | Content.SuggestedInstances.CollectionReset += SuggestedInstancesOnChanged;
|
---|
| 68 | Content.SuggestedInstances.ItemsAdded += SuggestedInstancesOnChanged;
|
---|
| 69 | Content.SuggestedInstances.ItemsMoved += SuggestedInstancesOnChanged;
|
---|
| 70 | Content.SuggestedInstances.ItemsRemoved += SuggestedInstancesOnChanged;
|
---|
| 71 | Content.SuggestedInstances.ItemsReplaced += SuggestedInstancesOnChanged;
|
---|
[8955] | 72 | }
|
---|
| 73 |
|
---|
| 74 | protected override void OnContentChanged() {
|
---|
| 75 | base.OnContentChanged();
|
---|
| 76 | SuppressEvents = true;
|
---|
| 77 | try {
|
---|
| 78 | if (Content == null) {
|
---|
[12804] | 79 | maxEvaluationsTextBox.Text = String.Empty;
|
---|
| 80 |
|
---|
[12847] | 81 | problemViewHost.Content = null;
|
---|
[8955] | 82 | algorithmViewHost.Content = null;
|
---|
| 83 | runsView.Content = null;
|
---|
[12860] | 84 | algorithmInstancesViewHost.Content = null;
|
---|
[8955] | 85 | } else {
|
---|
[12804] | 86 | maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString();
|
---|
[12847] | 87 | problemViewHost.Content = Content.Problem;
|
---|
[8955] | 88 | runsView.Content = Content.Runs;
|
---|
[12860] | 89 | algorithmInstancesViewHost.Content = Content.AlgorithmInstances;
|
---|
[8955] | 90 | }
|
---|
| 91 | } finally { SuppressEvents = false; }
|
---|
[12860] | 92 | UpdateSuggestedInstancesCombobox();
|
---|
[8955] | 93 | }
|
---|
[8956] | 94 |
|
---|
[8955] | 95 | protected override void SetEnabledStateOfControls() {
|
---|
| 96 | base.SetEnabledStateOfControls();
|
---|
[12804] | 97 | maxEvaluationsTextBox.Enabled = Content != null && !ReadOnly && !Locked;
|
---|
[12847] | 98 | newProblemButton.Enabled = Content != null && !ReadOnly && !Locked;
|
---|
| 99 | openProblemButton.Enabled = Content != null && !ReadOnly && !Locked;
|
---|
| 100 | problemViewHost.Enabled = Content != null && !ReadOnly && !Locked;
|
---|
| 101 | suggestedInstancesComboBox.Enabled = Content != null && !ReadOnly && !Locked;
|
---|
| 102 | algorithmViewHost.Enabled = Content != null && !ReadOnly && !Locked;
|
---|
[8955] | 103 | runsView.Enabled = Content != null;
|
---|
[12860] | 104 | algorithmInstancesViewHost.Enabled = Content != null;
|
---|
[8955] | 105 | }
|
---|
| 106 |
|
---|
[12847] | 107 | private void UpdateSuggestedInstancesCombobox() {
|
---|
[12860] | 108 | var prevSelection = (IAlgorithm)suggestedInstancesComboBox.SelectedItem;
|
---|
| 109 | var prevNewIndex = -1;
|
---|
| 110 | suggestedInstancesComboBox.Items.Clear();
|
---|
| 111 | if (Content == null) return;
|
---|
[12847] | 112 |
|
---|
[12860] | 113 | for (var i = 0; i < Content.SuggestedInstances.Count; i++) {
|
---|
| 114 | suggestedInstancesComboBox.Items.Add(Content.SuggestedInstances[i]);
|
---|
| 115 | if (prevSelection == null || Content.SuggestedInstances[i].Name == prevSelection.Name)
|
---|
| 116 | prevNewIndex = prevSelection == null ? 0 : i;
|
---|
| 117 | }
|
---|
| 118 | if (prevNewIndex >= 0) {
|
---|
| 119 | suggestedInstancesComboBox.SelectedIndex = prevNewIndex;
|
---|
| 120 | }
|
---|
[12847] | 121 | }
|
---|
| 122 |
|
---|
[8955] | 123 | #region Event Handlers
|
---|
| 124 | #region Content events
|
---|
| 125 | private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
[12825] | 126 | if (InvokeRequired) {
|
---|
| 127 | Invoke((Action<object, PropertyChangedEventArgs>)Content_PropertyChanged, sender, e);
|
---|
| 128 | return;
|
---|
| 129 | }
|
---|
[8961] | 130 | SuppressEvents = true;
|
---|
| 131 | try {
|
---|
[12804] | 132 | switch (e.PropertyName) {
|
---|
| 133 | case "MaximumEvaluations": maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString(); break;
|
---|
[12847] | 134 | case "Problem": problemViewHost.Content = Content.Problem; break;
|
---|
[12804] | 135 | }
|
---|
[8961] | 136 | } finally { SuppressEvents = false; }
|
---|
| 137 | }
|
---|
[12847] | 138 |
|
---|
| 139 | private void SuggestedInstancesOnChanged(object sender, EventArgs e) {
|
---|
[12860] | 140 | UpdateSuggestedInstancesCombobox();
|
---|
[12847] | 141 | }
|
---|
[8955] | 142 | #endregion
|
---|
| 143 |
|
---|
| 144 | #region Control events
|
---|
[12804] | 145 | private void maxEvaluationsTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
[8961] | 146 | if (SuppressEvents) return;
|
---|
[12804] | 147 | if (InvokeRequired) {
|
---|
| 148 | Invoke((Action<object, CancelEventArgs>)maxEvaluationsTextBox_Validating, sender, e);
|
---|
| 149 | return;
|
---|
| 150 | }
|
---|
| 151 | int value;
|
---|
| 152 | if (!int.TryParse(maxEvaluationsTextBox.Text, out value)) {
|
---|
| 153 | e.Cancel = !maxEvaluationsTextBox.ReadOnly && maxEvaluationsTextBox.Enabled;
|
---|
| 154 | errorProvider.SetError(maxEvaluationsTextBox, "Please enter a valid integer number.");
|
---|
| 155 | } else {
|
---|
| 156 | Content.MaximumEvaluations = value;
|
---|
| 157 | e.Cancel = false;
|
---|
| 158 | errorProvider.SetError(maxEvaluationsTextBox, null);
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
[8961] | 161 |
|
---|
[12847] | 162 | private void newProblemButton_Click(object sender, EventArgs e) {
|
---|
| 163 | if (problemTypeSelectorDialog == null) {
|
---|
| 164 | problemTypeSelectorDialog = new TypeSelectorDialog { Caption = "Select Problem" };
|
---|
| 165 | problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems";
|
---|
[12860] | 166 | problemTypeSelectorDialog.TypeSelector.Configure(typeof(ISingleObjectiveHeuristicOptimizationProblem)
|
---|
[12804] | 167 | , showNotInstantiableTypes: false, showGenericTypes: false);
|
---|
[8955] | 168 | }
|
---|
[12847] | 169 | if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
[8955] | 170 | try {
|
---|
[12860] | 171 | Content.Problem = (ISingleObjectiveHeuristicOptimizationProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
[8955] | 172 | } catch (Exception ex) {
|
---|
| 173 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[12847] | 178 | private void openProblemButton_Click(object sender, EventArgs e) {
|
---|
| 179 | openFileDialog.Title = "Open Problem";
|
---|
[8955] | 180 | if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
[12847] | 181 | newProblemButton.Enabled = openProblemButton.Enabled = false;
|
---|
| 182 | problemViewHost.Enabled = false;
|
---|
[8955] | 183 |
|
---|
| 184 | ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
|
---|
| 185 | try {
|
---|
| 186 | if (error != null) throw error;
|
---|
[12860] | 187 | var problem = content as ISingleObjectiveHeuristicOptimizationProblem;
|
---|
[12847] | 188 | if (problem == null) {
|
---|
| 189 | var algorithm = content as IAlgorithm;
|
---|
[12860] | 190 | if (algorithm == null || !(algorithm.Problem is ISingleObjectiveHeuristicOptimizationProblem))
|
---|
[12847] | 191 | MessageBox.Show(this, "The selected file is not a problem, nor an algorithm with a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
[12860] | 192 | else Content.Problem = (ISingleObjectiveHeuristicOptimizationProblem)algorithm.Problem;
|
---|
[12847] | 193 | } else
|
---|
| 194 | Content.Problem = problem;
|
---|
[8955] | 195 | } catch (Exception ex) {
|
---|
| 196 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
| 197 | } finally {
|
---|
| 198 | Invoke(new Action(delegate() {
|
---|
[12847] | 199 | problemViewHost.Enabled = true;
|
---|
| 200 | newProblemButton.Enabled = openProblemButton.Enabled = true;
|
---|
[8955] | 201 | }));
|
---|
| 202 | }
|
---|
| 203 | });
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[12847] | 207 | private void problemTabPage_DragEnterOver(object sender, DragEventArgs e) {
|
---|
[8955] | 208 | e.Effect = DragDropEffects.None;
|
---|
[12860] | 209 | var prob = e.Data.GetData(Constants.DragDropDataFormat) as ISingleObjectiveHeuristicOptimizationProblem;
|
---|
[12847] | 210 | if (!ReadOnly && prob != null) {
|
---|
[8955] | 211 | if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
|
---|
| 212 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
| 213 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
|
---|
| 214 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
|
---|
| 215 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[12847] | 219 | private void problemTabPage_DragDrop(object sender, DragEventArgs e) {
|
---|
[8955] | 220 | if (e.Effect != DragDropEffects.None) {
|
---|
[12860] | 221 | var prob = e.Data.GetData(Constants.DragDropDataFormat) as ISingleObjectiveHeuristicOptimizationProblem;
|
---|
| 222 | if (e.Effect.HasFlag(DragDropEffects.Copy)) prob = (ISingleObjectiveHeuristicOptimizationProblem)prob.Clone();
|
---|
[12847] | 223 | Content.Problem = prob;
|
---|
[8955] | 224 | }
|
---|
| 225 | }
|
---|
[12847] | 226 |
|
---|
| 227 | private void suggestedInstancesComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 228 | if (SuppressEvents) return;
|
---|
| 229 | if (InvokeRequired) { Invoke((Action<object, EventArgs>)suggestedInstancesComboBox_SelectedIndexChanged, sender, e); return; }
|
---|
| 230 | if (suggestedInstancesComboBox.SelectedIndex >= 0)
|
---|
| 231 | algorithmViewHost.Content = (IAlgorithm)suggestedInstancesComboBox.SelectedItem;
|
---|
| 232 | else algorithmViewHost.Content = null;
|
---|
| 233 | }
|
---|
[8955] | 234 | #endregion
|
---|
| 235 | #endregion
|
---|
| 236 | }
|
---|
| 237 | }
|
---|