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 System.Linq;
|
---|
25 | using HeuristicLab.Collections;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Optimization {
|
---|
31 | /// <summary>
|
---|
32 | /// An experiment which contains multiple batch runs of algorithms.
|
---|
33 | /// </summary>
|
---|
34 | [Item("Experiment", "An experiment which contains multiple batch runs of algorithms.")]
|
---|
35 | [Creatable("Testing & Analysis")]
|
---|
36 | [StorableClass]
|
---|
37 | public sealed class Experiment : NamedItem, IOptimizer {
|
---|
38 | public override Image ItemImage {
|
---|
39 | get {
|
---|
40 | if (ExecutionState == ExecutionState.Prepared) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutablePrepared;
|
---|
41 | else if (ExecutionState == ExecutionState.Started) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutableStarted;
|
---|
42 | else if (ExecutionState == ExecutionState.Paused) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutablePaused;
|
---|
43 | else if (ExecutionState == ExecutionState.Stopped) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutableStopped;
|
---|
44 | else return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event;
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | [Storable]
|
---|
49 | private ExecutionState executionState;
|
---|
50 | public ExecutionState ExecutionState {
|
---|
51 | get { return executionState; }
|
---|
52 | private set {
|
---|
53 | if (executionState != value) {
|
---|
54 | executionState = value;
|
---|
55 | OnExecutionStateChanged();
|
---|
56 | OnItemImageChanged();
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | [Storable]
|
---|
62 | private TimeSpan executionTime;
|
---|
63 | public TimeSpan ExecutionTime {
|
---|
64 | get { return executionTime; }
|
---|
65 | private set {
|
---|
66 | executionTime = value;
|
---|
67 | OnExecutionTimeChanged();
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | [Storable]
|
---|
72 | private OptimizerList optimizers;
|
---|
73 | public OptimizerList Optimizers {
|
---|
74 | get { return optimizers; }
|
---|
75 | }
|
---|
76 |
|
---|
77 | [Storable]
|
---|
78 | private RunCollection runs;
|
---|
79 | public RunCollection Runs {
|
---|
80 | get { return runs; }
|
---|
81 | }
|
---|
82 |
|
---|
83 | private bool stopPending;
|
---|
84 |
|
---|
85 | public Experiment()
|
---|
86 | : base() {
|
---|
87 | name = ItemName;
|
---|
88 | description = ItemDescription;
|
---|
89 | executionState = ExecutionState.Stopped;
|
---|
90 | executionTime = TimeSpan.Zero;
|
---|
91 | optimizers = new OptimizerList();
|
---|
92 | runs = new RunCollection();
|
---|
93 | stopPending = false;
|
---|
94 | Initialize();
|
---|
95 | }
|
---|
96 | public Experiment(string name)
|
---|
97 | : base(name) {
|
---|
98 | description = ItemDescription;
|
---|
99 | executionState = ExecutionState.Stopped;
|
---|
100 | executionTime = TimeSpan.Zero;
|
---|
101 | optimizers = new OptimizerList();
|
---|
102 | runs = new RunCollection();
|
---|
103 | stopPending = false;
|
---|
104 | Initialize();
|
---|
105 | }
|
---|
106 | public Experiment(string name, string description)
|
---|
107 | : base(name, description) {
|
---|
108 | executionState = ExecutionState.Stopped;
|
---|
109 | executionTime = TimeSpan.Zero;
|
---|
110 | optimizers = new OptimizerList();
|
---|
111 | runs = new RunCollection();
|
---|
112 | stopPending = false;
|
---|
113 | Initialize();
|
---|
114 | }
|
---|
115 | [StorableConstructor]
|
---|
116 | private Experiment(bool deserializing)
|
---|
117 | : base(deserializing) {
|
---|
118 | stopPending = false;
|
---|
119 | }
|
---|
120 |
|
---|
121 | [StorableHook(HookType.AfterDeserialization)]
|
---|
122 | private void Initialize() {
|
---|
123 | RegisterOptimizersEvents();
|
---|
124 | foreach (IOptimizer optimizer in optimizers)
|
---|
125 | RegisterOptimizerEvents(optimizer);
|
---|
126 | }
|
---|
127 |
|
---|
128 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
129 | if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
|
---|
130 | Experiment clone = (Experiment)base.Clone(cloner);
|
---|
131 | clone.executionState = executionState;
|
---|
132 | clone.executionTime = executionTime;
|
---|
133 | clone.optimizers = (OptimizerList)cloner.Clone(optimizers);
|
---|
134 | clone.runs = (RunCollection)cloner.Clone(runs);
|
---|
135 | clone.stopPending = stopPending;
|
---|
136 | clone.Initialize();
|
---|
137 | return clone;
|
---|
138 | }
|
---|
139 |
|
---|
140 | public void Prepare() {
|
---|
141 | Prepare(false);
|
---|
142 | }
|
---|
143 | public void Prepare(bool clearRuns) {
|
---|
144 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
|
---|
145 | throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
|
---|
146 | if (Optimizers.Count > 0) {
|
---|
147 | if (clearRuns) runs.Clear();
|
---|
148 | foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started))
|
---|
149 | optimizer.Prepare(clearRuns);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | public void Start() {
|
---|
153 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
|
---|
154 | throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
|
---|
155 | stopPending = false;
|
---|
156 | if (Optimizers.Count > 0) {
|
---|
157 | IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused));
|
---|
158 | if (optimizer != null) optimizer.Start();
|
---|
159 | }
|
---|
160 | }
|
---|
161 | public void Pause() {
|
---|
162 | if (ExecutionState != ExecutionState.Started)
|
---|
163 | throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
|
---|
164 | if (Optimizers.Count > 0) {
|
---|
165 | foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started))
|
---|
166 | optimizer.Pause();
|
---|
167 | }
|
---|
168 | }
|
---|
169 | public void Stop() {
|
---|
170 | if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
|
---|
171 | throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
|
---|
172 | stopPending = true;
|
---|
173 | if (Optimizers.Count > 0) {
|
---|
174 | foreach (IOptimizer optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused)))
|
---|
175 | optimizer.Stop();
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | #region Events
|
---|
180 | public event EventHandler ExecutionStateChanged;
|
---|
181 | private void OnExecutionStateChanged() {
|
---|
182 | EventHandler handler = ExecutionStateChanged;
|
---|
183 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
184 | }
|
---|
185 | public event EventHandler ExecutionTimeChanged;
|
---|
186 | private void OnExecutionTimeChanged() {
|
---|
187 | EventHandler handler = ExecutionTimeChanged;
|
---|
188 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
189 | }
|
---|
190 | public event EventHandler Prepared;
|
---|
191 | private void OnPrepared() {
|
---|
192 | ExecutionState = ExecutionState.Prepared;
|
---|
193 | EventHandler handler = Prepared;
|
---|
194 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
195 | }
|
---|
196 | public event EventHandler Started;
|
---|
197 | private void OnStarted() {
|
---|
198 | ExecutionState = ExecutionState.Started;
|
---|
199 | EventHandler handler = Started;
|
---|
200 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
201 | }
|
---|
202 | public event EventHandler Paused;
|
---|
203 | private void OnPaused() {
|
---|
204 | ExecutionState = ExecutionState.Paused;
|
---|
205 | EventHandler handler = Paused;
|
---|
206 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
207 | }
|
---|
208 | public event EventHandler Stopped;
|
---|
209 | private void OnStopped() {
|
---|
210 | ExecutionState = ExecutionState.Stopped;
|
---|
211 | EventHandler handler = Stopped;
|
---|
212 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
213 | }
|
---|
214 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
215 | private void OnExceptionOccurred(Exception exception) {
|
---|
216 | EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
|
---|
217 | if (handler != null) handler(this, new EventArgs<Exception>(exception));
|
---|
218 | }
|
---|
219 |
|
---|
220 | private void RegisterOptimizersEvents() {
|
---|
221 | Optimizers.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset);
|
---|
222 | Optimizers.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded);
|
---|
223 | Optimizers.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved);
|
---|
224 | Optimizers.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced);
|
---|
225 | }
|
---|
226 | private void DeregisterOptimizersEvents() {
|
---|
227 | Optimizers.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset);
|
---|
228 | Optimizers.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded);
|
---|
229 | Optimizers.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved);
|
---|
230 | Optimizers.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced);
|
---|
231 | }
|
---|
232 | private void Optimizers_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
|
---|
233 | foreach (IndexedItem<IOptimizer> item in e.OldItems) {
|
---|
234 | DeregisterOptimizerEvents(item.Value);
|
---|
235 | }
|
---|
236 | foreach (IndexedItem<IOptimizer> item in e.Items) {
|
---|
237 | RegisterOptimizerEvents(item.Value);
|
---|
238 | }
|
---|
239 | }
|
---|
240 | private void Optimizers_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
|
---|
241 | foreach (IndexedItem<IOptimizer> item in e.Items) {
|
---|
242 | RegisterOptimizerEvents(item.Value);
|
---|
243 | }
|
---|
244 | }
|
---|
245 | private void Optimizers_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
|
---|
246 | foreach (IndexedItem<IOptimizer> item in e.Items) {
|
---|
247 | DeregisterOptimizerEvents(item.Value);
|
---|
248 | }
|
---|
249 | }
|
---|
250 | private void Optimizers_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
|
---|
251 | foreach (IndexedItem<IOptimizer> item in e.OldItems) {
|
---|
252 | DeregisterOptimizerEvents(item.Value);
|
---|
253 | }
|
---|
254 | foreach (IndexedItem<IOptimizer> item in e.Items) {
|
---|
255 | RegisterOptimizerEvents(item.Value);
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | private void RegisterOptimizerEvents(IOptimizer optimizer) {
|
---|
260 | optimizer.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
|
---|
261 | optimizer.ExecutionTimeChanged += new EventHandler(optimizer_ExecutionTimeChanged);
|
---|
262 | optimizer.Paused += new EventHandler(optimizer_Paused);
|
---|
263 | optimizer.Prepared += new EventHandler(optimizer_Prepared);
|
---|
264 | optimizer.Started += new EventHandler(optimizer_Started);
|
---|
265 | optimizer.Stopped += new EventHandler(optimizer_Stopped);
|
---|
266 | optimizer.Runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_CollectionReset);
|
---|
267 | optimizer.Runs.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsAdded);
|
---|
268 | optimizer.Runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsRemoved);
|
---|
269 | }
|
---|
270 | private void DeregisterOptimizerEvents(IOptimizer optimizer) {
|
---|
271 | optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
|
---|
272 | optimizer.ExecutionTimeChanged -= new EventHandler(optimizer_ExecutionTimeChanged);
|
---|
273 | optimizer.Paused -= new EventHandler(optimizer_Paused);
|
---|
274 | optimizer.Prepared -= new EventHandler(optimizer_Prepared);
|
---|
275 | optimizer.Started -= new EventHandler(optimizer_Started);
|
---|
276 | optimizer.Stopped -= new EventHandler(optimizer_Stopped);
|
---|
277 | optimizer.Runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_CollectionReset);
|
---|
278 | optimizer.Runs.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsAdded);
|
---|
279 | optimizer.Runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsRemoved);
|
---|
280 | }
|
---|
281 | private void optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
282 | OnExceptionOccurred(e.Value);
|
---|
283 | }
|
---|
284 | private void optimizer_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
285 | ExecutionTime = Optimizers.Aggregate(TimeSpan.Zero, (t, o) => t + o.ExecutionTime);
|
---|
286 | }
|
---|
287 | private void optimizer_Paused(object sender, EventArgs e) {
|
---|
288 | if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started))
|
---|
289 | OnPaused();
|
---|
290 | }
|
---|
291 | private void optimizer_Prepared(object sender, EventArgs e) {
|
---|
292 | if (ExecutionState == ExecutionState.Stopped)
|
---|
293 | OnPrepared();
|
---|
294 | }
|
---|
295 | private void optimizer_Started(object sender, EventArgs e) {
|
---|
296 | if (ExecutionState != ExecutionState.Started)
|
---|
297 | OnStarted();
|
---|
298 | }
|
---|
299 | private void optimizer_Stopped(object sender, EventArgs e) {
|
---|
300 | if (!stopPending && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) {
|
---|
301 | Optimizers.First(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)).Start();
|
---|
302 | } else {
|
---|
303 | if (Optimizers.All(x => (x.ExecutionState != ExecutionState.Started) && (x.ExecutionState != ExecutionState.Paused))) {
|
---|
304 | stopPending = false;
|
---|
305 | OnStopped();
|
---|
306 | }
|
---|
307 | }
|
---|
308 | }
|
---|
309 | private void optimizer_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
310 | Runs.RemoveRange(e.OldItems);
|
---|
311 | Runs.AddRange(e.Items);
|
---|
312 | }
|
---|
313 | private void optimizer_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
314 | Runs.AddRange(e.Items);
|
---|
315 | }
|
---|
316 | private void optimizer_Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
317 | Runs.RemoveRange(e.Items);
|
---|
318 | }
|
---|
319 | #endregion
|
---|
320 | }
|
---|
321 | }
|
---|