Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/03/12 16:46:35 (12 years ago)
Author:
gkronber
Message:

#1847: merged r8084:8205 from trunk into GP move operators branch

Location:
branches/GP-MoveOperators
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/GP-MoveOperators

  • branches/GP-MoveOperators/HeuristicLab.Optimization/3.3/BatchRun.cs

    r7576 r8206  
    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.
     
    7981      }
    8082    }
     83
     84    [Storable]
     85    private TimeSpan runsExecutionTime;
    8186
    8287    [Storable]
     
    150155    }
    151156
    152     private bool batchRunStarted = false;
    153     private bool batchRunPaused = false;
    154     private bool batchRunStopped = false;
     157    private BatchRunAction batchRunAction = BatchRunAction.None;
    155158
    156159    public BatchRun()
     
    160163      executionState = ExecutionState.Stopped;
    161164      executionTime = TimeSpan.Zero;
     165      runsExecutionTime = TimeSpan.Zero;
    162166      repetitions = 10;
    163167      repetitionsCounter = 0;
     
    169173      executionState = ExecutionState.Stopped;
    170174      executionTime = TimeSpan.Zero;
     175      runsExecutionTime = TimeSpan.Zero;
    171176      repetitions = 10;
    172177      repetitionsCounter = 0;
     
    177182      executionState = ExecutionState.Stopped;
    178183      executionTime = TimeSpan.Zero;
     184      runsExecutionTime = TimeSpan.Zero;
    179185      repetitions = 10;
    180186      repetitionsCounter = 0;
     
    192198      executionState = original.executionState;
    193199      executionTime = original.executionTime;
     200      runsExecutionTime = original.runsExecutionTime;
    194201      optimizer = cloner.Clone(original.optimizer);
    195202      repetitions = original.repetitions;
    196203      repetitionsCounter = original.repetitionsCounter;
    197204      runs = cloner.Clone(original.runs);
    198       batchRunStarted = original.batchRunStarted;
    199       batchRunPaused = original.batchRunPaused;
    200       batchRunStopped = original.batchRunStopped;
     205      batchRunAction = original.batchRunAction;
    201206      Initialize();
    202207    }
     
    218223        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
    219224      if (Optimizer != null) {
     225        ExecutionTime = TimeSpan.Zero;
    220226        repetitionsCounter = 0;
    221227        if (clearRuns) runs.Clear();
    222         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) { }
    223231      } else {
    224232        ExecutionState = ExecutionState.Stopped;
     
    229237        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
    230238      if (Optimizer == null) return;
    231 
    232       batchRunStarted = true;
    233       batchRunPaused = false;
    234       batchRunStopped = false;
     239      batchRunAction = BatchRunAction.Start;
    235240      if (Optimizer.ExecutionState == ExecutionState.Stopped) Optimizer.Prepare();
    236       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) { }
    237243    }
    238244    public void Pause() {
     
    240246        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
    241247      if (Optimizer == null) return;
     248      batchRunAction = BatchRunAction.Pause;
    242249      if (Optimizer.ExecutionState != ExecutionState.Started) return;
    243 
    244       batchRunStarted = false;
    245       batchRunPaused = true;
    246       batchRunStopped = false;
    247       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) { }
    248252    }
    249253    public void Stop() {
     
    251255        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
    252256      if (Optimizer == null) return;
    253       if (Optimizer.ExecutionState != ExecutionState.Started && Optimizer.ExecutionState != ExecutionState.Paused) return;
    254       batchRunStarted = false;
    255       batchRunPaused = false;
    256       batchRunStopped = true;
    257       Optimizer.Stop();
     257      batchRunAction = BatchRunAction.Stop;
     258      if (Optimizer.ExecutionState != ExecutionState.Started && Optimizer.ExecutionState != ExecutionState.Paused) {
     259        OnStopped();
     260        return;
     261      }
     262      // a race-condition may occur when the optimizer has changed the state by itself in the meantime
     263      try { Optimizer.Stop(); } catch (InvalidOperationException) { }
    258264    }
    259265
     
    281287    public event EventHandler Prepared;
    282288    private void OnPrepared() {
     289      batchRunAction = BatchRunAction.None;
    283290      ExecutionState = ExecutionState.Prepared;
    284291      EventHandler handler = Prepared;
     
    287294    public event EventHandler Started;
    288295    private void OnStarted() {
     296      // no reset of BatchRunAction.Started, because we need to differ which of the two was started by the user
    289297      ExecutionState = ExecutionState.Started;
    290298      EventHandler handler = Started;
     
    293301    public event EventHandler Paused;
    294302    private void OnPaused() {
     303      batchRunAction = BatchRunAction.None;
    295304      ExecutionState = ExecutionState.Paused;
    296305      EventHandler handler = Paused;
     
    299308    public event EventHandler Stopped;
    300309    private void OnStopped() {
     310      batchRunAction = BatchRunAction.None;
    301311      ExecutionState = ExecutionState.Stopped;
    302312      EventHandler handler = Stopped;
     
    343353    }
    344354    private void Optimizer_Prepared(object sender, EventArgs e) {
    345       if (ExecutionState == ExecutionState.Stopped || !batchRunStarted) {
     355      if (batchRunAction == BatchRunAction.Prepare || ExecutionState == ExecutionState.Stopped) {
     356        ExecutionTime = TimeSpan.Zero;
     357        runsExecutionTime = TimeSpan.Zero;
     358        repetitionsCounter = 0;
    346359        OnPrepared();
    347360      }
     
    353366    private void Optimizer_Stopped(object sender, EventArgs e) {
    354367      repetitionsCounter++;
    355 
    356       if (batchRunStopped) OnStopped();
     368      ExecutionTime += runsExecutionTime;
     369      runsExecutionTime = TimeSpan.Zero;
     370
     371      if (batchRunAction == BatchRunAction.Stop) OnStopped();
    357372      else if (repetitionsCounter >= repetitions) OnStopped();
    358       else if (batchRunPaused) OnPaused();
    359       else if (batchRunStarted) {
     373      else if (batchRunAction == BatchRunAction.Pause) OnPaused();
     374      else if (batchRunAction == BatchRunAction.Start) {
    360375        Optimizer.Prepare();
    361376        Optimizer.Start();
     377      } else if (executionState == ExecutionState.Started) {
     378        // if the batch run hasn't been started but the inner optimizer was run then pause
     379        OnPaused();
    362380      } else OnStopped();
    363381    }
     
    385403    }
    386404    private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
    387       foreach (IRun run in e.OldItems) {
    388         IItem item;
    389         run.Results.TryGetValue("Execution Time", out item);
    390         TimeSpanValue executionTime = item as TimeSpanValue;
    391         if (executionTime != null) ExecutionTime -= executionTime.Value;
    392       }
    393405      if (Optimizer != null) Optimizer.Runs.RemoveRange(e.OldItems);
    394406      foreach (IRun run in e.Items) {
     
    404416        run.Results.TryGetValue("Execution Time", out item);
    405417        TimeSpanValue executionTime = item as TimeSpanValue;
    406         if (executionTime != null) ExecutionTime += executionTime.Value;
     418        if (executionTime != null) {
     419          if (Optimizer.ExecutionState == ExecutionState.Started)
     420            runsExecutionTime += executionTime.Value;
     421          else
     422            ExecutionTime += executionTime.Value;
     423        }
    407424      }
    408425    }
    409426    private void Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
    410       foreach (IRun run in e.Items) {
    411         IItem item;
    412         run.Results.TryGetValue("Execution Time", out item);
    413         TimeSpanValue executionTime = item as TimeSpanValue;
    414         if (executionTime != null) ExecutionTime -= executionTime.Value;
    415       }
    416427      if (Optimizer != null) Optimizer.Runs.RemoveRange(e.Items);
    417428    }
Note: See TracChangeset for help on using the changeset viewer.