Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1159 some minor cleanups

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