Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Optimization/3.3/BatchRun.cs @ 12344

Last change on this file since 12344 was 8206, checked in by gkronber, 12 years ago

#1847: merged r8084:8205 from trunk into GP move operators branch

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