Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis/3.4/CrossValidation.cs @ 15318

Last change on this file since 15318 was 15292, checked in by jkarder, 7 years ago

#2258: merged r15287 into stable

File size: 34.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 System.Threading;
27using System.Threading.Tasks;
28using HeuristicLab.Collections;
29using HeuristicLab.Common;
30using HeuristicLab.Core;
31using HeuristicLab.Data;
32using HeuristicLab.Optimization;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.Problems.DataAnalysis;
35using HeuristicLab.Problems.DataAnalysis.Symbolic;
36using HeuristicLab.Random;
37
38namespace HeuristicLab.Algorithms.DataAnalysis {
39  [Item("Cross Validation (CV)", "Cross-validation wrapper for data analysis algorithms.")]
40  [Creatable(CreatableAttribute.Categories.DataAnalysis, Priority = 100)]
41  [StorableClass]
42  public sealed class CrossValidation : ParameterizedNamedItem, IAlgorithm, IStorableContent {
43    [Storable]
44    private int seed;
45
46    private SemaphoreSlim availableWorkers; // limits the number of concurrent algorithm executions
47    private ManualResetEventSlim allAlgorithmsFinished; // this indicates that all started algorithms have been paused or stopped
48
49    public CrossValidation()
50      : base() {
51      name = ItemName;
52      description = ItemDescription;
53
54      executionState = ExecutionState.Stopped;
55      runs = new RunCollection { OptimizerName = name };
56      runsCounter = 0;
57
58      algorithm = null;
59      clonedAlgorithms = new ItemCollection<IAlgorithm>();
60      results = new ResultCollection();
61
62      folds = new IntValue(2);
63      numberOfWorkers = new IntValue(1);
64      samplesStart = new IntValue(0);
65      samplesEnd = new IntValue(0);
66      shuffleSamples = new BoolValue(false);
67      storeAlgorithmInEachRun = false;
68
69      RegisterEvents();
70      if (Algorithm != null) RegisterAlgorithmEvents();
71    }
72
73    public string Filename { get; set; }
74
75    #region persistence and cloning
76    [StorableConstructor]
77    private CrossValidation(bool deserializing)
78      : base(deserializing) {
79    }
80    [StorableHook(HookType.AfterDeserialization)]
81    private void AfterDeserialization() {
82      // BackwardsCompatibility3.3
83      #region Backwards compatible code, remove with 3.4
84      if (shuffleSamples == null) shuffleSamples = new BoolValue(false);
85      #endregion
86
87      RegisterEvents();
88      if (Algorithm != null) RegisterAlgorithmEvents();
89    }
90
91    private CrossValidation(CrossValidation original, Cloner cloner)
92      : base(original, cloner) {
93      executionState = original.executionState;
94      storeAlgorithmInEachRun = original.storeAlgorithmInEachRun;
95      runs = cloner.Clone(original.runs);
96      runsCounter = original.runsCounter;
97      algorithm = cloner.Clone(original.algorithm);
98      clonedAlgorithms = cloner.Clone(original.clonedAlgorithms);
99      results = cloner.Clone(original.results);
100
101      folds = cloner.Clone(original.folds);
102      numberOfWorkers = cloner.Clone(original.numberOfWorkers);
103      samplesStart = cloner.Clone(original.samplesStart);
104      samplesEnd = cloner.Clone(original.samplesEnd);
105      shuffleSamples = cloner.Clone(original.shuffleSamples);
106      seed = original.seed;
107
108      RegisterEvents();
109      if (Algorithm != null) RegisterAlgorithmEvents();
110    }
111    public override IDeepCloneable Clone(Cloner cloner) {
112      return new CrossValidation(this, cloner);
113    }
114
115    #endregion
116
117    #region properties
118    [Storable]
119    private IAlgorithm algorithm;
120    public IAlgorithm Algorithm {
121      get { return algorithm; }
122      set {
123        if (ExecutionState != ExecutionState.Prepared && ExecutionState != ExecutionState.Stopped)
124          throw new InvalidOperationException("Changing the algorithm is only allowed if the CrossValidation is stopped or prepared.");
125        if (algorithm != value) {
126          if (value != null && value.Problem != null && !(value.Problem is IDataAnalysisProblem))
127            throw new ArgumentException("Only algorithms with a DataAnalysisProblem could be used for the cross validation.");
128          if (algorithm != null) DeregisterAlgorithmEvents();
129          algorithm = value;
130          Parameters.Clear();
131
132          if (algorithm != null) {
133            algorithm.StoreAlgorithmInEachRun = false;
134            RegisterAlgorithmEvents();
135            algorithm.Prepare(true);
136            Parameters.AddRange(algorithm.Parameters);
137          }
138          OnAlgorithmChanged();
139          Prepare();
140        }
141      }
142    }
143
144
145    [Storable]
146    private IDataAnalysisProblem problem;
147    public IDataAnalysisProblem Problem {
148      get {
149        if (algorithm == null)
150          return null;
151        return (IDataAnalysisProblem)algorithm.Problem;
152      }
153      set {
154        if (ExecutionState != ExecutionState.Prepared && ExecutionState != ExecutionState.Stopped)
155          throw new InvalidOperationException("Changing the problem is only allowed if the CrossValidation is stopped or prepared.");
156        if (algorithm == null) throw new ArgumentNullException("Could not set a problem before an algorithm was set.");
157        algorithm.Problem = value;
158        problem = value;
159      }
160    }
161
162    IProblem IAlgorithm.Problem {
163      get { return Problem; }
164      set {
165        if (value != null && !ProblemType.IsInstanceOfType(value))
166          throw new ArgumentException("Only DataAnalysisProblems could be used for the cross validation.");
167        Problem = (IDataAnalysisProblem)value;
168      }
169    }
170    public Type ProblemType {
171      get { return typeof(IDataAnalysisProblem); }
172    }
173
174    [Storable]
175    private ItemCollection<IAlgorithm> clonedAlgorithms;
176
177    public IEnumerable<IOptimizer> NestedOptimizers {
178      get {
179        if (Algorithm == null) yield break;
180        yield return Algorithm;
181      }
182    }
183
184    [Storable]
185    private ResultCollection results;
186    public ResultCollection Results {
187      get { return results; }
188    }
189    [Storable]
190    private BoolValue shuffleSamples;
191    public BoolValue ShuffleSamples {
192      get { return shuffleSamples; }
193    }
194    [Storable]
195    private IntValue folds;
196    public IntValue Folds {
197      get { return folds; }
198    }
199    [Storable]
200    private IntValue samplesStart;
201    public IntValue SamplesStart {
202      get { return samplesStart; }
203    }
204    [Storable]
205    private IntValue samplesEnd;
206    public IntValue SamplesEnd {
207      get { return samplesEnd; }
208    }
209    [Storable]
210    private IntValue numberOfWorkers;
211    public IntValue NumberOfWorkers {
212      get { return numberOfWorkers; }
213    }
214
215    [Storable]
216    private bool storeAlgorithmInEachRun;
217    public bool StoreAlgorithmInEachRun {
218      get { return storeAlgorithmInEachRun; }
219      set {
220        if (storeAlgorithmInEachRun != value) {
221          storeAlgorithmInEachRun = value;
222          OnStoreAlgorithmInEachRunChanged();
223        }
224      }
225    }
226
227    [Storable]
228    private int runsCounter;
229    [Storable]
230    private RunCollection runs;
231    public RunCollection Runs {
232      get { return runs; }
233    }
234
235    [Storable]
236    private ExecutionState executionState;
237    public ExecutionState ExecutionState {
238      get { return executionState; }
239      private set {
240        if (executionState != value) {
241          executionState = value;
242          OnExecutionStateChanged();
243          OnItemImageChanged();
244        }
245      }
246    }
247    public static new Image StaticItemImage {
248      get { return HeuristicLab.Common.Resources.VSImageLibrary.Event; }
249    }
250    public override Image ItemImage {
251      get {
252        if (ExecutionState == ExecutionState.Prepared) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePrepared;
253        else if (ExecutionState == ExecutionState.Started) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStarted;
254        else if (ExecutionState == ExecutionState.Paused) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePaused;
255        else if (ExecutionState == ExecutionState.Stopped) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStopped;
256        else return base.ItemImage;
257      }
258    }
259
260    public TimeSpan ExecutionTime {
261      get {
262        if (ExecutionState != ExecutionState.Prepared)
263          return TimeSpan.FromMilliseconds(clonedAlgorithms.Select(x => x.ExecutionTime.TotalMilliseconds).Sum());
264        return TimeSpan.Zero;
265      }
266    }
267    #endregion
268
269    protected override void OnNameChanged() {
270      base.OnNameChanged();
271      Runs.OptimizerName = Name;
272    }
273
274    public void Prepare() {
275      if (startPending) return;
276      if (ExecutionState == ExecutionState.Started)
277        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
278      results.Clear();
279      clonedAlgorithms.Clear();
280      if (Algorithm != null) {
281        Algorithm.Prepare();
282        if (Algorithm.ExecutionState == ExecutionState.Prepared) OnPrepared();
283      }
284    }
285    public void Prepare(bool clearRuns) {
286      if (clearRuns) runs.Clear();
287      Prepare();
288    }
289
290    private bool startPending;
291    public void Start() {
292      Start(CancellationToken.None);
293    }
294    public void Start(CancellationToken cancellationToken) {
295      lock (locker) {
296        if (startPending) return;
297        startPending = true;
298      }
299
300      try {
301        if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
302          throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
303        seed = new FastRandom().NextInt();
304
305        if (Algorithm == null) return;
306        //create cloned algorithms
307        if (clonedAlgorithms.Count == 0) {
308          int testSamplesCount = (SamplesEnd.Value - SamplesStart.Value) / Folds.Value;
309          IDataset shuffledDataset = null;
310          for (int i = 0; i < Folds.Value; i++) {
311            var cloner = new Cloner();
312            if (ShuffleSamples.Value) {
313              var random = new FastRandom(seed);
314              var dataAnalysisProblem = (IDataAnalysisProblem)algorithm.Problem;
315              var dataset = (Dataset)dataAnalysisProblem.ProblemData.Dataset;
316              shuffledDataset = shuffledDataset ?? dataset.Shuffle(random);
317              cloner.RegisterClonedObject(dataset, shuffledDataset);
318            }
319            IAlgorithm clonedAlgorithm = cloner.Clone(Algorithm);
320            clonedAlgorithm.Name = algorithm.Name + " Fold " + i;
321            IDataAnalysisProblem problem = clonedAlgorithm.Problem as IDataAnalysisProblem;
322            ISymbolicDataAnalysisProblem symbolicProblem = problem as ISymbolicDataAnalysisProblem;
323
324            int testStart = (i * testSamplesCount) + SamplesStart.Value;
325            int testEnd = (i + 1) == Folds.Value ? SamplesEnd.Value : (i + 1) * testSamplesCount + SamplesStart.Value;
326
327            problem.ProblemData.TrainingPartition.Start = SamplesStart.Value;
328            problem.ProblemData.TrainingPartition.End = SamplesEnd.Value;
329            problem.ProblemData.TestPartition.Start = testStart;
330            problem.ProblemData.TestPartition.End = testEnd;
331            DataAnalysisProblemData problemData = problem.ProblemData as DataAnalysisProblemData;
332            if (problemData != null) {
333              problemData.TrainingPartitionParameter.Hidden = false;
334              problemData.TestPartitionParameter.Hidden = false;
335            }
336
337            if (symbolicProblem != null) {
338              symbolicProblem.FitnessCalculationPartition.Start = SamplesStart.Value;
339              symbolicProblem.FitnessCalculationPartition.End = SamplesEnd.Value;
340            }
341            clonedAlgorithm.Prepare();
342            clonedAlgorithms.Add(clonedAlgorithm);
343          }
344        }
345
346        OnStarted();
347      } finally {
348        if (startPending) startPending = false;
349      }
350
351      availableWorkers = new SemaphoreSlim(NumberOfWorkers.Value, NumberOfWorkers.Value);
352      allAlgorithmsFinished = new ManualResetEventSlim(false);
353
354      //start prepared or paused cloned algorithms
355      foreach (IAlgorithm clonedAlgorithm in clonedAlgorithms) {
356        if (pausePending || stopPending || ExecutionState != ExecutionState.Started) break;
357        if (clonedAlgorithm.ExecutionState == ExecutionState.Prepared ||
358            clonedAlgorithm.ExecutionState == ExecutionState.Paused) {
359          availableWorkers.Wait();
360          lock (locker) {
361            if (pausePending || stopPending || ExecutionState != ExecutionState.Started) break;
362            clonedAlgorithm.StartAsync(cancellationToken);
363          }
364        }
365      }
366
367      allAlgorithmsFinished.Wait();
368    }
369
370    public async Task StartAsync() { await StartAsync(CancellationToken.None); }
371    public async Task StartAsync(CancellationToken cancellationToken) {
372      await AsyncHelper.DoAsync(Start, cancellationToken);
373    }
374
375    private bool pausePending;
376    public void Pause() {
377      if (startPending) return;
378      if (ExecutionState != ExecutionState.Started)
379        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
380      if (!pausePending) {
381        pausePending = true;
382        lock (locker) {
383          var toPause = clonedAlgorithms.Where(x => x.ExecutionState == ExecutionState.Started).ToList();
384          foreach (var optimizer in toPause) {
385            // a race-condition may occur when the optimizer has changed the state by itself in the meantime
386            try { optimizer.Pause(); } catch (InvalidOperationException) { }
387          }
388        }
389      }
390    }
391
392    private bool stopPending;
393    public void Stop() {
394      if (startPending) return;
395      if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
396        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".",
397                                                          ExecutionState));
398      if (!stopPending) {
399        stopPending = true;
400        lock (locker) {
401          var toStop = clonedAlgorithms.Where(x => x.ExecutionState == ExecutionState.Started || x.ExecutionState == ExecutionState.Paused).ToList();
402          foreach (var optimizer in toStop) {
403            // a race-condition may occur when the optimizer has changed the state by itself in the meantime
404            try { optimizer.Stop(); } catch (InvalidOperationException) { }
405          }
406        }
407      }
408    }
409
410    #region collect parameters and results
411    public override void CollectParameterValues(IDictionary<string, IItem> values) {
412      values.Add("Algorithm Name", new StringValue(Name));
413      values.Add("Algorithm Type", new StringValue(GetType().GetPrettyName()));
414      values.Add("Folds", new IntValue(Folds.Value));
415
416      if (algorithm != null) {
417        values.Add("CrossValidation Algorithm Name", new StringValue(Algorithm.Name));
418        values.Add("CrossValidation Algorithm Type", new StringValue(Algorithm.GetType().GetPrettyName()));
419        base.CollectParameterValues(values);
420      }
421      if (Problem != null) {
422        values.Add("Problem Name", new StringValue(Problem.Name));
423        values.Add("Problem Type", new StringValue(Problem.GetType().GetPrettyName()));
424        Problem.CollectParameterValues(values);
425      }
426    }
427
428    public void CollectResultValues(IDictionary<string, IItem> results) {
429      var clonedResults = (ResultCollection)this.results.Clone();
430      foreach (var result in clonedResults) {
431        results.Add(result.Name, result.Value);
432      }
433    }
434
435    private void AggregateResultValues(IDictionary<string, IItem> results) {
436      IEnumerable<IRun> runs = clonedAlgorithms.Select(alg => alg.Runs.FirstOrDefault()).Where(run => run != null);
437      IEnumerable<KeyValuePair<string, IItem>> resultCollections = runs.Where(x => x != null).SelectMany(x => x.Results).ToList();
438
439      foreach (IResult result in ExtractAndAggregateResults<IntValue>(resultCollections))
440        results.Add(result.Name, result.Value);
441      foreach (IResult result in ExtractAndAggregateResults<DoubleValue>(resultCollections))
442        results.Add(result.Name, result.Value);
443      foreach (IResult result in ExtractAndAggregateResults<PercentValue>(resultCollections))
444        results.Add(result.Name, result.Value);
445      foreach (IResult result in ExtractAndAggregateRegressionSolutions(resultCollections)) {
446        results.Add(result.Name, result.Value);
447      }
448      foreach (IResult result in ExtractAndAggregateClassificationSolutions(resultCollections)) {
449        results.Add(result.Name, result.Value);
450      }
451      results.Add("Execution Time", new TimeSpanValue(this.ExecutionTime));
452      results.Add("CrossValidation Folds", new RunCollection(runs));
453    }
454
455    private IEnumerable<IResult> ExtractAndAggregateRegressionSolutions(IEnumerable<KeyValuePair<string, IItem>> resultCollections) {
456      Dictionary<string, List<IRegressionSolution>> resultSolutions = new Dictionary<string, List<IRegressionSolution>>();
457      foreach (var result in resultCollections) {
458        var regressionSolution = result.Value as IRegressionSolution;
459        if (regressionSolution != null) {
460          if (resultSolutions.ContainsKey(result.Key)) {
461            resultSolutions[result.Key].Add(regressionSolution);
462          } else {
463            resultSolutions.Add(result.Key, new List<IRegressionSolution>() { regressionSolution });
464          }
465        }
466      }
467      List<IResult> aggregatedResults = new List<IResult>();
468      foreach (KeyValuePair<string, List<IRegressionSolution>> solutions in resultSolutions) {
469        // clone manually to correctly clone references between cloned root objects
470        Cloner cloner = new Cloner();
471        if (ShuffleSamples.Value) {
472          var dataset = (Dataset)Problem.ProblemData.Dataset;
473          var random = new FastRandom(seed);
474          var shuffledDataset = dataset.Shuffle(random);
475          cloner.RegisterClonedObject(dataset, shuffledDataset);
476        }
477        var problemDataClone = (IRegressionProblemData)cloner.Clone(Problem.ProblemData);
478        // set partitions of problem data clone correctly
479        problemDataClone.TrainingPartition.Start = SamplesStart.Value; problemDataClone.TrainingPartition.End = SamplesEnd.Value;
480        problemDataClone.TestPartition.Start = SamplesStart.Value; problemDataClone.TestPartition.End = SamplesEnd.Value;
481        // clone models
482        var ensembleSolution = new RegressionEnsembleSolution(problemDataClone);
483        ensembleSolution.AddRegressionSolutions(solutions.Value);
484
485        aggregatedResults.Add(new Result(solutions.Key + " (ensemble)", ensembleSolution));
486      }
487      List<IResult> flattenedResults = new List<IResult>();
488      CollectResultsRecursively("", aggregatedResults, flattenedResults);
489      return flattenedResults;
490    }
491
492    private IEnumerable<IResult> ExtractAndAggregateClassificationSolutions(IEnumerable<KeyValuePair<string, IItem>> resultCollections) {
493      Dictionary<string, List<IClassificationSolution>> resultSolutions = new Dictionary<string, List<IClassificationSolution>>();
494      foreach (var result in resultCollections) {
495        var classificationSolution = result.Value as IClassificationSolution;
496        if (classificationSolution != null) {
497          if (resultSolutions.ContainsKey(result.Key)) {
498            resultSolutions[result.Key].Add(classificationSolution);
499          } else {
500            resultSolutions.Add(result.Key, new List<IClassificationSolution>() { classificationSolution });
501          }
502        }
503      }
504      var aggregatedResults = new List<IResult>();
505      foreach (KeyValuePair<string, List<IClassificationSolution>> solutions in resultSolutions) {
506        // at least one algorithm (GBT with logistic regression loss) produces a classification solution even though the original problem is a regression problem.
507        var targetVariable = solutions.Value.First().ProblemData.TargetVariable;
508        var dataset = (Dataset)Problem.ProblemData.Dataset;
509        if (ShuffleSamples.Value) {
510          var random = new FastRandom(seed);
511          dataset = dataset.Shuffle(random);
512        }
513        var problemDataClone = new ClassificationProblemData(dataset, Problem.ProblemData.AllowedInputVariables, targetVariable);
514        // set partitions of problem data clone correctly
515        problemDataClone.TrainingPartition.Start = SamplesStart.Value; problemDataClone.TrainingPartition.End = SamplesEnd.Value;
516        problemDataClone.TestPartition.Start = SamplesStart.Value; problemDataClone.TestPartition.End = SamplesEnd.Value;
517        // clone models
518        var ensembleSolution = new ClassificationEnsembleSolution(problemDataClone);
519        ensembleSolution.AddClassificationSolutions(solutions.Value);
520
521        aggregatedResults.Add(new Result(solutions.Key + " (ensemble)", ensembleSolution));
522      }
523      List<IResult> flattenedResults = new List<IResult>();
524      CollectResultsRecursively("", aggregatedResults, flattenedResults);
525      return flattenedResults;
526    }
527
528    private void CollectResultsRecursively(string path, IEnumerable<IResult> results, IList<IResult> flattenedResults) {
529      foreach (IResult result in results) {
530        flattenedResults.Add(new Result(path + result.Name, result.Value));
531        ResultCollection childCollection = result.Value as ResultCollection;
532        if (childCollection != null) {
533          CollectResultsRecursively(path + result.Name + ".", childCollection, flattenedResults);
534        }
535      }
536    }
537
538    private static IEnumerable<IResult> ExtractAndAggregateResults<T>(IEnumerable<KeyValuePair<string, IItem>> results)
539  where T : class, IItem, new() {
540      Dictionary<string, List<double>> resultValues = new Dictionary<string, List<double>>();
541      foreach (var resultValue in results.Where(r => r.Value.GetType() == typeof(T))) {
542        if (!resultValues.ContainsKey(resultValue.Key))
543          resultValues[resultValue.Key] = new List<double>();
544        resultValues[resultValue.Key].Add(ConvertToDouble(resultValue.Value));
545      }
546
547      DoubleValue doubleValue;
548      if (typeof(T) == typeof(PercentValue))
549        doubleValue = new PercentValue();
550      else if (typeof(T) == typeof(DoubleValue))
551        doubleValue = new DoubleValue();
552      else if (typeof(T) == typeof(IntValue))
553        doubleValue = new DoubleValue();
554      else
555        throw new NotSupportedException();
556
557      List<IResult> aggregatedResults = new List<IResult>();
558      foreach (KeyValuePair<string, List<double>> resultValue in resultValues) {
559        doubleValue.Value = resultValue.Value.Average();
560        aggregatedResults.Add(new Result(resultValue.Key + " (average)", (IItem)doubleValue.Clone()));
561        doubleValue.Value = resultValue.Value.StandardDeviation();
562        aggregatedResults.Add(new Result(resultValue.Key + " (std.dev.)", (IItem)doubleValue.Clone()));
563      }
564      return aggregatedResults;
565    }
566
567    private static double ConvertToDouble(IItem item) {
568      if (item is DoubleValue) return ((DoubleValue)item).Value;
569      else if (item is IntValue) return ((IntValue)item).Value;
570      else throw new NotSupportedException("Could not convert any item type to double");
571    }
572    #endregion
573
574    #region events
575    private void RegisterEvents() {
576      Folds.ValueChanged += new EventHandler(Folds_ValueChanged);
577      RegisterClonedAlgorithmsEvents();
578    }
579    private void Folds_ValueChanged(object sender, EventArgs e) {
580      if (ExecutionState != ExecutionState.Prepared)
581        throw new InvalidOperationException("Can not change number of folds if the execution state is not prepared.");
582    }
583
584
585    #region template algorithms events
586    public event EventHandler AlgorithmChanged;
587    private void OnAlgorithmChanged() {
588      EventHandler handler = AlgorithmChanged;
589      if (handler != null) handler(this, EventArgs.Empty);
590      OnProblemChanged();
591      if (Problem == null) ExecutionState = ExecutionState.Stopped;
592    }
593    private void RegisterAlgorithmEvents() {
594      algorithm.ProblemChanged += new EventHandler(Algorithm_ProblemChanged);
595      algorithm.ExecutionStateChanged += new EventHandler(Algorithm_ExecutionStateChanged);
596      if (Problem != null) {
597        Problem.Reset += new EventHandler(Problem_Reset);
598      }
599    }
600    private void DeregisterAlgorithmEvents() {
601      algorithm.ProblemChanged -= new EventHandler(Algorithm_ProblemChanged);
602      algorithm.ExecutionStateChanged -= new EventHandler(Algorithm_ExecutionStateChanged);
603      if (Problem != null) {
604        Problem.Reset -= new EventHandler(Problem_Reset);
605      }
606    }
607    private void Algorithm_ProblemChanged(object sender, EventArgs e) {
608      if (algorithm.Problem != null && !(algorithm.Problem is IDataAnalysisProblem)) {
609        algorithm.Problem = problem;
610        throw new ArgumentException("A cross validation algorithm can only contain DataAnalysisProblems.");
611      }
612      if (problem != null) problem.Reset -= new EventHandler(Problem_Reset);
613      problem = (IDataAnalysisProblem)algorithm.Problem;
614      if (problem != null) problem.Reset += new EventHandler(Problem_Reset);
615      OnProblemChanged();
616    }
617    public event EventHandler ProblemChanged;
618    private void OnProblemChanged() {
619      EventHandler handler = ProblemChanged;
620      if (handler != null) handler(this, EventArgs.Empty);
621      ConfigureProblem();
622    }
623    private void Problem_Reset(object sender, EventArgs e) {
624      ConfigureProblem();
625    }
626    private void ConfigureProblem() {
627      SamplesStart.Value = 0;
628      if (Problem != null) {
629        SamplesEnd.Value = Problem.ProblemData.Dataset.Rows;
630
631        DataAnalysisProblemData problemData = Problem.ProblemData as DataAnalysisProblemData;
632        if (problemData != null) {
633          problemData.TrainingPartitionParameter.Hidden = true;
634          problemData.TestPartitionParameter.Hidden = true;
635        }
636        ISymbolicDataAnalysisProblem symbolicProblem = Problem as ISymbolicDataAnalysisProblem;
637        if (symbolicProblem != null) {
638          symbolicProblem.FitnessCalculationPartitionParameter.Hidden = true;
639          symbolicProblem.FitnessCalculationPartition.Start = SamplesStart.Value;
640          symbolicProblem.FitnessCalculationPartition.End = SamplesEnd.Value;
641          symbolicProblem.ValidationPartitionParameter.Hidden = true;
642          symbolicProblem.ValidationPartition.Start = 0;
643          symbolicProblem.ValidationPartition.End = 0;
644        }
645      } else
646        SamplesEnd.Value = 0;
647    }
648
649    private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) {
650      switch (Algorithm.ExecutionState) {
651        case ExecutionState.Prepared:
652          OnPrepared();
653          break;
654        case ExecutionState.Started: throw new InvalidOperationException("Algorithm template can not be started.");
655        case ExecutionState.Paused: throw new InvalidOperationException("Algorithm template can not be paused.");
656        case ExecutionState.Stopped:
657          OnStopped();
658          break;
659      }
660    }
661    #endregion
662
663    #region clonedAlgorithms events
664    private void RegisterClonedAlgorithmsEvents() {
665      clonedAlgorithms.ItemsAdded += new CollectionItemsChangedEventHandler<IAlgorithm>(ClonedAlgorithms_ItemsAdded);
666      clonedAlgorithms.ItemsRemoved += new CollectionItemsChangedEventHandler<IAlgorithm>(ClonedAlgorithms_ItemsRemoved);
667      clonedAlgorithms.CollectionReset += new CollectionItemsChangedEventHandler<IAlgorithm>(ClonedAlgorithms_CollectionReset);
668      foreach (IAlgorithm algorithm in clonedAlgorithms)
669        RegisterClonedAlgorithmEvents(algorithm);
670    }
671    private void DeregisterClonedAlgorithmsEvents() {
672      clonedAlgorithms.ItemsAdded -= new CollectionItemsChangedEventHandler<IAlgorithm>(ClonedAlgorithms_ItemsAdded);
673      clonedAlgorithms.ItemsRemoved -= new CollectionItemsChangedEventHandler<IAlgorithm>(ClonedAlgorithms_ItemsRemoved);
674      clonedAlgorithms.CollectionReset -= new CollectionItemsChangedEventHandler<IAlgorithm>(ClonedAlgorithms_CollectionReset);
675      foreach (IAlgorithm algorithm in clonedAlgorithms)
676        DeregisterClonedAlgorithmEvents(algorithm);
677    }
678    private void ClonedAlgorithms_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IAlgorithm> e) {
679      foreach (IAlgorithm algorithm in e.Items)
680        RegisterClonedAlgorithmEvents(algorithm);
681    }
682    private void ClonedAlgorithms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IAlgorithm> e) {
683      foreach (IAlgorithm algorithm in e.Items)
684        DeregisterClonedAlgorithmEvents(algorithm);
685    }
686    private void ClonedAlgorithms_CollectionReset(object sender, CollectionItemsChangedEventArgs<IAlgorithm> e) {
687      foreach (IAlgorithm algorithm in e.OldItems)
688        DeregisterClonedAlgorithmEvents(algorithm);
689      foreach (IAlgorithm algorithm in e.Items)
690        RegisterClonedAlgorithmEvents(algorithm);
691    }
692    private void RegisterClonedAlgorithmEvents(IAlgorithm algorithm) {
693      algorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(ClonedAlgorithm_ExceptionOccurred);
694      algorithm.ExecutionTimeChanged += new EventHandler(ClonedAlgorithm_ExecutionTimeChanged);
695      algorithm.Started += new EventHandler(ClonedAlgorithm_Started);
696      algorithm.Paused += new EventHandler(ClonedAlgorithm_Paused);
697      algorithm.Stopped += new EventHandler(ClonedAlgorithm_Stopped);
698    }
699    private void DeregisterClonedAlgorithmEvents(IAlgorithm algorithm) {
700      algorithm.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(ClonedAlgorithm_ExceptionOccurred);
701      algorithm.ExecutionTimeChanged -= new EventHandler(ClonedAlgorithm_ExecutionTimeChanged);
702      algorithm.Started -= new EventHandler(ClonedAlgorithm_Started);
703      algorithm.Paused -= new EventHandler(ClonedAlgorithm_Paused);
704      algorithm.Stopped -= new EventHandler(ClonedAlgorithm_Stopped);
705    }
706    private void ClonedAlgorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
707      Pause();
708      OnExceptionOccurred(e.Value);
709    }
710    private void ClonedAlgorithm_ExecutionTimeChanged(object sender, EventArgs e) {
711      OnExecutionTimeChanged();
712    }
713
714    private readonly object locker = new object();
715    private readonly object resultLocker = new object();
716    private void ClonedAlgorithm_Started(object sender, EventArgs e) {
717      IAlgorithm algorithm = sender as IAlgorithm;
718      lock (resultLocker) {
719        if (algorithm != null && !results.ContainsKey(algorithm.Name))
720          results.Add(new Result(algorithm.Name, "Contains results for the specific fold.", algorithm.Results));
721      }
722    }
723
724    private void ClonedAlgorithm_Paused(object sender, EventArgs e) {
725      lock (locker) {
726        availableWorkers.Release();
727        if (clonedAlgorithms.All(alg => alg.ExecutionState != ExecutionState.Started)) {
728          OnPaused();
729          allAlgorithmsFinished.Set();
730        }
731      }
732    }
733
734    private void ClonedAlgorithm_Stopped(object sender, EventArgs e) {
735      lock (locker) {
736        // if the algorithm was in paused state, its worker has already been released
737        if (availableWorkers.CurrentCount < NumberOfWorkers.Value)
738          availableWorkers.Release();
739        if (clonedAlgorithms.All(alg => alg.ExecutionState == ExecutionState.Stopped)) {
740          OnStopped();
741          allAlgorithmsFinished.Set();
742        } else if (stopPending && clonedAlgorithms.All(alg => alg.ExecutionState == ExecutionState.Prepared || alg.ExecutionState == ExecutionState.Stopped)) {
743          OnStopped();
744          allAlgorithmsFinished.Set();
745        }
746      }
747    }
748    #endregion
749    #endregion
750
751    #region event firing
752    public event EventHandler ExecutionStateChanged;
753    private void OnExecutionStateChanged() {
754      EventHandler handler = ExecutionStateChanged;
755      if (handler != null) handler(this, EventArgs.Empty);
756    }
757    public event EventHandler ExecutionTimeChanged;
758    private void OnExecutionTimeChanged() {
759      EventHandler handler = ExecutionTimeChanged;
760      if (handler != null) handler(this, EventArgs.Empty);
761    }
762    public event EventHandler Prepared;
763    private void OnPrepared() {
764      ExecutionState = ExecutionState.Prepared;
765      EventHandler handler = Prepared;
766      if (handler != null) handler(this, EventArgs.Empty);
767      OnExecutionTimeChanged();
768    }
769    public event EventHandler Started;
770    private void OnStarted() {
771      startPending = false;
772      ExecutionState = ExecutionState.Started;
773      EventHandler handler = Started;
774      if (handler != null) handler(this, EventArgs.Empty);
775    }
776    public event EventHandler Paused;
777    private void OnPaused() {
778      pausePending = false;
779      ExecutionState = ExecutionState.Paused;
780      EventHandler handler = Paused;
781      if (handler != null) handler(this, EventArgs.Empty);
782    }
783    public event EventHandler Stopped;
784    private void OnStopped() {
785      stopPending = false;
786      Dictionary<string, IItem> collectedResults = new Dictionary<string, IItem>();
787      AggregateResultValues(collectedResults);
788      results.AddRange(collectedResults.Select(x => new Result(x.Key, x.Value)).Cast<IResult>().ToArray());
789      clonedAlgorithms.Clear();
790      runsCounter++;
791      runs.Add(new Run(string.Format("{0} Run {1}", Name, runsCounter), this));
792      ExecutionState = ExecutionState.Stopped;
793      EventHandler handler = Stopped;
794      if (handler != null) handler(this, EventArgs.Empty);
795    }
796    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
797    private void OnExceptionOccurred(Exception exception) {
798      EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
799      if (handler != null) handler(this, new EventArgs<Exception>(exception));
800    }
801    public event EventHandler StoreAlgorithmInEachRunChanged;
802    private void OnStoreAlgorithmInEachRunChanged() {
803      EventHandler handler = StoreAlgorithmInEachRunChanged;
804      if (handler != null) handler(this, EventArgs.Empty);
805    }
806    #endregion
807  }
808}
Note: See TracBrowser for help on using the repository browser.