Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3276 was 3276, checked in by swagner, 12 years ago

Continued work on algorithm batch processing (#947).

File size: 11.1 KB
Line 
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 HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Collections;
28
29namespace HeuristicLab.Optimization {
30  /// <summary>
31  /// A run in which an algorithm is executed a given number of times.
32  /// </summary>
33  [Item("Batch Run", "A run in which an algorithm is executed a given number of times.")]
34  [Creatable("Testing & Analysis")]
35  [StorableClass]
36  public sealed class BatchRun : NamedItem, IOptimizer {
37    public override Image ItemImage {
38      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event; }
39    }
40
41    [Storable]
42    private ExecutionState executionState;
43    public ExecutionState ExecutionState {
44      get { return executionState; }
45      private set {
46        if (executionState != value) {
47          executionState = value;
48          OnExecutionStateChanged();
49        }
50      }
51    }
52
53    [Storable]
54    private TimeSpan executionTime;
55    public TimeSpan ExecutionTime {
56      get {
57        if ((Algorithm != null) && (Algorithm.ExecutionState != ExecutionState.Stopped))
58          return executionTime + Algorithm.ExecutionTime;
59        else
60          return executionTime;
61      }
62      private set {
63        executionTime = value;
64        OnExecutionTimeChanged();
65      }
66    }
67
68    private IAlgorithm algorithm;
69    [Storable]
70    private IAlgorithm AlgorithmPersistence {
71      get { return algorithm; }
72      set {
73        if (algorithm != null) DeregisterAlgorithmEvents();
74        algorithm = value;
75        if (algorithm != null) RegisterAlgorithmEvents();
76      }
77    }
78    public IAlgorithm Algorithm {
79      get { return algorithm; }
80      set {
81        if (algorithm != value) {
82          if (algorithm != null) DeregisterAlgorithmEvents();
83          algorithm = value;
84          if (algorithm != null) RegisterAlgorithmEvents();
85          OnAlgorithmChanged();
86          Prepare(true);
87        }
88      }
89    }
90
91    [Storable]
92    private int repetitions;
93    public int Repetitions {
94      get { return repetitions; }
95      set {
96        if (repetitions != value) {
97          repetitions = value;
98          OnRepetitionsChanged();
99          if ((runs.Count < repetitions) && (Algorithm != null) && (Algorithm.ExecutionState == ExecutionState.Stopped))
100            Prepare();
101        }
102      }
103    }
104
105    [Storable]
106    private RunCollection runs;
107    public RunCollection Runs {
108      get { return runs; }
109    }
110
111    private bool stopPending;
112
113    public BatchRun()
114      : base() {
115      executionState = ExecutionState.Stopped;
116      executionTime = TimeSpan.Zero;
117      repetitions = 10;
118      runs = new RunCollection();
119      stopPending = false;
120    }
121    public BatchRun(string name) : base(name) {
122      executionState = ExecutionState.Stopped;
123      executionTime = TimeSpan.Zero;
124      repetitions = 10;
125      runs = new RunCollection();
126      stopPending = false;
127    }
128    public BatchRun(string name, string description) : base(name, description) {
129      executionState = ExecutionState.Stopped;
130      executionTime = TimeSpan.Zero;
131      repetitions = 10;
132      runs = new RunCollection();
133      stopPending = false;
134    }
135
136    public override IDeepCloneable Clone(Cloner cloner) {
137      BatchRun clone = (BatchRun)base.Clone(cloner);
138      clone.executionState = executionState;
139      clone.executionTime = executionTime;
140      clone.Algorithm = (IAlgorithm)cloner.Clone(algorithm);
141      clone.repetitions = repetitions;
142      clone.runs = (RunCollection)cloner.Clone(runs);
143      clone.stopPending = stopPending;
144      return clone;
145    }
146
147    public void Prepare() {
148      Prepare(false);
149    }
150    public void Prepare(bool clearRuns) {
151      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
152        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
153      if (Algorithm != null) {
154        if (clearRuns) {
155          ExecutionTime = TimeSpan.Zero;
156          runs.Clear();
157        }
158        Algorithm.Prepare(clearRuns);
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      if (Algorithm != null) Algorithm.Start();
165    }
166    public void Pause() {
167      if (ExecutionState != ExecutionState.Started)
168        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
169      if (Algorithm != null) Algorithm.Pause();
170    }
171    public void Stop() {
172      if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
173        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
174      stopPending = true;
175      if (Algorithm != null) Algorithm.Stop();
176    }
177
178    #region Events
179    public event EventHandler ExecutionStateChanged;
180    private void OnExecutionStateChanged() {
181      EventHandler handler = ExecutionStateChanged;
182      if (handler != null) handler(this, EventArgs.Empty);
183    }
184    public event EventHandler ExecutionTimeChanged;
185    private void OnExecutionTimeChanged() {
186      EventHandler handler = ExecutionTimeChanged;
187      if (handler != null) handler(this, EventArgs.Empty);
188    }
189    public event EventHandler AlgorithmChanged;
190    private void OnAlgorithmChanged() {
191      EventHandler handler = AlgorithmChanged;
192      if (handler != null) handler(this, EventArgs.Empty);
193    }
194    public event EventHandler RepetitionsChanged;
195    private void OnRepetitionsChanged() {
196      EventHandler handler = RepetitionsChanged;
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
229    private void RegisterAlgorithmEvents() {
230      algorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Algorithm_ExceptionOccurred);
231      algorithm.ExecutionTimeChanged += new EventHandler(Algorithm_ExecutionTimeChanged);
232      algorithm.Paused += new EventHandler(Algorithm_Paused);
233      algorithm.Prepared += new EventHandler(Algorithm_Prepared);
234      algorithm.Started += new EventHandler(Algorithm_Started);
235      algorithm.Stopped += new EventHandler(Algorithm_Stopped);
236      algorithm.Runs.CollectionReset += new CollectionItemsChangedEventHandler<Run>(Algorithm_Runs_CollectionReset);
237      algorithm.Runs.ItemsAdded += new CollectionItemsChangedEventHandler<Run>(Algorithm_Runs_ItemsAdded);
238      algorithm.Runs.ItemsRemoved += new CollectionItemsChangedEventHandler<Run>(Algorithm_Runs_ItemsRemoved);
239    }
240    private void DeregisterAlgorithmEvents() {
241      algorithm.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Algorithm_ExceptionOccurred);
242      algorithm.ExecutionTimeChanged -= new EventHandler(Algorithm_ExecutionTimeChanged);
243      algorithm.Paused -= new EventHandler(Algorithm_Paused);
244      algorithm.Prepared -= new EventHandler(Algorithm_Prepared);
245      algorithm.Started -= new EventHandler(Algorithm_Started);
246      algorithm.Stopped -= new EventHandler(Algorithm_Stopped);
247      algorithm.Runs.CollectionReset -= new CollectionItemsChangedEventHandler<Run>(Algorithm_Runs_CollectionReset);
248      algorithm.Runs.ItemsAdded -= new CollectionItemsChangedEventHandler<Run>(Algorithm_Runs_ItemsAdded);
249      algorithm.Runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<Run>(Algorithm_Runs_ItemsRemoved);
250    }
251    private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
252      OnExceptionOccurred(e.Value);
253    }
254    private void Algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
255      OnExecutionTimeChanged();
256    }
257    private void Algorithm_Paused(object sender, EventArgs e) {
258      OnPaused();
259    }
260    private void Algorithm_Prepared(object sender, EventArgs e) {
261      if ((ExecutionState == ExecutionState.Paused) || (ExecutionState == ExecutionState.Stopped))
262        OnPrepared();
263    }
264    private void Algorithm_Started(object sender, EventArgs e) {
265      stopPending = false;
266      if (ExecutionState != ExecutionState.Started)
267        OnStarted();
268    }
269    private void Algorithm_Stopped(object sender, EventArgs e) {
270      ExecutionTime += Algorithm.ExecutionTime;
271
272      if (!stopPending && (runs.Count < repetitions)) {
273        Algorithm.Prepare();
274        Algorithm.Start();
275      } else {
276        OnStopped();
277      }
278    }
279    private void Algorithm_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<Run> e) {
280      Runs.RemoveRange(e.OldItems);
281      Runs.AddRange(e.Items);
282    }
283    private void Algorithm_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<Run> e) {
284      Runs.AddRange(e.Items);
285    }
286    private void Algorithm_Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Run> e) {
287      Runs.RemoveRange(e.Items);
288    }
289    #endregion
290  }
291}
Note: See TracBrowser for help on using the repository browser.