Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Classification/HeuristicLab.Algorithms.DataAnalysis/3.3/CrossValidation.cs @ 4472

Last change on this file since 4472 was 4472, checked in by mkommend, 13 years ago

Added empty plugin for cross validation view and stub for the cross validation class (ticket #1199).

File size: 11.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Collections;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.DataAnalysis;
30
31namespace HeuristicLab.Algorithms.DataAnalysis {
32  [Item("Cross Validation", "Cross Validation wrapper for data analysis algorithms.")]
33  [Creatable("Data Analysis")]
34  [StorableClass]
35  public sealed class CrossValidation : NamedItem, IOptimizer {
36    //TODO cloning, persistence, creation of clonedAlgs, execution logic, ...
37    public CrossValidation()
38      : base() {
39      name = ItemName;
40      description = ItemDescription;
41
42      executionState = ExecutionState.Stopped;
43      executionTime = TimeSpan.Zero;
44      Runs = new RunCollection();
45
46      algorithmTemplate = null;
47      clonedAlgorithms = new ItemCollection<IAlgorithm>();
48      RegisterClonedAlgorithmEvents();
49      readOnlyClonedAlgorithms = null;
50
51      folds = new IntValue(1);
52      samplesStart = new IntValue(0);
53      samplesEnd = new IntValue(0);
54      numberOfWorkers = new IntValue(1);
55    }
56
57    #region properties
58    private IAlgorithm algorithmTemplate;
59    public IAlgorithm AlgorithmTemplate {
60      get { return algorithmTemplate; }
61      set {
62        if (algorithmTemplate != value) {
63          if (value != null && value.Problem != null && !(value.Problem is IDataAnalysisProblem))
64            throw new ArgumentException("Only algorithms with a DataAnalysisProblem could be used for the cross validation.");
65          if (algorithmTemplate != null) DeregisterAlgorithmTemplateEvents();
66          algorithmTemplate = value;
67          if (algorithmTemplate != null) RegisterAlgorithmTemplateEvents();
68          OnAlgorithmTemplateChanged();
69        }
70      }
71    }
72    public IDataAnalysisProblem Problem {
73      get {
74        if (algorithmTemplate == null)
75          return null;
76        return (IDataAnalysisProblem)algorithmTemplate.Problem;
77      }
78    }
79    private ItemCollection<IAlgorithm> clonedAlgorithms;
80    private ReadOnlyItemCollection<IAlgorithm> readOnlyClonedAlgorithms;
81    public IItemCollection<IAlgorithm> ClonedAlgorithms {
82      get {
83        if (readOnlyClonedAlgorithms == null) readOnlyClonedAlgorithms = clonedAlgorithms.AsReadOnly();
84        return readOnlyClonedAlgorithms;
85      }
86    }
87
88    private IntValue folds;
89    public IntValue Folds {
90      get { return folds; }
91    }
92    private IntValue samplesStart;
93    public IntValue SamplesStart {
94      get { return SamplesStart; }
95    }
96    private IntValue samplesEnd;
97    public IntValue SamplesEnd {
98      get { return SamplesEnd; }
99    }
100    private IntValue numberOfWorkers;
101    public IntValue NumberOfWorkers {
102      get { return numberOfWorkers; }
103    }
104
105    private RunCollection runs;
106    public RunCollection Runs {
107      get { return runs; }
108      private set {
109        if (value == null) throw new ArgumentNullException();
110        if (runs != value) {
111          if (runs != null) DeregisterRunsEvents();
112          runs = value;
113          if (runs != null) RegisterRunsEvents();
114        }
115      }
116    }
117    private ExecutionState executionState;
118    public ExecutionState ExecutionState {
119      get { return executionState; }
120      private set {
121        if (executionState != value) {
122          executionState = value;
123          OnExecutionStateChanged();
124          OnItemImageChanged();
125        }
126      }
127    }
128    private TimeSpan executionTime;
129    public TimeSpan ExecutionTime {
130      get { return executionTime; }
131      private set {
132        executionTime = value;
133        OnExecutionTimeChanged();
134      }
135    }
136    #endregion
137
138    public void Prepare() {
139      OnPrepared();
140    }
141    public void Prepare(bool clearRuns) {
142      if (clearRuns) runs.Clear();
143      Prepare();
144    }
145    public void Start() {
146      OnStarted();
147    }
148    public void Pause() {
149      OnPaused();
150    }
151    public void Stop() {
152      OnStopped();
153    }
154
155    #region events
156    public event EventHandler AlgorithmTemplateChanged;
157    private void OnAlgorithmTemplateChanged() {
158      Prepare();
159      EventHandler handler = AlgorithmTemplateChanged;
160      if (handler != null) handler(this, EventArgs.Empty);
161    }
162    private void RegisterAlgorithmTemplateEvents() {
163      algorithmTemplate.ProblemChanged += new EventHandler(algorithmTemplate_ProblemChanged);
164    }
165    private void DeregisterAlgorithmTemplateEvents() {
166      algorithmTemplate.ProblemChanged -= new EventHandler(algorithmTemplate_ProblemChanged);
167    }
168    private void algorithmTemplate_ProblemChanged(object sender, EventArgs e) {
169      SamplesStart.Value = 0;
170      SamplesEnd.Value = Problem.DataAnalysisProblemData.Dataset.Rows;
171    }
172
173    private void RegisterClonedAlgorithmEvents() {
174      clonedAlgorithms.ItemsAdded += new CollectionItemsChangedEventHandler<IAlgorithm>(ClonedAlgorithms_ItemsAdded);
175      clonedAlgorithms.ItemsRemoved += new CollectionItemsChangedEventHandler<IAlgorithm>(ClonedAlgorithms_ItemsRemoved);
176      clonedAlgorithms.CollectionReset += new CollectionItemsChangedEventHandler<IAlgorithm>(ClonedAlgorithms_CollectionReset);
177    }
178    private void ClonedAlgorithms_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IAlgorithm> e) {
179      foreach (IAlgorithm algorithm in e.Items)
180        RegisterClonedAlgorithmEvents(algorithm);
181    }
182    private void ClonedAlgorithms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IAlgorithm> e) {
183      foreach (IAlgorithm algorithm in e.Items)
184        DeregisterClonedAlgorithmEvents(algorithm);
185    }
186    private void ClonedAlgorithms_CollectionReset(object sender, CollectionItemsChangedEventArgs<IAlgorithm> e) {
187      foreach (IAlgorithm algorithm in e.Items)
188        RegisterClonedAlgorithmEvents(algorithm);
189      foreach (IAlgorithm algorithm in e.OldItems)
190        DeregisterClonedAlgorithmEvents(algorithm);
191    }
192    private void RegisterClonedAlgorithmEvents(IAlgorithm algorithm) {
193      algorithm.Runs.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Algorithm_Runs_Added);
194      algorithm.Paused += new EventHandler(Algorithm_Paused);
195      algorithm.Stopped += new EventHandler(Algorithm_Stopped);
196    }
197    private void DeregisterClonedAlgorithmEvents(IAlgorithm algorithm) {
198      algorithm.Runs.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Algorithm_Runs_Added);
199      algorithm.Paused -= new EventHandler(Algorithm_Paused);
200      algorithm.Stopped -= new EventHandler(Algorithm_Stopped);
201    }
202    private void Algorithm_Runs_Added(object sender, CollectionItemsChangedEventArgs<IRun> e) {
203      throw new NotImplementedException("TODO added finished run to actual results if the cross validation is running");
204    }
205    private void Algorithm_Paused(object sender, EventArgs e) {
206      throw new NotImplementedException("TODO pause the cross validation if an algorithm no algorithm is running.");
207    }
208    private void Algorithm_Stopped(object sender, EventArgs e) {
209      throw new NotImplementedException("TODO stop the cross validation if all algorithms are stopped. and start remaining prepared algs");
210    }
211    public event EventHandler ExecutionStateChanged;
212    private void OnExecutionStateChanged() {
213      EventHandler handler = ExecutionStateChanged;
214      if (handler != null) handler(this, EventArgs.Empty);
215    }
216    public event EventHandler ExecutionTimeChanged;
217    private void OnExecutionTimeChanged() {
218      EventHandler handler = ExecutionTimeChanged;
219      if (handler != null) handler(this, EventArgs.Empty);
220    }
221    public event EventHandler Prepared;
222    private void OnPrepared() {
223      ExecutionState = ExecutionState.Prepared;
224      EventHandler handler = Prepared;
225      if (handler != null) handler(this, EventArgs.Empty);
226    }
227    public event EventHandler Started;
228    private void OnStarted() {
229      ExecutionState = ExecutionState.Started;
230      EventHandler handler = Started;
231      if (handler != null) handler(this, EventArgs.Empty);
232    }
233    public event EventHandler Paused;
234    private void OnPaused() {
235      ExecutionState = ExecutionState.Paused;
236      EventHandler handler = Paused;
237      if (handler != null) handler(this, EventArgs.Empty);
238    }
239    public event EventHandler Stopped;
240    private void OnStopped() {
241      ExecutionState = ExecutionState.Stopped;
242      EventHandler handler = Stopped;
243      if (handler != null) handler(this, EventArgs.Empty);
244    }
245    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
246    private void OnExceptionOccurred(Exception exception) {
247      EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
248      if (handler != null) handler(this, new EventArgs<Exception>(exception));
249    }
250
251    private void RegisterRunsEvents() {
252      runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
253      runs.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsAdded);
254      runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsRemoved);
255    }
256    private void DeregisterRunsEvents() {
257      runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
258      runs.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsAdded);
259      runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsRemoved);
260    }
261    private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
262      foreach (IRun run in e.OldItems) {
263        IItem item;
264        run.Results.TryGetValue("Execution Time", out item);
265        TimeSpanValue executionTime = item as TimeSpanValue;
266        if (executionTime != null) ExecutionTime -= executionTime.Value;
267      }
268      foreach (IRun run in e.Items) {
269        IItem item;
270        run.Results.TryGetValue("Execution Time", out item);
271        TimeSpanValue executionTime = item as TimeSpanValue;
272        if (executionTime != null) ExecutionTime += executionTime.Value;
273      }
274    }
275    private void Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
276      foreach (IRun run in e.Items) {
277        IItem item;
278        run.Results.TryGetValue("Execution Time", out item);
279        TimeSpanValue executionTime = item as TimeSpanValue;
280        if (executionTime != null) ExecutionTime += executionTime.Value;
281      }
282    }
283    private void Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
284      foreach (IRun run in e.Items) {
285        IItem item;
286        run.Results.TryGetValue("Execution Time", out item);
287        TimeSpanValue executionTime = item as TimeSpanValue;
288        if (executionTime != null) ExecutionTime -= executionTime.Value;
289      }
290    }
291    #endregion
292  }
293}
Note: See TracBrowser for help on using the repository browser.