- Timestamp:
- 03/18/19 17:24:30 (6 years ago)
- Location:
- branches/2521_ProblemRefactoring
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring
- Property svn:ignore
-
old new 24 24 protoc.exe 25 25 obj 26 .vs
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/2521_ProblemRefactoring/HeuristicLab.Optimization
- Property svn:mergeinfo changed
-
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/MetaOptimizers/BatchRun.cs
r12504 r16692 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 5Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 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 if (ExecutionState == ExecutionState.Paused || ExecutionState == ExecutionState.Stopped) break; 261 } 262 } 263 public async Task StartAsync() { await StartAsync(CancellationToken.None); } 264 public async Task StartAsync(CancellationToken cancellationToken) { 265 await AsyncHelper.DoAsync(Start, cancellationToken); 254 266 } 255 267 public void Pause() { … … 260 272 if (Optimizer.ExecutionState != ExecutionState.Started) return; 261 273 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 262 try { Optimizer.Pause(); } 263 catch (InvalidOperationException) { } 274 try { Optimizer.Pause(); } catch (InvalidOperationException) { } catch (NotSupportedException) { } 264 275 } 265 276 public void Stop() { … … 273 284 } 274 285 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 275 try { Optimizer.Stop(); } 276 catch (InvalidOperationException) { } 286 try { Optimizer.Stop(); } catch (InvalidOperationException) { } 277 287 } 278 288 … … 395 405 else if (repetitionsCounter >= repetitions) OnStopped(); 396 406 else if (batchRunAction == BatchRunAction.Pause) OnPaused(); 397 else if (batchRunAction == BatchRunAction.Start) { 398 Optimizer.Prepare(); 399 Optimizer.Start(); 400 } else if (executionState == ExecutionState.Started) { 407 else if (batchRunAction == BatchRunAction.Start) return; 408 else if (executionState == ExecutionState.Started) { 401 409 // if the batch run hasn't been started but the inner optimizer was run then pause 402 410 OnPaused(); -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs
r13000 r16692 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 5Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 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; … … 96 97 } 97 98 99 [Storable] 100 private int numberOfWorkers = 1; 101 public int NumberOfWorkers { 102 get { return numberOfWorkers; } 103 set { 104 if (value < 1) throw new ArgumentException("Number of Workers must not be lower than one."); 105 numberOfWorkers = value; 106 } 107 } 108 98 109 public IEnumerable<IOptimizer> NestedOptimizers { 99 110 get { … … 110 121 private bool experimentStarted = false; 111 122 private bool experimentStopped = false; 123 124 // track already started optimizers (.StartAsync does not set the executionstate immediately) 125 // and to avoid restarting optimizers that were manually paused/stopped by the user 126 private readonly IDictionary<IOptimizer, Task> startedOptimizers = new Dictionary<IOptimizer, Task>(); 127 private IEnumerable<IOptimizer> StartableOptimizers { 128 get { 129 return Optimizers 130 .Where(x => x.ExecutionState == ExecutionState.Prepared || x.ExecutionState == ExecutionState.Paused) 131 .Where(o => !startedOptimizers.ContainsKey(o)); // all startable optimizers that were not startet yet 132 } 133 } 112 134 113 135 public Experiment() … … 153 175 experimentStarted = original.experimentStarted; 154 176 experimentStopped = original.experimentStopped; 177 numberOfWorkers = original.numberOfWorkers; 155 178 Initialize(); 156 179 } … … 181 204 foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started)) { 182 205 // 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) { } 206 try { optimizer.Prepare(clearRuns); } catch (InvalidOperationException) { } 185 207 } 186 208 } 187 209 public void Start() { 210 Start(CancellationToken.None); 211 } 212 public void Start(CancellationToken cancellationToken) { 188 213 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) 189 214 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); 190 if (Optimizers.Count == 0) return; 215 216 startedOptimizers.Clear(); 217 if (!StartableOptimizers.Any()) return; 191 218 192 219 experimentStarted = true; 193 220 experimentStopped = false; 194 IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)); 195 if (optimizer != null) { 196 // 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 } 221 222 using (var availableWorkers = new SemaphoreSlim(NumberOfWorkers, NumberOfWorkers)) { 223 while (StartableOptimizers.Any()) { 224 try { 225 availableWorkers.Wait(cancellationToken); 226 var optimizer = StartableOptimizers.FirstOrDefault(); 227 if (experimentStopped || !experimentStarted || optimizer == null) break; 228 229 var startedTask = optimizer.StartAsync(cancellationToken).ContinueWith(async t => { 230 availableWorkers.Release(); // is guaranteed to be not disposed yet because Task.WaitAll blocks before the end of the using 231 await t; // trigger a potential exception on the optimizerTask 232 }); 233 startedOptimizers.Add(optimizer, startedTask.Unwrap()); // unwrap task because lambda of .ContinueWith is async 234 } catch (InvalidOperationException) { } catch (OperationCanceledException) { } 235 } 236 237 Task.WaitAll(startedOptimizers.Values.ToArray()); // retrieve exeptions of the asyncrounously started optimizer 238 } 239 } 240 public async Task StartAsync() { await StartAsync(CancellationToken.None); } 241 public async Task StartAsync(CancellationToken cancellationToken) { 242 await AsyncHelper.DoAsync(Start, cancellationToken); 200 243 } 201 244 public void Pause() { … … 208 251 foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started)) { 209 252 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 210 try { optimizer.Pause(); } 211 catch (InvalidOperationException) { } 253 try { optimizer.Pause(); } catch (InvalidOperationException) { } catch (NotSupportedException) { } 212 254 } 213 255 } … … 222 264 foreach (var optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused))) { 223 265 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 224 try { optimizer.Stop(); } 225 catch (InvalidOperationException) { } 266 try { optimizer.Stop(); } catch (InvalidOperationException) { } 226 267 } 227 268 } else { … … 248 289 public event EventHandler Prepared; 249 290 private void OnPrepared() { 291 if (ExecutionState == ExecutionState.Prepared) return; 250 292 ExecutionState = ExecutionState.Prepared; 251 293 EventHandler handler = Prepared; … … 254 296 public event EventHandler Started; 255 297 private void OnStarted() { 298 if (ExecutionState == ExecutionState.Started) return; 256 299 ExecutionState = ExecutionState.Started; 257 300 EventHandler handler = Started; … … 260 303 public event EventHandler Paused; 261 304 private void OnPaused() { 305 if (ExecutionState == ExecutionState.Paused) return; 262 306 ExecutionState = ExecutionState.Paused; 263 307 EventHandler handler = Paused; … … 266 310 public event EventHandler Stopped; 267 311 private void OnStopped() { 312 if (ExecutionState == ExecutionState.Stopped) return; 268 313 ExecutionState = ExecutionState.Stopped; 269 314 EventHandler handler = Stopped; … … 357 402 try { 358 403 ExecutionTime = Optimizers.Aggregate(TimeSpan.Zero, (t, o) => t + o.ExecutionTime); 359 } 360 finally { 404 } finally { 361 405 Monitor.Exit(locker); 362 406 } 363 407 } 364 408 private void optimizer_Paused(object sender, EventArgs e) { 365 lock (locker) 366 if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started)) OnPaused(); 409 UpdateExecutionState(); 367 410 } 368 411 private void optimizer_Prepared(object sender, EventArgs e) { 369 lock (locker) 370 if (Optimizers.All(x => x.ExecutionState == ExecutionState.Prepared)) OnPrepared(); 412 UpdateExecutionState(); 371 413 } 372 414 private void optimizer_Started(object sender, EventArgs e) { … … 375 417 } 376 418 private void optimizer_Stopped(object sender, EventArgs e) { 419 UpdateExecutionState(); 420 } 421 private void UpdateExecutionState() { 422 // Execution states of the Experiment are determined using the following _basic_ rules: 423 // if any Optimizer is Started => Experiment is Started (2. if) 424 // if any Optimizer is Paused => Experiment is Paused (3. if) 425 // if any Optimizer is Prepared => Experiment is Prepared (5. if) 426 // else (all Optimizer are Stopped) => Experiment is Stopped (6. if) 427 // Additional there are two extra rules: 428 // if the Experiment is running and there are still optimizers that can be started => keep the Experiment Running (1. if) 429 // if experiment-stop is pending: Stop Experiment even if there are still Prepared Optimizer (4. if) 430 377 431 lock (locker) { 378 if (experimentStopped) { 379 if (Optimizers.All(x => (x.ExecutionState == ExecutionState.Stopped) || (x.ExecutionState == ExecutionState.Prepared))) OnStopped(); 380 } 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(); 385 } 386 } 387 } 432 // 1. experiment is running & further startable optimizers are available => continue executing 433 if (experimentStarted && StartableOptimizers.Any()) 434 return; 435 436 // 2. any optimizer is running => continue executing 437 if (Optimizers.Any(x => x.ExecutionState == ExecutionState.Started)) 438 return; 439 440 experimentStarted = false; 441 // 3. any optimizer is paused => experiment paused 442 if (Optimizers.Any(x => x.ExecutionState == ExecutionState.Paused)) 443 OnPaused(); 444 445 // 4. stop pending & all optimizers either stopped or prepared => experiment stopped 446 else if (experimentStopped) 447 OnStopped(); 448 449 // 5. any optimizer prepared => experiment prepared 450 else if (Optimizers.Any(x => x.ExecutionState == ExecutionState.Prepared)) 451 OnPrepared(); 452 453 // 6. (else) all optimizers stopped 454 else 455 OnStopped(); 456 } 457 } 458 388 459 private void optimizer_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) { 389 460 lock (runsLocker) { -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/MetaOptimizers/TimeLimitRun.cs
r13321 r16692 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 5Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 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.