Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1233

  • moved ExperimentManager into separate plugin
  • moved Administration into separate plugin
File size: 6.9 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    }
69    public override IDeepCloneable Clone(Cloner cloner) {
70      return new OptimizerJob(this, cloner);
71    }
72
73    /// <summary>
74    /// Casts the Optimizer to an Experiment. Returns null if cast was not successfull.
75    /// </summary>
76    public Experiment OptimizerAsExperiment {
77      get { return Item as Experiment; }
78    }
79
80    /// <summary>
81    /// Casts the Optimizer to an BatchRun. Returns null if cast was not successfull.
82    /// </summary>
83    public BatchRun OptimizerAsBatchRun {
84      get { return Item as BatchRun; }
85    }
86
87    #region IJob Members
88
89    public override ExecutionState ExecutionState {
90      get { return Item.ExecutionState; }
91    }
92
93    public override TimeSpan ExecutionTime {
94      get { return Item.ExecutionTime; }
95    }
96
97    public override void Prepare() {
98      Item.Prepare();
99    }
100
101    public override void Start() {
102      if ((Item is Experiment && OptimizerAsExperiment.Optimizers.Count == 0) || // experiment would not fire OnStopped if it has 0 optimizers
103          (Item is BatchRun && OptimizerAsBatchRun.Optimizer == null)) { // batchrun would not fire OnStopped if algorithm == null
104        OnJobStopped();
105      } else {
106        Item.Start();
107      }
108    }
109
110    public override void Pause() {
111      Item.Pause();
112    }
113
114    public override void Stop() {
115      Item.Stop();
116    }
117
118    public override void Resume(IEnumerable<IJob> childJobs) {
119      OnJobStopped();
120    }
121    #endregion
122
123    #region Optimizer Events
124    protected override void RegisterItemEvents() {
125      base.RegisterItemEvents();
126      Item.Stopped += new EventHandler(optimizer_Stopped);
127      Item.Paused += new EventHandler(optimizer_Paused);
128      Item.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
129      Item.DescriptionChanged += new EventHandler(optimizer_DescriptionChanged);
130      Item.NameChanged += new EventHandler(optimizer_NameChanged);
131      Item.NameChanging += new EventHandler<CancelEventArgs<string>>(optimizer_NameChanging);
132      Item.ExecutionStateChanged += new EventHandler(optimizer_ExecutionStateChanged);
133      Item.ExecutionTimeChanged += new EventHandler(optimizer_ExecutionTimeChanged);
134      Item.Started += new EventHandler(optimizer_Started);
135    }
136
137    protected virtual void DeregisterOptimizerEvents() {
138      Item.Stopped -= new EventHandler(optimizer_Stopped);
139      Item.Paused -= new EventHandler(optimizer_Paused);
140      Item.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
141      Item.DescriptionChanged -= new EventHandler(optimizer_DescriptionChanged);
142      Item.NameChanged -= new EventHandler(optimizer_NameChanged);
143      Item.NameChanging -= new EventHandler<CancelEventArgs<string>>(optimizer_NameChanging);
144      Item.ExecutionStateChanged -= new EventHandler(optimizer_ExecutionStateChanged);
145      Item.ExecutionTimeChanged -= new EventHandler(optimizer_ExecutionTimeChanged);
146      Item.Started -= new EventHandler(optimizer_Started);
147      base.DeregisterItemEvents();
148    }
149
150    protected void optimizer_NameChanging(object sender, CancelEventArgs<string> e) {
151      this.OnNameChanging(e.Value, e.Cancel);
152    }
153
154    protected void optimizer_NameChanged(object sender, EventArgs e) {
155      this.OnNameChanged();
156    }
157
158    protected void optimizer_DescriptionChanged(object sender, EventArgs e) {
159      this.OnDescriptionChanged();
160    }
161
162    protected virtual void optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) {
163      OnJobFailed(e);
164    }
165
166    protected virtual void optimizer_Started(object sender, EventArgs e) {
167      OnJobStarted();
168    }
169
170    protected virtual void optimizer_Stopped(object sender, EventArgs e) {
171      OnJobStopped();
172    }
173
174    protected virtual void optimizer_Paused(object sender, EventArgs e) {
175      OnJobPaused();
176    }
177
178    protected virtual void optimizer_ExecutionTimeChanged(object sender, EventArgs e) {
179      OnExecutionTimeChanged();
180    }
181
182    protected virtual void optimizer_ExecutionStateChanged(object sender, EventArgs e) {
183      OnExecutionStateChanged();
184    }
185    #endregion
186
187    #region INamedItem Members
188    public override bool CanChangeDescription {
189      get { return Item.CanChangeDescription; }
190    }
191
192    public override bool CanChangeName {
193      get { return Item.CanChangeName; }
194    }
195
196    public override string Description {
197      get { return Item.Description; }
198      set { Item.Description = value; }
199    }
200
201    public override string Name {
202      get { return Item.Name; }
203      set { Item.Name = value; }
204    }
205    #endregion
206  }
207}
Note: See TracBrowser for help on using the repository browser.