Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17181 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

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