Changeset 12847 for branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Views/3.3/ExpertSystemOptimizerView.cs
- Timestamp:
- 08/12/15 10:29:01 (9 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Views/3.3/ExpertSystemOptimizerView.cs
r12839 r12847 30 30 using HeuristicLab.Core.Views; 31 31 using HeuristicLab.MainForm; 32 using HeuristicLab.Optimization; 33 using HeuristicLab.Optimization.Views; 32 34 using HeuristicLab.PluginInfrastructure; 33 35 34 namespace HeuristicLab.Optimization .Views {35 [View(" Independent Random Restarter View")]36 [Content(typeof( IndepdentRandomRestarter), IsDefaultView = true)]37 public partial class IndependentRandomRestarterView : IOptimizerView {38 protected TypeSelectorDialog algorithmTypeSelectorDialog;36 namespace HeuristicLab.OptimizationExpertSystem.Views { 37 [View("Expert-system Optimizer View")] 38 [Content(typeof(ExpertSystemOptimizer), IsDefaultView = true)] 39 public partial class ExpertSystemOptimizerView : IOptimizerView { 40 protected TypeSelectorDialog problemTypeSelectorDialog; 39 41 protected virtual bool SuppressEvents { get; set; } 40 42 41 public new IndepdentRandomRestarter Content {42 get { return ( IndepdentRandomRestarter)base.Content; }43 public new ExpertSystemOptimizer Content { 44 get { return (ExpertSystemOptimizer)base.Content; } 43 45 set { base.Content = value; } 44 46 } 45 47 46 public IndependentRandomRestarterView() {48 public ExpertSystemOptimizerView() { 47 49 InitializeComponent(); 48 terminationComboBox.Items.AddRange(Enum.GetValues(typeof(TerminationCriterium)).Cast<object>().ToArray());49 50 } 50 51 51 52 protected override void Dispose(bool disposing) { 52 53 if (disposing) { 53 if ( algorithmTypeSelectorDialog != null) algorithmTypeSelectorDialog.Dispose();54 if (problemTypeSelectorDialog != null) problemTypeSelectorDialog.Dispose(); 54 55 if (components != null) components.Dispose(); 55 56 } … … 59 60 protected override void DeregisterContentEvents() { 60 61 Content.PropertyChanged -= Content_PropertyChanged; 62 Content.SuggestedInstances.CollectionReset -= SuggestedInstancesOnChanged; 63 Content.SuggestedInstances.ItemsAdded -= SuggestedInstancesOnChanged; 64 Content.SuggestedInstances.ItemsMoved -= SuggestedInstancesOnChanged; 65 Content.SuggestedInstances.ItemsRemoved -= SuggestedInstancesOnChanged; 66 Content.SuggestedInstances.ItemsReplaced -= SuggestedInstancesOnChanged; 61 67 base.DeregisterContentEvents(); 62 68 } … … 64 70 base.RegisterContentEvents(); 65 71 Content.PropertyChanged += Content_PropertyChanged; 72 Content.SuggestedInstances.CollectionReset += SuggestedInstancesOnChanged; 73 Content.SuggestedInstances.ItemsAdded += SuggestedInstancesOnChanged; 74 Content.SuggestedInstances.ItemsMoved += SuggestedInstancesOnChanged; 75 Content.SuggestedInstances.ItemsRemoved += SuggestedInstancesOnChanged; 76 Content.SuggestedInstances.ItemsReplaced += SuggestedInstancesOnChanged; 66 77 } 67 78 … … 71 82 try { 72 83 if (Content == null) { 73 maxExecutionTimeTextBox.Text = String.Empty;74 84 maxEvaluationsTextBox.Text = String.Empty; 75 moveCostPerSolutionTextBox.Text = String.Empty; 76 targetValueTextBox.Text = String.Empty; 77 maximizationCheckBox.CheckState = CheckState.Indeterminate; 78 terminationComboBox.SelectedIndex = -1; 79 storeBestSolutionCheckBox.CheckState = CheckState.Indeterminate; 80 85 86 problemViewHost.Content = null; 81 87 algorithmViewHost.Content = null; 82 currentRunView.Content = null;83 88 runsView.Content = null; 89 okbRunCollectionView.Content = null; 84 90 } else { 85 maxExecutionTimeTextBox.Text = TimeSpanHelper.FormatNatural(Content.MaximumExecutionTime);86 91 maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString(); 87 moveCostPerSolutionTextBox.Text = Content.MoveCostPerSolution.ToString(CultureInfo.CurrentCulture.NumberFormat); 88 targetValueTextBox.Text = Content.TargetValue.ToString(CultureInfo.CurrentCulture.NumberFormat); 89 maximizationCheckBox.Checked = Content.Maximization; 90 terminationComboBox.SelectedItem = Content.TerminationCriterium; 91 storeBestSolutionCheckBox.Checked = Content.StoreSolutionInRun; 92 93 algorithmViewHost.Content = Content.Algorithm; 94 currentRunView.Content = Content.CurrentRun; 92 problemViewHost.Content = Content.Problem; 95 93 runsView.Content = Content.Runs; 96 } 94 okbRunCollectionView.Content = Content.KnowledgeBase; 95 } 96 UpdateSuggestedInstancesCombobox(); 97 97 } finally { SuppressEvents = false; } 98 98 } … … 100 100 protected override void SetEnabledStateOfControls() { 101 101 base.SetEnabledStateOfControls(); 102 maxExecutionTimeTextBox.Enabled = Content != null && !ReadOnly && !Locked;103 102 maxEvaluationsTextBox.Enabled = Content != null && !ReadOnly && !Locked; 104 moveCostPerSolutionTextBox.Enabled = Content != null && !ReadOnly && !Locked; 105 targetValueTextBox.Enabled = Content != null && !ReadOnly && !Locked; 106 maximizationCheckBox.Enabled = Content != null && !ReadOnly && !Locked; 107 terminationComboBox.Enabled = Content != null && !ReadOnly && !Locked; 108 storeBestSolutionCheckBox.Enabled = Content != null && !ReadOnly && !Locked; 109 newAlgorithmButton.Enabled = Content != null && !ReadOnly; 110 openAlgorithmButton.Enabled = Content != null && !ReadOnly; 111 algorithmViewHost.Enabled = Content != null; 112 currentRunView.Enabled = Content != null; 103 newProblemButton.Enabled = Content != null && !ReadOnly && !Locked; 104 openProblemButton.Enabled = Content != null && !ReadOnly && !Locked; 105 problemViewHost.Enabled = Content != null && !ReadOnly && !Locked; 106 suggestedInstancesComboBox.Enabled = Content != null && !ReadOnly && !Locked; 107 algorithmViewHost.Enabled = Content != null && !ReadOnly && !Locked; 113 108 runsView.Enabled = Content != null; 109 okbRunCollectionView.Enabled = Content != null; 114 110 } 115 111 … … 117 113 if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) { 118 114 //The content must be stopped if no other view showing the content is available 119 var optimizers = MainFormManager.MainForm.Views.OfType<IContentView>().Where(v => v != this).Select(v => v.Content).OfType<I Algorithm>();120 if (!optimizers.Contains(Content .Algorithm)) {115 var optimizers = MainFormManager.MainForm.Views.OfType<IContentView>().Where(v => v != this).Select(v => v.Content).OfType<IOptimizer>(); 116 if (!optimizers.Contains(Content)) { 121 117 var nestedOptimizers = optimizers.SelectMany(opt => opt.NestedOptimizers); 122 118 if (!nestedOptimizers.Contains(Content)) Content.Stop(); … … 124 120 } 125 121 base.OnClosed(e); 122 } 123 124 private void UpdateSuggestedInstancesCombobox() { 125 SuspendRepaint(); 126 try { 127 var prevSelection = (IAlgorithm)suggestedInstancesComboBox.SelectedItem; 128 var prevNewIndex = -1; 129 suggestedInstancesComboBox.Items.Clear(); 130 if (Content == null) return; 131 132 for (var i = 0; i < Content.SuggestedInstances.Count; i++) { 133 suggestedInstancesComboBox.Items.Add(Content.SuggestedInstances[i]); 134 if (prevSelection == null || Content.SuggestedInstances[i].Name == prevSelection.Name) 135 prevNewIndex = prevSelection == null ? 0 : i; 136 } 137 if (prevNewIndex >= 0) { 138 suggestedInstancesComboBox.SelectedIndex = prevNewIndex; 139 } 140 } finally { ResumeRepaint(true); } 126 141 } 127 142 … … 136 151 try { 137 152 switch (e.PropertyName) { 138 case "TerminationCriterium": terminationComboBox.SelectedItem = Content.TerminationCriterium; break;139 case "MaximumExecutionTime": maxExecutionTimeTextBox.Text = TimeSpanHelper.FormatNatural(Content.MaximumExecutionTime); break;140 153 case "MaximumEvaluations": maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString(); break; 141 case "TargetValue": targetValueTextBox.Text = Content.TargetValue.ToString(CultureInfo.CurrentCulture.NumberFormat); break;142 case "Maximization": maximizationCheckBox.Checked = Content.Maximization; break;143 case "MoveCostPerSolution": moveCostPerSolutionTextBox.Text = Content.MoveCostPerSolution.ToString(CultureInfo.CurrentCulture.NumberFormat); break;144 case "StoreSolutionInRun": storeBestSolutionCheckBox.Checked = Content.StoreSolutionInRun; break;145 case "Algorithm": algorithmViewHost.Content = Content.Algorithm; break;146 case "CurrentRun": currentRunView.Content = Content.CurrentRun; break;147 154 case "Runs": runsView.Content = Content.Runs; break; 155 case "KnowledgeBase": okbRunCollectionView.Content = Content.KnowledgeBase; break; 156 case "Problem": problemViewHost.Content = Content.Problem; break; 148 157 } 149 158 } finally { SuppressEvents = false; } 150 159 } 160 161 private void SuggestedInstancesOnChanged(object sender, EventArgs e) { 162 SuppressEvents = true; 163 try { 164 UpdateSuggestedInstancesCombobox(); 165 } finally { SuppressEvents = false; } 166 } 151 167 #endregion 152 168 153 169 #region Control events 154 private void maxExecutionTimeTextBox_Validating(object sender, CancelEventArgs e) {155 if (SuppressEvents) return;156 if (InvokeRequired) {157 Invoke((Action<object, CancelEventArgs>)maxExecutionTimeTextBox_Validating, sender, e);158 return;159 }160 TimeSpan ts;161 if (!TimeSpanHelper.TryGetFromNaturalFormat(maxExecutionTimeTextBox.Text, out ts)) {162 e.Cancel = !maxExecutionTimeTextBox.ReadOnly && maxExecutionTimeTextBox.Enabled;163 errorProvider.SetError(maxExecutionTimeTextBox, "Please enter a valid time span, e.g. 20 seconds ; 45s ; 4min ; 1h ; 3 hours ; 2 days ; 4d");164 } else {165 Content.MaximumExecutionTime = ts;166 e.Cancel = false;167 errorProvider.SetError(maxExecutionTimeTextBox, null);168 }169 }170 171 170 private void maxEvaluationsTextBox_Validating(object sender, CancelEventArgs e) { 172 171 if (SuppressEvents) return; … … 186 185 } 187 186 188 private void moveCostPerSolutionTextBox_Validating(object sender, CancelEventArgs e) { 189 if (SuppressEvents) return; 190 if (InvokeRequired) { 191 Invoke((Action<object, CancelEventArgs>)moveCostPerSolutionTextBox_Validating, sender, e); 192 return; 193 } 194 double value; 195 if (!double.TryParse(moveCostPerSolutionTextBox.Text, out value)) { 196 e.Cancel = !moveCostPerSolutionTextBox.ReadOnly && moveCostPerSolutionTextBox.Enabled; 197 errorProvider.SetError(moveCostPerSolutionTextBox, "Please enter a valid integer number."); 198 } else { 199 Content.MoveCostPerSolution = value; 200 e.Cancel = false; 201 errorProvider.SetError(moveCostPerSolutionTextBox, null); 202 } 203 } 204 205 private void targetValueTextBox_Validating(object sender, CancelEventArgs e) { 206 if (SuppressEvents) return; 207 if (InvokeRequired) { 208 Invoke((Action<object, CancelEventArgs>)targetValueTextBox_Validating, sender, e); 209 return; 210 } 211 double value; 212 if (!double.TryParse(targetValueTextBox.Text, out value)) { 213 e.Cancel = !targetValueTextBox.ReadOnly && targetValueTextBox.Enabled; 214 errorProvider.SetError(targetValueTextBox, "Please enter a valid integer number."); 215 } else { 216 Content.TargetValue = value; 217 e.Cancel = false; 218 errorProvider.SetError(targetValueTextBox, null); 219 } 220 } 221 222 private void maximizationCheckBox_CheckedChanged(object sender, EventArgs e) { 223 if (SuppressEvents) return; 224 if (InvokeRequired) { 225 Invoke((Action<object, EventArgs>)maximizationCheckBox_CheckedChanged, sender, e); 226 return; 227 } 228 Content.Maximization = maximizationCheckBox.Checked; 229 } 230 231 private void terminationComboBox_SelectedIndexChanged(object sender, EventArgs e) { 232 if (SuppressEvents) return; 233 if (InvokeRequired) { 234 Invoke((Action<object, EventArgs>)terminationComboBox_SelectedIndexChanged, sender, e); 235 return; 236 } 237 Content.TerminationCriterium = (TerminationCriterium)terminationComboBox.SelectedItem; 238 } 239 240 private void storeBestSolutionCheckBox_CheckedChanged(object sender, EventArgs e) { 241 if (SuppressEvents) return; 242 if (InvokeRequired) { 243 Invoke((Action<object, EventArgs>)storeBestSolutionCheckBox_CheckedChanged, sender, e); 244 return; 245 } 246 Content.StoreSolutionInRun = storeBestSolutionCheckBox.Checked; 247 } 248 249 private void newAlgorithmButton_Click(object sender, EventArgs e) { 250 if (algorithmTypeSelectorDialog == null) { 251 algorithmTypeSelectorDialog = new TypeSelectorDialog { Caption = "Select Algorithm" }; 252 algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms"; 253 algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm) 187 private void newProblemButton_Click(object sender, EventArgs e) { 188 if (problemTypeSelectorDialog == null) { 189 problemTypeSelectorDialog = new TypeSelectorDialog { Caption = "Select Problem" }; 190 problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems"; 191 problemTypeSelectorDialog.TypeSelector.Configure(typeof(IProblem) 254 192 , showNotInstantiableTypes: false, showGenericTypes: false); 255 193 } 256 if ( algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {194 if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) { 257 195 try { 258 Content. Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();196 Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType(); 259 197 } catch (Exception ex) { 260 198 ErrorHandling.ShowErrorDialog(this, ex); … … 263 201 } 264 202 265 private void open AlgorithmButton_Click(object sender, EventArgs e) {266 openFileDialog.Title = "Open Algorithm";203 private void openProblemButton_Click(object sender, EventArgs e) { 204 openFileDialog.Title = "Open Problem"; 267 205 if (openFileDialog.ShowDialog(this) == DialogResult.OK) { 268 new AlgorithmButton.Enabled = openAlgorithmButton.Enabled = false;269 algorithmViewHost.Enabled = false;206 newProblemButton.Enabled = openProblemButton.Enabled = false; 207 problemViewHost.Enabled = false; 270 208 271 209 ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) { 272 210 try { 273 211 if (error != null) throw error; 274 var algorithm = content as IAlgorithm; 275 if (algorithm == null) 276 MessageBox.Show(this, "The selected file does not contain an algorithm.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error); 277 else 278 Content.Algorithm = algorithm; 212 var problem = content as IProblem; 213 if (problem == null) { 214 var algorithm = content as IAlgorithm; 215 if (algorithm == null || algorithm.Problem == null) 216 MessageBox.Show(this, "The selected file is not a problem, nor an algorithm with a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error); 217 else Content.Problem = algorithm.Problem; 218 } else 219 Content.Problem = problem; 279 220 } catch (Exception ex) { 280 221 ErrorHandling.ShowErrorDialog(this, ex); 281 222 } finally { 282 223 Invoke(new Action(delegate() { 283 algorithmViewHost.Enabled = true;284 new AlgorithmButton.Enabled = openAlgorithmButton.Enabled = true;224 problemViewHost.Enabled = true; 225 newProblemButton.Enabled = openProblemButton.Enabled = true; 285 226 })); 286 227 } … … 289 230 } 290 231 291 private void algorithmTabPage_DragEnterOver(object sender, DragEventArgs e) {232 private void problemTabPage_DragEnterOver(object sender, DragEventArgs e) { 292 233 e.Effect = DragDropEffects.None; 293 var alg = e.Data.GetData(Constants.DragDropDataFormat) as IAlgorithm; 294 if (!ReadOnly && alg != null) { 295 if (!typeof(ISingleObjectiveHeuristicOptimizationProblem).IsAssignableFrom(alg.ProblemType)) 296 return; 234 var prob = e.Data.GetData(Constants.DragDropDataFormat) as IProblem; 235 if (!ReadOnly && prob != null) { 297 236 if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key 298 237 else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key … … 303 242 } 304 243 305 private void algorithmTabPage_DragDrop(object sender, DragEventArgs e) {244 private void problemTabPage_DragDrop(object sender, DragEventArgs e) { 306 245 if (e.Effect != DragDropEffects.None) { 307 var algorithm = e.Data.GetData(Constants.DragDropDataFormat) as IAlgorithm; 308 if (e.Effect.HasFlag(DragDropEffects.Copy)) algorithm = (IAlgorithm)algorithm.Clone(); 309 Content.Algorithm = algorithm; 310 } 246 var prob = e.Data.GetData(Constants.DragDropDataFormat) as IProblem; 247 if (e.Effect.HasFlag(DragDropEffects.Copy)) prob = (IProblem)prob.Clone(); 248 Content.Problem = prob; 249 } 250 } 251 252 private void suggestedInstancesComboBox_SelectedIndexChanged(object sender, EventArgs e) { 253 if (SuppressEvents) return; 254 if (InvokeRequired) { Invoke((Action<object, EventArgs>)suggestedInstancesComboBox_SelectedIndexChanged, sender, e); return; } 255 if (suggestedInstancesComboBox.SelectedIndex >= 0) 256 algorithmViewHost.Content = (IAlgorithm)suggestedInstancesComboBox.SelectedItem; 257 else algorithmViewHost.Content = null; 311 258 } 312 259 #endregion
Note: See TracChangeset
for help on using the changeset viewer.