Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis SolutionEnsembles/HeuristicLab.Algorithms.DataAnalysis/3.4/CrossValidation.cs @ 5816

Last change on this file since 5816 was 5816, checked in by gkronber, 13 years ago

#1450 Added preliminary implementation for solution ensemble support.

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