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

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

#1260

  • migrated to .NET 4.0
  • moved state-information about heartbeat timestamps into DB to reduce IIS-recycling issues
  • optimized memory usage of ExperimentManager when lots of large jobs are downloaded
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    [StorableHook(HookType.AfterDeserialization)]
111    protected virtual void AfterDeserialization() {
112      RegisterEvents();
113    }
114
115    /// <summary>
116    /// Casts the Optimizer to an Experiment. Returns null if cast was not successfull.
117    /// </summary>
118    public Optimization.Experiment OptimizerAsExperiment {
119      get { return Optimizer as Optimization.Experiment; }
120    }
121
122    /// <summary>
123    /// Casts the Optimizer to an BatchRun. Returns null if cast was not successfull.
124    /// </summary>
125    public Optimization.BatchRun OptimizerAsBatchRun {
126      get { return Optimizer as Optimization.BatchRun; }
127    }
128
129    #region IJob Members
130
131    public virtual ExecutionState ExecutionState {
132      get { return optimizer.ExecutionState; }
133    }
134
135    public TimeSpan ExecutionTime {
136      get { return optimizer.ExecutionTime; }
137    }
138
139    public virtual void Run() {
140      throw new NotSupportedException();
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.Algorithm == null)) { // batchrun would not fire OnStopped if algorithm == null
150        OnJobStopped();
151      } else {
152        optimizer.Start();
153      }
154    }
155
156    public virtual void Stop() {
157      optimizer.Pause();
158    }
159
160    public virtual void Resume(IEnumerable<IJob> childJobs) {
161      OnJobStopped();
162    }
163
164    public event EventHandler JobStopped;
165    protected virtual void OnJobStopped() {
166      EventHandler handler = JobStopped;
167      if (handler != null) handler(this, EventArgs.Empty);
168    }
169
170    public event EventHandler JobFailed;
171    protected virtual void OnJobFailed(EventArgs<Exception> e) {
172      EventHandler handler = JobFailed;
173      if (handler != null) handler(this, e);
174    }
175
176    public event EventHandler<EventArgs<IJob>> NewChildJob;
177    protected virtual void OnNewChildJob(IJob job) {
178      EventHandler<Common.EventArgs<IJob>> handler = NewChildJob;
179      if (handler != null) handler(this, new EventArgs<IJob>(job));
180    }
181
182    public event EventHandler WaitForChildJobs;
183    protected virtual void OnWaitForChildJobs() {
184      EventHandler handler = WaitForChildJobs;
185      if (handler != null) handler(this, EventArgs.Empty);
186    }
187
188    public event EventHandler DeleteChildJobs;
189    protected virtual void OnDeleteChildJobs() {
190      EventHandler handler = DeleteChildJobs;
191      if (handler != null) handler(this, EventArgs.Empty);
192    }
193
194    public event EventHandler ComputeInParallelChanged;
195    protected virtual void OnComputeInParallelChanged() {
196      EventHandler handler = ComputeInParallelChanged;
197      if (handler != null) handler(this, EventArgs.Empty);
198    }
199
200    public event EventHandler OptimizerChanged;
201    protected virtual void OnOptimizerChanged() {
202      EventHandler handler = OptimizerChanged;
203      if (handler != null) handler(this, EventArgs.Empty);
204    }
205    #endregion
206
207    #region Optimizer Events
208    protected virtual void RegisterEvents() {
209      optimizer.Stopped += new EventHandler(optimizer_Stopped);
210      optimizer.Paused += new EventHandler(optimizer_Paused);
211      optimizer.ExceptionOccurred += new EventHandler<Common.EventArgs<Exception>>(optimizer_ExceptionOccurred);
212      optimizer.DescriptionChanged += new EventHandler(optimizer_DescriptionChanged);
213      optimizer.ItemImageChanged += new EventHandler(optimizer_ItemImageChanged);
214      optimizer.NameChanged += new EventHandler(optimizer_NameChanged);
215      optimizer.NameChanging += new EventHandler<CancelEventArgs<string>>(optimizer_NameChanging);
216      optimizer.ToStringChanged += new EventHandler(optimizer_ToStringChanged);
217    }
218
219    protected virtual void DeregisterEvents() {
220      optimizer.Stopped -= new EventHandler(optimizer_Stopped);
221      optimizer.Paused -= new EventHandler(optimizer_Paused);
222      optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
223      optimizer.DescriptionChanged -= this.DescriptionChanged;
224      optimizer.ItemImageChanged -= this.ItemImageChanged;
225      optimizer.NameChanged -= this.NameChanged;
226      optimizer.NameChanging -= this.NameChanging;
227      optimizer.ToStringChanged -= this.ToStringChanged;
228    }
229
230    void optimizer_ToStringChanged(object sender, EventArgs e) {
231      this.OnToStringChanged();
232    }
233
234    void optimizer_NameChanging(object sender, CancelEventArgs<string> e) {
235      this.OnNameChanging(e.Value, e.Cancel);
236    }
237
238    void optimizer_NameChanged(object sender, EventArgs e) {
239      this.OnNameChanged();
240    }
241
242    void optimizer_ItemImageChanged(object sender, EventArgs e) {
243      this.OnItemImageChanged();
244    }
245
246    void optimizer_DescriptionChanged(object sender, EventArgs e) {
247      this.OnDescriptionChanged();
248    }
249
250    protected virtual void optimizer_ExceptionOccurred(object sender, Common.EventArgs<Exception> e) {
251      OnJobFailed(e);
252    }
253
254    protected virtual void optimizer_Stopped(object sender, EventArgs e) {
255      optimizer.Prepare(); // reduce memory consumption
256      OnJobStopped();
257    }
258
259    void optimizer_Paused(object sender, EventArgs e) {
260      OnJobStopped();
261    }
262    #endregion
263
264    #region INamedItem Members
265
266    public bool CanChangeDescription {
267      get { return optimizer.CanChangeDescription; }
268    }
269
270    public bool CanChangeName {
271      get { return optimizer.CanChangeName; }
272    }
273
274    public string Description {
275      get { return optimizer.Description; }
276      set { optimizer.Description = value; }
277    }
278
279    public string Name {
280      get { return optimizer.Name; }
281      set { optimizer.Name = value; }
282    }
283    #endregion
284
285    #region Events
286    public event EventHandler DescriptionChanged;
287    protected virtual void OnDescriptionChanged() {
288      var handler = DescriptionChanged;
289      if (handler != null) handler(this, EventArgs.Empty);
290    }
291    public event EventHandler ItemImageChanged;
292    protected virtual void OnItemImageChanged() {
293      var handler = ItemImageChanged;
294      if (handler != null) handler(this, EventArgs.Empty);
295    }
296    public event EventHandler ToStringChanged;
297    protected virtual void OnToStringChanged() {
298      var handler = ToStringChanged;
299      if (handler != null) handler(this, EventArgs.Empty);
300    }
301    public event EventHandler NameChanged;
302    protected virtual void OnNameChanged() {
303      var handler = NameChanged;
304      if (handler != null) handler(this, EventArgs.Empty);
305    }
306    public event EventHandler<CancelEventArgs<string>> NameChanging;
307    protected virtual void OnNameChanging(string value, bool cancel) {
308      var handler = NameChanging;
309      if (handler != null) handler(this, new CancelEventArgs<string>(value, cancel));
310    }
311    #endregion
312
313    #region IItem Members
314
315    public string ItemDescription {
316      get { return optimizer.ItemDescription; }
317    }
318
319    public System.Drawing.Image ItemImage {
320      get { return optimizer.ItemImage; }
321    }
322
323    public string ItemName {
324      get { return optimizer.ItemName; }
325    }
326
327    public Version ItemVersion {
328      get { return optimizer.ItemVersion; }
329    }
330
331    #endregion
332
333    /// <summary>
334    /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
335    /// </summary>
336    /// <returns>The current instance as a string.</returns>
337    public override string ToString() {
338      return Name;
339    }
340
341    public virtual bool IsParallelizable {
342      get { return this.Optimizer is Optimization.Experiment || this.Optimizer is BatchRun; }
343    }
344
345  }
346}
Note: See TracBrowser for help on using the repository browser.