[4629] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
[5363] | 26 | using HeuristicLab.Hive;
|
---|
[4629] | 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Clients.Hive.Jobs {
|
---|
[4796] | 31 | [Item("Optimizer Job", "Represents Job which executes a IOptimizer object.")]
|
---|
[4629] | 32 | [StorableClass]
|
---|
[4796] | 33 | public class OptimizerJob : DeepCloneable, IJob {
|
---|
[5062] | 34 | public virtual bool IsParallelizable {
|
---|
| 35 | get { return this.Optimizer is Optimization.Experiment || this.Optimizer is BatchRun; }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[4629] | 38 | [Storable]
|
---|
| 39 | protected IOptimizer optimizer;
|
---|
| 40 | public IOptimizer Optimizer {
|
---|
| 41 | get { return optimizer; }
|
---|
| 42 | set {
|
---|
| 43 | if (value != optimizer) {
|
---|
| 44 | if (optimizer != null) DeregisterEvents();
|
---|
| 45 | optimizer = value;
|
---|
| 46 | if (optimizer != null) RegisterEvents();
|
---|
| 47 | OnOptimizerChanged();
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | [Storable]
|
---|
| 53 | protected ILog log;
|
---|
| 54 | public ILog Log {
|
---|
| 55 | get { return log; }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | [Storable]
|
---|
| 59 | protected bool computeInParallel;
|
---|
| 60 | public bool ComputeInParallel {
|
---|
| 61 | get { return computeInParallel; }
|
---|
| 62 | set {
|
---|
| 63 | if (computeInParallel != value) {
|
---|
| 64 | computeInParallel = value;
|
---|
| 65 | OnComputeInParallelChanged();
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | [Storable]
|
---|
| 71 | private int indexInParentOptimizerList = -1;
|
---|
| 72 | public int IndexInParentOptimizerList {
|
---|
| 73 | get { return indexInParentOptimizerList; }
|
---|
| 74 | set { this.indexInParentOptimizerList = value; }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | [Storable]
|
---|
| 78 | private bool collectChildJobs;
|
---|
| 79 | public bool CollectChildJobs {
|
---|
| 80 | get { return collectChildJobs; }
|
---|
| 81 | set { collectChildJobs = value; }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | public OptimizerJob() {
|
---|
| 85 | this.log = new Log();
|
---|
| 86 | }
|
---|
[5363] | 87 | public OptimizerJob(IOptimizer optimizer)
|
---|
| 88 | : this() {
|
---|
[4629] | 89 | this.Optimizer = optimizer;
|
---|
| 90 |
|
---|
| 91 | if (optimizer is Optimization.Experiment) {
|
---|
| 92 | this.ComputeInParallel = true;
|
---|
| 93 | } else if (optimizer is Optimization.BatchRun) {
|
---|
| 94 | this.ComputeInParallel = false;
|
---|
| 95 | } else {
|
---|
| 96 | this.ComputeInParallel = false;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
[4796] | 99 | [StorableConstructor]
|
---|
| 100 | protected OptimizerJob(bool deserializing) { }
|
---|
[5363] | 101 | protected OptimizerJob(OptimizerJob original, Cloner cloner)
|
---|
| 102 | : base(original, cloner) {
|
---|
[4796] | 103 | this.Optimizer = cloner.Clone(original.Optimizer);
|
---|
| 104 | this.log = cloner.Clone(original.Log);
|
---|
| 105 | this.ComputeInParallel = original.ComputeInParallel;
|
---|
| 106 | this.IndexInParentOptimizerList = original.IndexInParentOptimizerList;
|
---|
| 107 | this.CollectChildJobs = original.CollectChildJobs;
|
---|
| 108 | this.RegisterEvents();
|
---|
| 109 | }
|
---|
| 110 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 111 | return new OptimizerJob(this, cloner);
|
---|
| 112 | }
|
---|
[4629] | 113 |
|
---|
| 114 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 115 | protected virtual void AfterDeserialization() {
|
---|
| 116 | RegisterEvents();
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | /// <summary>
|
---|
| 120 | /// Casts the Optimizer to an Experiment. Returns null if cast was not successfull.
|
---|
| 121 | /// </summary>
|
---|
| 122 | public Optimization.Experiment OptimizerAsExperiment {
|
---|
| 123 | get { return Optimizer as Optimization.Experiment; }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /// <summary>
|
---|
| 127 | /// Casts the Optimizer to an BatchRun. Returns null if cast was not successfull.
|
---|
| 128 | /// </summary>
|
---|
| 129 | public Optimization.BatchRun OptimizerAsBatchRun {
|
---|
| 130 | get { return Optimizer as Optimization.BatchRun; }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | #region IJob Members
|
---|
| 134 |
|
---|
| 135 | public virtual ExecutionState ExecutionState {
|
---|
| 136 | get { return optimizer.ExecutionState; }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | public TimeSpan ExecutionTime {
|
---|
| 140 | get { return optimizer.ExecutionTime; }
|
---|
| 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
|
---|
[5363] | 149 | (optimizer is Optimization.BatchRun && OptimizerAsBatchRun.Optimizer == null)) { // batchrun would not fire OnStopped if algorithm == null
|
---|
[4629] | 150 | OnJobStopped();
|
---|
| 151 | } else {
|
---|
| 152 | optimizer.Start();
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[5062] | 156 | public void Pause() {
|
---|
[5450] | 157 | optimizer.Pause();
|
---|
[5062] | 158 | }
|
---|
| 159 |
|
---|
[4629] | 160 | public virtual void Stop() {
|
---|
| 161 | optimizer.Stop();
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | public virtual void Resume(IEnumerable<IJob> childJobs) {
|
---|
| 165 | OnJobStopped();
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | public event EventHandler JobStopped;
|
---|
| 169 | protected virtual void OnJobStopped() {
|
---|
| 170 | EventHandler handler = JobStopped;
|
---|
| 171 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[5450] | 174 | public event EventHandler JobPaused;
|
---|
| 175 | protected void OnJobPaused(object sender, EventArgs e) {
|
---|
| 176 | EventHandler handler = JobPaused;
|
---|
| 177 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[4629] | 180 | public event EventHandler JobFailed;
|
---|
| 181 | protected virtual void OnJobFailed(EventArgs<Exception> e) {
|
---|
| 182 | EventHandler handler = JobFailed;
|
---|
| 183 | if (handler != null) handler(this, e);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | public event EventHandler<EventArgs<IJob>> NewChildJob;
|
---|
| 187 | protected virtual void OnNewChildJob(IJob job) {
|
---|
| 188 | EventHandler<EventArgs<IJob>> handler = NewChildJob;
|
---|
| 189 | if (handler != null) handler(this, new EventArgs<IJob>(job));
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | public event EventHandler WaitForChildJobs;
|
---|
| 193 | protected virtual void OnWaitForChildJobs() {
|
---|
| 194 | EventHandler handler = WaitForChildJobs;
|
---|
| 195 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | public event EventHandler DeleteChildJobs;
|
---|
| 199 | protected virtual void OnDeleteChildJobs() {
|
---|
| 200 | EventHandler handler = DeleteChildJobs;
|
---|
| 201 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | public event EventHandler ComputeInParallelChanged;
|
---|
| 205 | protected virtual void OnComputeInParallelChanged() {
|
---|
| 206 | EventHandler handler = ComputeInParallelChanged;
|
---|
| 207 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | public event EventHandler OptimizerChanged;
|
---|
| 211 | protected virtual void OnOptimizerChanged() {
|
---|
| 212 | EventHandler handler = OptimizerChanged;
|
---|
| 213 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 214 | }
|
---|
| 215 | #endregion
|
---|
| 216 |
|
---|
| 217 | #region Optimizer Events
|
---|
| 218 | protected virtual void RegisterEvents() {
|
---|
| 219 | optimizer.Stopped += new EventHandler(optimizer_Stopped);
|
---|
[5450] | 220 | optimizer.Paused += new EventHandler(OnJobPaused);
|
---|
[4629] | 221 | optimizer.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
|
---|
| 222 | optimizer.DescriptionChanged += new EventHandler(optimizer_DescriptionChanged);
|
---|
| 223 | optimizer.ItemImageChanged += new EventHandler(optimizer_ItemImageChanged);
|
---|
| 224 | optimizer.NameChanged += new EventHandler(optimizer_NameChanged);
|
---|
| 225 | optimizer.NameChanging += new EventHandler<CancelEventArgs<string>>(optimizer_NameChanging);
|
---|
| 226 | optimizer.ToStringChanged += new EventHandler(optimizer_ToStringChanged);
|
---|
| 227 | }
|
---|
[5450] | 228 |
|
---|
[4629] | 229 | protected virtual void DeregisterEvents() {
|
---|
| 230 | optimizer.Stopped -= new EventHandler(optimizer_Stopped);
|
---|
[5450] | 231 | optimizer.Paused -= new EventHandler(OnJobPaused);
|
---|
[4629] | 232 | optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
|
---|
| 233 | optimizer.DescriptionChanged -= this.DescriptionChanged;
|
---|
| 234 | optimizer.ItemImageChanged -= this.ItemImageChanged;
|
---|
| 235 | optimizer.NameChanged -= this.NameChanged;
|
---|
| 236 | optimizer.NameChanging -= this.NameChanging;
|
---|
| 237 | optimizer.ToStringChanged -= this.ToStringChanged;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | void optimizer_ToStringChanged(object sender, EventArgs e) {
|
---|
| 241 | this.OnToStringChanged();
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | void optimizer_NameChanging(object sender, CancelEventArgs<string> e) {
|
---|
| 245 | this.OnNameChanging(e.Value, e.Cancel);
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | void optimizer_NameChanged(object sender, EventArgs e) {
|
---|
| 249 | this.OnNameChanged();
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | void optimizer_ItemImageChanged(object sender, EventArgs e) {
|
---|
| 253 | this.OnItemImageChanged();
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | void optimizer_DescriptionChanged(object sender, EventArgs e) {
|
---|
| 257 | this.OnDescriptionChanged();
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | protected virtual void optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
| 261 | OnJobFailed(e);
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | protected virtual void optimizer_Stopped(object sender, EventArgs e) {
|
---|
| 265 | OnJobStopped();
|
---|
| 266 | }
|
---|
| 267 | #endregion
|
---|
| 268 |
|
---|
| 269 | #region INamedItem Members
|
---|
| 270 |
|
---|
| 271 | public bool CanChangeDescription {
|
---|
| 272 | get { return optimizer.CanChangeDescription; }
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | public bool CanChangeName {
|
---|
| 276 | get { return optimizer.CanChangeName; }
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | public string Description {
|
---|
| 280 | get { return optimizer.Description; }
|
---|
| 281 | set { optimizer.Description = value; }
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | public string Name {
|
---|
| 285 | get { return optimizer.Name; }
|
---|
| 286 | set { optimizer.Name = value; }
|
---|
| 287 | }
|
---|
| 288 | #endregion
|
---|
| 289 |
|
---|
| 290 | #region Events
|
---|
| 291 | public event EventHandler DescriptionChanged;
|
---|
| 292 | protected virtual void OnDescriptionChanged() {
|
---|
| 293 | var handler = DescriptionChanged;
|
---|
[5363] | 294 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[4629] | 295 | }
|
---|
| 296 | public event EventHandler ItemImageChanged;
|
---|
| 297 | protected virtual void OnItemImageChanged() {
|
---|
| 298 | var handler = ItemImageChanged;
|
---|
| 299 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 300 | }
|
---|
| 301 | public event EventHandler ToStringChanged;
|
---|
| 302 | protected virtual void OnToStringChanged() {
|
---|
| 303 | var handler = ToStringChanged;
|
---|
| 304 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 305 | }
|
---|
| 306 | public event EventHandler NameChanged;
|
---|
| 307 | protected virtual void OnNameChanged() {
|
---|
| 308 | var handler = NameChanged;
|
---|
| 309 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 310 | }
|
---|
| 311 | public event EventHandler<CancelEventArgs<string>> NameChanging;
|
---|
| 312 | protected virtual void OnNameChanging(string value, bool cancel) {
|
---|
| 313 | var handler = NameChanging;
|
---|
| 314 | if (handler != null) handler(this, new CancelEventArgs<string>(value, cancel));
|
---|
| 315 | }
|
---|
[5062] | 316 | public event EventHandler ExecutionTimeChanged;
|
---|
| 317 | protected virtual void OnExecutionTimeChanged() {
|
---|
| 318 | EventHandler handler = ExecutionTimeChanged;
|
---|
| 319 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 320 | }
|
---|
| 321 | public event EventHandler ExecutionStateChanged;
|
---|
| 322 | protected virtual void OnExecutionStateChanged() {
|
---|
| 323 | EventHandler handler = ExecutionStateChanged;
|
---|
| 324 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 325 | }
|
---|
[4629] | 326 | #endregion
|
---|
| 327 |
|
---|
| 328 | #region IItem Members
|
---|
| 329 |
|
---|
| 330 | public string ItemDescription {
|
---|
| 331 | get { return optimizer.ItemDescription; }
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | public System.Drawing.Image ItemImage {
|
---|
| 335 | get { return optimizer.ItemImage; }
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | public string ItemName {
|
---|
| 339 | get { return optimizer.ItemName; }
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | public Version ItemVersion {
|
---|
| 343 | get { return optimizer.ItemVersion; }
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | #endregion
|
---|
| 347 |
|
---|
| 348 | /// <summary>
|
---|
| 349 | /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
|
---|
| 350 | /// </summary>
|
---|
| 351 | /// <returns>The current instance as a string.</returns>
|
---|
| 352 | public override string ToString() {
|
---|
| 353 | return Name;
|
---|
| 354 | }
|
---|
| 355 | }
|
---|
| 356 | }
|
---|