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 @ 5132

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

#1260

  • added Prepare() after a job finished on slave to delete unnecessary executioncontexts and scopes
File size: 11.1 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.Stop();
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      optimizer.Prepare(); // nuke executioncontext and scopes to reduce memory footprint
170      EventHandler handler = JobStopped;
171      if (handler != null) handler(this, EventArgs.Empty);
172    }
173
174    public event EventHandler JobFailed;
175    protected virtual void OnJobFailed(EventArgs<Exception> e) {
176      EventHandler handler = JobFailed;
177      if (handler != null) handler(this, e);
178    }
179
180    public event EventHandler<EventArgs<IJob>> NewChildJob;
181    protected virtual void OnNewChildJob(IJob job) {
182      EventHandler<Common.EventArgs<IJob>> handler = NewChildJob;
183      if (handler != null) handler(this, new EventArgs<IJob>(job));
184    }
185
186    public event EventHandler WaitForChildJobs;
187    protected virtual void OnWaitForChildJobs() {
188      EventHandler handler = WaitForChildJobs;
189      if (handler != null) handler(this, EventArgs.Empty);
190    }
191
192    public event EventHandler DeleteChildJobs;
193    protected virtual void OnDeleteChildJobs() {
194      EventHandler handler = DeleteChildJobs;
195      if (handler != null) handler(this, EventArgs.Empty);
196    }
197
198    public event EventHandler ComputeInParallelChanged;
199    protected virtual void OnComputeInParallelChanged() {
200      EventHandler handler = ComputeInParallelChanged;
201      if (handler != null) handler(this, EventArgs.Empty);
202    }
203
204    public event EventHandler OptimizerChanged;
205    protected virtual void OnOptimizerChanged() {
206      EventHandler handler = OptimizerChanged;
207      if (handler != null) handler(this, EventArgs.Empty);
208    }
209    #endregion
210
211    #region Optimizer Events
212    protected virtual void RegisterEvents() {
213      optimizer.Stopped += new EventHandler(optimizer_Stopped);
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    protected virtual void DeregisterEvents() {
222      optimizer.Stopped -= new EventHandler(optimizer_Stopped);
223      optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
224      optimizer.DescriptionChanged -= this.DescriptionChanged;
225      optimizer.ItemImageChanged -= this.ItemImageChanged;
226      optimizer.NameChanged -= this.NameChanged;
227      optimizer.NameChanging -= this.NameChanging;
228      optimizer.ToStringChanged -= this.ToStringChanged;
229    }
230
231    void optimizer_ToStringChanged(object sender, EventArgs e) {
232      this.OnToStringChanged();
233    }
234
235    void optimizer_NameChanging(object sender, CancelEventArgs<string> e) {
236      this.OnNameChanging(e.Value, e.Cancel);
237    }
238
239    void optimizer_NameChanged(object sender, EventArgs e) {
240      this.OnNameChanged();
241    }
242
243    void optimizer_ItemImageChanged(object sender, EventArgs e) {
244      this.OnItemImageChanged();
245    }
246
247    void optimizer_DescriptionChanged(object sender, EventArgs e) {
248      this.OnDescriptionChanged();
249    }
250
251    protected virtual void optimizer_ExceptionOccurred(object sender, Common.EventArgs<Exception> e) {
252      OnJobFailed(e);
253    }
254
255    protected virtual void optimizer_Stopped(object sender, EventArgs e) {
256      OnJobStopped();
257    }
258    #endregion
259
260    #region INamedItem Members
261
262    public bool CanChangeDescription {
263      get { return optimizer.CanChangeDescription; }
264    }
265
266    public bool CanChangeName {
267      get { return optimizer.CanChangeName; }
268    }
269
270    public string Description {
271      get { return optimizer.Description; }
272      set { optimizer.Description = value; }
273    }
274
275    public string Name {
276      get { return optimizer.Name; }
277      set { optimizer.Name = value; }
278    }
279    #endregion
280
281    #region Events
282    public event EventHandler DescriptionChanged;
283    protected virtual void OnDescriptionChanged() {
284      var handler = DescriptionChanged;
285      if (handler != null) handler(this, EventArgs.Empty);
286    }
287    public event EventHandler ItemImageChanged;
288    protected virtual void OnItemImageChanged() {
289      var handler = ItemImageChanged;
290      if (handler != null) handler(this, EventArgs.Empty);
291    }
292    public event EventHandler ToStringChanged;
293    protected virtual void OnToStringChanged() {
294      var handler = ToStringChanged;
295      if (handler != null) handler(this, EventArgs.Empty);
296    }
297    public event EventHandler NameChanged;
298    protected virtual void OnNameChanged() {
299      var handler = NameChanged;
300      if (handler != null) handler(this, EventArgs.Empty);
301    }
302    public event EventHandler<CancelEventArgs<string>> NameChanging;
303    protected virtual void OnNameChanging(string value, bool cancel) {
304      var handler = NameChanging;
305      if (handler != null) handler(this, new CancelEventArgs<string>(value, cancel));
306    }
307    #endregion
308
309    #region IItem Members
310
311    public string ItemDescription {
312      get { return optimizer.ItemDescription; }
313    }
314
315    public System.Drawing.Image ItemImage {
316      get { return optimizer.ItemImage; }
317    }
318
319    public string ItemName {
320      get { return optimizer.ItemName; }
321    }
322
323    public Version ItemVersion {
324      get { return optimizer.ItemVersion; }
325    }
326
327    #endregion
328
329    /// <summary>
330    /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
331    /// </summary>
332    /// <returns>The current instance as a string.</returns>
333    public override string ToString() {
334      return Name;
335    }
336
337    public virtual bool IsParallelizable {
338      get { return this.Optimizer is Optimization.Experiment || this.Optimizer is BatchRun; }
339    }
340
341  }
342}
Note: See TracBrowser for help on using the repository browser.