source: branches/HeuristicLab.DatastreamAnalysis/HeuristicLab.DatastreamAnalysis/3.4/DatastreamAnalysisOptimizer.cs @ 14488

Last change on this file since 14488 was 14488, checked in by jzenisek, 4 years ago

#2719 initializing ticket: adding optimizer + views for datastream analysis

File size: 7.2 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 HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Optimization;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis;
31
32namespace HeuristicLab.DatastreamAnalysis {
33  internal enum DatastreamAnalysisOptimizerAction { None, Prepare, Start, Stop, Pause }
34
35  [StorableClass]
36  [Item("DatastreamAnalysis Optimizer", "The main loop for evaluating ensemble models against a incoming datastream of time series fashion.")]
37  [Creatable("Algorithms")]
38  public class DatastreamAnalysisOptimizer : NamedItem, IOptimizer, IStorableContent {
39
40    // TODO: introduce members -> ensembles, datastream
41
42
43    // VAR 1: datastream = problem data
44    [Storable]
45    private IRegressionProblemData problemData;
46
47    public IRegressionProblemData ProblemData {
48      get { return problemData; }
49      set {
50        if (value == null || value == problemData)
51          return;
52
53        problemData = value;
54      }
55    }
56
57    // VAR 2: datastream = external source (webservice, AMQP-Queue, etc.)
58    // TODO
59
60    public DatastreamAnalysisOptimizer() {
61      Name = "Datastream Analysis";
62    }
63
64    [StorableConstructor]
65    protected DatastreamAnalysisOptimizer(bool deserializing) : base(deserializing) { }
66
67    [StorableHook(HookType.AfterDeserialization)]
68    private void AfterDeserialization() {
69      // nothing to do in here
70    }
71
72    protected DatastreamAnalysisOptimizer(DatastreamAnalysisOptimizer original, Cloner cloner) : base(original, cloner) {
73      // TODO: clone members (ensembles, datastream)
74      this.ProblemData = (IRegressionProblemData)original.ProblemData.Clone(cloner);
75    }
76
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return  new DatastreamAnalysisOptimizer(this, cloner);
79    }
80
81
82    // TODO: introduce EventHandler for EnsemblesChanged, DatastreamChanged
83
84    public EventHandler ProblemDataChanged;
85    private void OnProblemDataChanged() {
86      var changed = ProblemDataChanged;
87      if (changed != null)
88        changed(this, EventArgs.Empty);
89    }
90
91
92    public void Prepare() {
93      Prepare(false);
94    }
95
96    public void Start() {
97      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
98        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
99    }
100
101    public void Pause() {
102      if (ExecutionState != ExecutionState.Started)
103        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
104    }
105
106    public void Stop() {
107      if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
108        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
109    }
110
111    public void Prepare(bool clearRuns) {
112      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
113        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
114    }
115
116
117    [Storable]
118    private TimeSpan executionTime;
119    public TimeSpan ExecutionTime {
120      get { return executionTime; }
121      private set {
122        executionTime = value;
123        OnExecutionTimeChanged();
124      }
125    }
126
127    public RunCollection Runs {
128      get {
129        return new RunCollection();
130      }
131    }
132
133    public IEnumerable<IOptimizer> NestedOptimizers { get; }
134    public string Filename { get; set; }
135
136    public new static Image StaticItemImage
137    {
138      get { return HeuristicLab.Common.Resources.VSImageLibrary.Event; }
139    }
140
141    public override Image ItemImage {
142      get {
143        if (ExecutionState == ExecutionState.Prepared)
144          return HeuristicLab.Common.Resources.VSImageLibrary.BatchRunPrepared;
145        else if (ExecutionState == ExecutionState.Started)
146          return HeuristicLab.Common.Resources.VSImageLibrary.BatchRunStarted;
147        else if (ExecutionState == ExecutionState.Paused) return HeuristicLab.Common.Resources.VSImageLibrary.BatchRunPaused;
148        else if (ExecutionState == ExecutionState.Stopped)
149          return HeuristicLab.Common.Resources.VSImageLibrary.BatchRunStopped;
150        else return base.ItemImage;
151      }
152    }
153
154    #region Events
155
156    public ExecutionState ExecutionState { get; private set; }
157    public event EventHandler ExecutionStateChanged;
158    public event EventHandler ExecutionTimeChanged;
159    public event EventHandler Prepared;
160    public event EventHandler Started;
161    public event EventHandler Paused;
162    public event EventHandler Stopped;
163    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
164
165
166    #region event firing
167
168    protected override void OnNameChanged() {
169      base.OnNameChanged();
170    }
171
172    private void OnExecutionStateChanged() {
173      EventHandler handler = ExecutionStateChanged;
174      if (handler != null) handler(this, EventArgs.Empty);
175    }
176
177    private void OnExecutionTimeChanged() {
178      EventHandler handler = ExecutionTimeChanged;
179      if (handler != null) handler(this, EventArgs.Empty);
180    }
181
182    private void OnPrepared() {
183      ExecutionState = ExecutionState.Prepared;
184      EventHandler handler = Prepared;
185      if (handler != null) handler(this, EventArgs.Empty);
186    }
187
188    private void OnStarted() {
189      ExecutionState = ExecutionState.Started;
190      EventHandler handler = Started;
191      if (handler != null) handler(this, EventArgs.Empty);
192    }
193
194    private void OnPaused() {
195      ExecutionState = ExecutionState.Paused;
196      EventHandler handler = Paused;
197      if (handler != null) handler(this, EventArgs.Empty);
198    }
199
200    private void OnStopped() {
201      ExecutionState = ExecutionState.Stopped;
202      EventHandler handler = Stopped;
203      if (handler != null) handler(this, EventArgs.Empty);
204    }
205
206    private void OnExceptionOccurred(Exception exception) {
207      EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
208      if (handler != null) handler(this, new EventArgs<Exception>(exception));
209    }
210
211    #endregion
212
213
214    #endregion
215
216  }
217}
Note: See TracBrowser for help on using the repository browser.