Ignore:
Timestamp:
08/12/15 10:29:01 (4 years ago)
Author:
abeham
Message:

#2457: Added view

File:
1 copied

Legend:

Unmodified
Added
Removed
  • branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Views/3.3/ExpertSystemOptimizerView.cs

    r12839 r12847  
    3030using HeuristicLab.Core.Views;
    3131using HeuristicLab.MainForm;
     32using HeuristicLab.Optimization;
     33using HeuristicLab.Optimization.Views;
    3234using HeuristicLab.PluginInfrastructure;
    3335
    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;
     36namespace 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;
    3941    protected virtual bool SuppressEvents { get; set; }
    4042
    41     public new IndepdentRandomRestarter Content {
    42       get { return (IndepdentRandomRestarter)base.Content; }
     43    public new ExpertSystemOptimizer Content {
     44      get { return (ExpertSystemOptimizer)base.Content; }
    4345      set { base.Content = value; }
    4446    }
    4547
    46     public IndependentRandomRestarterView() {
     48    public ExpertSystemOptimizerView() {
    4749      InitializeComponent();
    48       terminationComboBox.Items.AddRange(Enum.GetValues(typeof(TerminationCriterium)).Cast<object>().ToArray());
    4950    }
    5051
    5152    protected override void Dispose(bool disposing) {
    5253      if (disposing) {
    53         if (algorithmTypeSelectorDialog != null) algorithmTypeSelectorDialog.Dispose();
     54        if (problemTypeSelectorDialog != null) problemTypeSelectorDialog.Dispose();
    5455        if (components != null) components.Dispose();
    5556      }
     
    5960    protected override void DeregisterContentEvents() {
    6061      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;
    6167      base.DeregisterContentEvents();
    6268    }
     
    6470      base.RegisterContentEvents();
    6571      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;
    6677    }
    6778
     
    7182      try {
    7283        if (Content == null) {
    73           maxExecutionTimeTextBox.Text = String.Empty;
    7484          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;
    8187          algorithmViewHost.Content = null;
    82           currentRunView.Content = null;
    8388          runsView.Content = null;
     89          okbRunCollectionView.Content = null;
    8490        } else {
    85           maxExecutionTimeTextBox.Text = TimeSpanHelper.FormatNatural(Content.MaximumExecutionTime);
    8691          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;
    9593          runsView.Content = Content.Runs;
    96         }
     94          okbRunCollectionView.Content = Content.KnowledgeBase;
     95        }
     96        UpdateSuggestedInstancesCombobox();
    9797      } finally { SuppressEvents = false; }
    9898    }
     
    100100    protected override void SetEnabledStateOfControls() {
    101101      base.SetEnabledStateOfControls();
    102       maxExecutionTimeTextBox.Enabled = Content != null && !ReadOnly && !Locked;
    103102      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;
    113108      runsView.Enabled = Content != null;
     109      okbRunCollectionView.Enabled = Content != null;
    114110    }
    115111
     
    117113      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) {
    118114        //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<IAlgorithm>();
    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)) {
    121117          var nestedOptimizers = optimizers.SelectMany(opt => opt.NestedOptimizers);
    122118          if (!nestedOptimizers.Contains(Content)) Content.Stop();
     
    124120      }
    125121      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); }
    126141    }
    127142
     
    136151      try {
    137152        switch (e.PropertyName) {
    138           case "TerminationCriterium": terminationComboBox.SelectedItem = Content.TerminationCriterium; break;
    139           case "MaximumExecutionTime": maxExecutionTimeTextBox.Text = TimeSpanHelper.FormatNatural(Content.MaximumExecutionTime); break;
    140153          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;
    147154          case "Runs": runsView.Content = Content.Runs; break;
     155          case "KnowledgeBase": okbRunCollectionView.Content = Content.KnowledgeBase; break;
     156          case "Problem": problemViewHost.Content = Content.Problem; break;
    148157        }
    149158      } finally { SuppressEvents = false; }
    150159    }
     160
     161    private void SuggestedInstancesOnChanged(object sender, EventArgs e) {
     162      SuppressEvents = true;
     163      try {
     164        UpdateSuggestedInstancesCombobox();
     165      } finally { SuppressEvents = false; }
     166    }
    151167    #endregion
    152168
    153169    #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 
    171170    private void maxEvaluationsTextBox_Validating(object sender, CancelEventArgs e) {
    172171      if (SuppressEvents) return;
     
    186185    }
    187186
    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)
    254192          , showNotInstantiableTypes: false, showGenericTypes: false);
    255193      }
    256       if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
     194      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
    257195        try {
    258           Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
     196          Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
    259197        } catch (Exception ex) {
    260198          ErrorHandling.ShowErrorDialog(this, ex);
     
    263201    }
    264202
    265     private void openAlgorithmButton_Click(object sender, EventArgs e) {
    266       openFileDialog.Title = "Open Algorithm";
     203    private void openProblemButton_Click(object sender, EventArgs e) {
     204      openFileDialog.Title = "Open Problem";
    267205      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
    268         newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = false;
    269         algorithmViewHost.Enabled = false;
     206        newProblemButton.Enabled = openProblemButton.Enabled = false;
     207        problemViewHost.Enabled = false;
    270208
    271209        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
    272210          try {
    273211            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;
    279220          } catch (Exception ex) {
    280221            ErrorHandling.ShowErrorDialog(this, ex);
    281222          } finally {
    282223            Invoke(new Action(delegate() {
    283               algorithmViewHost.Enabled = true;
    284               newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = true;
     224              problemViewHost.Enabled = true;
     225              newProblemButton.Enabled = openProblemButton.Enabled = true;
    285226            }));
    286227          }
     
    289230    }
    290231
    291     private void algorithmTabPage_DragEnterOver(object sender, DragEventArgs e) {
     232    private void problemTabPage_DragEnterOver(object sender, DragEventArgs e) {
    292233      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) {
    297236        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
    298237        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
     
    303242    }
    304243
    305     private void algorithmTabPage_DragDrop(object sender, DragEventArgs e) {
     244    private void problemTabPage_DragDrop(object sender, DragEventArgs e) {
    306245      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;
    311258    }
    312259    #endregion
Note: See TracChangeset for help on using the changeset viewer.