Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/26/15 21:32:52 (9 years ago)
Author:
abeham
Message:

#2431:

  • worked on IRRRun (early abort still troublesome)
  • Updated RLD view to allow defining targets
  • Attempting to handle maximization/minimization
File:
1 copied

Legend:

Unmodified
Added
Removed
  • branches/PerformanceComparison/HeuristicLab.Optimization.Views/3.3/IRRestarterView.cs

    r12764 r12804  
    2222using System;
    2323using System.ComponentModel;
     24using System.Globalization;
    2425using System.Linq;
    25 using System.Text.RegularExpressions;
    2626using System.Windows.Forms;
    27 using HeuristicLab.Collections;
     27using HeuristicLab.Analysis;
    2828using HeuristicLab.Common;
    29 using HeuristicLab.Common.Resources;
    3029using HeuristicLab.Core;
    3130using HeuristicLab.Core.Views;
    3231using HeuristicLab.MainForm;
    33 using HeuristicLab.MainForm.WindowsForms;
    3432using HeuristicLab.PluginInfrastructure;
    3533
    3634namespace HeuristicLab.Optimization.Views {
    37   [View("TimeLimit Run View")]
    38   [Content(typeof(TimeLimitRun), IsDefaultView = true)]
    39   public partial class TimeLimitRunView : IOptimizerView {
     35  [View("Independent Random Restarter View")]
     36  [Content(typeof(IndepdentRandomRestarter), IsDefaultView = true)]
     37  public partial class IndependentRandomRestarterView : IOptimizerView {
    4038    protected TypeSelectorDialog algorithmTypeSelectorDialog;
    4139    protected virtual bool SuppressEvents { get; set; }
    4240
    43     public new TimeLimitRun Content {
    44       get { return (TimeLimitRun)base.Content; }
     41    public new IndepdentRandomRestarter Content {
     42      get { return (IndepdentRandomRestarter)base.Content; }
    4543      set { base.Content = value; }
    4644    }
    4745
    48     public TimeLimitRunView() {
     46    public IndependentRandomRestarterView() {
    4947      InitializeComponent();
    50       snapshotButton.Text = String.Empty;
    51       snapshotButton.Image = VSImageLibrary.Camera;
     48      terminationComboBox.Items.AddRange(Enum.GetValues(typeof(TerminationCriterium)).Cast<object>().ToArray());
    5249    }
    5350
     
    6259    protected override void DeregisterContentEvents() {
    6360      Content.PropertyChanged -= Content_PropertyChanged;
    64       Content.SnapshotTimes.ItemsAdded -= Content_SnapshotTimes_Changed;
    65       Content.SnapshotTimes.ItemsMoved -= Content_SnapshotTimes_Changed;
    66       Content.SnapshotTimes.ItemsRemoved -= Content_SnapshotTimes_Changed;
    67       Content.SnapshotTimes.ItemsReplaced -= Content_SnapshotTimes_Changed;
    68       Content.SnapshotTimes.CollectionReset -= Content_SnapshotTimes_Changed;
    6961      base.DeregisterContentEvents();
    7062    }
     
    7264      base.RegisterContentEvents();
    7365      Content.PropertyChanged += Content_PropertyChanged;
    74       Content.SnapshotTimes.ItemsAdded += Content_SnapshotTimes_Changed;
    75       Content.SnapshotTimes.ItemsMoved += Content_SnapshotTimes_Changed;
    76       Content.SnapshotTimes.ItemsRemoved += Content_SnapshotTimes_Changed;
    77       Content.SnapshotTimes.ItemsReplaced += Content_SnapshotTimes_Changed;
    78       Content.SnapshotTimes.CollectionReset += Content_SnapshotTimes_Changed;
    7966    }
    8067
     
    8471      try {
    8572        if (Content == null) {
    86           timeLimitTextBox.Text = TimeSpanHelper.FormatNatural(TimeSpan.FromSeconds(60));
    87           snapshotsTextBox.Text = String.Empty;
    88           storeAlgorithmInEachSnapshotCheckBox.Checked = false;
     73          maxExecutionTimeTextBox.Text = String.Empty;
     74          maxEvaluationsTextBox.Text = String.Empty;
     75          moveCostPerSolutionTextBox.Text = String.Empty;
     76          targetValueTextBox.Text = String.Empty;
     77          maximizationCheckBox.CheckState = CheckState.Indeterminate;
     78          terminationComboBox.SelectedIndex = -1;
     79
    8980          algorithmViewHost.Content = null;
    90           snapshotsView.Content = null;
     81          currentRunView.Content = null;
    9182          runsView.Content = null;
    9283        } else {
    93           timeLimitTextBox.Text = TimeSpanHelper.FormatNatural(Content.MaximumExecutionTime);
    94           snapshotsTextBox.Text = String.Join(" ; ", Content.SnapshotTimes.Select(x => TimeSpanHelper.FormatNatural(x, true)));
    95           storeAlgorithmInEachSnapshotCheckBox.Checked = Content.StoreAlgorithmInEachSnapshot;
     84          maxExecutionTimeTextBox.Text = TimeSpanHelper.FormatNatural(Content.MaximumExecutionTime);
     85          maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString();
     86          moveCostPerSolutionTextBox.Text = Content.MoveCostPerSolution.ToString(CultureInfo.CurrentCulture.NumberFormat);
     87          targetValueTextBox.Text = Content.TargetValue.ToString(CultureInfo.CurrentCulture.NumberFormat);
     88          maximizationCheckBox.Checked = Content.Maximization;
     89          terminationComboBox.SelectedItem = Content.TerminationCriterium;
     90
    9691          algorithmViewHost.Content = Content.Algorithm;
    97           snapshotsView.Content = Content.Snapshots;
     92          currentRunView.Content = Content.CurrentRun;
    9893          runsView.Content = Content.Runs;
    9994        }
     
    10398    protected override void SetEnabledStateOfControls() {
    10499      base.SetEnabledStateOfControls();
    105       timeLimitTextBox.Enabled = Content != null && !ReadOnly;
    106       snapshotsTextBox.Enabled = Content != null && !ReadOnly;
    107       storeAlgorithmInEachSnapshotCheckBox.Enabled = Content != null && !ReadOnly;
     100      maxExecutionTimeTextBox.Enabled = Content != null && !ReadOnly && !Locked;
     101      maxEvaluationsTextBox.Enabled = Content != null && !ReadOnly && !Locked;
     102      moveCostPerSolutionTextBox.Enabled = Content != null && !ReadOnly && !Locked;
     103      targetValueTextBox.Enabled = Content != null && !ReadOnly && !Locked;
     104      maximizationCheckBox.Enabled = Content != null && !ReadOnly && !Locked;
     105      terminationComboBox.Enabled = Content != null && !ReadOnly && !Locked;
    108106      newAlgorithmButton.Enabled = Content != null && !ReadOnly;
    109107      openAlgorithmButton.Enabled = Content != null && !ReadOnly;
    110108      algorithmViewHost.Enabled = Content != null;
    111       snapshotsView.Enabled = Content != null;
     109      currentRunView.Enabled = Content != null;
    112110      runsView.Enabled = Content != null;
    113     }
    114 
    115     protected override void SetEnabledStateOfExecutableButtons() {
    116       base.SetEnabledStateOfExecutableButtons();
    117       snapshotButton.Enabled = Content != null && Content.Algorithm != null && Content.ExecutionState == ExecutionState.Paused;
    118111    }
    119112
     
    133126    #region Content events
    134127    private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
    135       switch (e.PropertyName) {
    136         case "MaximumExecutionTime":
    137           SuppressEvents = true;
    138           try {
    139             timeLimitTextBox.Text = TimeSpanHelper.FormatNatural(Content.MaximumExecutionTime);
    140           } finally { SuppressEvents = false; }
    141           break;
    142         case "SnapshotTimes":
    143           SuppressEvents = true;
    144           try {
    145             if (Content.SnapshotTimes.Any())
    146               snapshotsTextBox.Text = String.Join(" ; ", Content.SnapshotTimes.Select(x => TimeSpanHelper.FormatNatural(x, true)));
    147             else snapshotsTextBox.Text = String.Empty;
    148             Content.SnapshotTimes.ItemsAdded += Content_SnapshotTimes_Changed;
    149             Content.SnapshotTimes.ItemsMoved += Content_SnapshotTimes_Changed;
    150             Content.SnapshotTimes.ItemsRemoved += Content_SnapshotTimes_Changed;
    151             Content.SnapshotTimes.ItemsReplaced += Content_SnapshotTimes_Changed;
    152             Content.SnapshotTimes.CollectionReset += Content_SnapshotTimes_Changed;
    153           } finally { SuppressEvents = false; }
    154           break;
    155         case "StoreAlgorithmInEachSnapshot":
    156           SuppressEvents = true;
    157           try {
    158             storeAlgorithmInEachSnapshotCheckBox.Checked = Content.StoreAlgorithmInEachSnapshot;
    159           } finally { SuppressEvents = false; }
    160           break;
    161         case "Algorithm":
    162           SuppressEvents = true;
    163           try {
    164             algorithmViewHost.Content = Content.Algorithm;
    165           } finally { SuppressEvents = false; }
    166           break;
    167         case "Snapshots":
    168           SuppressEvents = true;
    169           try {
    170             snapshotsView.Content = Content.Snapshots;
    171           } finally { SuppressEvents = false; }
    172           break;
    173         case "Runs":
    174           SuppressEvents = true;
    175           try {
    176             runsView.Content = Content.Runs;
    177           } finally { SuppressEvents = false; }
    178           break;
    179       }
    180     }
    181 
    182     private void Content_SnapshotTimes_Changed(object sender, EventArgs e) {
    183128      SuppressEvents = true;
    184129      try {
    185         if (Content.SnapshotTimes.Any())
    186           snapshotsTextBox.Text = string.Join(" ; ", Content.SnapshotTimes.Select(x => TimeSpanHelper.FormatNatural(x, true)));
    187         else snapshotsTextBox.Text = String.Empty;
     130        switch (e.PropertyName) {
     131          case "TerminationCriterium": terminationComboBox.SelectedItem = Content.TerminationCriterium; break;
     132          case "MaximumExecutionTime": maxExecutionTimeTextBox.Text = TimeSpanHelper.FormatNatural(Content.MaximumExecutionTime); break;
     133          case "MaximumEvaluations": maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString(); break;
     134          case "TargetValue": targetValueTextBox.Text = Content.TargetValue.ToString(CultureInfo.CurrentCulture.NumberFormat); break;
     135          case "Maximization": maximizationCheckBox.Checked = Content.Maximization; break;
     136          case "MoveCostPerSolution": moveCostPerSolutionTextBox.Text = Content.MoveCostPerSolution.ToString(CultureInfo.CurrentCulture.NumberFormat); break;
     137          case "Algorithm": algorithmViewHost.Content = Content.Algorithm; break;
     138          case "CurrentRun": currentRunView.Content = Content.CurrentRun; break;
     139          case "Runs": runsView.Content = Content.Runs; break;
     140        }
    188141      } finally { SuppressEvents = false; }
    189142    }
     
    191144
    192145    #region Control events
    193     private void timeLimitTextBox_Validating(object sender, CancelEventArgs e) {
    194       if (SuppressEvents) return;
     146    private void maxExecutionTimeTextBox_Validating(object sender, CancelEventArgs e) {
     147      if (SuppressEvents) return;
     148      if (InvokeRequired) {
     149        Invoke((Action<object, CancelEventArgs>)maxExecutionTimeTextBox_Validating, sender, e);
     150        return;
     151      }
    195152      TimeSpan ts;
    196       if (!TimeSpanHelper.TryGetFromNaturalFormat(timeLimitTextBox.Text, out ts)) {
    197         e.Cancel = !timeLimitTextBox.ReadOnly && timeLimitTextBox.Enabled;
    198         errorProvider.SetError(timeLimitTextBox, "Please enter a valid time span, e.g. 20 seconds ; 45s ; 4min ; 1h ; 3 hours ; 2 days ; 4d");
     153      if (!TimeSpanHelper.TryGetFromNaturalFormat(maxExecutionTimeTextBox.Text, out ts)) {
     154        e.Cancel = !maxExecutionTimeTextBox.ReadOnly && maxExecutionTimeTextBox.Enabled;
     155        errorProvider.SetError(maxExecutionTimeTextBox, "Please enter a valid time span, e.g. 20 seconds ; 45s ; 4min ; 1h ; 3 hours ; 2 days ; 4d");
    199156      } else {
    200157        Content.MaximumExecutionTime = ts;
    201158        e.Cancel = false;
    202         errorProvider.SetError(timeLimitTextBox, null);
    203       }
    204     }
    205 
    206     private void snapshotsTextBox_Validating(object sender, CancelEventArgs e) {
    207       if (SuppressEvents) return;
    208       e.Cancel = false;
    209       errorProvider.SetError(snapshotsTextBox, null);
    210 
    211       var snapshotTimes = new ObservableList<TimeSpan>();
    212       var matches = Regex.Matches(snapshotsTextBox.Text, @"(\d+[ ;,\t]*\w+)");
    213       foreach (Match m in matches) {
    214         TimeSpan value;
    215         if (!TimeSpanHelper.TryGetFromNaturalFormat(m.Value, out value)) {
    216           e.Cancel = !snapshotsTextBox.ReadOnly && snapshotsTextBox.Enabled; // don't cancel an operation that cannot be edited
    217           errorProvider.SetError(snapshotsTextBox, "Error parsing " + m.Value + ", please provide a valid time span, e.g. 20 seconds ; 45s ; 4min ; 1h ; 3 hours ; 2 days ; 4d");
    218           return;
    219         } else {
    220           snapshotTimes.Add(value);
    221         }
    222       }
    223       Content.SnapshotTimes = snapshotTimes;
    224     }
    225 
    226     private void storeAlgorithmInEachSnapshotCheckBox_CheckedChanged(object sender, EventArgs e) {
    227       if (SuppressEvents) return;
    228       SuppressEvents = true;
    229       try {
    230         Content.StoreAlgorithmInEachSnapshot = storeAlgorithmInEachSnapshotCheckBox.Checked;
    231       } finally { SuppressEvents = false; }
     159        errorProvider.SetError(maxExecutionTimeTextBox, null);
     160      }
     161    }
     162
     163    private void maxEvaluationsTextBox_Validating(object sender, CancelEventArgs e) {
     164      if (SuppressEvents) return;
     165      if (InvokeRequired) {
     166        Invoke((Action<object, CancelEventArgs>)maxEvaluationsTextBox_Validating, sender, e);
     167        return;
     168      }
     169      int value;
     170      if (!int.TryParse(maxEvaluationsTextBox.Text, out value)) {
     171        e.Cancel = !maxEvaluationsTextBox.ReadOnly && maxEvaluationsTextBox.Enabled;
     172        errorProvider.SetError(maxEvaluationsTextBox, "Please enter a valid integer number.");
     173      } else {
     174        Content.MaximumEvaluations = value;
     175        e.Cancel = false;
     176        errorProvider.SetError(maxEvaluationsTextBox, null);
     177      }
     178    }
     179
     180    private void moveCostPerSolutionTextBox_Validating(object sender, CancelEventArgs e) {
     181      if (SuppressEvents) return;
     182      if (InvokeRequired) {
     183        Invoke((Action<object, CancelEventArgs>)moveCostPerSolutionTextBox_Validating, sender, e);
     184        return;
     185      }
     186      double value;
     187      if (!double.TryParse(moveCostPerSolutionTextBox.Text, out value)) {
     188        e.Cancel = !moveCostPerSolutionTextBox.ReadOnly && moveCostPerSolutionTextBox.Enabled;
     189        errorProvider.SetError(moveCostPerSolutionTextBox, "Please enter a valid integer number.");
     190      } else {
     191        Content.MoveCostPerSolution = value;
     192        e.Cancel = false;
     193        errorProvider.SetError(moveCostPerSolutionTextBox, null);
     194      }
     195    }
     196
     197    private void targetValueTextBox_Validating(object sender, CancelEventArgs e) {
     198      if (SuppressEvents) return;
     199      if (InvokeRequired) {
     200        Invoke((Action<object, CancelEventArgs>)targetValueTextBox_Validating, sender, e);
     201        return;
     202      }
     203      double value;
     204      if (!double.TryParse(targetValueTextBox.Text, out value)) {
     205        e.Cancel = !targetValueTextBox.ReadOnly && targetValueTextBox.Enabled;
     206        errorProvider.SetError(targetValueTextBox, "Please enter a valid integer number.");
     207      } else {
     208        Content.TargetValue = value;
     209        e.Cancel = false;
     210        errorProvider.SetError(targetValueTextBox, null);
     211      }
     212    }
     213
     214    private void maximizationCheckBox_CheckedChanged(object sender, EventArgs e) {
     215      if (SuppressEvents) return;
     216      if (InvokeRequired) {
     217        Invoke((Action<object, EventArgs>)maximizationCheckBox_CheckedChanged, sender, e);
     218        return;
     219      }
     220      Content.Maximization = maximizationCheckBox.Checked;
     221    }
     222
     223    private void terminationComboBox_SelectedIndexChanged(object sender, EventArgs e) {
     224      if (SuppressEvents) return;
     225      if (InvokeRequired) {
     226        Invoke((Action<object, EventArgs>)terminationComboBox_SelectedIndexChanged, sender, e);
     227        return;
     228      }
     229      Content.TerminationCriterium = (TerminationCriterium)terminationComboBox.SelectedItem;
    232230    }
    233231
     
    236234        algorithmTypeSelectorDialog = new TypeSelectorDialog { Caption = "Select Algorithm" };
    237235        algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
    238         algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
     236        algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm)
     237          , showNotInstantiableTypes: false, showGenericTypes: false);
    239238      }
    240239      if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
     
    275274    private void algorithmTabPage_DragEnterOver(object sender, DragEventArgs e) {
    276275      e.Effect = DragDropEffects.None;
    277       if (!ReadOnly && (e.Data.GetData(Constants.DragDropDataFormat) is IAlgorithm)) {
     276      var alg = e.Data.GetData(Constants.DragDropDataFormat) as IAlgorithm;
     277      if (!ReadOnly && alg != null) {
     278        if (!typeof(ISingleObjectiveHeuristicOptimizationProblem).IsAssignableFrom(alg.ProblemType))
     279          return;
    278280        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
    279281        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
     
    291293      }
    292294    }
    293 
    294     private void snapshotButton_Click(object sender, EventArgs e) {
    295       Content.Snapshot();
    296     }
    297 
    298     private void sequenceButton_Click(object sender, EventArgs e) {
    299       using (var dialog = new DefineArithmeticTimeSpanProgressionDialog(TimeSpan.FromSeconds(1), Content.MaximumExecutionTime, TimeSpan.FromSeconds(1))) {
    300         if (dialog.ShowDialog() == DialogResult.OK) {
    301           if (dialog.Values.Any())
    302             Content.SnapshotTimes = new ObservableList<TimeSpan>(dialog.Values);
    303           else Content.SnapshotTimes = new ObservableList<TimeSpan>();
    304         }
    305       }
    306     }
    307295    #endregion
    308296    #endregion
Note: See TracChangeset for help on using the changeset viewer.