Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Algorithms.DataAnalysis/3.4/CrossValidation.cs @ 12017

Last change on this file since 12017 was 8660, checked in by gkronber, 12 years ago

#1847 merged r8205:8635 from trunk into branch

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