Changeset 16651


Ignore:
Timestamp:
03/06/19 10:54:20 (3 months ago)
Author:
abeham
Message:

#2561: Decoupled execution state of timelimitrun and its embedded algorithm

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Optimization.Views/3.3/TimeLimitRunView.cs

    r16565 r16651  
    134134    private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
    135135      switch (e.PropertyName) {
    136         case "MaximumExecutionTime":
     136        case nameof(Content.MaximumExecutionTime):
    137137          SuppressEvents = true;
    138138          try {
     
    140140          } finally { SuppressEvents = false; }
    141141          break;
    142         case "SnapshotTimes":
     142        case nameof(Content.SnapshotTimes):
    143143          SuppressEvents = true;
    144144          try {
     
    153153          } finally { SuppressEvents = false; }
    154154          break;
    155         case "StoreAlgorithmInEachSnapshot":
     155        case nameof(Content.StoreAlgorithmInEachSnapshot):
    156156          SuppressEvents = true;
    157157          try {
     
    159159          } finally { SuppressEvents = false; }
    160160          break;
    161         case "Algorithm":
     161        case nameof(Content.Algorithm):
    162162          SuppressEvents = true;
    163163          try {
     
    165165          } finally { SuppressEvents = false; }
    166166          break;
    167         case "Snapshots":
     167        case nameof(Content.Snapshots):
    168168          SuppressEvents = true;
    169169          try {
     
    171171          } finally { SuppressEvents = false; }
    172172          break;
    173         case "Runs":
     173        case nameof(Content.Runs):
    174174          SuppressEvents = true;
    175175          try {
  • trunk/HeuristicLab.Optimization/3.3/MetaOptimizers/TimeLimitRun.cs

    r16565 r16651  
    2727using System.Threading;
    2828using System.Threading.Tasks;
     29using HEAL.Attic;
    2930using HeuristicLab.Collections;
    3031using HeuristicLab.Common;
    3132using HeuristicLab.Common.Resources;
    3233using HeuristicLab.Core;
    33 using HEAL.Attic;
    3434
    3535namespace HeuristicLab.Optimization {
     
    6262        if (maximumExecutionTime == value) return;
    6363        maximumExecutionTime = value;
    64         OnPropertyChanged("MaximumExecutionTime");
     64        OnPropertyChanged(nameof(MaximumExecutionTime));
    6565      }
    6666    }
     
    7777        snapshotTimes.Sort();
    7878        FindNextSnapshotTimeIndex(ExecutionTime);
    79         OnPropertyChanged("SnapshotTimes");
     79        OnPropertyChanged(nameof(SnapshotTimes));
    8080      }
    8181    }
     
    8989        if (storeAlgorithmInEachSnapshot == value) return;
    9090        storeAlgorithmInEachSnapshot = value;
    91         OnPropertyChanged("StoreAlgorithmInEachSnapshot");
     91        OnPropertyChanged(nameof(StoreAlgorithmInEachSnapshot));
    9292      }
    9393    }
     
    100100        if (snapshots == value) return;
    101101        snapshots = value;
    102         OnPropertyChanged("Snapshots");
     102        OnPropertyChanged(nameof(Snapshots));
    103103      }
    104104    }
    105105
    106106    #region Inherited Properties
     107    [Storable]
     108    private ExecutionState executionState;
    107109    public ExecutionState ExecutionState {
    108       get { return (Algorithm != null) ? Algorithm.ExecutionState : ExecutionState.Stopped; }
     110      get { return executionState; }
     111      private set {
     112        if (executionState == value) return;
     113        executionState = value;
     114        OnExecutionStateChanged();
     115        OnPropertyChanged(nameof(ExecutionState));
     116      }
    109117    }
    110118
     
    119127      set {
    120128        if (algorithm == value) return;
     129        if (ExecutionState == ExecutionState.Started || ExecutionState == ExecutionState.Paused)
     130          throw new InvalidOperationException("Cannot change algorithm while the TimeLimitRun is running or paused.");
    121131        if (algorithm != null) DeregisterAlgorithmEvents();
    122132        algorithm = value;
     133        if (algorithm != null) RegisterAlgorithmEvents();
     134        OnPropertyChanged(nameof(Algorithm));
     135        OnPropertyChanged(nameof(NestedOptimizers));
    123136        if (algorithm != null) {
    124           RegisterAlgorithmEvents();
    125         }
    126         OnPropertyChanged("Algorithm");
    127         Prepare();
     137          if (algorithm.ExecutionState == ExecutionState.Started || algorithm.ExecutionState == ExecutionState.Paused)
     138            throw new InvalidOperationException("Cannot assign a running or paused algorithm to a TimeLimitRun.");
     139          Prepare();
     140          if (algorithm.ExecutionState == ExecutionState.Stopped) OnStopped();
     141        } else OnStopped();
    128142      }
    129143    }
     
    137151        if (runs == value) return;
    138152        runs = value;
    139         OnPropertyChanged("Runs");
     153        OnPropertyChanged(nameof(Runs));
    140154      }
    141155    }
     
    160174      snapshots = cloner.Clone(original.snapshots);
    161175      storeAlgorithmInEachSnapshot = original.storeAlgorithmInEachSnapshot;
     176      executionState = original.executionState;
    162177      algorithm = cloner.Clone(original.algorithm);
    163178      runs = cloner.Clone(original.runs);
     
    169184      name = ItemName;
    170185      description = ItemDescription;
     186      executionState = ExecutionState.Stopped;
    171187      maximumExecutionTime = TimeSpan.FromMinutes(.5);
    172188      snapshotTimes = new ObservableList<TimeSpan>(new[] {
     
    182198      : base(name) {
    183199      description = ItemDescription;
     200      executionState = ExecutionState.Stopped;
    184201      maximumExecutionTime = TimeSpan.FromMinutes(.5);
    185202      snapshotTimes = new ObservableList<TimeSpan>(new[] {
     
    194211    public TimeLimitRun(string name, string description)
    195212      : base(name, description) {
     213      executionState = ExecutionState.Stopped;
    196214      maximumExecutionTime = TimeSpan.FromMinutes(.5);
    197215      snapshotTimes = new ObservableList<TimeSpan>(new[] {
     
    213231    private void AfterDeserialization() {
    214232      Initialize();
     233      // BackwardsCompatibility3.3
     234      #region Backwards compatible code, remove with 3.4
     235      if (Algorithm != null && executionState != Algorithm.ExecutionState) {
     236        executionState = Algorithm.ExecutionState;
     237      } else if (Algorithm == null) executionState = ExecutionState.Stopped;
     238      #endregion
    215239    }
    216240
     
    238262    }
    239263    public void Prepare(bool clearRuns) {
     264      if (Algorithm == null) return;
    240265      Algorithm.Prepare(clearRuns);
    241266    }
     
    282307    public event EventHandler Prepared;
    283308    private void OnPrepared() {
     309      ExecutionState = ExecutionState.Prepared;
    284310      var handler = Prepared;
    285311      if (handler != null) handler(this, EventArgs.Empty);
     
    287313    public event EventHandler Started;
    288314    private void OnStarted() {
     315      ExecutionState = ExecutionState.Started;
    289316      var handler = Started;
    290317      if (handler != null) handler(this, EventArgs.Empty);
     
    292319    public event EventHandler Paused;
    293320    private void OnPaused() {
     321      ExecutionState = ExecutionState.Paused;
    294322      var handler = Paused;
    295323      if (handler != null) handler(this, EventArgs.Empty);
     
    297325    public event EventHandler Stopped;
    298326    private void OnStopped() {
     327      ExecutionState = ExecutionState.Stopped;
    299328      var handler = Stopped;
    300329      if (handler != null) handler(this, EventArgs.Empty);
     
    302331    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
    303332    private void OnExceptionOccurred(Exception exception) {
     333      ExecutionState = ExecutionState.Paused;
    304334      var handler = ExceptionOccurred;
    305335      if (handler != null) handler(this, new EventArgs<Exception>(exception));
     
    311341      algorithm.ExceptionOccurred += Algorithm_ExceptionOccurred;
    312342      algorithm.ExecutionTimeChanged += Algorithm_ExecutionTimeChanged;
    313       algorithm.ExecutionStateChanged += Algorithm_ExecutionStateChanged;
    314343      algorithm.Paused += Algorithm_Paused;
    315344      algorithm.Prepared += Algorithm_Prepared;
     
    320349      algorithm.ExceptionOccurred -= Algorithm_ExceptionOccurred;
    321350      algorithm.ExecutionTimeChanged -= Algorithm_ExecutionTimeChanged;
    322       algorithm.ExecutionStateChanged -= Algorithm_ExecutionStateChanged;
    323351      algorithm.Paused -= Algorithm_Paused;
    324352      algorithm.Prepared -= Algorithm_Prepared;
     
    340368      }
    341369      OnExecutionTimeChanged();
    342     }
    343     private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) {
    344       OnExecutionStateChanged();
    345370    }
    346371    private void Algorithm_Paused(object sender, EventArgs e) {
     
    350375        MakeSnapshot();
    351376        FindNextSnapshotTimeIndex(ExecutionTime);
    352       }
    353       OnPaused();
     377      } else OnPaused();
    354378      if (action == ExecutionState.Started) Algorithm.Start();
    355379      else if (action == ExecutionState.Stopped) Algorithm.Stop();
     
    361385    }
    362386    private void Algorithm_Started(object sender, EventArgs e) {
    363       OnStarted();
     387      if (ExecutionState == ExecutionState.Prepared || ExecutionState == ExecutionState.Paused)
     388        OnStarted();
     389      if (ExecutionState == ExecutionState.Stopped)
     390        throw new InvalidOperationException("Algorithm was started although TimeLimitRun was in state Stopped.");
     391      // otherwise the algorithm was just started after being snapshotted
    364392    }
    365393    private void Algorithm_Stopped(object sender, EventArgs e) {
    366       var cloner = new Cloner();
    367       var algRun = cloner.Clone(Algorithm.Runs.Last());
    368       var clonedSnapshots = cloner.Clone(snapshots);
    369       algRun.Results.Add("TimeLimitRunSnapshots", clonedSnapshots);
    370       Runs.Add(algRun);
    371       Algorithm.Runs.Clear();
    372       OnStopped();
     394      try {
     395        var cloner = new Cloner();
     396        var algRun = cloner.Clone(Algorithm.Runs.Last());
     397        var clonedSnapshots = cloner.Clone(snapshots);
     398        algRun.Results.Add("TimeLimitRunSnapshots", clonedSnapshots);
     399        Runs.Add(algRun);
     400        Algorithm.Runs.Clear();
     401      } finally { OnStopped(); }
    373402    }
    374403    #endregion
     
    379408      while (index < snapshotTimes.Count && snapshotTimes[index] <= reference) {
    380409        index++;
    381       };
     410      }
    382411      snapshotTimesIndex = index;
    383412    }
Note: See TracChangeset for help on using the changeset viewer.