Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/BatchRun.cs @ 6762

Last change on this file since 6762 was 6534, checked in by mkommend, 13 years ago

#1555: Changed icons for batch runs and experiments.

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