Changeset 15287 for trunk/sources/HeuristicLab.Optimization
- Timestamp:
- 07/26/17 15:48:32 (7 years ago)
- Location:
- trunk/sources
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources
- Property svn:mergeinfo changed
/branches/Async (added) merged: 13329,13349,13354-13355,13401,15065,15190,15204,15212,15215-15216,15232,15280-15281,15284,15286
- Property svn:mergeinfo changed
-
trunk/sources/HeuristicLab.Optimization
- Property svn:mergeinfo changed
/branches/Async/HeuristicLab.Optimization (added) merged: 13349,13354,13401,15065,15215,15232,15280-15281,15284
- Property svn:mergeinfo changed
-
trunk/sources/HeuristicLab.Optimization/3.3/Algorithms/Algorithm.cs
r14433 r15287 24 24 using System.Drawing; 25 25 using System.Linq; 26 using System.Threading; 27 using System.Threading.Tasks; 26 28 using HeuristicLab.Collections; 27 29 using HeuristicLab.Common; … … 203 205 } 204 206 public virtual void Start() { 207 Start(CancellationToken.None); 208 } 209 public virtual void Start(CancellationToken cancellationToken) { 205 210 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) 206 211 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); 212 } 213 public virtual async Task StartAsync() { await StartAsync(CancellationToken.None); } 214 public virtual async Task StartAsync(CancellationToken cancellationToken) { 215 await AsyncHelper.DoAsync(Start, cancellationToken); 207 216 } 208 217 public virtual void Pause() { -
trunk/sources/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs
r14523 r15287 21 21 22 22 using System; 23 using System.Linq; 23 24 using System.Threading; 24 using System.Threading.Tasks;25 25 using HeuristicLab.Common; 26 26 using HeuristicLab.Core; … … 71 71 } 72 72 73 public override void Start( ) {74 base.Start( );75 CancellationTokenSource = new CancellationTokenSource();73 public override void Start(CancellationToken cancellationToken) { 74 base.Start(cancellationToken); 75 CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); 76 76 pausePending = false; 77 77 OnStarted(); 78 78 79 Task task = Task.Factory.StartNew(Run, CancellationTokenSource.Token, CancellationTokenSource.Token); 80 task.ContinueWith(t => { 81 try { 82 t.Wait(); 83 } 84 catch (AggregateException ex) { 85 try { 86 ex.Flatten().Handle(x => x is OperationCanceledException); 87 } 88 catch (AggregateException remaining) { 89 if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]); 90 else OnExceptionOccurred(remaining); 91 } 92 } 93 CancellationTokenSource.Dispose(); 94 CancellationTokenSource = null; 95 if (pausePending) OnPaused(); 96 else OnStopped(); 97 }); 79 try { 80 Run((object)cancellationTokenSource.Token); 81 } catch (OperationCanceledException) { 82 } catch (AggregateException ae) { 83 OnExceptionOccurred(ae.InnerExceptions.SingleOrDefault() ?? ae); 84 } catch (Exception e) { 85 OnExceptionOccurred(e); 86 } 87 88 CancellationTokenSource.Dispose(); 89 CancellationTokenSource = null; 90 if (pausePending) OnPaused(); 91 else OnStopped(); 98 92 } 99 93 -
trunk/sources/HeuristicLab.Optimization/3.3/Algorithms/EngineAlgorithm.cs
r14185 r15287 168 168 } 169 169 } 170 public override void Start( ) {171 base.Start( );172 if (engine != null) engine.Start( );170 public override void Start(System.Threading.CancellationToken cancellationToken) { 171 base.Start(cancellationToken); 172 if (engine != null) engine.Start(cancellationToken); 173 173 } 174 174 public override void Pause() { -
trunk/sources/HeuristicLab.Optimization/3.3/MetaOptimizers/BatchRun.cs
r14185 r15287 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(); -
trunk/sources/HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs
r14185 r15287 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; … … 181 182 foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started)) { 182 183 // 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) { } 184 try { optimizer.Prepare(clearRuns); } catch (InvalidOperationException) { } 185 185 } 186 186 } 187 187 public void Start() { 188 Start(CancellationToken.None); 189 } 190 public void Start(CancellationToken cancellationToken) { 188 191 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) 189 192 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); … … 192 195 experimentStarted = true; 193 196 experimentStopped = false; 194 IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused));195 if (optimizer!= null) {197 IOptimizer optimizer; 198 while ((optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) != null) { 196 199 // 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 } 200 try { optimizer.Start(cancellationToken); } catch (InvalidOperationException) { } 201 if (ExecutionState == ExecutionState.Paused || ExecutionState == ExecutionState.Stopped) break; 202 } 203 } 204 public async Task StartAsync() { await StartAsync(CancellationToken.None); } 205 public async Task StartAsync(CancellationToken cancellationToken) { 206 await AsyncHelper.DoAsync(Start, cancellationToken); 200 207 } 201 208 public void Pause() { … … 208 215 foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started)) { 209 216 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 210 try { optimizer.Pause(); } 211 catch (InvalidOperationException) { } 217 try { optimizer.Pause(); } catch (InvalidOperationException) { } 212 218 } 213 219 } … … 222 228 foreach (var optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused))) { 223 229 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 224 try { optimizer.Stop(); } 225 catch (InvalidOperationException) { } 230 try { optimizer.Stop(); } catch (InvalidOperationException) { } 226 231 } 227 232 } else { … … 357 362 try { 358 363 ExecutionTime = Optimizers.Aggregate(TimeSpan.Zero, (t, o) => t + o.ExecutionTime); 359 } 360 finally { 364 } finally { 361 365 Monitor.Exit(locker); 362 366 } … … 379 383 if (Optimizers.All(x => (x.ExecutionState == ExecutionState.Stopped) || (x.ExecutionState == ExecutionState.Prepared))) OnStopped(); 380 384 } 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(); 385 if (experimentStarted && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) return; 386 else if (Optimizers.All(x => x.ExecutionState == ExecutionState.Stopped)) OnStopped(); 384 387 else if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)) && Optimizers.All(o => o.ExecutionState != ExecutionState.Started)) OnPaused(); 385 388 } -
trunk/sources/HeuristicLab.Optimization/3.3/MetaOptimizers/TimeLimitRun.cs
r14185 r15287 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() { -
trunk/sources/HeuristicLab.Optimization/3.3/Problems/SingleObjectiveHeuristicOptimizationProblem.cs
- Property svn:mergeinfo changed
/branches/Async/HeuristicLab.Optimization/3.3/Problems/SingleObjectiveHeuristicOptimizationProblem.cs (added) merged: 15281
- Property svn:mergeinfo changed
Note: See TracChangeset
for help on using the changeset viewer.