Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Clients.Hive/3.3/Tasks/OptimizerTask.cs @ 10138

Last change on this file since 10138 was 9886, checked in by mkommend, 11 years ago

#1099: Merged changesets for named item unit test into the stable branch.

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