Changeset 15973 for branches/2522_RefactorPluginInfrastructure/HeuristicLab.Optimization/3.3/Algorithms
- Timestamp:
- 06/28/18 11:13:37 (6 years ago)
- Location:
- branches/2522_RefactorPluginInfrastructure
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2522_RefactorPluginInfrastructure
- Property svn:ignore
-
old new 24 24 protoc.exe 25 25 obj 26 .vs
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/2522_RefactorPluginInfrastructure/HeuristicLab.Optimization
- Property svn:mergeinfo changed
-
branches/2522_RefactorPluginInfrastructure/HeuristicLab.Optimization/3.3/Algorithms/Algorithm.cs
r12012 r15973 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. … … 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() { … … 289 298 public event EventHandler Stopped; 290 299 protected virtual void OnStopped() { 291 foreach (IStatefulItem statefulObject in this.GetObjectGraphObjects(new HashSet<object>() { Runs }).OfType<IStatefulItem>()) { 292 statefulObject.ClearState(); 293 } 294 runsCounter++; 295 runs.Add(new Run(string.Format("{0} Run {1}", Name, runsCounter), this)); 296 ExecutionState = ExecutionState.Stopped; 297 EventHandler handler = Stopped; 298 if (handler != null) handler(this, EventArgs.Empty); 300 try { 301 foreach ( 302 IStatefulItem statefulObject in 303 this.GetObjectGraphObjects(new HashSet<object>() {Runs}).OfType<IStatefulItem>()) { 304 statefulObject.ClearState(); 305 } 306 runsCounter++; 307 try { 308 runs.Add(new Run(string.Format("{0} Run {1}", Name, runsCounter), this)); 309 } 310 catch (ArgumentException e) { 311 OnExceptionOccurred(new InvalidOperationException("Run creation failed.", e)); 312 } 313 } 314 finally { 315 ExecutionState = ExecutionState.Stopped; 316 EventHandler handler = Stopped; 317 if (handler != null) handler(this, EventArgs.Empty); 318 } 299 319 } 300 320 public event EventHandler<EventArgs<Exception>> ExceptionOccurred; -
branches/2522_RefactorPluginInfrastructure/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs
r11878 r15973 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. … … 22 22 using System; 23 23 using System.Threading; 24 using System.Threading.Tasks;25 24 using HeuristicLab.Common; 25 using HeuristicLab.Core; 26 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 27 … … 29 29 [StorableClass] 30 30 public abstract class BasicAlgorithm : Algorithm, IStorableContent { 31 32 private bool pausePending; 33 private DateTime lastUpdateTime; 34 31 35 public string Filename { get; set; } 32 36 37 public abstract bool SupportsPause { get; } 38 33 39 [Storable] 34 private ResultCollection results; 40 private bool initialized; 41 [Storable] 42 private readonly ResultCollection results; 35 43 public override ResultCollection Results { 36 44 get { return results; } … … 48 56 : base(original, cloner) { 49 57 results = cloner.Clone(original.Results); 58 initialized = original.initialized; 50 59 } 51 60 protected BasicAlgorithm() … … 58 67 base.Prepare(); 59 68 results.Clear(); 69 initialized = false; 60 70 OnPrepared(); 61 71 } 62 72 63 public override void Start() { 64 base.Start(); 65 CancellationTokenSource = new CancellationTokenSource(); 73 public override void Start(CancellationToken cancellationToken) { 74 base.Start(cancellationToken); 75 CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); 76 pausePending = false; 77 OnStarted(); 66 78 67 OnStarted(); 68 Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token); 69 task.ContinueWith(t => { 70 try { 71 t.Wait(); 72 } catch (AggregateException ex) { 73 try { 74 ex.Flatten().Handle(x => x is OperationCanceledException); 75 } catch (AggregateException remaining) { 76 if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]); 77 else OnExceptionOccurred(remaining); 78 } 79 } 80 CancellationTokenSource.Dispose(); 81 CancellationTokenSource = null; 82 OnStopped(); 83 }); 79 try { 80 Run((object)cancellationTokenSource.Token); 81 } catch (OperationCanceledException) { 82 } catch (AggregateException ae) { 83 ae.FlattenAndHandle(new[] { typeof(OperationCanceledException) }, e => OnExceptionOccurred(e)); 84 } catch (Exception e) { 85 OnExceptionOccurred(e); 86 } 87 88 CancellationTokenSource.Dispose(); 89 CancellationTokenSource = null; 90 if (pausePending) OnPaused(); 91 else OnStopped(); 84 92 } 85 93 86 94 public override void Pause() { 87 throw new NotSupportedException("Pause is not supported in basic algorithms."); 95 // CancellationToken.ThrowIfCancellationRequested() must be called from within the Run method, otherwise pause does nothing 96 // alternatively check the IsCancellationRequested property of the cancellation token 97 if (!SupportsPause) 98 throw new NotSupportedException("Pause is not supported by this algorithm."); 99 100 base.Pause(); 101 pausePending = true; 102 if (CancellationTokenSource != null) CancellationTokenSource.Cancel(); 88 103 } 89 104 … … 92 107 // alternatively check the IsCancellationRequested property of the cancellation token 93 108 base.Stop(); 94 CancellationTokenSource.Cancel(); 109 if (ExecutionState == ExecutionState.Paused) OnStopped(); 110 else if (CancellationTokenSource != null) CancellationTokenSource.Cancel(); 95 111 } 96 112 97 98 private DateTime lastUpdateTime;99 113 private void Run(object state) { 100 114 CancellationToken cancellationToken = (CancellationToken)state; … … 105 119 timer.Start(); 106 120 try { 121 if (!initialized) 122 Initialize(cancellationToken); 123 initialized = true; 107 124 Run(cancellationToken); 108 125 } finally { … … 113 130 } 114 131 132 protected virtual void Initialize(CancellationToken cancellationToken) { } 115 133 protected abstract void Run(CancellationToken cancellationToken); 116 134 -
branches/2522_RefactorPluginInfrastructure/HeuristicLab.Optimization/3.3/Algorithms/EngineAlgorithm.cs
r12012 r15973 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. … … 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() { -
branches/2522_RefactorPluginInfrastructure/HeuristicLab.Optimization/3.3/Algorithms/HeuristicOptimizationAlgorithm.cs
r12012 r15973 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. -
branches/2522_RefactorPluginInfrastructure/HeuristicLab.Optimization/3.3/Algorithms/HeuristicOptimizationEngineAlgorithm.cs
r12012 r15973 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. -
branches/2522_RefactorPluginInfrastructure/HeuristicLab.Optimization/3.3/Algorithms/UserDefinedAlgorithm.cs
r12504 r15973 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.
Note: See TracChangeset
for help on using the changeset viewer.