Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4424 was 4424, checked in by cneumuel, 14 years ago
  • Added and updated License Information in every file
  • Sort and remove usings in every file
  • Deleted obsolete DataAccess.ADOHelper
  • Deleted some obsolete files
File size: 9.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.JobBase;
27using HeuristicLab.Optimization;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Hive.Experiment.Jobs {
31  [StorableClass]
32  public class OptimizerJob : IJob {
33    [Storable]
34    protected IOptimizer optimizer;
35    public IOptimizer Optimizer {
36      get { return optimizer; }
37      set {
38        if (value != optimizer) {
39          if (optimizer != null) {
40            DeregisterEvents();
41          }
42          optimizer = value;
43          if (optimizer != null) {
44            RegisterEvents();
45          }
46          OnOptimizerChanged();
47        }
48      }
49    }
50
51    [Storable]
52    protected ILog log;
53    public ILog Log {
54      get { return log; }
55    }
56
57    [Storable]
58    protected bool computeInParallel;
59    public bool ComputeInParallel {
60      get { return computeInParallel; }
61      set {
62        if (computeInParallel != value) {
63          computeInParallel = value;
64          OnComputeInParallelChanged();
65        }
66      }
67    }
68
69    [Storable]
70    private int indexInParentOptimizerList = -1;
71    public int IndexInParentOptimizerList {
72      get { return indexInParentOptimizerList; }
73      set { this.indexInParentOptimizerList = value; }
74    }
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
88    public OptimizerJob(IOptimizer optimizer)
89      : this() {
90      this.optimizer = optimizer;
91
92      if (optimizer is Optimization.Experiment) {
93        this.ComputeInParallel = true;
94      } else if (optimizer is Optimization.BatchRun) {
95        this.ComputeInParallel = false;
96      } else {
97        this.ComputeInParallel = false;
98      }
99    }
100
101    [StorableHook(HookType.AfterDeserialization)]
102    protected virtual void AfterDeserialization() {
103      RegisterEvents();
104    }
105
106    /// <summary>
107    /// Casts the Optimizer to an Experiment. Returns null if cast was not successfull.
108    /// </summary>
109    public Optimization.Experiment OptimizerAsExperiment {
110      get { return Optimizer as Optimization.Experiment; }
111    }
112
113    /// <summary>
114    /// Casts the Optimizer to an BatchRun. Returns null if cast was not successfull.
115    /// </summary>
116    public Optimization.BatchRun OptimizerAsBatchRun {
117      get { return Optimizer as Optimization.BatchRun; }
118    }
119
120    #region IJob Members
121
122    public virtual ExecutionState ExecutionState {
123      get { return optimizer.ExecutionState; }
124    }
125
126    public TimeSpan ExecutionTime {
127      get { return optimizer.ExecutionTime; }
128    }
129
130    public virtual void Run() {
131      throw new NotSupportedException();
132    }
133
134    public virtual void Prepare() {
135      optimizer.Prepare();
136    }
137
138    public virtual void Start() {
139      if ((optimizer is Optimization.Experiment && OptimizerAsExperiment.Optimizers.Count == 0) || // experiment would not fire OnStopped if it has 0 optimizers
140          (optimizer is Optimization.BatchRun && OptimizerAsBatchRun.Algorithm == null)) { // batchrun would not fire OnStopped if algorithm == null
141        OnJobStopped();
142      } else {
143        optimizer.Start();
144      }
145    }
146
147    public virtual void Stop() {
148      optimizer.Stop();
149    }
150
151    public virtual void Resume(IEnumerable<IJob> childJobs) {
152      OnJobStopped();
153    }
154
155    public event EventHandler JobStopped;
156    protected virtual void OnJobStopped() {
157      EventHandler handler = JobStopped;
158      if (handler != null) handler(this, EventArgs.Empty);
159    }
160
161    public event EventHandler JobFailed;
162    protected virtual void OnJobFailed(EventArgs<Exception> e) {
163      EventHandler handler = JobFailed;
164      if (handler != null) handler(this, e);
165    }
166
167    public event EventHandler<EventArgs<IJob>> NewChildJob;
168    protected virtual void OnNewChildJob(IJob job) {
169      EventHandler<Common.EventArgs<IJob>> handler = NewChildJob;
170      if (handler != null) handler(this, new EventArgs<IJob>(job));
171    }
172
173    public event EventHandler WaitForChildJobs;
174    protected virtual void OnWaitForChildJobs() {
175      EventHandler handler = WaitForChildJobs;
176      if (handler != null) handler(this, EventArgs.Empty);
177    }
178
179    public event EventHandler DeleteChildJobs;
180    protected virtual void OnDeleteChildJobs() {
181      EventHandler handler = DeleteChildJobs;
182      if (handler != null) handler(this, EventArgs.Empty);
183    }
184
185    public event EventHandler ComputeInParallelChanged;
186    protected virtual void OnComputeInParallelChanged() {
187      EventHandler handler = ComputeInParallelChanged;
188      if (handler != null) handler(this, EventArgs.Empty);
189    }
190
191    public event EventHandler OptimizerChanged;
192    protected virtual void OnOptimizerChanged() {
193      EventHandler handler = OptimizerChanged;
194      if (handler != null) handler(this, EventArgs.Empty);
195    }
196    #endregion
197
198    #region Events
199    protected virtual void RegisterEvents() {
200      optimizer.Stopped += new EventHandler(optimizer_Stopped);
201      optimizer.ExceptionOccurred += new EventHandler<Common.EventArgs<Exception>>(optimizer_ExceptionOccurred);
202      optimizer.DescriptionChanged += this.DescriptionChanged;
203      optimizer.FilenameChanged += this.FilenameChanged;
204      optimizer.ItemImageChanged += this.ItemImageChanged;
205      optimizer.NameChanged += this.NameChanged;
206      optimizer.NameChanging += this.NameChanging;
207      optimizer.ToStringChanged += this.ToStringChanged;
208    }
209
210    protected virtual void DeregisterEvents() {
211      optimizer.Stopped -= new EventHandler(optimizer_Stopped);
212      optimizer.ExceptionOccurred -= new EventHandler<Common.EventArgs<Exception>>(optimizer_ExceptionOccurred);
213      optimizer.DescriptionChanged -= this.DescriptionChanged;
214      optimizer.FilenameChanged -= this.FilenameChanged;
215      optimizer.ItemImageChanged -= this.ItemImageChanged;
216      optimizer.NameChanged -= this.NameChanged;
217      optimizer.NameChanging -= this.NameChanging;
218      optimizer.ToStringChanged -= this.ToStringChanged;
219    }
220
221    protected virtual void optimizer_ExceptionOccurred(object sender, Common.EventArgs<Exception> e) {
222      OnJobFailed(e);
223    }
224
225    protected virtual void optimizer_Stopped(object sender, EventArgs e) {
226      OnJobStopped();
227    }
228    #endregion
229
230
231    #region INamedItem Members
232
233    public bool CanChangeDescription {
234      get { return optimizer.CanChangeDescription; }
235    }
236
237    public bool CanChangeName {
238      get { return optimizer.CanChangeName; }
239    }
240
241    public string Description {
242      get { return optimizer.Description; }
243      set { optimizer.Description = value; }
244    }
245    public event EventHandler DescriptionChanged;
246
247    public string Name {
248      get { return optimizer.Name; }
249      set { optimizer.Name = value; }
250    }
251    public event EventHandler NameChanged;
252    public event EventHandler<Common.CancelEventArgs<string>> NameChanging;
253
254    #endregion
255
256    #region IItem Members
257
258    public string ItemDescription {
259      get { return optimizer.ItemDescription; }
260    }
261
262    public System.Drawing.Image ItemImage {
263      get { return optimizer.ItemImage; }
264    }
265
266    public event EventHandler ItemImageChanged;
267    public string ItemName {
268      get { return optimizer.ItemName; }
269    }
270
271    public Version ItemVersion {
272      get { return optimizer.ItemVersion; }
273    }
274    public event EventHandler ToStringChanged;
275
276    #endregion
277
278    #region IStorableContent Members
279
280    public string Filename {
281      get;
282      set;
283    }
284    public event EventHandler FilenameChanged;
285
286    #endregion
287
288    #region IDeepCloneable Members
289
290    public Common.IDeepCloneable Clone(Cloner cloner) {
291      OptimizerJob clone = (OptimizerJob)Activator.CreateInstance(this.GetType());
292      cloner.RegisterClonedObject(this, clone);
293      clone.Optimizer = (IOptimizer)cloner.Clone(this.Optimizer);
294      clone.log = (ILog)cloner.Clone(this.Log);
295      clone.ComputeInParallel = this.ComputeInParallel;
296      clone.IndexInParentOptimizerList = this.IndexInParentOptimizerList;
297      clone.CollectChildJobs = this.CollectChildJobs;
298      clone.RegisterEvents();
299      return clone;
300    }
301
302    #endregion
303
304    #region ICloneable Members
305
306    public object Clone() {
307      return Clone(new Cloner());
308    }
309
310    #endregion
311
312    /// <summary>
313    /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
314    /// </summary>
315    /// <returns>The current instance as a string.</returns>
316    public override string ToString() {
317      return Name;
318    }
319
320    public virtual bool IsParallelizable {
321      get { return this.Optimizer is Optimization.Experiment || this.Optimizer is BatchRun; }
322    }
323
324  }
325}
Note: See TracBrowser for help on using the repository browser.