Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.3/sources/HeuristicLab.Hive/HeuristicLab.Hive.ExperimentManager/3.3/Jobs/OptimizerJob.cs @ 5153

Last change on this file since 5153 was 5153, checked in by cneumuel, 13 years ago

#1260

  • increased timeouts for sent jobs (which are needed if the jobs take long time to deserialize on slave)
  • added DeleteJob to ClientService
  • made optimizer actually Pause instead of Stop when stop is called explicitly (so they can be resumed later)
  • temporarily disabled job-abortion from server because it aborted jobs which took too long to deserialize on slaves (this issue needs to be investigated)
  • reduced locking of engines on slave so that the deserialization does not block heartbeats

#1347

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