Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/03/12 13:58:47 (12 years ago)
Author:
abeham
Message:

#1783:

  • Replaced in BatchRun the four boolean action flags with an enum
  • Caught InvalidOperationException when changing the state of the nested optimizer, which usually occurs in a race-condition
Location:
trunk/sources/HeuristicLab.Optimization/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Optimization/3.3/BatchRun.cs

    r8190 r8194  
    3030
    3131namespace HeuristicLab.Optimization {
     32  internal enum BatchRunAction { None, Prepare, Start, Pause, Stop };
     33
    3234  /// <summary>
    3335  /// A run in which an optimizer is executed a given number of times.
     
    153155    }
    154156
    155     private bool batchRunPrepared = false;
    156     private bool batchRunStarted = false;
    157     private bool batchRunPaused = false;
    158     private bool batchRunStopped = false;
     157    private BatchRunAction batchRunAction = BatchRunAction.None;
    159158
    160159    public BatchRun()
     
    204203      repetitionsCounter = original.repetitionsCounter;
    205204      runs = cloner.Clone(original.runs);
    206       batchRunStarted = original.batchRunStarted;
    207       batchRunPaused = original.batchRunPaused;
    208       batchRunStopped = original.batchRunStopped;
    209       batchRunPrepared = original.batchRunPrepared;
     205      batchRunAction = original.batchRunAction;
    210206      Initialize();
    211207    }
     
    230226        repetitionsCounter = 0;
    231227        if (clearRuns) runs.Clear();
    232         batchRunPrepared = true;
    233         batchRunStarted = false;
    234         batchRunPaused = false;
    235         batchRunStopped = false;
    236         Optimizer.Prepare(clearRuns);
     228        batchRunAction = BatchRunAction.Prepare;
     229        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
     230        try { Optimizer.Prepare(clearRuns); } catch (InvalidOperationException) { }
    237231      } else {
    238232        ExecutionState = ExecutionState.Stopped;
     
    243237        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
    244238      if (Optimizer == null) return;
    245 
    246       batchRunPrepared = false;
    247       batchRunStarted = true;
    248       batchRunPaused = false;
    249       batchRunStopped = false;
     239      batchRunAction = BatchRunAction.Start;
    250240      if (Optimizer.ExecutionState == ExecutionState.Stopped) Optimizer.Prepare();
    251       Optimizer.Start();
     241      // a race-condition may occur when the optimizer has changed the state by itself in the meantime
     242      try { Optimizer.Start(); } catch (InvalidOperationException) { }
    252243    }
    253244    public void Pause() {
     
    255246        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
    256247      if (Optimizer == null) return;
     248      batchRunAction = BatchRunAction.Pause;
    257249      if (Optimizer.ExecutionState != ExecutionState.Started) return;
    258 
    259       batchRunPrepared = false;
    260       batchRunStarted = false;
    261       batchRunPaused = true;
    262       batchRunStopped = false;
    263       Optimizer.Pause();
     250      // a race-condition may occur when the optimizer has changed the state by itself in the meantime
     251      try { Optimizer.Pause(); } catch (InvalidOperationException) { }
    264252    }
    265253    public void Stop() {
     
    267255        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
    268256      if (Optimizer == null) return;
     257      batchRunAction = BatchRunAction.Stop;
    269258      if (Optimizer.ExecutionState != ExecutionState.Started && Optimizer.ExecutionState != ExecutionState.Paused) {
    270259        OnStopped();
    271260        return;
    272261      }
    273 
    274       batchRunPrepared = false;
    275       batchRunStarted = false;
    276       batchRunPaused = false;
    277       batchRunStopped = true;
    278       Optimizer.Stop();
     262      // a race-condition may occur when the optimizer has changed the state by itself in the meantime
     263      try { Optimizer.Stop(); } catch (InvalidOperationException) { }
    279264    }
    280265
     
    302287    public event EventHandler Prepared;
    303288    private void OnPrepared() {
    304       batchRunPrepared = false;
     289      batchRunAction = BatchRunAction.None;
    305290      ExecutionState = ExecutionState.Prepared;
    306291      EventHandler handler = Prepared;
     
    309294    public event EventHandler Started;
    310295    private void OnStarted() {
    311       //TODO add coment
     296      // no reset of BatchRunAction.Started, because we need to differ which of the two was started by the user
    312297      ExecutionState = ExecutionState.Started;
    313298      EventHandler handler = Started;
     
    316301    public event EventHandler Paused;
    317302    private void OnPaused() {
    318       batchRunPaused = false;
     303      batchRunAction = BatchRunAction.None;
    319304      ExecutionState = ExecutionState.Paused;
    320305      EventHandler handler = Paused;
     
    323308    public event EventHandler Stopped;
    324309    private void OnStopped() {
    325       //reset flags because it cannot be done if the optimizer gets prepared
    326       batchRunStopped = false;
     310      batchRunAction = BatchRunAction.None;
    327311      ExecutionState = ExecutionState.Stopped;
    328312      EventHandler handler = Stopped;
     
    369353    }
    370354    private void Optimizer_Prepared(object sender, EventArgs e) {
    371       if (batchRunPrepared || (ExecutionState == ExecutionState.Stopped)) {
     355      if (batchRunAction == BatchRunAction.Prepare || ExecutionState == ExecutionState.Stopped) {
    372356        ExecutionTime = TimeSpan.Zero;
    373357        runsExecutionTime = TimeSpan.Zero;
     
    385369      runsExecutionTime = TimeSpan.Zero;
    386370
    387       if (batchRunStopped) OnStopped();
     371      if (batchRunAction == BatchRunAction.Stop) OnStopped();
    388372      else if (repetitionsCounter >= repetitions) OnStopped();
    389       else if (batchRunPaused) OnPaused();
    390       else if (batchRunStarted) {
     373      else if (batchRunAction == BatchRunAction.Pause) OnPaused();
     374      else if (batchRunAction == BatchRunAction.Start) {
    391375        Optimizer.Prepare();
    392376        Optimizer.Start();
  • trunk/sources/HeuristicLab.Optimization/3.3/Experiment.cs

    r8170 r8194  
    178178      experimentStarted = false;
    179179      experimentStopped = false;
    180       foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started))
    181         //if (clearRuns || optimizer.ExecutionState != ExecutionState.Prepared)
    182         optimizer.Prepare(clearRuns);
     180      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started)) {
     181        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
     182        try { optimizer.Prepare(clearRuns); } catch (InvalidOperationException) { }
     183      }
    183184    }
    184185    public void Start() {
     
    190191      experimentStopped = false;
    191192      IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused));
    192       if (optimizer != null) optimizer.Start();
     193      if (optimizer != null) {
     194        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
     195        try { optimizer.Start(); } catch (InvalidOperationException) { }
     196      }
    193197    }
    194198    public void Pause() {
     
    199203      experimentStarted = false;
    200204      experimentStopped = false;
    201       foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started))
    202         optimizer.Pause();
     205      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started)) {
     206        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
     207        try { optimizer.Pause(); } catch (InvalidOperationException) { }
     208      }
    203209    }
    204210    public void Stop() {
     
    210216      experimentStopped = true;
    211217      if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused))) {
    212         foreach (IOptimizer optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused)))
    213           optimizer.Stop();
     218        foreach (var optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused))) {
     219          // a race-condition may occur when the optimizer has changed the state by itself in the meantime
     220          try { optimizer.Stop(); } catch (InvalidOperationException) { }
     221        }
    214222      } else {
    215223        OnStopped();
Note: See TracChangeset for help on using the changeset viewer.