1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Hive.JobBase;
|
---|
6 | using HeuristicLab.Optimization;
|
---|
7 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
8 | using HeuristicLab.Common;
|
---|
9 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Hive.Experiment.Jobs {
|
---|
12 | [StorableClass]
|
---|
13 | public class OptimizerJob : IJob {
|
---|
14 | [Storable]
|
---|
15 | protected IOptimizer optimizer;
|
---|
16 | public IOptimizer Optimizer {
|
---|
17 | get { return optimizer; }
|
---|
18 | set {
|
---|
19 | if (value != optimizer) {
|
---|
20 | if (optimizer != null) {
|
---|
21 | DeregisterEvents();
|
---|
22 | }
|
---|
23 | optimizer = value;
|
---|
24 | if (optimizer != null) {
|
---|
25 | RegisterEvents();
|
---|
26 | }
|
---|
27 | OnOptimizerChanged();
|
---|
28 | }
|
---|
29 | }
|
---|
30 | }
|
---|
31 |
|
---|
32 | [Storable]
|
---|
33 | protected ILog log;
|
---|
34 | public ILog Log {
|
---|
35 | get { return log; }
|
---|
36 | }
|
---|
37 |
|
---|
38 | [Storable]
|
---|
39 | protected bool computeInParallel;
|
---|
40 | public bool ComputeInParallel {
|
---|
41 | get { return computeInParallel; }
|
---|
42 | set {
|
---|
43 | if (computeInParallel != value) {
|
---|
44 | computeInParallel = value;
|
---|
45 | OnComputeInParallelChanged();
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | [Storable]
|
---|
51 | private int indexInParentOptimizerList = -1;
|
---|
52 | public int IndexInParentOptimizerList {
|
---|
53 | get { return indexInParentOptimizerList; }
|
---|
54 | set { this.indexInParentOptimizerList = value; }
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | [Storable]
|
---|
59 | private bool collectChildJobs;
|
---|
60 | public bool CollectChildJobs {
|
---|
61 | get { return collectChildJobs; }
|
---|
62 | set { collectChildJobs = value; }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public OptimizerJob() {
|
---|
66 | this.log = new Log();
|
---|
67 | }
|
---|
68 |
|
---|
69 | public OptimizerJob(IOptimizer optimizer)
|
---|
70 | : this() {
|
---|
71 | this.optimizer = optimizer;
|
---|
72 |
|
---|
73 | if (optimizer is Optimization.Experiment) {
|
---|
74 | this.ComputeInParallel = true;
|
---|
75 | } else if (optimizer is Optimization.BatchRun) {
|
---|
76 | this.ComputeInParallel = false;
|
---|
77 | } else {
|
---|
78 | this.ComputeInParallel = false;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | [StorableHook(HookType.AfterDeserialization)]
|
---|
83 | protected virtual void AfterDeserialization() {
|
---|
84 | RegisterEvents();
|
---|
85 | }
|
---|
86 |
|
---|
87 | /// <summary>
|
---|
88 | /// Casts the Optimizer to an Experiment. Returns null if cast was not successfull.
|
---|
89 | /// </summary>
|
---|
90 | public Optimization.Experiment OptimizerAsExperiment {
|
---|
91 | get { return Optimizer as Optimization.Experiment; }
|
---|
92 | }
|
---|
93 |
|
---|
94 | /// <summary>
|
---|
95 | /// Casts the Optimizer to an BatchRun. Returns null if cast was not successfull.
|
---|
96 | /// </summary>
|
---|
97 | public Optimization.BatchRun OptimizerAsBatchRun {
|
---|
98 | get { return Optimizer as Optimization.BatchRun; }
|
---|
99 | }
|
---|
100 |
|
---|
101 | #region IJob Members
|
---|
102 |
|
---|
103 | public virtual ExecutionState ExecutionState {
|
---|
104 | get { return optimizer.ExecutionState; }
|
---|
105 | }
|
---|
106 |
|
---|
107 | public TimeSpan ExecutionTime {
|
---|
108 | get { return optimizer.ExecutionTime; }
|
---|
109 | }
|
---|
110 |
|
---|
111 | public virtual void Run() {
|
---|
112 | throw new NotSupportedException();
|
---|
113 | }
|
---|
114 |
|
---|
115 | public virtual void Prepare() {
|
---|
116 | optimizer.Prepare();
|
---|
117 | }
|
---|
118 |
|
---|
119 | public virtual void Start() {
|
---|
120 | if ((optimizer is Optimization.Experiment && OptimizerAsExperiment.Optimizers.Count == 0) || // experiment would not fire OnStopped if it has 0 optimizers
|
---|
121 | (optimizer is Optimization.BatchRun && OptimizerAsBatchRun.Algorithm == null)) { // batchrun would not fire OnStopped if algorithm == null
|
---|
122 | OnJobStopped();
|
---|
123 | } else {
|
---|
124 | optimizer.Start();
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | public virtual void Stop() {
|
---|
129 | optimizer.Stop();
|
---|
130 | }
|
---|
131 |
|
---|
132 | public virtual void Resume(IEnumerable<IJob> childJobs) {
|
---|
133 | OnJobStopped();
|
---|
134 | }
|
---|
135 |
|
---|
136 | public event EventHandler JobStopped;
|
---|
137 | protected virtual void OnJobStopped() {
|
---|
138 | EventHandler handler = JobStopped;
|
---|
139 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
140 | }
|
---|
141 |
|
---|
142 | public event EventHandler JobFailed;
|
---|
143 | protected virtual void OnJobFailed(EventArgs<Exception> e) {
|
---|
144 | EventHandler handler = JobFailed;
|
---|
145 | if (handler != null) handler(this, e);
|
---|
146 | }
|
---|
147 |
|
---|
148 | public event EventHandler<EventArgs<IJob>> NewChildJob;
|
---|
149 | protected virtual void OnNewChildJob(IJob job) {
|
---|
150 | EventHandler<Common.EventArgs<IJob>> handler = NewChildJob;
|
---|
151 | if (handler != null) handler(this, new EventArgs<IJob>(job));
|
---|
152 | }
|
---|
153 |
|
---|
154 | public event EventHandler WaitForChildJobs;
|
---|
155 | protected virtual void OnWaitForChildJobs() {
|
---|
156 | EventHandler handler = WaitForChildJobs;
|
---|
157 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
158 | }
|
---|
159 |
|
---|
160 | public event EventHandler DeleteChildJobs;
|
---|
161 | protected virtual void OnDeleteChildJobs() {
|
---|
162 | EventHandler handler = DeleteChildJobs;
|
---|
163 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
164 | }
|
---|
165 |
|
---|
166 | public event EventHandler ComputeInParallelChanged;
|
---|
167 | protected virtual void OnComputeInParallelChanged() {
|
---|
168 | EventHandler handler = ComputeInParallelChanged;
|
---|
169 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
170 | }
|
---|
171 |
|
---|
172 | public event EventHandler OptimizerChanged;
|
---|
173 | protected virtual void OnOptimizerChanged() {
|
---|
174 | EventHandler handler = OptimizerChanged;
|
---|
175 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
176 | }
|
---|
177 | #endregion
|
---|
178 |
|
---|
179 | #region Events
|
---|
180 | protected virtual void RegisterEvents() {
|
---|
181 | optimizer.Stopped += new EventHandler(optimizer_Stopped);
|
---|
182 | optimizer.ExceptionOccurred += new EventHandler<Common.EventArgs<Exception>>(optimizer_ExceptionOccurred);
|
---|
183 | optimizer.DescriptionChanged += this.DescriptionChanged;
|
---|
184 | optimizer.FilenameChanged += this.FilenameChanged;
|
---|
185 | optimizer.ItemImageChanged += this.ItemImageChanged;
|
---|
186 | optimizer.NameChanged += this.NameChanged;
|
---|
187 | optimizer.NameChanging += this.NameChanging;
|
---|
188 | optimizer.ToStringChanged += this.ToStringChanged;
|
---|
189 | }
|
---|
190 |
|
---|
191 | protected virtual void DeregisterEvents() {
|
---|
192 | optimizer.Stopped -= new EventHandler(optimizer_Stopped);
|
---|
193 | optimizer.ExceptionOccurred -= new EventHandler<Common.EventArgs<Exception>>(optimizer_ExceptionOccurred);
|
---|
194 | optimizer.DescriptionChanged -= this.DescriptionChanged;
|
---|
195 | optimizer.FilenameChanged -= this.FilenameChanged;
|
---|
196 | optimizer.ItemImageChanged -= this.ItemImageChanged;
|
---|
197 | optimizer.NameChanged -= this.NameChanged;
|
---|
198 | optimizer.NameChanging -= this.NameChanging;
|
---|
199 | optimizer.ToStringChanged -= this.ToStringChanged;
|
---|
200 | }
|
---|
201 |
|
---|
202 | protected virtual void optimizer_ExceptionOccurred(object sender, Common.EventArgs<Exception> e) {
|
---|
203 | OnJobFailed(e);
|
---|
204 | }
|
---|
205 |
|
---|
206 | protected virtual void optimizer_Stopped(object sender, EventArgs e) {
|
---|
207 | OnJobStopped();
|
---|
208 | }
|
---|
209 | #endregion
|
---|
210 |
|
---|
211 |
|
---|
212 | #region INamedItem Members
|
---|
213 |
|
---|
214 | public bool CanChangeDescription {
|
---|
215 | get { return optimizer.CanChangeDescription; }
|
---|
216 | }
|
---|
217 |
|
---|
218 | public bool CanChangeName {
|
---|
219 | get { return optimizer.CanChangeName; }
|
---|
220 | }
|
---|
221 |
|
---|
222 | public string Description {
|
---|
223 | get { return optimizer.Description; }
|
---|
224 | set { optimizer.Description = value; }
|
---|
225 | }
|
---|
226 | public event EventHandler DescriptionChanged;
|
---|
227 |
|
---|
228 | public string Name {
|
---|
229 | get { return optimizer.Name; }
|
---|
230 | set { optimizer.Name = value; }
|
---|
231 | }
|
---|
232 | public event EventHandler NameChanged;
|
---|
233 | public event EventHandler<Common.CancelEventArgs<string>> NameChanging;
|
---|
234 |
|
---|
235 | #endregion
|
---|
236 |
|
---|
237 | #region IItem Members
|
---|
238 |
|
---|
239 | public string ItemDescription {
|
---|
240 | get { return optimizer.ItemDescription; }
|
---|
241 | }
|
---|
242 |
|
---|
243 | public System.Drawing.Image ItemImage {
|
---|
244 | get { return optimizer.ItemImage; }
|
---|
245 | }
|
---|
246 |
|
---|
247 | public event EventHandler ItemImageChanged;
|
---|
248 | public string ItemName {
|
---|
249 | get { return optimizer.ItemName; }
|
---|
250 | }
|
---|
251 |
|
---|
252 | public Version ItemVersion {
|
---|
253 | get { return optimizer.ItemVersion; }
|
---|
254 | }
|
---|
255 | public event EventHandler ToStringChanged;
|
---|
256 |
|
---|
257 | #endregion
|
---|
258 |
|
---|
259 | #region IStorableContent Members
|
---|
260 |
|
---|
261 | public string Filename {
|
---|
262 | get;
|
---|
263 | set;
|
---|
264 | }
|
---|
265 | public event EventHandler FilenameChanged;
|
---|
266 |
|
---|
267 | #endregion
|
---|
268 |
|
---|
269 | #region IDeepCloneable Members
|
---|
270 |
|
---|
271 | public Common.IDeepCloneable Clone(Cloner cloner) {
|
---|
272 | OptimizerJob clone = (OptimizerJob)Activator.CreateInstance(this.GetType());
|
---|
273 | cloner.RegisterClonedObject(this, clone);
|
---|
274 | clone.Optimizer = (IOptimizer)cloner.Clone(this.Optimizer);
|
---|
275 | clone.log = (ILog)cloner.Clone(this.Log);
|
---|
276 | clone.ComputeInParallel = this.ComputeInParallel;
|
---|
277 | clone.IndexInParentOptimizerList = this.IndexInParentOptimizerList;
|
---|
278 | clone.CollectChildJobs = this.CollectChildJobs;
|
---|
279 | clone.RegisterEvents();
|
---|
280 | return clone;
|
---|
281 | }
|
---|
282 |
|
---|
283 | #endregion
|
---|
284 |
|
---|
285 | #region ICloneable Members
|
---|
286 |
|
---|
287 | public object Clone() {
|
---|
288 | return Clone(new Cloner());
|
---|
289 | }
|
---|
290 |
|
---|
291 | #endregion
|
---|
292 |
|
---|
293 | /// <summary>
|
---|
294 | /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
|
---|
295 | /// </summary>
|
---|
296 | /// <returns>The current instance as a string.</returns>
|
---|
297 | public override string ToString() {
|
---|
298 | return Name;
|
---|
299 | }
|
---|
300 |
|
---|
301 | public virtual bool IsParallelizable {
|
---|
302 | get { return this.Optimizer is Optimization.Experiment || this.Optimizer is BatchRun; }
|
---|
303 | }
|
---|
304 |
|
---|
305 | }
|
---|
306 | }
|
---|