Ignore:
Timestamp:
11/23/15 21:18:26 (4 years ago)
Author:
jkarder
Message:

#2258: added StartAsync to IExecutable

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/CrossValidation.cs

    r13238 r13349  
    2525using System.Linq;
    2626using System.Threading;
     27using System.Threading.Tasks;
    2728using HeuristicLab.Collections;
    2829using HeuristicLab.Common;
     
    3940  [StorableClass]
    4041  public sealed class CrossValidation : ParameterizedNamedItem, IAlgorithm, IStorableContent {
     42    private readonly ManualResetEvent signaler = new ManualResetEvent(true);
     43
    4144    public CrossValidation()
    4245      : base() {
     
    265268      Prepare();
    266269    }
    267 
    268270    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);
    294312            }
    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);
    302313          }
    303         }
    304 
    305         //start prepared or paused cloned algorithms
    306         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 started
    313               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                }
    321332              }
    322333            }
    323334          }
    324         }
    325         OnStarted();
    326       }
     335          OnStarted();
     336        }
     337      }, cancellationToken);
    327338    }
    328339
     
    671682          IAlgorithm preparedAlgorithm = clonedAlgorithms.FirstOrDefault(alg => alg.ExecutionState == ExecutionState.Prepared ||
    672683                                                                                alg.ExecutionState == ExecutionState.Paused);
    673           if (preparedAlgorithm != null) preparedAlgorithm.Start();
     684          if (preparedAlgorithm != null) preparedAlgorithm.StartAsync();
    674685        }
    675686        if (ExecutionState != ExecutionState.Stopped) {
     
    714725      pausePending = false;
    715726      ExecutionState = ExecutionState.Paused;
     727      signaler.Set();
    716728      EventHandler handler = Paused;
    717729      if (handler != null) handler(this, EventArgs.Empty);
     
    726738      runs.Add(new Run(string.Format("{0} Run {1}", Name, runsCounter), this));
    727739      ExecutionState = ExecutionState.Stopped;
     740      signaler.Set();
    728741      EventHandler handler = Stopped;
    729742      if (handler != null) handler(this, EventArgs.Empty);
Note: See TracChangeset for help on using the changeset viewer.