Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/Jobs/OptimizerJob.cs @ 5050

Last change on this file since 5050 was 4796, checked in by cneumuel, 14 years ago

#1233 applied new cloning mechanism

File size: 10.9 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.Optimization;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Hive;
29
30namespace HeuristicLab.Clients.Hive.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    [Storable]
74    private bool collectChildJobs;
75    public bool CollectChildJobs {
76      get { return collectChildJobs; }
77      set { collectChildJobs = value; }
78    }
79
80    public OptimizerJob() {
81      this.log = new Log();
82    }
83    public OptimizerJob(IOptimizer optimizer)
84      : this() {
85      this.Optimizer = optimizer;
86
87      if (optimizer is Optimization.Experiment) {
88        this.ComputeInParallel = true;
89      } else if (optimizer is Optimization.BatchRun) {
90        this.ComputeInParallel = false;
91      } else {
92        this.ComputeInParallel = false;
93      }
94    }
95    [StorableConstructor]
96    protected OptimizerJob(bool deserializing) { }
97    protected OptimizerJob(OptimizerJob original, Cloner cloner)
98      : base(original, cloner) {
99      this.Optimizer = cloner.Clone(original.Optimizer);
100      this.log = cloner.Clone(original.Log);
101      this.ComputeInParallel = original.ComputeInParallel;
102      this.IndexInParentOptimizerList = original.IndexInParentOptimizerList;
103      this.CollectChildJobs = original.CollectChildJobs;
104      this.RegisterEvents();
105    }
106    public override IDeepCloneable Clone(Cloner cloner) {
107      return new OptimizerJob(this, cloner);
108    }
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.Stop();
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<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.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
211      optimizer.DescriptionChanged += new EventHandler(optimizer_DescriptionChanged);
212      optimizer.ItemImageChanged += new EventHandler(optimizer_ItemImageChanged);
213      optimizer.NameChanged += new EventHandler(optimizer_NameChanged);
214      optimizer.NameChanging += new EventHandler<CancelEventArgs<string>>(optimizer_NameChanging);
215      optimizer.ToStringChanged += new EventHandler(optimizer_ToStringChanged);
216    }
217    protected virtual void DeregisterEvents() {
218      optimizer.Stopped -= new EventHandler(optimizer_Stopped);
219      optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
220      optimizer.DescriptionChanged -= this.DescriptionChanged;
221      optimizer.ItemImageChanged -= this.ItemImageChanged;
222      optimizer.NameChanged -= this.NameChanged;
223      optimizer.NameChanging -= this.NameChanging;
224      optimizer.ToStringChanged -= this.ToStringChanged;
225    }
226
227    void optimizer_ToStringChanged(object sender, EventArgs e) {
228      this.OnToStringChanged();
229    }
230
231    void optimizer_NameChanging(object sender, CancelEventArgs<string> e) {
232      this.OnNameChanging(e.Value, e.Cancel);
233    }
234
235    void optimizer_NameChanged(object sender, EventArgs e) {
236      this.OnNameChanged();
237    }
238
239    void optimizer_ItemImageChanged(object sender, EventArgs e) {
240      this.OnItemImageChanged();
241    }
242
243    void optimizer_DescriptionChanged(object sender, EventArgs e) {
244      this.OnDescriptionChanged();
245    }
246
247    protected virtual void optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) {
248      OnJobFailed(e);
249    }
250
251    protected virtual void optimizer_Stopped(object sender, EventArgs e) {
252      OnJobStopped();
253    }
254    #endregion
255
256    #region INamedItem Members
257
258    public bool CanChangeDescription {
259      get { return optimizer.CanChangeDescription; }
260    }
261
262    public bool CanChangeName {
263      get { return optimizer.CanChangeName; }
264    }
265
266    public string Description {
267      get { return optimizer.Description; }
268      set { optimizer.Description = value; }
269    }
270
271    public string Name {
272      get { return optimizer.Name; }
273      set { optimizer.Name = value; }
274    }
275    #endregion
276
277    #region Events
278    public event EventHandler DescriptionChanged;
279    protected virtual void OnDescriptionChanged() {
280      var handler = DescriptionChanged;
281      if(handler != null) handler(this, EventArgs.Empty);
282    }
283    public event EventHandler ItemImageChanged;
284    protected virtual void OnItemImageChanged() {
285      var handler = ItemImageChanged;
286      if (handler != null) handler(this, EventArgs.Empty);
287    }
288    public event EventHandler ToStringChanged;
289    protected virtual void OnToStringChanged() {
290      var handler = ToStringChanged;
291      if (handler != null) handler(this, EventArgs.Empty);
292    }
293    public event EventHandler NameChanged;
294    protected virtual void OnNameChanged() {
295      var handler = NameChanged;
296      if (handler != null) handler(this, EventArgs.Empty);
297    }
298    public event EventHandler<CancelEventArgs<string>> NameChanging;
299    protected virtual void OnNameChanging(string value, bool cancel) {
300      var handler = NameChanging;
301      if (handler != null) handler(this, new CancelEventArgs<string>(value, cancel));
302    }
303    #endregion
304
305    #region IItem Members
306
307    public string ItemDescription {
308      get { return optimizer.ItemDescription; }
309    }
310
311    public System.Drawing.Image ItemImage {
312      get { return optimizer.ItemImage; }
313    }
314
315    public string ItemName {
316      get { return optimizer.ItemName; }
317    }
318
319    public Version ItemVersion {
320      get { return optimizer.ItemVersion; }
321    }
322
323    #endregion
324
325    /// <summary>
326    /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
327    /// </summary>
328    /// <returns>The current instance as a string.</returns>
329    public override string ToString() {
330      return Name;
331    }
332
333    public virtual bool IsParallelizable {
334      get { return this.Optimizer is Optimization.Experiment || this.Optimizer is BatchRun; }
335    }
336
337  }
338}
Note: See TracBrowser for help on using the repository browser.