- Timestamp:
- 08/17/17 15:47:54 (7 years ago)
- Location:
- branches/PerformanceComparison/HeuristicLab.Optimization
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PerformanceComparison/HeuristicLab.Optimization
- Property svn:mergeinfo changed
/branches/Async/HeuristicLab.Optimization (added) merged: 13349,13354,13401,15065,15215,15232,15280-15281,15284 /trunk/sources/HeuristicLab.Optimization merged: 15287,15302,15327
- Property svn:mergeinfo changed
-
branches/PerformanceComparison/HeuristicLab.Optimization/3.3/MetaOptimizers/BatchRun.cs
r14600 r15330 23 23 using System.Collections.Generic; 24 24 using System.Drawing; 25 using System.Threading; 26 using System.Threading.Tasks; 25 27 using HeuristicLab.Collections; 26 28 using HeuristicLab.Common; … … 237 239 batchRunAction = BatchRunAction.Prepare; 238 240 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 239 try { Optimizer.Prepare(clearRuns); } 240 catch (InvalidOperationException) { } 241 try { Optimizer.Prepare(clearRuns); } catch (InvalidOperationException) { } 241 242 } else { 242 243 ExecutionState = ExecutionState.Stopped; … … 244 245 } 245 246 public void Start() { 247 Start(CancellationToken.None); 248 } 249 public void Start(CancellationToken cancellationToken) { 246 250 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) 247 251 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); … … 249 253 batchRunAction = BatchRunAction.Start; 250 254 if (Optimizer.ExecutionState == ExecutionState.Stopped) Optimizer.Prepare(); 251 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 252 try { Optimizer.Start(); } 253 catch (InvalidOperationException) { } 255 for (int i = repetitionsCounter; i < repetitions; i++) { 256 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 257 try { Optimizer.Start(cancellationToken); } catch (InvalidOperationException) { } 258 if (ExecutionState == ExecutionState.Paused || ExecutionState == ExecutionState.Stopped) break; 259 Optimizer.Prepare(); 260 } 261 } 262 public async Task StartAsync() { await StartAsync(CancellationToken.None); } 263 public async Task StartAsync(CancellationToken cancellationToken) { 264 await AsyncHelper.DoAsync(Start, cancellationToken); 254 265 } 255 266 public void Pause() { … … 260 271 if (Optimizer.ExecutionState != ExecutionState.Started) return; 261 272 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 262 try { Optimizer.Pause(); } 263 catch (InvalidOperationException) { } 273 try { Optimizer.Pause(); } catch (InvalidOperationException) { } 264 274 } 265 275 public void Stop() { … … 273 283 } 274 284 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 275 try { Optimizer.Stop(); } 276 catch (InvalidOperationException) { } 285 try { Optimizer.Stop(); } catch (InvalidOperationException) { } 277 286 } 278 287 … … 395 404 else if (repetitionsCounter >= repetitions) OnStopped(); 396 405 else if (batchRunAction == BatchRunAction.Pause) OnPaused(); 397 else if (batchRunAction == BatchRunAction.Start) { 398 Optimizer.Prepare(); 399 Optimizer.Start(); 400 } else if (executionState == ExecutionState.Started) { 406 else if (batchRunAction == BatchRunAction.Start) return; 407 else if (executionState == ExecutionState.Started) { 401 408 // if the batch run hasn't been started but the inner optimizer was run then pause 402 409 OnPaused(); -
branches/PerformanceComparison/HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs
r14600 r15330 25 25 using System.Linq; 26 26 using System.Threading; 27 using System.Threading.Tasks; 27 28 using HeuristicLab.Collections; 28 29 using HeuristicLab.Common; … … 110 111 private bool experimentStarted = false; 111 112 private bool experimentStopped = false; 113 private ManualResetEventSlim allOptimizerFinished; // this indicates that all started optimizers have been paused or stopped 112 114 113 115 public Experiment() … … 181 183 foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started)) { 182 184 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 183 try { optimizer.Prepare(clearRuns); } 184 catch (InvalidOperationException) { } 185 try { optimizer.Prepare(clearRuns); } catch (InvalidOperationException) { } 185 186 } 186 187 } 187 188 public void Start() { 189 Start(CancellationToken.None); 190 } 191 public void Start(CancellationToken cancellationToken) { 188 192 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) 189 193 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); … … 192 196 experimentStarted = true; 193 197 experimentStopped = false; 194 IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)); 195 if (optimizer != null) { 198 allOptimizerFinished = new ManualResetEventSlim(false); 199 200 IOptimizer optimizer; 201 while ((optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) != null) { 196 202 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 197 try { optimizer.Start(); } 198 catch (InvalidOperationException) { } 199 } 203 try { optimizer.Start(cancellationToken); } catch (InvalidOperationException) { } 204 if (ExecutionState == ExecutionState.Paused || ExecutionState == ExecutionState.Stopped) break; 205 } 206 207 allOptimizerFinished.Wait(); 208 } 209 public async Task StartAsync() { await StartAsync(CancellationToken.None); } 210 public async Task StartAsync(CancellationToken cancellationToken) { 211 await AsyncHelper.DoAsync(Start, cancellationToken); 200 212 } 201 213 public void Pause() { … … 208 220 foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started)) { 209 221 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 210 try { optimizer.Pause(); } 211 catch (InvalidOperationException) { } 222 try { optimizer.Pause(); } catch (InvalidOperationException) { } 212 223 } 213 224 } … … 222 233 foreach (var optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused))) { 223 234 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 224 try { optimizer.Stop(); } 225 catch (InvalidOperationException) { } 235 try { optimizer.Stop(); } catch (InvalidOperationException) { } 226 236 } 227 237 } else { … … 357 367 try { 358 368 ExecutionTime = Optimizers.Aggregate(TimeSpan.Zero, (t, o) => t + o.ExecutionTime); 359 } 360 finally { 369 } finally { 361 370 Monitor.Exit(locker); 362 371 } … … 364 373 private void optimizer_Paused(object sender, EventArgs e) { 365 374 lock (locker) 366 if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started)) OnPaused(); 375 if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started)) { 376 OnPaused(); 377 allOptimizerFinished.Set(); 378 } 367 379 } 368 380 private void optimizer_Prepared(object sender, EventArgs e) { … … 377 389 lock (locker) { 378 390 if (experimentStopped) { 379 if (Optimizers.All(x => (x.ExecutionState == ExecutionState.Stopped) || (x.ExecutionState == ExecutionState.Prepared))) OnStopped(); 391 if (Optimizers.All(x => (x.ExecutionState == ExecutionState.Stopped) || (x.ExecutionState == ExecutionState.Prepared))) { 392 OnStopped(); 393 allOptimizerFinished.Set(); 394 } 380 395 } else { 381 if (experimentStarted && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) { 382 Optimizers.First(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)).Start(); 383 } else if (Optimizers.All(x => x.ExecutionState == ExecutionState.Stopped)) OnStopped(); 384 else if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)) && Optimizers.All(o => o.ExecutionState != ExecutionState.Started)) OnPaused(); 396 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 } 385 404 } 386 405 } -
branches/PerformanceComparison/HeuristicLab.Optimization/3.3/MetaOptimizers/TimeLimitRun.cs
r14600 r15330 25 25 using System.Drawing; 26 26 using System.Linq; 27 using System.Threading; 27 28 using System.Threading.Tasks; 28 29 using HeuristicLab.Collections; … … 238 239 } 239 240 public void Start() { 240 Algorithm.Start(); 241 Start(CancellationToken.None); 242 } 243 public void Start(CancellationToken cancellationToken) { 244 Algorithm.Start(cancellationToken); 245 } 246 public async Task StartAsync() { await StartAsync(CancellationToken.None); } 247 public async Task StartAsync(CancellationToken cancellationToken) { 248 await AsyncHelper.DoAsync(Start, cancellationToken); 241 249 } 242 250 public void Pause() {
Note: See TracChangeset
for help on using the changeset viewer.