Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3528 was 3372, checked in by swagner, 15 years ago

Implemented reviewers' comments (#893).

File size: 14.2 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; }
81    }
82
[3267]83    private bool stopPending;
84
85    public Experiment()
86      : base() {
[3280]87      name = ItemName;
88      description = ItemDescription;
[3267]89      executionState = ExecutionState.Stopped;
90      executionTime = TimeSpan.Zero;
[3280]91      optimizers = new OptimizerList();
[3275]92      runs = new RunCollection();
[3267]93      stopPending = false;
[3280]94      Initialize();
[3267]95    }
[3280]96    public Experiment(string name)
97      : base(name) {
98      description = ItemDescription;
[3267]99      executionState = ExecutionState.Stopped;
100      executionTime = TimeSpan.Zero;
[3280]101      optimizers = new OptimizerList();
[3275]102      runs = new RunCollection();
[3267]103      stopPending = false;
[3280]104      Initialize();
[3267]105    }
[3280]106    public Experiment(string name, string description)
107      : base(name, description) {
[3267]108      executionState = ExecutionState.Stopped;
109      executionTime = TimeSpan.Zero;
[3280]110      optimizers = new OptimizerList();
[3275]111      runs = new RunCollection();
[3267]112      stopPending = false;
[3280]113      Initialize();
[3267]114    }
[3280]115    [StorableConstructor]
116    private Experiment(bool deserializing)
117      : base(deserializing) {
118      stopPending = false;
119    }
[3267]120
[3280]121    [StorableHook(HookType.AfterDeserialization)]
122    private void Initialize() {
123      RegisterOptimizersEvents();
124      foreach (IOptimizer optimizer in optimizers)
125        RegisterOptimizerEvents(optimizer);
126    }
127
[3267]128    public override IDeepCloneable Clone(Cloner cloner) {
[3280]129      if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
[3267]130      Experiment clone = (Experiment)base.Clone(cloner);
131      clone.executionState = executionState;
132      clone.executionTime = executionTime;
[3280]133      clone.optimizers = (OptimizerList)cloner.Clone(optimizers);
[3275]134      clone.runs = (RunCollection)cloner.Clone(runs);
[3267]135      clone.stopPending = stopPending;
[3280]136      clone.Initialize();
[3267]137      return clone;
138    }
139
140    public void Prepare() {
[3275]141      Prepare(false);
[3267]142    }
[3275]143    public void Prepare(bool clearRuns) {
[3267]144      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
145        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
[3274]146      if (Optimizers.Count > 0) {
[3275]147        if (clearRuns) runs.Clear();
[3274]148        foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started))
[3275]149          optimizer.Prepare(clearRuns);
[3267]150      }
151    }
152    public void Start() {
153      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
154        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
155      stopPending = false;
[3274]156      if (Optimizers.Count > 0) {
157        IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused));
158        if (optimizer != null) optimizer.Start();
[3267]159      }
160    }
161    public void Pause() {
162      if (ExecutionState != ExecutionState.Started)
163        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
[3274]164      if (Optimizers.Count > 0) {
165        foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started))
166          optimizer.Pause();
[3267]167      }
168    }
169    public void Stop() {
170      if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
171        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
172      stopPending = true;
[3274]173      if (Optimizers.Count > 0) {
174        foreach (IOptimizer optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused)))
175          optimizer.Stop();
[3267]176      }
177    }
178
179    #region Events
180    public event EventHandler ExecutionStateChanged;
181    private void OnExecutionStateChanged() {
182      EventHandler handler = ExecutionStateChanged;
183      if (handler != null) handler(this, EventArgs.Empty);
184    }
185    public event EventHandler ExecutionTimeChanged;
186    private void OnExecutionTimeChanged() {
187      EventHandler handler = ExecutionTimeChanged;
188      if (handler != null) handler(this, EventArgs.Empty);
189    }
190    public event EventHandler Prepared;
191    private void OnPrepared() {
192      ExecutionState = ExecutionState.Prepared;
193      EventHandler handler = Prepared;
194      if (handler != null) handler(this, EventArgs.Empty);
195    }
196    public event EventHandler Started;
197    private void OnStarted() {
198      ExecutionState = ExecutionState.Started;
199      EventHandler handler = Started;
200      if (handler != null) handler(this, EventArgs.Empty);
201    }
202    public event EventHandler Paused;
203    private void OnPaused() {
204      ExecutionState = ExecutionState.Paused;
205      EventHandler handler = Paused;
206      if (handler != null) handler(this, EventArgs.Empty);
207    }
208    public event EventHandler Stopped;
209    private void OnStopped() {
210      ExecutionState = ExecutionState.Stopped;
211      EventHandler handler = Stopped;
212      if (handler != null) handler(this, EventArgs.Empty);
213    }
214    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
215    private void OnExceptionOccurred(Exception exception) {
216      EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
217      if (handler != null) handler(this, new EventArgs<Exception>(exception));
218    }
219
[3274]220    private void RegisterOptimizersEvents() {
221      Optimizers.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset);
222      Optimizers.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded);
223      Optimizers.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved);
224      Optimizers.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced);
[3267]225    }
[3274]226    private void DeregisterOptimizersEvents() {
227      Optimizers.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset);
228      Optimizers.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded);
229      Optimizers.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved);
230      Optimizers.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced);
[3267]231    }
[3274]232    private void Optimizers_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
233      foreach (IndexedItem<IOptimizer> item in e.OldItems) {
234        DeregisterOptimizerEvents(item.Value);
[3267]235      }
[3274]236      foreach (IndexedItem<IOptimizer> item in e.Items) {
237        RegisterOptimizerEvents(item.Value);
[3267]238      }
239    }
[3274]240    private void Optimizers_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
241      foreach (IndexedItem<IOptimizer> item in e.Items) {
242        RegisterOptimizerEvents(item.Value);
[3267]243      }
244    }
[3274]245    private void Optimizers_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
246      foreach (IndexedItem<IOptimizer> item in e.Items) {
247        DeregisterOptimizerEvents(item.Value);
[3267]248      }
249    }
[3274]250    private void Optimizers_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
251      foreach (IndexedItem<IOptimizer> item in e.OldItems) {
252        DeregisterOptimizerEvents(item.Value);
[3267]253      }
[3274]254      foreach (IndexedItem<IOptimizer> item in e.Items) {
255        RegisterOptimizerEvents(item.Value);
[3267]256      }
257    }
258
[3274]259    private void RegisterOptimizerEvents(IOptimizer optimizer) {
260      optimizer.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
261      optimizer.ExecutionTimeChanged += new EventHandler(optimizer_ExecutionTimeChanged);
262      optimizer.Paused += new EventHandler(optimizer_Paused);
263      optimizer.Prepared += new EventHandler(optimizer_Prepared);
264      optimizer.Started += new EventHandler(optimizer_Started);
265      optimizer.Stopped += new EventHandler(optimizer_Stopped);
[3280]266      optimizer.Runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_CollectionReset);
267      optimizer.Runs.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsAdded);
268      optimizer.Runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsRemoved);
[3267]269    }
[3274]270    private void DeregisterOptimizerEvents(IOptimizer optimizer) {
271      optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
272      optimizer.ExecutionTimeChanged -= new EventHandler(optimizer_ExecutionTimeChanged);
273      optimizer.Paused -= new EventHandler(optimizer_Paused);
274      optimizer.Prepared -= new EventHandler(optimizer_Prepared);
275      optimizer.Started -= new EventHandler(optimizer_Started);
276      optimizer.Stopped -= new EventHandler(optimizer_Stopped);
[3280]277      optimizer.Runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_CollectionReset);
278      optimizer.Runs.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsAdded);
279      optimizer.Runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsRemoved);
[3267]280    }
[3274]281    private void optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) {
[3267]282      OnExceptionOccurred(e.Value);
283    }
[3274]284    private void optimizer_ExecutionTimeChanged(object sender, EventArgs e) {
285      ExecutionTime = Optimizers.Aggregate(TimeSpan.Zero, (t, o) => t + o.ExecutionTime);
286    }
287    private void optimizer_Paused(object sender, EventArgs e) {
288      if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started))
[3267]289        OnPaused();
290    }
[3274]291    private void optimizer_Prepared(object sender, EventArgs e) {
[3267]292      if (ExecutionState == ExecutionState.Stopped)
293        OnPrepared();
294    }
[3274]295    private void optimizer_Started(object sender, EventArgs e) {
[3267]296      if (ExecutionState != ExecutionState.Started)
297        OnStarted();
298    }
[3274]299    private void optimizer_Stopped(object sender, EventArgs e) {
[3276]300      if (!stopPending && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) {
301        Optimizers.First(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)).Start();
302      } else {
303        if (Optimizers.All(x => (x.ExecutionState != ExecutionState.Started) && (x.ExecutionState != ExecutionState.Paused))) {
304          stopPending = false;
305          OnStopped();
306        }
[3267]307      }
308    }
[3280]309    private void optimizer_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[3275]310      Runs.RemoveRange(e.OldItems);
311      Runs.AddRange(e.Items);
312    }
[3280]313    private void optimizer_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[3275]314      Runs.AddRange(e.Items);
315    }
[3280]316    private void optimizer_Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[3275]317      Runs.RemoveRange(e.Items);
318    }
[3267]319    #endregion
320  }
321}
Note: See TracBrowser for help on using the repository browser.