Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/Jobs/OptimizerJob.cs @ 5782

Last change on this file since 5782 was 5782, checked in by ascheibe, 13 years ago

#1233

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