Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5138 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

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