Free cookie consent management tool by TermsFeed Policy Generator

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

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

Continued work on algorithm batch processing (#947).

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