Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive/sources/HeuristicLab.Hive/HeuristicLab.Hive.Experiment/3.3/Jobs/OptimizerJob.cs @ 4557

Last change on this file since 4557 was 4557, checked in by cneumuel, 14 years ago
  • added presentation for heal stammtisch
  • fixed: wrong ExecutionState bug in HiveExperiment
  • fixed: not all results of a batchRun are downloaded
  • fixed: cannot create WindowHandle Exception because nesting level too high (by using ViewHost)
  • fixed: events are not properly routed by OptimizerJob

#1092

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