Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveHiveEngine/HeuristicLab.Optimization/3.3/BatchRun.cs @ 8694

Last change on this file since 8694 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 16.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 HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Optimization {
32  /// <summary>
33  /// A run in which an optimizer is executed a given number of times.
34  /// </summary>
35  [Item("Batch Run", "A run in which an optimizer is executed a given number of times.")]
36  [Creatable("Testing & Analysis")]
37  [StorableClass]
38  public sealed class BatchRun : NamedItem, IOptimizer, IStorableContent {
39    public string Filename { get; set; }
40
41    public static new Image StaticItemImage {
42      get { return HeuristicLab.Common.Resources.VSImageLibrary.Event; }
43    }
44    public override Image ItemImage {
45      get {
46        if (ExecutionState == ExecutionState.Prepared) return HeuristicLab.Common.Resources.VSImageLibrary.BatchRunPrepared;
47        else if (ExecutionState == ExecutionState.Started) return HeuristicLab.Common.Resources.VSImageLibrary.BatchRunStarted;
48        else if (ExecutionState == ExecutionState.Paused) return HeuristicLab.Common.Resources.VSImageLibrary.BatchRunPaused;
49        else if (ExecutionState == ExecutionState.Stopped) return HeuristicLab.Common.Resources.VSImageLibrary.BatchRunStopped;
50        else return base.ItemImage;
51      }
52    }
53
54    [Storable]
55    private ExecutionState executionState;
56    public ExecutionState ExecutionState {
57      get { return executionState; }
58      private set {
59        if (executionState != value) {
60          executionState = value;
61          OnExecutionStateChanged();
62          OnItemImageChanged();
63        }
64      }
65    }
66
67    [Storable]
68    private TimeSpan executionTime;
69    public TimeSpan ExecutionTime {
70      get {
71        if ((Optimizer != null) && (Optimizer.ExecutionState != ExecutionState.Stopped))
72          return executionTime + Optimizer.ExecutionTime;
73        else
74          return executionTime;
75      }
76      private set {
77        executionTime = value;
78        OnExecutionTimeChanged();
79      }
80    }
81
82    [Storable]
83    private IOptimizer optimizer;
84    public IOptimizer Optimizer {
85      get { return optimizer; }
86      set {
87        if (optimizer != value) {
88          if (optimizer != null) {
89            DeregisterOptimizerEvents();
90            IEnumerable<IRun> runs = optimizer.Runs;
91            optimizer = null; //necessary to avoid removing the runs from the old optimizer
92            Runs.RemoveRange(runs);
93          }
94          optimizer = value;
95          if (optimizer != null) {
96            RegisterOptimizerEvents();
97            Runs.AddRange(optimizer.Runs);
98          }
99          OnOptimizerChanged();
100          Prepare();
101        }
102      }
103    }
104    // BackwardsCompatibility3.3
105    #region Backwards compatible code (remove with 3.4)
106    [Storable(AllowOneWay = true)]
107    private IAlgorithm algorithm {
108      set { optimizer = value; }
109    }
110    #endregion
111
112    [Storable]
113    private int repetitions;
114    public int Repetitions {
115      get { return repetitions; }
116      set {
117        if (repetitions != value) {
118          repetitions = value;
119          OnRepetitionsChanged();
120          if ((Optimizer != null) && (Optimizer.ExecutionState == ExecutionState.Stopped))
121            Prepare();
122        }
123      }
124    }
125    [Storable]
126    private int repetitionsCounter;
127
128    [Storable]
129    private RunCollection runs;
130    public RunCollection Runs {
131      get { return runs; }
132      private set {
133        if (value == null) throw new ArgumentNullException();
134        if (runs != value) {
135          if (runs != null) DeregisterRunsEvents();
136          runs = value;
137          if (runs != null) RegisterRunsEvents();
138        }
139      }
140    }
141
142    public IEnumerable<IOptimizer> NestedOptimizers {
143      get {
144        if (Optimizer == null) yield break;
145
146        yield return Optimizer;
147        foreach (IOptimizer opt in Optimizer.NestedOptimizers)
148          yield return opt;
149      }
150    }
151
152    private bool batchRunStarted = false;
153    private bool batchRunPaused = false;
154    private bool batchRunStopped = false;
155
156    public BatchRun()
157      : base() {
158      name = ItemName;
159      description = ItemDescription;
160      executionState = ExecutionState.Stopped;
161      executionTime = TimeSpan.Zero;
162      repetitions = 10;
163      repetitionsCounter = 0;
164      Runs = new RunCollection();
165    }
166    public BatchRun(string name)
167      : base(name) {
168      description = ItemDescription;
169      executionState = ExecutionState.Stopped;
170      executionTime = TimeSpan.Zero;
171      repetitions = 10;
172      repetitionsCounter = 0;
173      Runs = new RunCollection();
174    }
175    public BatchRun(string name, string description)
176      : base(name, description) {
177      executionState = ExecutionState.Stopped;
178      executionTime = TimeSpan.Zero;
179      repetitions = 10;
180      repetitionsCounter = 0;
181      Runs = new RunCollection();
182    }
183    [StorableConstructor]
184    private BatchRun(bool deserializing) : base(deserializing) { }
185    [StorableHook(HookType.AfterDeserialization)]
186    private void AfterDeserialization() {
187      Initialize();
188    }
189
190    private BatchRun(BatchRun original, Cloner cloner)
191      : base(original, cloner) {
192      executionState = original.executionState;
193      executionTime = original.executionTime;
194      optimizer = cloner.Clone(original.optimizer);
195      repetitions = original.repetitions;
196      repetitionsCounter = original.repetitionsCounter;
197      runs = cloner.Clone(original.runs);
198      batchRunStarted = original.batchRunStarted;
199      batchRunPaused = original.batchRunPaused;
200      batchRunStopped = original.batchRunStopped;
201      Initialize();
202    }
203    public override IDeepCloneable Clone(Cloner cloner) {
204      if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
205      return new BatchRun(this, cloner);
206    }
207
208    private void Initialize() {
209      if (optimizer != null) RegisterOptimizerEvents();
210      if (runs != null) RegisterRunsEvents();
211    }
212
213    public void Prepare() {
214      Prepare(false);
215    }
216    public void Prepare(bool clearRuns) {
217      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
218        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
219      if (Optimizer != null) {
220        repetitionsCounter = 0;
221        if (clearRuns) runs.Clear();
222        Optimizer.Prepare(clearRuns);
223      } else {
224        ExecutionState = ExecutionState.Stopped;
225      }
226    }
227    public void Start() {
228      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
229        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
230      if (Optimizer == null) return;
231
232      batchRunStarted = true;
233      batchRunPaused = false;
234      batchRunStopped = false;
235      if (Optimizer.ExecutionState == ExecutionState.Stopped) Optimizer.Prepare();
236      Optimizer.Start();
237    }
238    public void Pause() {
239      if (ExecutionState != ExecutionState.Started)
240        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
241      if (Optimizer == null) return;
242      if (Optimizer.ExecutionState != ExecutionState.Started) return;
243
244      batchRunStarted = false;
245      batchRunPaused = true;
246      batchRunStopped = false;
247      Optimizer.Pause();
248    }
249    public void Stop() {
250      if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
251        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
252      if (Optimizer == null) return;
253      if (Optimizer.ExecutionState != ExecutionState.Started && Optimizer.ExecutionState != ExecutionState.Paused) return;
254      batchRunStarted = false;
255      batchRunPaused = false;
256      batchRunStopped = true;
257      Optimizer.Stop();
258    }
259
260    #region Events
261    public event EventHandler ExecutionStateChanged;
262    private void OnExecutionStateChanged() {
263      EventHandler handler = ExecutionStateChanged;
264      if (handler != null) handler(this, EventArgs.Empty);
265    }
266    public event EventHandler ExecutionTimeChanged;
267    private void OnExecutionTimeChanged() {
268      EventHandler handler = ExecutionTimeChanged;
269      if (handler != null) handler(this, EventArgs.Empty);
270    }
271    public event EventHandler OptimizerChanged;
272    private void OnOptimizerChanged() {
273      EventHandler handler = OptimizerChanged;
274      if (handler != null) handler(this, EventArgs.Empty);
275    }
276    public event EventHandler RepetitionsChanged;
277    private void OnRepetitionsChanged() {
278      EventHandler handler = RepetitionsChanged;
279      if (handler != null) handler(this, EventArgs.Empty);
280    }
281    public event EventHandler Prepared;
282    private void OnPrepared() {
283      ExecutionState = ExecutionState.Prepared;
284      EventHandler handler = Prepared;
285      if (handler != null) handler(this, EventArgs.Empty);
286    }
287    public event EventHandler Started;
288    private void OnStarted() {
289      ExecutionState = ExecutionState.Started;
290      EventHandler handler = Started;
291      if (handler != null) handler(this, EventArgs.Empty);
292    }
293    public event EventHandler Paused;
294    private void OnPaused() {
295      ExecutionState = ExecutionState.Paused;
296      EventHandler handler = Paused;
297      if (handler != null) handler(this, EventArgs.Empty);
298    }
299    public event EventHandler Stopped;
300    private void OnStopped() {
301      ExecutionState = ExecutionState.Stopped;
302      EventHandler handler = Stopped;
303      if (handler != null) handler(this, EventArgs.Empty);
304    }
305    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
306    private void OnExceptionOccurred(Exception exception) {
307      EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
308      if (handler != null) handler(this, new EventArgs<Exception>(exception));
309    }
310
311    private void RegisterOptimizerEvents() {
312      optimizer.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Optimizer_ExceptionOccurred);
313      optimizer.ExecutionTimeChanged += new EventHandler(Optimizer_ExecutionTimeChanged);
314      optimizer.Paused += new EventHandler(Optimizer_Paused);
315      optimizer.Prepared += new EventHandler(Optimizer_Prepared);
316      optimizer.Started += new EventHandler(Optimizer_Started);
317      optimizer.Stopped += new EventHandler(Optimizer_Stopped);
318      optimizer.Runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Optimizer_Runs_CollectionReset);
319      optimizer.Runs.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Optimizer_Runs_ItemsAdded);
320      optimizer.Runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Optimizer_Runs_ItemsRemoved);
321    }
322    private void DeregisterOptimizerEvents() {
323      optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Optimizer_ExceptionOccurred);
324      optimizer.ExecutionTimeChanged -= new EventHandler(Optimizer_ExecutionTimeChanged);
325      optimizer.Paused -= new EventHandler(Optimizer_Paused);
326      optimizer.Prepared -= new EventHandler(Optimizer_Prepared);
327      optimizer.Started -= new EventHandler(Optimizer_Started);
328      optimizer.Stopped -= new EventHandler(Optimizer_Stopped);
329      optimizer.Runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Optimizer_Runs_CollectionReset);
330      optimizer.Runs.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Optimizer_Runs_ItemsAdded);
331      optimizer.Runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Optimizer_Runs_ItemsRemoved);
332    }
333    private void Optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) {
334      OnExceptionOccurred(e.Value);
335    }
336    private void Optimizer_ExecutionTimeChanged(object sender, EventArgs e) {
337      OnExecutionTimeChanged();
338    }
339    private void Optimizer_Paused(object sender, EventArgs e) {
340      if (ExecutionState == ExecutionState.Started) {
341        batchRunStarted = false;
342        batchRunPaused = true;
343        batchRunStopped = false;
344        OnPaused();
345      }
346    }
347    private void Optimizer_Prepared(object sender, EventArgs e) {
348      if (ExecutionState == ExecutionState.Stopped || !batchRunStarted) {
349        OnPrepared();
350      }
351    }
352    private void Optimizer_Started(object sender, EventArgs e) {
353      if (ExecutionState != ExecutionState.Started)
354        OnStarted();
355    }
356    private void Optimizer_Stopped(object sender, EventArgs e) {
357      repetitionsCounter++;
358
359      if (batchRunStopped) OnStopped();
360      else if (repetitionsCounter >= repetitions) OnStopped();
361      else if (batchRunPaused) OnPaused();
362      else if (batchRunStarted) {
363        Optimizer.Prepare();
364        Optimizer.Start();
365      } else OnStopped();
366    }
367    private void Optimizer_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
368      Runs.RemoveRange(e.OldItems);
369      Runs.AddRange(e.Items);
370    }
371    private void Optimizer_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
372      Runs.AddRange(e.Items);
373    }
374    private void Optimizer_Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
375      Runs.RemoveRange(e.Items);
376    }
377
378    private void RegisterRunsEvents() {
379      runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
380      runs.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsAdded);
381      runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsRemoved);
382    }
383
384    private void DeregisterRunsEvents() {
385      runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
386      runs.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsAdded);
387      runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsRemoved);
388    }
389    private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
390      foreach (IRun run in e.OldItems) {
391        IItem item;
392        run.Results.TryGetValue("Execution Time", out item);
393        TimeSpanValue executionTime = item as TimeSpanValue;
394        if (executionTime != null) ExecutionTime -= executionTime.Value;
395      }
396      if (Optimizer != null) Optimizer.Runs.RemoveRange(e.OldItems);
397      foreach (IRun run in e.Items) {
398        IItem item;
399        run.Results.TryGetValue("Execution Time", out item);
400        TimeSpanValue executionTime = item as TimeSpanValue;
401        if (executionTime != null) ExecutionTime += executionTime.Value;
402      }
403    }
404    private void Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
405      foreach (IRun run in e.Items) {
406        IItem item;
407        run.Results.TryGetValue("Execution Time", out item);
408        TimeSpanValue executionTime = item as TimeSpanValue;
409        if (executionTime != null) ExecutionTime += executionTime.Value;
410      }
411    }
412    private void Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
413      foreach (IRun run in e.Items) {
414        IItem item;
415        run.Results.TryGetValue("Execution Time", out item);
416        TimeSpanValue executionTime = item as TimeSpanValue;
417        if (executionTime != null) ExecutionTime -= executionTime.Value;
418      }
419      if (Optimizer != null) Optimizer.Runs.RemoveRange(e.Items);
420    }
421    #endregion
422  }
423}
Note: See TracBrowser for help on using the repository browser.