Changeset 15339 for trunk/sources/HeuristicLab.Optimization
- Timestamp:
- 08/22/17 15:15:38 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs
r15327 r15339 111 111 private bool experimentStarted = false; 112 112 private bool experimentStopped = false; 113 private ManualResetEventSlim allOptimizerFinished; // this indicates that all started optimizers have been paused or stopped113 private readonly ManualResetEventSlim allOptimizerFinished = new ManualResetEventSlim(false); // this indicates that all started optimizers have been paused or stopped 114 114 115 115 public Experiment() … … 192 192 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) 193 193 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); 194 if ( Optimizers.Count == 0) return;194 if (!Optimizers.Any(x => x.ExecutionState == ExecutionState.Prepared || x.ExecutionState == ExecutionState.Paused)) return; 195 195 196 196 experimentStarted = true; 197 197 experimentStopped = false; 198 allOptimizerFinished = new ManualResetEventSlim(false); 199 198 allOptimizerFinished.Reset(); 200 199 IOptimizer optimizer; 201 while ((optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) != null) {200 while ((optimizer = Optimizers.FirstOrDefault(x => x.ExecutionState == ExecutionState.Prepared || x.ExecutionState == ExecutionState.Paused)) != null) { 202 201 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 203 202 try { optimizer.Start(cancellationToken); } catch (InvalidOperationException) { } 204 203 if (ExecutionState == ExecutionState.Paused || ExecutionState == ExecutionState.Stopped) break; 205 204 } 206 207 205 allOptimizerFinished.Wait(); 208 206 } … … 271 269 private void OnPaused() { 272 270 ExecutionState = ExecutionState.Paused; 271 allOptimizerFinished.Set(); 273 272 EventHandler handler = Paused; 274 273 if (handler != null) handler(this, EventArgs.Empty); … … 277 276 private void OnStopped() { 278 277 ExecutionState = ExecutionState.Stopped; 278 allOptimizerFinished.Set(); 279 279 EventHandler handler = Stopped; 280 280 if (handler != null) handler(this, EventArgs.Empty); … … 373 373 private void optimizer_Paused(object sender, EventArgs e) { 374 374 lock (locker) 375 if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started)) { 376 OnPaused(); 377 allOptimizerFinished.Set(); 378 } 375 if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started)) OnPaused(); 379 376 } 380 377 private void optimizer_Prepared(object sender, EventArgs e) { … … 389 386 lock (locker) { 390 387 if (experimentStopped) { 391 if (Optimizers.All(x => (x.ExecutionState == ExecutionState.Stopped) || (x.ExecutionState == ExecutionState.Prepared))) { 392 OnStopped(); 393 allOptimizerFinished.Set(); 394 } 388 if (Optimizers.All(x => (x.ExecutionState == ExecutionState.Stopped) || (x.ExecutionState == ExecutionState.Prepared))) OnStopped(); 395 389 } else { 396 390 if (experimentStarted && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) return; 397 else if (Optimizers.All(x => x.ExecutionState == ExecutionState.Stopped)) { 398 OnStopped(); 399 allOptimizerFinished.Set(); 400 } else if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)) && Optimizers.All(o => o.ExecutionState != ExecutionState.Started)) { 401 OnPaused(); 402 allOptimizerFinished.Set(); 403 } 391 else if (Optimizers.All(x => x.ExecutionState == ExecutionState.Stopped)) OnStopped(); 392 else if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)) && Optimizers.All(o => o.ExecutionState != ExecutionState.Started)) OnPaused(); 404 393 } 405 394 }
Note: See TracChangeset
for help on using the changeset viewer.