Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.3/CrossValidation.cs @ 6721

Last change on this file since 6721 was 5809, checked in by mkommend, 14 years ago

#1418: Reintegrated branch into trunk.

File size: 25.8 KB
RevLine 
[4472]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4472]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
[4561]23using System.Collections.Generic;
[4536]24using System.Drawing;
25using System.Linq;
[4472]26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Optimization;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.DataAnalysis;
[5809]33using HeuristicLab.PluginInfrastructure;
[4472]34
35namespace HeuristicLab.Algorithms.DataAnalysis {
[5809]36  [NonDiscoverableType]
[4472]37  [Item("Cross Validation", "Cross Validation wrapper for data analysis algorithms.")]
38  [StorableClass]
[4617]39  public sealed class CrossValidation : ParameterizedNamedItem, IAlgorithm, IStorableContent {
[4472]40    public CrossValidation()
41      : base() {
42      name = ItemName;
43      description = ItemDescription;
44
45      executionState = ExecutionState.Stopped;
[4542]46      runs = new RunCollection();
[4561]47      runsCounter = 0;
[4472]48
[4536]49      algorithm = null;
[4472]50      clonedAlgorithms = new ItemCollection<IAlgorithm>();
[4561]51      results = new ResultCollection();
[4472]52
[4561]53      folds = new IntValue(2);
[4542]54      numberOfWorkers = new IntValue(1);
[4472]55      samplesStart = new IntValue(0);
56      samplesEnd = new IntValue(0);
[4561]57      storeAlgorithmInEachRun = false;
[4542]58
59      RegisterEvents();
[5125]60      if (Algorithm != null) RegisterAlgorithmEvents();
[4472]61    }
62
[4617]63    public string Filename { get; set; }
64
[4542]65    #region persistence and cloning
66    [StorableConstructor]
67    private CrossValidation(bool deserializing)
68      : base(deserializing) {
69    }
70    [StorableHook(HookType.AfterDeserialization)]
71    private void AfterDeserialization() {
72      RegisterEvents();
[5125]73      if (Algorithm != null) RegisterAlgorithmEvents();
[4542]74    }
75
[4722]76    private CrossValidation(CrossValidation original, Cloner cloner)
77      : base(original, cloner) {
78      executionState = original.executionState;
79      storeAlgorithmInEachRun = original.storeAlgorithmInEachRun;
80      runs = cloner.Clone(original.runs);
81      runsCounter = original.runsCounter;
82      algorithm = cloner.Clone(original.algorithm);
83      clonedAlgorithms = cloner.Clone(original.clonedAlgorithms);
[4735]84      results = cloner.Clone(original.results);
85
[4722]86      folds = cloner.Clone(original.folds);
87      numberOfWorkers = cloner.Clone(original.numberOfWorkers);
88      samplesStart = cloner.Clone(original.samplesStart);
89      samplesEnd = cloner.Clone(original.samplesEnd);
90      RegisterEvents();
[5125]91      if (Algorithm != null) RegisterAlgorithmEvents();
[4722]92    }
[4542]93    public override IDeepCloneable Clone(Cloner cloner) {
[4722]94      return new CrossValidation(this, cloner);
[4542]95    }
[4722]96
[4542]97    #endregion
98
[4472]99    #region properties
[4542]100    [Storable]
[4536]101    private IAlgorithm algorithm;
102    public IAlgorithm Algorithm {
103      get { return algorithm; }
[4472]104      set {
[4542]105        if (ExecutionState != ExecutionState.Prepared && ExecutionState != ExecutionState.Stopped)
106          throw new InvalidOperationException("Changing the algorithm is only allowed if the CrossValidation is stopped or prepared.");
[4536]107        if (algorithm != value) {
[4472]108          if (value != null && value.Problem != null && !(value.Problem is IDataAnalysisProblem))
109            throw new ArgumentException("Only algorithms with a DataAnalysisProblem could be used for the cross validation.");
[4536]110          if (algorithm != null) DeregisterAlgorithmEvents();
111          algorithm = value;
[4561]112          Parameters.Clear();
[4542]113
114          if (algorithm != null) {
[4563]115            algorithm.StoreAlgorithmInEachRun = false;
[4542]116            RegisterAlgorithmEvents();
117            algorithm.Prepare(true);
[4561]118            Parameters.AddRange(algorithm.Parameters);
[4542]119          }
[4536]120          OnAlgorithmChanged();
[4561]121          if (algorithm != null) OnProblemChanged();
[4542]122          Prepare();
[4472]123        }
124      }
125    }
[4542]126
[4561]127
[4542]128    [Storable]
[4563]129    private ISingleObjectiveDataAnalysisProblem problem;
130    public ISingleObjectiveDataAnalysisProblem Problem {
[4472]131      get {
[4536]132        if (algorithm == null)
[4472]133          return null;
[4563]134        return (ISingleObjectiveDataAnalysisProblem)algorithm.Problem;
[4472]135      }
[4536]136      set {
[4542]137        if (ExecutionState != ExecutionState.Prepared && ExecutionState != ExecutionState.Stopped)
138          throw new InvalidOperationException("Changing the problem is only allowed if the CrossValidation is stopped or prepared.");
[4536]139        if (algorithm == null) throw new ArgumentNullException("Could not set a problem before an algorithm was set.");
140        algorithm.Problem = value;
[4563]141        problem = value;
[4536]142      }
[4472]143    }
[4536]144
[4561]145    IProblem IAlgorithm.Problem {
146      get { return Problem; }
147      set {
148        if (value != null && !ProblemType.IsInstanceOfType(value))
149          throw new ArgumentException("Only DataAnalysisProblems could be used for the cross validation.");
[4563]150        Problem = (ISingleObjectiveDataAnalysisProblem)value;
[4561]151      }
152    }
153    public Type ProblemType {
[4563]154      get { return typeof(ISingleObjectiveDataAnalysisProblem); }
[4561]155    }
156
[4542]157    [Storable]
[4472]158    private ItemCollection<IAlgorithm> clonedAlgorithms;
159
[5419]160    public IEnumerable<IOptimizer> NestedOptimizers {
[5430]161      get {
162        if (Algorithm == null) yield break;
163        yield return Algorithm;
164      }
[5419]165    }
166
[4542]167    [Storable]
[4561]168    private ResultCollection results;
169    public ResultCollection Results {
170      get { return results; }
171    }
172
173    [Storable]
[4472]174    private IntValue folds;
175    public IntValue Folds {
176      get { return folds; }
177    }
[4542]178    [Storable]
[4472]179    private IntValue samplesStart;
180    public IntValue SamplesStart {
[4536]181      get { return samplesStart; }
[4472]182    }
[4542]183    [Storable]
[4472]184    private IntValue samplesEnd;
185    public IntValue SamplesEnd {
[4536]186      get { return samplesEnd; }
[4472]187    }
[4542]188    [Storable]
[4472]189    private IntValue numberOfWorkers;
190    public IntValue NumberOfWorkers {
191      get { return numberOfWorkers; }
192    }
193
[4542]194    [Storable]
[4561]195    private bool storeAlgorithmInEachRun;
196    public bool StoreAlgorithmInEachRun {
197      get { return storeAlgorithmInEachRun; }
198      set {
199        if (storeAlgorithmInEachRun != value) {
200          storeAlgorithmInEachRun = value;
201          OnStoreAlgorithmInEachRunChanged();
202        }
203      }
204    }
205
206    [Storable]
207    private int runsCounter;
208    [Storable]
[4472]209    private RunCollection runs;
210    public RunCollection Runs {
211      get { return runs; }
212    }
[4561]213
[4542]214    [Storable]
[4472]215    private ExecutionState executionState;
216    public ExecutionState ExecutionState {
217      get { return executionState; }
218      private set {
219        if (executionState != value) {
220          executionState = value;
221          OnExecutionStateChanged();
222          OnItemImageChanged();
223        }
224      }
225    }
[4536]226    public override Image ItemImage {
227      get {
[5287]228        if (ExecutionState == ExecutionState.Prepared) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePrepared;
229        else if (ExecutionState == ExecutionState.Started) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStarted;
230        else if (ExecutionState == ExecutionState.Paused) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePaused;
231        else if (ExecutionState == ExecutionState.Stopped) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStopped;
232        else return HeuristicLab.Common.Resources.VSImageLibrary.Event;
[4536]233      }
234    }
235
[4472]236    public TimeSpan ExecutionTime {
[4536]237      get {
[4567]238        if (ExecutionState != ExecutionState.Prepared)
239          return TimeSpan.FromMilliseconds(clonedAlgorithms.Select(x => x.ExecutionTime.TotalMilliseconds).Sum());
240        return TimeSpan.Zero;
[4536]241      }
[4472]242    }
243    #endregion
244
245    public void Prepare() {
[4563]246      if (ExecutionState == ExecutionState.Started)
[4542]247        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
[4567]248      results.Clear();
[4536]249      clonedAlgorithms.Clear();
[4542]250      if (Algorithm != null) {
251        Algorithm.Prepare();
252        if (Algorithm.ExecutionState == ExecutionState.Prepared) OnPrepared();
253      }
[4472]254    }
255    public void Prepare(bool clearRuns) {
256      if (clearRuns) runs.Clear();
257      Prepare();
258    }
[4542]259
260    private bool startPending;
[4472]261    public void Start() {
[4542]262      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
263        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
264
265      if (Algorithm != null && !startPending) {
266        startPending = true;
267        //create cloned algorithms
268        if (clonedAlgorithms.Count == 0) {
[4561]269          int testSamplesCount = (SamplesEnd.Value - SamplesStart.Value) / Folds.Value;
[4542]270          for (int i = 0; i < Folds.Value; i++) {
271            IAlgorithm clonedAlgorithm = (IAlgorithm)algorithm.Clone();
272            clonedAlgorithm.Name = algorithm.Name + " Fold " + i;
[4561]273            IDataAnalysisProblem problem = clonedAlgorithm.Problem as IDataAnalysisProblem;
274            problem.DataAnalysisProblemData.TestSamplesEnd.Value = (i + 1) == Folds.Value ? SamplesEnd.Value : (i + 1) * testSamplesCount + SamplesStart.Value;
275            problem.DataAnalysisProblemData.TestSamplesStart.Value = (i * testSamplesCount) + SamplesStart.Value;
[4542]276            clonedAlgorithms.Add(clonedAlgorithm);
277          }
278        }
279
280        //start prepared or paused cloned algorithms
281        int startedAlgorithms = 0;
282        foreach (IAlgorithm clonedAlgorithm in clonedAlgorithms) {
283          if (startedAlgorithms < NumberOfWorkers.Value) {
284            if (clonedAlgorithm.ExecutionState == ExecutionState.Prepared ||
285                clonedAlgorithm.ExecutionState == ExecutionState.Paused) {
286              clonedAlgorithm.Start();
287              startedAlgorithms++;
288            }
289          }
290        }
291        OnStarted();
[4536]292      }
[4472]293    }
[4542]294
295    private bool pausePending;
[4472]296    public void Pause() {
[4542]297      if (ExecutionState != ExecutionState.Started)
298        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
299      if (!pausePending) {
300        pausePending = true;
301        if (!startPending) PauseAllClonedAlgorithms();
302      }
[4472]303    }
[4542]304    private void PauseAllClonedAlgorithms() {
[5420]305      foreach (IAlgorithm clonedAlgorithm in clonedAlgorithms) {
[4542]306        if (clonedAlgorithm.ExecutionState == ExecutionState.Started)
307          clonedAlgorithm.Pause();
308      }
309    }
310
311    private bool stopPending;
[4472]312    public void Stop() {
[4542]313      if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
314        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".",
315                                                          ExecutionState));
316      if (!stopPending) {
317        stopPending = true;
318        if (!startPending) StopAllClonedAlgorithms();
319      }
[4472]320    }
[4542]321    private void StopAllClonedAlgorithms() {
[5420]322      foreach (IAlgorithm clonedAlgorithm in clonedAlgorithms) {
[4542]323        if (clonedAlgorithm.ExecutionState == ExecutionState.Started ||
324            clonedAlgorithm.ExecutionState == ExecutionState.Paused)
325          clonedAlgorithm.Stop();
326      }
327    }
[4472]328
[4561]329    #region collect parameters and results
330    public override void CollectParameterValues(IDictionary<string, IItem> values) {
331      values.Add("Algorithm Name", new StringValue(Name));
332      values.Add("Algorithm Type", new StringValue(GetType().GetPrettyName()));
333      values.Add("Folds", new IntValue(Folds.Value));
334
335      if (algorithm != null) {
336        values.Add("CrossValidation Algorithm Name", new StringValue(Algorithm.Name));
337        values.Add("CrossValidation Algorithm Type", new StringValue(Algorithm.GetType().GetPrettyName()));
338        base.CollectParameterValues(values);
339      }
340      if (Problem != null) {
341        values.Add("Problem Name", new StringValue(Problem.Name));
342        values.Add("Problem Type", new StringValue(Problem.GetType().GetPrettyName()));
343        Problem.CollectParameterValues(values);
344      }
345    }
346
347    public void CollectResultValues(IDictionary<string, IItem> results) {
348      Dictionary<string, List<double>> resultValues = new Dictionary<string, List<double>>();
[5420]349      IEnumerable<IRun> runs = clonedAlgorithms.Select(alg => alg.Runs.FirstOrDefault()).Where(run => run != null);
[4561]350      IEnumerable<KeyValuePair<string, IItem>> resultCollections = runs.Where(x => x != null).SelectMany(x => x.Results).ToList();
351
352      foreach (IResult result in ExtractAndAggregateResults<IntValue>(resultCollections))
353        results.Add(result.Name, result.Value);
354      foreach (IResult result in ExtractAndAggregateResults<DoubleValue>(resultCollections))
355        results.Add(result.Name, result.Value);
356      foreach (IResult result in ExtractAndAggregateResults<PercentValue>(resultCollections))
357        results.Add(result.Name, result.Value);
358
[4567]359      results.Add("Execution Time", new TimeSpanValue(this.ExecutionTime));
[4561]360      results.Add("CrossValidation Folds", new RunCollection(runs));
361    }
362
363    private static IEnumerable<IResult> ExtractAndAggregateResults<T>(IEnumerable<KeyValuePair<string, IItem>> results)
364  where T : class, IItem, new() {
365      Dictionary<string, List<double>> resultValues = new Dictionary<string, List<double>>();
366      foreach (var resultValue in results.Where(r => r.Value.GetType() == typeof(T))) {
367        if (!resultValues.ContainsKey(resultValue.Key))
368          resultValues[resultValue.Key] = new List<double>();
369        resultValues[resultValue.Key].Add(ConvertToDouble(resultValue.Value));
370      }
371
372      DoubleValue doubleValue;
373      if (typeof(T) == typeof(PercentValue))
374        doubleValue = new PercentValue();
375      else if (typeof(T) == typeof(DoubleValue))
376        doubleValue = new DoubleValue();
377      else if (typeof(T) == typeof(IntValue))
378        doubleValue = new DoubleValue();
379      else
380        throw new NotSupportedException();
381
382      List<IResult> aggregatedResults = new List<IResult>();
383      foreach (KeyValuePair<string, List<double>> resultValue in resultValues) {
384        doubleValue.Value = resultValue.Value.Average();
385        aggregatedResults.Add(new Result(resultValue.Key, (IItem)doubleValue.Clone()));
386        doubleValue.Value = resultValue.Value.StandardDeviation();
387        aggregatedResults.Add(new Result(resultValue.Key + " StdDev", (IItem)doubleValue.Clone()));
388      }
389      return aggregatedResults;
390    }
391
392    private static double ConvertToDouble(IItem item) {
393      if (item is DoubleValue) return ((DoubleValue)item).Value;
394      else if (item is IntValue) return ((IntValue)item).Value;
395      else throw new NotSupportedException("Could not convert any item type to double");
396    }
397    #endregion
398
[4472]399    #region events
[4542]400    private void RegisterEvents() {
401      Folds.ValueChanged += new EventHandler(Folds_ValueChanged);
[4561]402      SamplesStart.ValueChanged += new EventHandler(SamplesStart_ValueChanged);
403      SamplesEnd.ValueChanged += new EventHandler(SamplesEnd_ValueChanged);
[4542]404      RegisterClonedAlgorithmsEvents();
405    }
406    private void Folds_ValueChanged(object sender, EventArgs e) {
407      if (ExecutionState != ExecutionState.Prepared)
408        throw new InvalidOperationException("Can not change number of folds if the execution state is not prepared.");
409    }
[4561]410    private void SamplesStart_ValueChanged(object sender, EventArgs e) {
411      if (Problem != null) Problem.DataAnalysisProblemData.TrainingSamplesStart.Value = SamplesStart.Value;
[4542]412    }
[4561]413    private void SamplesEnd_ValueChanged(object sender, EventArgs e) {
414      if (Problem != null) Problem.DataAnalysisProblemData.TrainingSamplesEnd.Value = SamplesEnd.Value;
415    }
[4542]416
417    #region template algorithms events
[4536]418    public event EventHandler AlgorithmChanged;
419    private void OnAlgorithmChanged() {
420      EventHandler handler = AlgorithmChanged;
[4472]421      if (handler != null) handler(this, EventArgs.Empty);
[4536]422      OnProblemChanged();
[4542]423      if (Problem == null) ExecutionState = ExecutionState.Stopped;
[4472]424    }
[4536]425    private void RegisterAlgorithmEvents() {
426      algorithm.ProblemChanged += new EventHandler(Algorithm_ProblemChanged);
[4542]427      algorithm.ExecutionStateChanged += new EventHandler(Algorithm_ExecutionStateChanged);
[4472]428    }
[4536]429    private void DeregisterAlgorithmEvents() {
430      algorithm.ProblemChanged -= new EventHandler(Algorithm_ProblemChanged);
[4542]431      algorithm.ExecutionStateChanged -= new EventHandler(Algorithm_ExecutionStateChanged);
[4472]432    }
[4536]433    private void Algorithm_ProblemChanged(object sender, EventArgs e) {
[4563]434      if (algorithm.Problem != null && !(algorithm.Problem is ISingleObjectiveDataAnalysisProblem)) {
435        algorithm.Problem = problem;
[4536]436        throw new ArgumentException("A cross validation algorithm can only contain DataAnalysisProblems.");
437      }
[4563]438      problem = (ISingleObjectiveDataAnalysisProblem)algorithm.Problem;
[4536]439      OnProblemChanged();
[4472]440    }
[4536]441    public event EventHandler ProblemChanged;
442    private void OnProblemChanged() {
443      EventHandler handler = ProblemChanged;
444      if (handler != null) handler(this, EventArgs.Empty);
[4561]445
446      SamplesStart.Value = 0;
447      if (Problem != null)
448        SamplesEnd.Value = Problem.DataAnalysisProblemData.Dataset.Rows;
449      else
450        SamplesEnd.Value = 0;
[4536]451    }
[4472]452
[4542]453    private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) {
454      switch (Algorithm.ExecutionState) {
455        case ExecutionState.Prepared: OnPrepared();
456          break;
457        case ExecutionState.Started: throw new InvalidOperationException("Algorithm template can not be started.");
458        case ExecutionState.Paused: throw new InvalidOperationException("Algorithm template can not be paused.");
459        case ExecutionState.Stopped: OnStopped();
460          break;
461      }
462    }
463    #endregion
464
465    #region clonedAlgorithms events
466    private void RegisterClonedAlgorithmsEvents() {
[4472]467      clonedAlgorithms.ItemsAdded += new CollectionItemsChangedEventHandler<IAlgorithm>(ClonedAlgorithms_ItemsAdded);
468      clonedAlgorithms.ItemsRemoved += new CollectionItemsChangedEventHandler<IAlgorithm>(ClonedAlgorithms_ItemsRemoved);
469      clonedAlgorithms.CollectionReset += new CollectionItemsChangedEventHandler<IAlgorithm>(ClonedAlgorithms_CollectionReset);
[4542]470      foreach (IAlgorithm algorithm in clonedAlgorithms)
471        RegisterClonedAlgorithmEvents(algorithm);
[4472]472    }
[4561]473    private void DeregisterClonedAlgorithmsEvents() {
474      clonedAlgorithms.ItemsAdded -= new CollectionItemsChangedEventHandler<IAlgorithm>(ClonedAlgorithms_ItemsAdded);
475      clonedAlgorithms.ItemsRemoved -= new CollectionItemsChangedEventHandler<IAlgorithm>(ClonedAlgorithms_ItemsRemoved);
476      clonedAlgorithms.CollectionReset -= new CollectionItemsChangedEventHandler<IAlgorithm>(ClonedAlgorithms_CollectionReset);
477      foreach (IAlgorithm algorithm in clonedAlgorithms)
478        DeregisterClonedAlgorithmEvents(algorithm);
479    }
[4472]480    private void ClonedAlgorithms_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IAlgorithm> e) {
481      foreach (IAlgorithm algorithm in e.Items)
482        RegisterClonedAlgorithmEvents(algorithm);
483    }
484    private void ClonedAlgorithms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IAlgorithm> e) {
485      foreach (IAlgorithm algorithm in e.Items)
486        DeregisterClonedAlgorithmEvents(algorithm);
487    }
488    private void ClonedAlgorithms_CollectionReset(object sender, CollectionItemsChangedEventArgs<IAlgorithm> e) {
[4542]489      foreach (IAlgorithm algorithm in e.OldItems)
490        DeregisterClonedAlgorithmEvents(algorithm);
[4472]491      foreach (IAlgorithm algorithm in e.Items)
492        RegisterClonedAlgorithmEvents(algorithm);
493    }
494    private void RegisterClonedAlgorithmEvents(IAlgorithm algorithm) {
[4542]495      algorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(ClonedAlgorithm_ExceptionOccurred);
496      algorithm.ExecutionTimeChanged += new EventHandler(ClonedAlgorithm_ExecutionTimeChanged);
497      algorithm.Started += new EventHandler(ClonedAlgorithm_Started);
498      algorithm.Paused += new EventHandler(ClonedAlgorithm_Paused);
499      algorithm.Stopped += new EventHandler(ClonedAlgorithm_Stopped);
[4472]500    }
501    private void DeregisterClonedAlgorithmEvents(IAlgorithm algorithm) {
[4542]502      algorithm.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(ClonedAlgorithm_ExceptionOccurred);
503      algorithm.ExecutionTimeChanged -= new EventHandler(ClonedAlgorithm_ExecutionTimeChanged);
504      algorithm.Started -= new EventHandler(ClonedAlgorithm_Started);
505      algorithm.Paused -= new EventHandler(ClonedAlgorithm_Paused);
506      algorithm.Stopped -= new EventHandler(ClonedAlgorithm_Stopped);
[4472]507    }
[4542]508    private void ClonedAlgorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
[4536]509      OnExceptionOccurred(e.Value);
510    }
[4542]511    private void ClonedAlgorithm_ExecutionTimeChanged(object sender, EventArgs e) {
[4536]512      OnExecutionTimeChanged();
513    }
[4542]514
515    private readonly object locker = new object();
516    private void ClonedAlgorithm_Started(object sender, EventArgs e) {
517      lock (locker) {
[4567]518        IAlgorithm algorithm = sender as IAlgorithm;
519        if (algorithm != null && !results.ContainsKey(algorithm.Name))
520          results.Add(new Result(algorithm.Name, "Contains results for the specific fold.", algorithm.Results));
521
[4542]522        if (startPending) {
523          int startedAlgorithms = clonedAlgorithms.Count(alg => alg.ExecutionState == ExecutionState.Started);
524          if (startedAlgorithms == NumberOfWorkers.Value ||
525             clonedAlgorithms.All(alg => alg.ExecutionState != ExecutionState.Prepared))
526            startPending = false;
527
528          if (pausePending) PauseAllClonedAlgorithms();
529          if (stopPending) StopAllClonedAlgorithms();
530        }
531      }
[4472]532    }
[4542]533
534    private void ClonedAlgorithm_Paused(object sender, EventArgs e) {
535      lock (locker) {
536        if (pausePending && clonedAlgorithms.All(alg => alg.ExecutionState != ExecutionState.Started))
537          OnPaused();
538      }
[4472]539    }
[4542]540
541    private void ClonedAlgorithm_Stopped(object sender, EventArgs e) {
542      lock (locker) {
543        if (!stopPending && ExecutionState == ExecutionState.Started) {
544          IAlgorithm preparedAlgorithm = clonedAlgorithms.Where(alg => alg.ExecutionState == ExecutionState.Prepared ||
545                                                                       alg.ExecutionState == ExecutionState.Paused).FirstOrDefault();
546          if (preparedAlgorithm != null) preparedAlgorithm.Start();
547        }
[4561]548        if (ExecutionState != ExecutionState.Stopped) {
549          if (clonedAlgorithms.All(alg => alg.ExecutionState == ExecutionState.Stopped))
550            OnStopped();
551          else if (stopPending &&
552                   clonedAlgorithms.All(
553                     alg => alg.ExecutionState == ExecutionState.Prepared || alg.ExecutionState == ExecutionState.Stopped))
554            OnStopped();
555        }
[4542]556      }
[4472]557    }
[4542]558    #endregion
559    #endregion
560
561    #region event firing
[4472]562    public event EventHandler ExecutionStateChanged;
563    private void OnExecutionStateChanged() {
564      EventHandler handler = ExecutionStateChanged;
565      if (handler != null) handler(this, EventArgs.Empty);
566    }
567    public event EventHandler ExecutionTimeChanged;
568    private void OnExecutionTimeChanged() {
569      EventHandler handler = ExecutionTimeChanged;
570      if (handler != null) handler(this, EventArgs.Empty);
571    }
572    public event EventHandler Prepared;
573    private void OnPrepared() {
574      ExecutionState = ExecutionState.Prepared;
575      EventHandler handler = Prepared;
576      if (handler != null) handler(this, EventArgs.Empty);
[4567]577      OnExecutionTimeChanged();
[4472]578    }
579    public event EventHandler Started;
580    private void OnStarted() {
[4542]581      startPending = false;
[4472]582      ExecutionState = ExecutionState.Started;
583      EventHandler handler = Started;
584      if (handler != null) handler(this, EventArgs.Empty);
585    }
586    public event EventHandler Paused;
587    private void OnPaused() {
[4542]588      pausePending = false;
[4472]589      ExecutionState = ExecutionState.Paused;
590      EventHandler handler = Paused;
591      if (handler != null) handler(this, EventArgs.Empty);
592    }
593    public event EventHandler Stopped;
594    private void OnStopped() {
[4542]595      stopPending = false;
[4567]596      Dictionary<string, IItem> collectedResults = new Dictionary<string, IItem>();
597      CollectResultValues(collectedResults);
598      results.AddRange(collectedResults.Select(x => new Result(x.Key, x.Value)).Cast<IResult>().ToArray());
[4561]599      runsCounter++;
600      runs.Add(new Run(string.Format("{0} Run {1}", Name, runsCounter), this));
[4472]601      ExecutionState = ExecutionState.Stopped;
602      EventHandler handler = Stopped;
603      if (handler != null) handler(this, EventArgs.Empty);
604    }
605    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
606    private void OnExceptionOccurred(Exception exception) {
607      EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
608      if (handler != null) handler(this, new EventArgs<Exception>(exception));
609    }
[4561]610    public event EventHandler StoreAlgorithmInEachRunChanged;
611    private void OnStoreAlgorithmInEachRunChanged() {
612      EventHandler handler = StoreAlgorithmInEachRunChanged;
613      if (handler != null) handler(this, EventArgs.Empty);
614    }
[4472]615    #endregion
616  }
617}
Note: See TracBrowser for help on using the repository browser.