Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6372 was 6372, checked in by ascheibe, 13 years ago

#1233 changed year to 2011

File size: 7.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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;
27using HeuristicLab.Optimization;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Clients.Hive.Jobs {
31  [Item("Optimizer Job", "Represents Job which executes a IOptimizer object.")]
32  [StorableClass]
33  public class OptimizerJob : ItemJob {
34    public override bool IsParallelizable {
35      get { return this.Item is Experiment || this.Item is BatchRun; }
36    }
37
38    public new IOptimizer Item {
39      get { return (IOptimizer)base.Item; }
40      set { base.Item = value; }
41    }
42
43    [Storable]
44    private int indexInParentOptimizerList = -1;
45    public int IndexInParentOptimizerList {
46      get { return indexInParentOptimizerList; }
47      set { this.indexInParentOptimizerList = value; }
48    }
49
50    public OptimizerJob() : base() { }
51    public OptimizerJob(IOptimizer optimizer)
52      : this() {
53      this.Item = optimizer;
54
55      if (optimizer is Experiment) {
56        this.ComputeInParallel = true;
57      } else if (optimizer is BatchRun) {
58        this.ComputeInParallel = false;
59      } else {
60        this.ComputeInParallel = false;
61      }
62    }
63    [StorableConstructor]
64    protected OptimizerJob(bool deserializing) { }
65    protected OptimizerJob(OptimizerJob original, Cloner cloner)
66      : base(original, cloner) {
67      this.IndexInParentOptimizerList = original.IndexInParentOptimizerList;
68      this.CollectChildJobs = original.CollectChildJobs;
69    }
70    public override IDeepCloneable Clone(Cloner cloner) {
71      return new OptimizerJob(this, cloner);
72    }
73
74    /// <summary>
75    /// Casts the Optimizer to an Experiment. Returns null if cast was not successfull.
76    /// </summary>
77    public Experiment OptimizerAsExperiment {
78      get { return Item as Experiment; }
79    }
80
81    /// <summary>
82    /// Casts the Optimizer to an BatchRun. Returns null if cast was not successfull.
83    /// </summary>
84    public BatchRun OptimizerAsBatchRun {
85      get { return Item as BatchRun; }
86    }
87
88    #region IJob Members
89
90    public override ExecutionState ExecutionState {
91      get { return Item.ExecutionState; }
92    }
93
94    public override TimeSpan ExecutionTime {
95      get { return Item.ExecutionTime; }
96    }
97
98    public override void Prepare() {
99      Item.Prepare();
100    }
101
102    public override void Start() {
103      if ((Item is Experiment && OptimizerAsExperiment.Optimizers.Count == 0) || // experiment would not fire OnStopped if it has 0 optimizers
104          (Item is BatchRun && OptimizerAsBatchRun.Optimizer == null)) { // batchrun would not fire OnStopped if algorithm == null
105        OnJobStopped();
106      } else {
107        Item.Start();
108      }
109    }
110
111    public override void Pause() {
112      Item.Pause();
113    }
114
115    public override void Stop() {
116      Item.Stop();
117    }
118
119    public override void Resume(IEnumerable<IJob> childJobs) {
120      OnJobStopped();
121    }
122    #endregion
123
124    #region Optimizer Events
125    protected override void RegisterItemEvents() {
126      base.RegisterItemEvents();
127      Item.Stopped += new EventHandler(optimizer_Stopped);
128      Item.Paused += new EventHandler(optimizer_Paused);
129      Item.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
130      Item.DescriptionChanged += new EventHandler(optimizer_DescriptionChanged);
131      Item.NameChanged += new EventHandler(optimizer_NameChanged);
132      Item.NameChanging += new EventHandler<CancelEventArgs<string>>(optimizer_NameChanging);
133      Item.ExecutionStateChanged += new EventHandler(optimizer_ExecutionStateChanged);
134      Item.ExecutionTimeChanged += new EventHandler(optimizer_ExecutionTimeChanged);
135      Item.Started += new EventHandler(optimizer_Started);
136    }
137
138    protected virtual void DeregisterOptimizerEvents() {
139      Item.Stopped -= new EventHandler(optimizer_Stopped);
140      Item.Paused -= new EventHandler(optimizer_Paused);
141      Item.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
142      Item.DescriptionChanged -= new EventHandler(optimizer_DescriptionChanged);
143      Item.NameChanged -= new EventHandler(optimizer_NameChanged);
144      Item.NameChanging -= new EventHandler<CancelEventArgs<string>>(optimizer_NameChanging);
145      Item.ExecutionStateChanged -= new EventHandler(optimizer_ExecutionStateChanged);
146      Item.ExecutionTimeChanged -= new EventHandler(optimizer_ExecutionTimeChanged);
147      Item.Started -= new EventHandler(optimizer_Started);
148      base.DeregisterItemEvents();
149    }
150
151    protected void optimizer_NameChanging(object sender, CancelEventArgs<string> e) {
152      this.OnNameChanging(e.Value, e.Cancel);
153    }
154
155    protected void optimizer_NameChanged(object sender, EventArgs e) {
156      this.OnNameChanged();
157    }
158
159    protected void optimizer_DescriptionChanged(object sender, EventArgs e) {
160      this.OnDescriptionChanged();
161    }
162
163    protected virtual void optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) {
164      OnJobFailed(e);
165    }
166
167    protected virtual void optimizer_Started(object sender, EventArgs e) {
168      OnJobStarted();
169    }
170
171    protected virtual void optimizer_Stopped(object sender, EventArgs e) {
172      OnJobStopped();
173    }
174
175    protected virtual void optimizer_Paused(object sender, EventArgs e) {
176      OnJobPaused();
177    }
178
179    protected virtual void optimizer_ExecutionTimeChanged(object sender, EventArgs e) {
180      OnExecutionTimeChanged();
181    }
182
183    protected virtual void optimizer_ExecutionStateChanged(object sender, EventArgs e) {
184      OnExecutionStateChanged();
185    }
186    #endregion
187
188    #region INamedItem Members
189    public override bool CanChangeDescription {
190      get { return Item.CanChangeDescription; }
191    }
192
193    public override bool CanChangeName {
194      get { return Item.CanChangeName; }
195    }
196
197    public override string Description {
198      get { return Item.Description; }
199      set { Item.Description = value; }
200    }
201
202    public override string Name {
203      get { return Item.Name; }
204      set { Item.Name = value; }
205    }
206    #endregion
207  }
208}
Note: See TracBrowser for help on using the repository browser.