[6976] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6976] | 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 HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Optimization;
|
---|
[17097] | 26 | using HEAL.Attic;
|
---|
[6976] | 27 |
|
---|
| 28 | namespace HeuristicLab.Clients.Hive.Jobs {
|
---|
| 29 | [Item("Optimizer Task", "Represents Task which executes a IOptimizer object.")]
|
---|
[17097] | 30 | [StorableType("9A59FAAF-3F5C-4629-B22D-714488DC2678")]
|
---|
[6976] | 31 | public class OptimizerTask : ItemTask {
|
---|
[11083] | 32 | public override HiveTask CreateHiveTask() {
|
---|
| 33 | return new OptimizerHiveTask(this);
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[6976] | 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 |
|
---|
[11083] | 52 | public OptimizerTask(IOptimizer optimizer)
|
---|
| 53 | : base(optimizer) {
|
---|
[6976] | 54 |
|
---|
[11083] | 55 | if (optimizer is Experiment || optimizer is BatchRun) {
|
---|
[6976] | 56 | this.ComputeInParallel = true;
|
---|
| 57 | } else {
|
---|
| 58 | this.ComputeInParallel = false;
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 | [StorableConstructor]
|
---|
[17097] | 62 | protected OptimizerTask(StorableConstructorFlag _) : base(_) { }
|
---|
[6976] | 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 |
|
---|
[15384] | 98 | public override async void Start() {
|
---|
[6976] | 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 {
|
---|
[17468] | 103 | try {
|
---|
| 104 | await Item.StartAsync();
|
---|
| 105 | } catch (Exception e) {
|
---|
| 106 | optimizer_ExceptionOccurred(Item, new EventArgs<Exception>(e));
|
---|
| 107 | }
|
---|
[6976] | 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | public override void Pause() {
|
---|
| 112 | Item.Pause();
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | public override void Stop() {
|
---|
| 116 | Item.Stop();
|
---|
| 117 | }
|
---|
| 118 | #endregion
|
---|
| 119 |
|
---|
| 120 | #region Optimizer Events
|
---|
| 121 | protected override void RegisterItemEvents() {
|
---|
| 122 | base.RegisterItemEvents();
|
---|
| 123 | Item.Stopped += new EventHandler(optimizer_Stopped);
|
---|
| 124 | Item.Paused += new EventHandler(optimizer_Paused);
|
---|
| 125 | Item.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
|
---|
| 126 | Item.DescriptionChanged += new EventHandler(optimizer_DescriptionChanged);
|
---|
| 127 | Item.NameChanged += new EventHandler(optimizer_NameChanged);
|
---|
| 128 | Item.NameChanging += new EventHandler<CancelEventArgs<string>>(optimizer_NameChanging);
|
---|
| 129 | Item.ExecutionStateChanged += new EventHandler(optimizer_ExecutionStateChanged);
|
---|
| 130 | Item.ExecutionTimeChanged += new EventHandler(optimizer_ExecutionTimeChanged);
|
---|
| 131 | Item.Started += new EventHandler(optimizer_Started);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | protected virtual void DeregisterOptimizerEvents() {
|
---|
| 135 | Item.Stopped -= new EventHandler(optimizer_Stopped);
|
---|
| 136 | Item.Paused -= new EventHandler(optimizer_Paused);
|
---|
| 137 | Item.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
|
---|
| 138 | Item.DescriptionChanged -= new EventHandler(optimizer_DescriptionChanged);
|
---|
| 139 | Item.NameChanged -= new EventHandler(optimizer_NameChanged);
|
---|
| 140 | Item.NameChanging -= new EventHandler<CancelEventArgs<string>>(optimizer_NameChanging);
|
---|
| 141 | Item.ExecutionStateChanged -= new EventHandler(optimizer_ExecutionStateChanged);
|
---|
| 142 | Item.ExecutionTimeChanged -= new EventHandler(optimizer_ExecutionTimeChanged);
|
---|
| 143 | Item.Started -= new EventHandler(optimizer_Started);
|
---|
| 144 | base.DeregisterItemEvents();
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | protected void optimizer_NameChanging(object sender, CancelEventArgs<string> e) {
|
---|
[7048] | 148 | OnNameChanging(new CancelEventArgs<string>(e.Value, e.Cancel));
|
---|
[6976] | 149 | }
|
---|
| 150 |
|
---|
| 151 | protected void optimizer_NameChanged(object sender, EventArgs e) {
|
---|
| 152 | this.OnNameChanged();
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | protected void optimizer_DescriptionChanged(object sender, EventArgs e) {
|
---|
| 156 | this.OnDescriptionChanged();
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | protected virtual void optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
| 160 | OnTaskFailed(e);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | protected virtual void optimizer_Started(object sender, EventArgs e) {
|
---|
| 164 | OnTaskStarted();
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | protected virtual void optimizer_Stopped(object sender, EventArgs e) {
|
---|
| 168 | OnTaskStopped();
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | protected virtual void optimizer_Paused(object sender, EventArgs e) {
|
---|
| 172 | OnTaskPaused();
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | protected virtual void optimizer_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
| 176 | OnExecutionTimeChanged();
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | protected virtual void optimizer_ExecutionStateChanged(object sender, EventArgs e) {
|
---|
| 180 | OnExecutionStateChanged();
|
---|
| 181 | }
|
---|
| 182 | #endregion
|
---|
| 183 |
|
---|
| 184 | #region INamedItem Members
|
---|
| 185 | public override bool CanChangeDescription {
|
---|
[7530] | 186 | get {
|
---|
| 187 | if (Item == null)
|
---|
| 188 | return false;
|
---|
| 189 | else
|
---|
| 190 | return Item.CanChangeDescription;
|
---|
| 191 | }
|
---|
[6976] | 192 | }
|
---|
| 193 |
|
---|
| 194 | public override bool CanChangeName {
|
---|
[7530] | 195 | get {
|
---|
| 196 | if (Item == null)
|
---|
| 197 | return false;
|
---|
| 198 | else
|
---|
| 199 | return Item.CanChangeName;
|
---|
| 200 | }
|
---|
[6976] | 201 | }
|
---|
| 202 |
|
---|
| 203 | public override string Description {
|
---|
[7530] | 204 | get {
|
---|
| 205 | if (Item == null)
|
---|
| 206 | return string.Empty;
|
---|
| 207 | else
|
---|
| 208 | return Item.Description;
|
---|
| 209 | }
|
---|
[6976] | 210 | set { Item.Description = value; }
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | public override string Name {
|
---|
[7530] | 214 | get {
|
---|
| 215 | if (Item == null)
|
---|
[9886] | 216 | return "Optimizer Task";
|
---|
[7530] | 217 | else
|
---|
| 218 | return Item.Name;
|
---|
| 219 | }
|
---|
[6976] | 220 | set { Item.Name = value; }
|
---|
| 221 | }
|
---|
| 222 | #endregion
|
---|
| 223 | }
|
---|
| 224 | }
|
---|