Changeset 13349 for branches/Async/HeuristicLab.Algorithms.DataAnalysis
- Timestamp:
- 11/23/15 21:18:26 (8 years ago)
- Location:
- branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/CrossValidation.cs
r13238 r13349 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; … … 39 40 [StorableClass] 40 41 public sealed class CrossValidation : ParameterizedNamedItem, IAlgorithm, IStorableContent { 42 private readonly ManualResetEvent signaler = new ManualResetEvent(true); 43 41 44 public CrossValidation() 42 45 : base() { … … 265 268 Prepare(); 266 269 } 267 268 270 public void Start() { 269 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) 270 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); 271 272 if (Algorithm != null) { 273 //create cloned algorithms 274 if (clonedAlgorithms.Count == 0) { 275 int testSamplesCount = (SamplesEnd.Value - SamplesStart.Value) / Folds.Value; 276 277 for (int i = 0; i < Folds.Value; i++) { 278 IAlgorithm clonedAlgorithm = (IAlgorithm)algorithm.Clone(); 279 clonedAlgorithm.Name = algorithm.Name + " Fold " + i; 280 IDataAnalysisProblem problem = clonedAlgorithm.Problem as IDataAnalysisProblem; 281 ISymbolicDataAnalysisProblem symbolicProblem = problem as ISymbolicDataAnalysisProblem; 282 283 int testStart = (i * testSamplesCount) + SamplesStart.Value; 284 int testEnd = (i + 1) == Folds.Value ? SamplesEnd.Value : (i + 1) * testSamplesCount + SamplesStart.Value; 285 286 problem.ProblemData.TrainingPartition.Start = SamplesStart.Value; 287 problem.ProblemData.TrainingPartition.End = SamplesEnd.Value; 288 problem.ProblemData.TestPartition.Start = testStart; 289 problem.ProblemData.TestPartition.End = testEnd; 290 DataAnalysisProblemData problemData = problem.ProblemData as DataAnalysisProblemData; 291 if (problemData != null) { 292 problemData.TrainingPartitionParameter.Hidden = false; 293 problemData.TestPartitionParameter.Hidden = false; 271 StartAsync().Wait(); 272 } 273 public async Task StartAsync() { 274 await StartAsync(new CancellationToken()); 275 } 276 public async Task StartAsync(CancellationToken cancellationToken) { 277 signaler.Reset(); 278 await Task.Run(() => { 279 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) 280 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); 281 282 if (Algorithm != null) { 283 //create cloned algorithms 284 if (clonedAlgorithms.Count == 0) { 285 int testSamplesCount = (SamplesEnd.Value - SamplesStart.Value) / Folds.Value; 286 287 for (int i = 0; i < Folds.Value; i++) { 288 IAlgorithm clonedAlgorithm = (IAlgorithm)algorithm.Clone(); 289 clonedAlgorithm.Name = algorithm.Name + " Fold " + i; 290 IDataAnalysisProblem problem = clonedAlgorithm.Problem as IDataAnalysisProblem; 291 ISymbolicDataAnalysisProblem symbolicProblem = problem as ISymbolicDataAnalysisProblem; 292 293 int testStart = (i * testSamplesCount) + SamplesStart.Value; 294 int testEnd = (i + 1) == Folds.Value ? SamplesEnd.Value : (i + 1) * testSamplesCount + SamplesStart.Value; 295 296 problem.ProblemData.TrainingPartition.Start = SamplesStart.Value; 297 problem.ProblemData.TrainingPartition.End = SamplesEnd.Value; 298 problem.ProblemData.TestPartition.Start = testStart; 299 problem.ProblemData.TestPartition.End = testEnd; 300 DataAnalysisProblemData problemData = problem.ProblemData as DataAnalysisProblemData; 301 if (problemData != null) { 302 problemData.TrainingPartitionParameter.Hidden = false; 303 problemData.TestPartitionParameter.Hidden = false; 304 } 305 306 if (symbolicProblem != null) { 307 symbolicProblem.FitnessCalculationPartition.Start = SamplesStart.Value; 308 symbolicProblem.FitnessCalculationPartition.End = SamplesEnd.Value; 309 } 310 clonedAlgorithm.Prepare(); 311 clonedAlgorithms.Add(clonedAlgorithm); 294 312 } 295 296 if (symbolicProblem != null) {297 symbolicProblem.FitnessCalculationPartition.Start = SamplesStart.Value;298 symbolicProblem.FitnessCalculationPartition.End = SamplesEnd.Value;299 }300 clonedAlgorithm.Prepare();301 clonedAlgorithms.Add(clonedAlgorithm);302 313 } 303 } 304 305 //start prepared or paused cloned algorithms306 int startedAlgorithms = 0;307 foreach (IAlgorithm clonedAlgorithm in clonedAlgorithms) {308 if (startedAlgorithms < NumberOfWorkers.Value) {309 if (clonedAlgorithm.ExecutionState == ExecutionState.Prepared ||310 clonedAlgorithm.ExecutionState == ExecutionState.Paused) { 311 312 // start and wait until the alg is started313 using (var signal = new ManualResetEvent(false)) {314 EventHandler signalSetter = (sender, args) => { signal.Set(); };315 clonedAlgorithm.Started += signalSetter;316 clonedAlgorithm.Start();317 signal.WaitOne();318 clonedAlgorithm.Started -= signalSetter; 319 320 startedAlgorithms++;314 315 //start prepared or paused cloned algorithms 316 int startedAlgorithms = 0; 317 foreach (IAlgorithm clonedAlgorithm in clonedAlgorithms) { 318 if (startedAlgorithms < NumberOfWorkers.Value) { 319 if (clonedAlgorithm.ExecutionState == ExecutionState.Prepared || 320 clonedAlgorithm.ExecutionState == ExecutionState.Paused) { 321 322 // start and wait until the alg is started 323 using (var signal = new ManualResetEvent(false)) { 324 EventHandler signalSetter = (sender, args) => { signal.Set(); }; 325 clonedAlgorithm.Started += signalSetter; 326 clonedAlgorithm.StartAsync(cancellationToken); 327 signal.WaitOne(); 328 clonedAlgorithm.Started -= signalSetter; 329 330 startedAlgorithms++; 331 } 321 332 } 322 333 } 323 334 } 324 }325 OnStarted();326 } 335 OnStarted(); 336 } 337 }, cancellationToken); 327 338 } 328 339 … … 671 682 IAlgorithm preparedAlgorithm = clonedAlgorithms.FirstOrDefault(alg => alg.ExecutionState == ExecutionState.Prepared || 672 683 alg.ExecutionState == ExecutionState.Paused); 673 if (preparedAlgorithm != null) preparedAlgorithm.Start ();684 if (preparedAlgorithm != null) preparedAlgorithm.StartAsync(); 674 685 } 675 686 if (ExecutionState != ExecutionState.Stopped) { … … 714 725 pausePending = false; 715 726 ExecutionState = ExecutionState.Paused; 727 signaler.Set(); 716 728 EventHandler handler = Paused; 717 729 if (handler != null) handler(this, EventArgs.Empty); … … 726 738 runs.Add(new Run(string.Format("{0} Run {1}", Name, runsCounter), this)); 727 739 ExecutionState = ExecutionState.Stopped; 740 signaler.Set(); 728 741 EventHandler handler = Stopped; 729 742 if (handler != null) handler(this, EventArgs.Empty); -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/FixedDataAnalysisAlgorithm.cs
r12012 r13349 70 70 } 71 71 72 public override void Start() {73 base.Start();72 public override async Task StartAsync(CancellationToken cancellationToken) { 73 await base.StartAsync(cancellationToken); 74 74 var cancellationTokenSource = new CancellationTokenSource(); 75 75 76 76 OnStarted(); 77 Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token); 78 task.ContinueWith(t => { 79 try { 80 t.Wait(); 81 } 82 catch (AggregateException ex) { 77 using (var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationTokenSource.Token, cancellationToken)) { 78 Task task = Task.Factory.StartNew(Run, cts.Token, cts.Token); 79 await task.ContinueWith(t => { 83 80 try { 84 ex.Flatten().Handle(x => x is OperationCanceledException);81 t.Wait(); 85 82 } 86 catch (AggregateException remaining) { 87 if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]); 88 else OnExceptionOccurred(remaining); 83 catch (AggregateException ex) { 84 try { 85 ex.Flatten().Handle(x => x is OperationCanceledException); 86 } 87 catch (AggregateException remaining) { 88 if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]); 89 else OnExceptionOccurred(remaining); 90 } 89 91 } 90 }91 cancellationTokenSource.Dispose();92 cancellationTokenSource = null;93 OnStopped();94 } );92 cancellationTokenSource.Dispose(); 93 cancellationTokenSource = null; 94 OnStopped(); 95 }); 96 } 95 97 } 96 98 private void Run(object state) {
Note: See TracChangeset
for help on using the changeset viewer.