Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/Experiment.cs @ 4047

Last change on this file since 4047 was 3716, checked in by swagner, 15 years ago

Implemented reviewers' comments (#947)

File size: 15.4 KB
RevLine 
[3267]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 System.Drawing;
24using System.Linq;
25using HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Optimization {
31  /// <summary>
32  /// An experiment which contains multiple batch runs of algorithms.
33  /// </summary>
34  [Item("Experiment", "An experiment which contains multiple batch runs of algorithms.")]
35  [Creatable("Testing & Analysis")]
36  [StorableClass]
[3274]37  public sealed class Experiment : NamedItem, IOptimizer {
[3267]38    public override Image ItemImage {
[3351]39      get {
[3372]40        if (ExecutionState == ExecutionState.Prepared) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutablePrepared;
41        else if (ExecutionState == ExecutionState.Started) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutableStarted;
42        else if (ExecutionState == ExecutionState.Paused) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutablePaused;
43        else if (ExecutionState == ExecutionState.Stopped) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutableStopped;
[3351]44        else return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event;
45      }
[3267]46    }
47
48    [Storable]
49    private ExecutionState executionState;
50    public ExecutionState ExecutionState {
51      get { return executionState; }
52      private set {
53        if (executionState != value) {
54          executionState = value;
55          OnExecutionStateChanged();
[3351]56          OnItemImageChanged();
[3267]57        }
58      }
59    }
60
61    [Storable]
62    private TimeSpan executionTime;
63    public TimeSpan ExecutionTime {
64      get { return executionTime; }
65      private set {
66        executionTime = value;
67        OnExecutionTimeChanged();
68      }
69    }
70
[3280]71    [Storable]
[3274]72    private OptimizerList optimizers;
73    public OptimizerList Optimizers {
74      get { return optimizers; }
[3267]75    }
76
[3275]77    [Storable]
78    private RunCollection runs;
79    public RunCollection Runs {
80      get { return runs; }
[3716]81      private set {
82        if (value == null) throw new ArgumentNullException();
83        if (runs != value) {
84          if (runs != null) DeregisterRunsEvents();
85          runs = value;
86          if (runs != null) RegisterRunsEvents();
87        }
88      }
[3275]89    }
90
[3267]91    private bool stopPending;
92
93    public Experiment()
94      : base() {
[3280]95      name = ItemName;
96      description = ItemDescription;
[3267]97      executionState = ExecutionState.Stopped;
98      executionTime = TimeSpan.Zero;
[3280]99      optimizers = new OptimizerList();
[3716]100      Runs = new RunCollection();
[3267]101      stopPending = false;
[3280]102      Initialize();
[3267]103    }
[3280]104    public Experiment(string name)
105      : base(name) {
106      description = ItemDescription;
[3267]107      executionState = ExecutionState.Stopped;
108      executionTime = TimeSpan.Zero;
[3280]109      optimizers = new OptimizerList();
[3716]110      Runs = new RunCollection();
[3267]111      stopPending = false;
[3280]112      Initialize();
[3267]113    }
[3280]114    public Experiment(string name, string description)
115      : base(name, description) {
[3267]116      executionState = ExecutionState.Stopped;
117      executionTime = TimeSpan.Zero;
[3280]118      optimizers = new OptimizerList();
[3716]119      Runs = new RunCollection();
[3267]120      stopPending = false;
[3280]121      Initialize();
[3267]122    }
[3280]123    [StorableConstructor]
124    private Experiment(bool deserializing)
125      : base(deserializing) {
126      stopPending = false;
127    }
[3267]128
[3280]129    [StorableHook(HookType.AfterDeserialization)]
130    private void Initialize() {
131      RegisterOptimizersEvents();
132      foreach (IOptimizer optimizer in optimizers)
133        RegisterOptimizerEvents(optimizer);
[3716]134      if (runs != null) RegisterRunsEvents();
[3280]135    }
136
[3267]137    public override IDeepCloneable Clone(Cloner cloner) {
[3280]138      if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
[3267]139      Experiment clone = (Experiment)base.Clone(cloner);
140      clone.executionState = executionState;
141      clone.executionTime = executionTime;
[3280]142      clone.optimizers = (OptimizerList)cloner.Clone(optimizers);
[3275]143      clone.runs = (RunCollection)cloner.Clone(runs);
[3267]144      clone.stopPending = stopPending;
[3280]145      clone.Initialize();
[3267]146      return clone;
147    }
148
149    public void Prepare() {
[3275]150      Prepare(false);
[3267]151    }
[3275]152    public void Prepare(bool clearRuns) {
[3267]153      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
154        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
[3274]155      if (Optimizers.Count > 0) {
[3275]156        if (clearRuns) runs.Clear();
[3274]157        foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started))
[3275]158          optimizer.Prepare(clearRuns);
[3267]159      }
160    }
161    public void Start() {
162      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
163        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
164      stopPending = false;
[3274]165      if (Optimizers.Count > 0) {
166        IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused));
167        if (optimizer != null) optimizer.Start();
[3267]168      }
169    }
170    public void Pause() {
171      if (ExecutionState != ExecutionState.Started)
172        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
[3274]173      if (Optimizers.Count > 0) {
174        foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started))
175          optimizer.Pause();
[3267]176      }
177    }
178    public void Stop() {
179      if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
180        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
181      stopPending = true;
[3274]182      if (Optimizers.Count > 0) {
183        foreach (IOptimizer optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused)))
184          optimizer.Stop();
[3267]185      }
186    }
187
188    #region Events
189    public event EventHandler ExecutionStateChanged;
190    private void OnExecutionStateChanged() {
191      EventHandler handler = ExecutionStateChanged;
192      if (handler != null) handler(this, EventArgs.Empty);
193    }
194    public event EventHandler ExecutionTimeChanged;
195    private void OnExecutionTimeChanged() {
196      EventHandler handler = ExecutionTimeChanged;
197      if (handler != null) handler(this, EventArgs.Empty);
198    }
199    public event EventHandler Prepared;
200    private void OnPrepared() {
201      ExecutionState = ExecutionState.Prepared;
202      EventHandler handler = Prepared;
203      if (handler != null) handler(this, EventArgs.Empty);
204    }
205    public event EventHandler Started;
206    private void OnStarted() {
207      ExecutionState = ExecutionState.Started;
208      EventHandler handler = Started;
209      if (handler != null) handler(this, EventArgs.Empty);
210    }
211    public event EventHandler Paused;
212    private void OnPaused() {
213      ExecutionState = ExecutionState.Paused;
214      EventHandler handler = Paused;
215      if (handler != null) handler(this, EventArgs.Empty);
216    }
217    public event EventHandler Stopped;
218    private void OnStopped() {
219      ExecutionState = ExecutionState.Stopped;
220      EventHandler handler = Stopped;
221      if (handler != null) handler(this, EventArgs.Empty);
222    }
223    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
224    private void OnExceptionOccurred(Exception exception) {
225      EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
226      if (handler != null) handler(this, new EventArgs<Exception>(exception));
227    }
228
[3274]229    private void RegisterOptimizersEvents() {
230      Optimizers.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset);
231      Optimizers.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded);
232      Optimizers.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved);
233      Optimizers.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced);
[3267]234    }
[3274]235    private void DeregisterOptimizersEvents() {
236      Optimizers.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset);
237      Optimizers.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded);
238      Optimizers.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved);
239      Optimizers.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced);
[3267]240    }
[3274]241    private void Optimizers_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
242      foreach (IndexedItem<IOptimizer> item in e.OldItems) {
243        DeregisterOptimizerEvents(item.Value);
[3267]244      }
[3274]245      foreach (IndexedItem<IOptimizer> item in e.Items) {
246        RegisterOptimizerEvents(item.Value);
[3716]247        item.Value.Prepare();
[3267]248      }
249    }
[3274]250    private void Optimizers_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
251      foreach (IndexedItem<IOptimizer> item in e.Items) {
252        RegisterOptimizerEvents(item.Value);
[3716]253        item.Value.Prepare();
[3267]254      }
255    }
[3274]256    private void Optimizers_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
257      foreach (IndexedItem<IOptimizer> item in e.Items) {
258        DeregisterOptimizerEvents(item.Value);
[3267]259      }
260    }
[3274]261    private void Optimizers_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
262      foreach (IndexedItem<IOptimizer> item in e.OldItems) {
263        DeregisterOptimizerEvents(item.Value);
[3267]264      }
[3274]265      foreach (IndexedItem<IOptimizer> item in e.Items) {
266        RegisterOptimizerEvents(item.Value);
[3716]267        item.Value.Prepare();
[3267]268      }
269    }
270
[3274]271    private void RegisterOptimizerEvents(IOptimizer optimizer) {
272      optimizer.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
273      optimizer.ExecutionTimeChanged += new EventHandler(optimizer_ExecutionTimeChanged);
274      optimizer.Paused += new EventHandler(optimizer_Paused);
275      optimizer.Prepared += new EventHandler(optimizer_Prepared);
276      optimizer.Started += new EventHandler(optimizer_Started);
277      optimizer.Stopped += new EventHandler(optimizer_Stopped);
[3280]278      optimizer.Runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_CollectionReset);
279      optimizer.Runs.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsAdded);
280      optimizer.Runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsRemoved);
[3267]281    }
[3274]282    private void DeregisterOptimizerEvents(IOptimizer optimizer) {
283      optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
284      optimizer.ExecutionTimeChanged -= new EventHandler(optimizer_ExecutionTimeChanged);
285      optimizer.Paused -= new EventHandler(optimizer_Paused);
286      optimizer.Prepared -= new EventHandler(optimizer_Prepared);
287      optimizer.Started -= new EventHandler(optimizer_Started);
288      optimizer.Stopped -= new EventHandler(optimizer_Stopped);
[3280]289      optimizer.Runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_CollectionReset);
290      optimizer.Runs.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsAdded);
291      optimizer.Runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsRemoved);
[3267]292    }
[3274]293    private void optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) {
[3267]294      OnExceptionOccurred(e.Value);
295    }
[3274]296    private void optimizer_ExecutionTimeChanged(object sender, EventArgs e) {
297      ExecutionTime = Optimizers.Aggregate(TimeSpan.Zero, (t, o) => t + o.ExecutionTime);
298    }
299    private void optimizer_Paused(object sender, EventArgs e) {
300      if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started))
[3267]301        OnPaused();
302    }
[3274]303    private void optimizer_Prepared(object sender, EventArgs e) {
[3267]304      if (ExecutionState == ExecutionState.Stopped)
305        OnPrepared();
306    }
[3274]307    private void optimizer_Started(object sender, EventArgs e) {
[3267]308      if (ExecutionState != ExecutionState.Started)
309        OnStarted();
310    }
[3274]311    private void optimizer_Stopped(object sender, EventArgs e) {
[3276]312      if (!stopPending && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) {
313        Optimizers.First(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)).Start();
314      } else {
315        if (Optimizers.All(x => (x.ExecutionState != ExecutionState.Started) && (x.ExecutionState != ExecutionState.Paused))) {
316          stopPending = false;
317          OnStopped();
318        }
[3267]319      }
320    }
[3280]321    private void optimizer_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[3275]322      Runs.RemoveRange(e.OldItems);
323      Runs.AddRange(e.Items);
324    }
[3280]325    private void optimizer_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[3275]326      Runs.AddRange(e.Items);
327    }
[3280]328    private void optimizer_Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[3275]329      Runs.RemoveRange(e.Items);
330    }
[3716]331
332    private void RegisterRunsEvents() {
333      runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
334      runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsRemoved);
335    }
336    private void DeregisterRunsEvents() {
337      runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
338      runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsRemoved);
339    }
340    private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
341      foreach (IOptimizer optimizer in Optimizers)
342        optimizer.Runs.RemoveRange(e.OldItems);
343    }
344    private void Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
345      foreach (IOptimizer optimizer in Optimizers)
346        optimizer.Runs.RemoveRange(e.Items);
347    }
[3267]348    #endregion
349  }
350}
Note: See TracBrowser for help on using the repository browser.