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 |
|
---|
22 | using System;
|
---|
23 | using System.Drawing;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using HeuristicLab.Collections;
|
---|
28 |
|
---|
29 | namespace 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 {
|
---|
39 | if (ExecutionState == ExecutionState.Prepared) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutablePrepared;
|
---|
40 | else if (ExecutionState == ExecutionState.Started) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutableStarted;
|
---|
41 | else if (ExecutionState == ExecutionState.Paused) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutablePaused;
|
---|
42 | else if (ExecutionState == ExecutionState.Stopped) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutableStopped;
|
---|
43 | else return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event;
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | [Storable]
|
---|
48 | private ExecutionState executionState;
|
---|
49 | public ExecutionState ExecutionState {
|
---|
50 | get { return executionState; }
|
---|
51 | private set {
|
---|
52 | if (executionState != value) {
|
---|
53 | executionState = value;
|
---|
54 | OnExecutionStateChanged();
|
---|
55 | OnItemImageChanged();
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | [Storable]
|
---|
61 | private TimeSpan executionTime;
|
---|
62 | public TimeSpan ExecutionTime {
|
---|
63 | get {
|
---|
64 | if ((Algorithm != null) && (Algorithm.ExecutionState != ExecutionState.Stopped))
|
---|
65 | return executionTime + Algorithm.ExecutionTime;
|
---|
66 | else
|
---|
67 | return executionTime;
|
---|
68 | }
|
---|
69 | private set {
|
---|
70 | executionTime = value;
|
---|
71 | OnExecutionTimeChanged();
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | [Storable]
|
---|
76 | private IAlgorithm algorithm;
|
---|
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(true);
|
---|
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();
|
---|
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 | name = ItemName;
|
---|
115 | description = ItemDescription;
|
---|
116 | executionState = ExecutionState.Stopped;
|
---|
117 | executionTime = TimeSpan.Zero;
|
---|
118 | repetitions = 10;
|
---|
119 | runs = new RunCollection();
|
---|
120 | stopPending = false;
|
---|
121 | }
|
---|
122 | public BatchRun(string name)
|
---|
123 | : base(name) {
|
---|
124 | description = ItemDescription;
|
---|
125 | executionState = ExecutionState.Stopped;
|
---|
126 | executionTime = TimeSpan.Zero;
|
---|
127 | repetitions = 10;
|
---|
128 | runs = new RunCollection();
|
---|
129 | stopPending = false;
|
---|
130 | }
|
---|
131 | public BatchRun(string name, string description)
|
---|
132 | : base(name, description) {
|
---|
133 | executionState = ExecutionState.Stopped;
|
---|
134 | executionTime = TimeSpan.Zero;
|
---|
135 | repetitions = 10;
|
---|
136 | runs = new RunCollection();
|
---|
137 | stopPending = false;
|
---|
138 | }
|
---|
139 | [StorableConstructor]
|
---|
140 | private BatchRun(bool deserializing)
|
---|
141 | : base(deserializing) {
|
---|
142 | stopPending = false;
|
---|
143 | }
|
---|
144 |
|
---|
145 | [StorableHook(HookType.AfterDeserialization)]
|
---|
146 | private void Initialize() {
|
---|
147 | if (algorithm != null) RegisterAlgorithmEvents();
|
---|
148 | }
|
---|
149 |
|
---|
150 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
151 | if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
|
---|
152 | BatchRun clone = (BatchRun)base.Clone(cloner);
|
---|
153 | clone.executionState = executionState;
|
---|
154 | clone.executionTime = executionTime;
|
---|
155 | clone.algorithm = (IAlgorithm)cloner.Clone(algorithm);
|
---|
156 | clone.repetitions = repetitions;
|
---|
157 | clone.runs = (RunCollection)cloner.Clone(runs);
|
---|
158 | clone.stopPending = stopPending;
|
---|
159 | clone.Initialize();
|
---|
160 | return clone;
|
---|
161 | }
|
---|
162 |
|
---|
163 | public void Prepare() {
|
---|
164 | Prepare(false);
|
---|
165 | }
|
---|
166 | public void Prepare(bool clearRuns) {
|
---|
167 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
|
---|
168 | throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
|
---|
169 | if (Algorithm != null) {
|
---|
170 | if (clearRuns) {
|
---|
171 | ExecutionTime = TimeSpan.Zero;
|
---|
172 | runs.Clear();
|
---|
173 | }
|
---|
174 | Algorithm.Prepare(clearRuns);
|
---|
175 | }
|
---|
176 | }
|
---|
177 | public void Start() {
|
---|
178 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
|
---|
179 | throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
|
---|
180 | if (Algorithm != null) Algorithm.Start();
|
---|
181 | }
|
---|
182 | public void Pause() {
|
---|
183 | if (ExecutionState != ExecutionState.Started)
|
---|
184 | throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
|
---|
185 | if (Algorithm != null) Algorithm.Pause();
|
---|
186 | }
|
---|
187 | public void Stop() {
|
---|
188 | if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
|
---|
189 | throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
|
---|
190 | stopPending = true;
|
---|
191 | if (Algorithm != null) Algorithm.Stop();
|
---|
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 AlgorithmChanged;
|
---|
206 | private void OnAlgorithmChanged() {
|
---|
207 | EventHandler handler = AlgorithmChanged;
|
---|
208 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
209 | }
|
---|
210 | public event EventHandler RepetitionsChanged;
|
---|
211 | private void OnRepetitionsChanged() {
|
---|
212 | EventHandler handler = RepetitionsChanged;
|
---|
213 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
214 | }
|
---|
215 | public event EventHandler Prepared;
|
---|
216 | private void OnPrepared() {
|
---|
217 | ExecutionState = ExecutionState.Prepared;
|
---|
218 | EventHandler handler = Prepared;
|
---|
219 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
220 | }
|
---|
221 | public event EventHandler Started;
|
---|
222 | private void OnStarted() {
|
---|
223 | ExecutionState = ExecutionState.Started;
|
---|
224 | EventHandler handler = Started;
|
---|
225 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
226 | }
|
---|
227 | public event EventHandler Paused;
|
---|
228 | private void OnPaused() {
|
---|
229 | ExecutionState = ExecutionState.Paused;
|
---|
230 | EventHandler handler = Paused;
|
---|
231 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
232 | }
|
---|
233 | public event EventHandler Stopped;
|
---|
234 | private void OnStopped() {
|
---|
235 | ExecutionState = ExecutionState.Stopped;
|
---|
236 | EventHandler handler = Stopped;
|
---|
237 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
238 | }
|
---|
239 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
240 | private void OnExceptionOccurred(Exception exception) {
|
---|
241 | EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
|
---|
242 | if (handler != null) handler(this, new EventArgs<Exception>(exception));
|
---|
243 | }
|
---|
244 |
|
---|
245 | private void RegisterAlgorithmEvents() {
|
---|
246 | algorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Algorithm_ExceptionOccurred);
|
---|
247 | algorithm.ExecutionTimeChanged += new EventHandler(Algorithm_ExecutionTimeChanged);
|
---|
248 | algorithm.Paused += new EventHandler(Algorithm_Paused);
|
---|
249 | algorithm.Prepared += new EventHandler(Algorithm_Prepared);
|
---|
250 | algorithm.Started += new EventHandler(Algorithm_Started);
|
---|
251 | algorithm.Stopped += new EventHandler(Algorithm_Stopped);
|
---|
252 | algorithm.Runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Algorithm_Runs_CollectionReset);
|
---|
253 | algorithm.Runs.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Algorithm_Runs_ItemsAdded);
|
---|
254 | algorithm.Runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Algorithm_Runs_ItemsRemoved);
|
---|
255 | }
|
---|
256 | private void DeregisterAlgorithmEvents() {
|
---|
257 | algorithm.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Algorithm_ExceptionOccurred);
|
---|
258 | algorithm.ExecutionTimeChanged -= new EventHandler(Algorithm_ExecutionTimeChanged);
|
---|
259 | algorithm.Paused -= new EventHandler(Algorithm_Paused);
|
---|
260 | algorithm.Prepared -= new EventHandler(Algorithm_Prepared);
|
---|
261 | algorithm.Started -= new EventHandler(Algorithm_Started);
|
---|
262 | algorithm.Stopped -= new EventHandler(Algorithm_Stopped);
|
---|
263 | algorithm.Runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Algorithm_Runs_CollectionReset);
|
---|
264 | algorithm.Runs.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Algorithm_Runs_ItemsAdded);
|
---|
265 | algorithm.Runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Algorithm_Runs_ItemsRemoved);
|
---|
266 | }
|
---|
267 | private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
268 | OnExceptionOccurred(e.Value);
|
---|
269 | }
|
---|
270 | private void Algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
271 | OnExecutionTimeChanged();
|
---|
272 | }
|
---|
273 | private void Algorithm_Paused(object sender, EventArgs e) {
|
---|
274 | OnPaused();
|
---|
275 | }
|
---|
276 | private void Algorithm_Prepared(object sender, EventArgs e) {
|
---|
277 | if ((ExecutionState == ExecutionState.Paused) || (ExecutionState == ExecutionState.Stopped))
|
---|
278 | OnPrepared();
|
---|
279 | }
|
---|
280 | private void Algorithm_Started(object sender, EventArgs e) {
|
---|
281 | stopPending = false;
|
---|
282 | if (ExecutionState != ExecutionState.Started)
|
---|
283 | OnStarted();
|
---|
284 | }
|
---|
285 | private void Algorithm_Stopped(object sender, EventArgs e) {
|
---|
286 | ExecutionTime += Algorithm.ExecutionTime;
|
---|
287 |
|
---|
288 | if (!stopPending && (runs.Count < repetitions)) {
|
---|
289 | Algorithm.Prepare();
|
---|
290 | Algorithm.Start();
|
---|
291 | } else {
|
---|
292 | OnStopped();
|
---|
293 | }
|
---|
294 | }
|
---|
295 | private void Algorithm_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
296 | Runs.RemoveRange(e.OldItems);
|
---|
297 | Runs.AddRange(e.Items);
|
---|
298 | }
|
---|
299 | private void Algorithm_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
300 | Runs.AddRange(e.Items);
|
---|
301 | }
|
---|
302 | private void Algorithm_Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
303 | Runs.RemoveRange(e.Items);
|
---|
304 | }
|
---|
305 | #endregion
|
---|
306 | }
|
---|
307 | }
|
---|