[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]
|
---|
[6033] | 33 | public class OptimizerJob : ItemJob {
|
---|
[5958] | 34 | public override bool IsParallelizable {
|
---|
[6033] | 35 | get { return this.Item is Experiment || this.Item is BatchRun; }
|
---|
[5062] | 36 | }
|
---|
| 37 |
|
---|
[6033] | 38 | public new IOptimizer Item {
|
---|
| 39 | get { return (IOptimizer)base.Item; }
|
---|
| 40 | set { base.Item = value; }
|
---|
[4629] | 41 | }
|
---|
| 42 |
|
---|
| 43 | [Storable]
|
---|
| 44 | private int indexInParentOptimizerList = -1;
|
---|
| 45 | public int IndexInParentOptimizerList {
|
---|
| 46 | get { return indexInParentOptimizerList; }
|
---|
| 47 | set { this.indexInParentOptimizerList = value; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[6033] | 50 | public OptimizerJob() : base() { }
|
---|
[5363] | 51 | public OptimizerJob(IOptimizer optimizer)
|
---|
| 52 | : this() {
|
---|
[6033] | 53 | this.Item = optimizer;
|
---|
[4629] | 54 |
|
---|
[6033] | 55 | if (optimizer is Experiment) {
|
---|
[4629] | 56 | this.ComputeInParallel = true;
|
---|
[6033] | 57 | } else if (optimizer is BatchRun) {
|
---|
[4629] | 58 | this.ComputeInParallel = false;
|
---|
| 59 | } else {
|
---|
| 60 | this.ComputeInParallel = false;
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
[4796] | 63 | [StorableConstructor]
|
---|
| 64 | protected OptimizerJob(bool deserializing) { }
|
---|
[5363] | 65 | protected OptimizerJob(OptimizerJob original, Cloner cloner)
|
---|
| 66 | : base(original, cloner) {
|
---|
[4796] | 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 | }
|
---|
[4629] | 73 |
|
---|
| 74 | /// <summary>
|
---|
| 75 | /// Casts the Optimizer to an Experiment. Returns null if cast was not successfull.
|
---|
| 76 | /// </summary>
|
---|
[6033] | 77 | public Experiment OptimizerAsExperiment {
|
---|
| 78 | get { return Item as Experiment; }
|
---|
[4629] | 79 | }
|
---|
| 80 |
|
---|
| 81 | /// <summary>
|
---|
| 82 | /// Casts the Optimizer to an BatchRun. Returns null if cast was not successfull.
|
---|
| 83 | /// </summary>
|
---|
[6033] | 84 | public BatchRun OptimizerAsBatchRun {
|
---|
| 85 | get { return Item as BatchRun; }
|
---|
[4629] | 86 | }
|
---|
| 87 |
|
---|
| 88 | #region IJob Members
|
---|
| 89 |
|
---|
[5958] | 90 | public override ExecutionState ExecutionState {
|
---|
[6033] | 91 | get { return Item.ExecutionState; }
|
---|
[4629] | 92 | }
|
---|
| 93 |
|
---|
[5958] | 94 | public override TimeSpan ExecutionTime {
|
---|
[6033] | 95 | get { return Item.ExecutionTime; }
|
---|
[4629] | 96 | }
|
---|
| 97 |
|
---|
[5958] | 98 | public override void Prepare() {
|
---|
[6033] | 99 | Item.Prepare();
|
---|
[4629] | 100 | }
|
---|
| 101 |
|
---|
[5958] | 102 | public override void Start() {
|
---|
[6033] | 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
|
---|
[4629] | 105 | OnJobStopped();
|
---|
| 106 | } else {
|
---|
[6033] | 107 | Item.Start();
|
---|
[4629] | 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[5958] | 111 | public override void Pause() {
|
---|
[6033] | 112 | Item.Pause();
|
---|
[5062] | 113 | }
|
---|
| 114 |
|
---|
[5958] | 115 | public override void Stop() {
|
---|
[6033] | 116 | Item.Stop();
|
---|
[4629] | 117 | }
|
---|
| 118 |
|
---|
[5958] | 119 | public override void Resume(IEnumerable<IJob> childJobs) {
|
---|
[4629] | 120 | OnJobStopped();
|
---|
| 121 | }
|
---|
| 122 | #endregion
|
---|
| 123 |
|
---|
| 124 | #region Optimizer Events
|
---|
[6033] | 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);
|
---|
[4629] | 133 | }
|
---|
[5450] | 134 |
|
---|
[5958] | 135 | protected virtual void DeregisterOptimizerEvents() {
|
---|
[6033] | 136 | Item.Stopped -= new EventHandler(optimizer_Stopped);
|
---|
| 137 | Item.Paused -= new EventHandler(optimizer_Paused);
|
---|
| 138 | Item.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
|
---|
| 139 | Item.DescriptionChanged -= new EventHandler(optimizer_DescriptionChanged);
|
---|
| 140 | Item.NameChanged -= new EventHandler(optimizer_NameChanged);
|
---|
| 141 | Item.NameChanging -= new EventHandler<CancelEventArgs<string>>(optimizer_NameChanging);
|
---|
| 142 | base.DeregisterItemEvents();
|
---|
[4629] | 143 | }
|
---|
| 144 |
|
---|
[5958] | 145 | protected void optimizer_NameChanging(object sender, CancelEventArgs<string> e) {
|
---|
[4629] | 146 | this.OnNameChanging(e.Value, e.Cancel);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[5958] | 149 | protected void optimizer_NameChanged(object sender, EventArgs e) {
|
---|
[4629] | 150 | this.OnNameChanged();
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[5958] | 153 | protected void optimizer_DescriptionChanged(object sender, EventArgs e) {
|
---|
[4629] | 154 | this.OnDescriptionChanged();
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | protected virtual void optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
| 158 | OnJobFailed(e);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | protected virtual void optimizer_Stopped(object sender, EventArgs e) {
|
---|
| 162 | OnJobStopped();
|
---|
| 163 | }
|
---|
[5782] | 164 |
|
---|
| 165 | protected virtual void optimizer_Paused(object sender, EventArgs e) {
|
---|
| 166 | OnJobPaused();
|
---|
| 167 | }
|
---|
[4629] | 168 | #endregion
|
---|
| 169 |
|
---|
| 170 | #region INamedItem Members
|
---|
[5958] | 171 | public override bool CanChangeDescription {
|
---|
[6033] | 172 | get { return Item.CanChangeDescription; }
|
---|
[4629] | 173 | }
|
---|
| 174 |
|
---|
[5958] | 175 | public override bool CanChangeName {
|
---|
[6033] | 176 | get { return Item.CanChangeName; }
|
---|
[4629] | 177 | }
|
---|
| 178 |
|
---|
[5958] | 179 | public override string Description {
|
---|
[6033] | 180 | get { return Item.Description; }
|
---|
| 181 | set { Item.Description = value; }
|
---|
[4629] | 182 | }
|
---|
| 183 |
|
---|
[5958] | 184 | public override string Name {
|
---|
[6033] | 185 | get { return Item.Name; }
|
---|
| 186 | set { Item.Name = value; }
|
---|
[4629] | 187 | }
|
---|
| 188 | #endregion
|
---|
| 189 | }
|
---|
| 190 | }
|
---|