Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1260

  • finished renaming of Hive.Experiment to Hive.ExperimentManager
  • renamed HiveClient to HiveExperimentManager
  • restructured menuitems in optimizer
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.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      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.ExceptionOccurred += new EventHandler<Common.EventArgs<Exception>>(optimizer_ExceptionOccurred);
214      optimizer.DescriptionChanged += new EventHandler(optimizer_DescriptionChanged);
215      optimizer.ItemImageChanged += new EventHandler(optimizer_ItemImageChanged);
216      optimizer.NameChanged += new EventHandler(optimizer_NameChanged);
217      optimizer.NameChanging += new EventHandler<CancelEventArgs<string>>(optimizer_NameChanging);
218      optimizer.ToStringChanged += new EventHandler(optimizer_ToStringChanged);
219    }
220    protected virtual void DeregisterEvents() {
221      optimizer.Stopped -= new EventHandler(optimizer_Stopped);
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      OnJobStopped();
256    }
257    #endregion
258
259    #region INamedItem Members
260
261    public bool CanChangeDescription {
262      get { return optimizer.CanChangeDescription; }
263    }
264
265    public bool CanChangeName {
266      get { return optimizer.CanChangeName; }
267    }
268
269    public string Description {
270      get { return optimizer.Description; }
271      set { optimizer.Description = value; }
272    }
273
274    public string Name {
275      get { return optimizer.Name; }
276      set { optimizer.Name = value; }
277    }
278    #endregion
279
280    #region Events
281    public event EventHandler DescriptionChanged;
282    protected virtual void OnDescriptionChanged() {
283      var handler = DescriptionChanged;
284      if (handler != null) handler(this, EventArgs.Empty);
285    }
286    public event EventHandler ItemImageChanged;
287    protected virtual void OnItemImageChanged() {
288      var handler = ItemImageChanged;
289      if (handler != null) handler(this, EventArgs.Empty);
290    }
291    public event EventHandler ToStringChanged;
292    protected virtual void OnToStringChanged() {
293      var handler = ToStringChanged;
294      if (handler != null) handler(this, EventArgs.Empty);
295    }
296    public event EventHandler NameChanged;
297    protected virtual void OnNameChanged() {
298      var handler = NameChanged;
299      if (handler != null) handler(this, EventArgs.Empty);
300    }
301    public event EventHandler<CancelEventArgs<string>> NameChanging;
302    protected virtual void OnNameChanging(string value, bool cancel) {
303      var handler = NameChanging;
304      if (handler != null) handler(this, new CancelEventArgs<string>(value, cancel));
305    }
306    #endregion
307
308    #region IItem Members
309
310    public string ItemDescription {
311      get { return optimizer.ItemDescription; }
312    }
313
314    public System.Drawing.Image ItemImage {
315      get { return optimizer.ItemImage; }
316    }
317
318    public string ItemName {
319      get { return optimizer.ItemName; }
320    }
321
322    public Version ItemVersion {
323      get { return optimizer.ItemVersion; }
324    }
325
326    #endregion
327
328    /// <summary>
329    /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
330    /// </summary>
331    /// <returns>The current instance as a string.</returns>
332    public override string ToString() {
333      return Name;
334    }
335
336    public virtual bool IsParallelizable {
337      get { return this.Optimizer is Optimization.Experiment || this.Optimizer is BatchRun; }
338    }
339
340  }
341}
Note: See TracBrowser for help on using the repository browser.