[6976] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16057] | 3 | * Copyright (C) 2002-2018 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 System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Clients.Hive.Jobs;
|
---|
| 26 | using HeuristicLab.Collections;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
[9895] | 28 | using HeuristicLab.MainForm;
|
---|
[6976] | 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.PluginInfrastructure;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Clients.Hive {
|
---|
| 33 | public class OptimizerHiveTask : HiveTask<OptimizerTask> {
|
---|
[9895] | 34 | public Progress Progress { get; private set; }
|
---|
| 35 |
|
---|
[6976] | 36 | #region Constructors and Cloning
|
---|
[9895] | 37 | public OptimizerHiveTask() {
|
---|
| 38 | Progress = new Progress();
|
---|
| 39 | }
|
---|
[6976] | 40 | public OptimizerHiveTask(IOptimizer optimizer)
|
---|
| 41 | : this() {
|
---|
| 42 | this.ItemTask = new OptimizerTask(optimizer);
|
---|
| 43 | }
|
---|
| 44 | public OptimizerHiveTask(OptimizerTask optimizerJob)
|
---|
| 45 | : this() {
|
---|
| 46 | this.ItemTask = optimizerJob;
|
---|
| 47 | }
|
---|
| 48 | protected OptimizerHiveTask(OptimizerHiveTask original, Cloner cloner)
|
---|
| 49 | : base(original, cloner) {
|
---|
[9895] | 50 | Progress = new Progress();
|
---|
[6976] | 51 | }
|
---|
| 52 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 53 | return new OptimizerHiveTask(this, cloner);
|
---|
| 54 | }
|
---|
| 55 | #endregion
|
---|
| 56 |
|
---|
| 57 | /// <summary>
|
---|
| 58 | /// if this.Optimizer is an experiment
|
---|
| 59 | /// Uses the child-optimizers of this.HiveTask and creates HiveTask-childs
|
---|
| 60 | /// if this.Optimizer is a batchrun
|
---|
| 61 | /// Creates a number of child-jobs according to repetitions
|
---|
| 62 | /// </summary>
|
---|
| 63 | protected override void UpdateChildHiveTasks() {
|
---|
| 64 | base.UpdateChildHiveTasks();
|
---|
[8939] | 65 | childHiveTasksLock.EnterWriteLock();
|
---|
| 66 | try {
|
---|
| 67 | if (Task != null && syncTasksWithOptimizers) {
|
---|
| 68 | if (!ItemTask.ComputeInParallel) {
|
---|
| 69 | this.childHiveTasks.Clear();
|
---|
| 70 | } else {
|
---|
| 71 | if (ItemTask.Item is Optimization.Experiment) {
|
---|
| 72 | Optimization.Experiment experiment = (Optimization.Experiment)ItemTask.Item;
|
---|
| 73 | foreach (IOptimizer childOpt in experiment.Optimizers) {
|
---|
| 74 | var optimizerHiveTask = new OptimizerHiveTask(childOpt);
|
---|
| 75 | optimizerHiveTask.Task.Priority = Task.Priority; //inherit priority from parent
|
---|
[7177] | 76 | this.childHiveTasks.Add(optimizerHiveTask);
|
---|
[6976] | 77 | }
|
---|
[8939] | 78 | } else if (ItemTask.Item is Optimization.BatchRun) {
|
---|
| 79 | Optimization.BatchRun batchRun = ItemTask.OptimizerAsBatchRun;
|
---|
| 80 | if (batchRun.Optimizer != null) {
|
---|
| 81 | while (this.childHiveTasks.Count < batchRun.Repetitions) {
|
---|
| 82 | var optimizerHiveTask = new OptimizerHiveTask(batchRun.Optimizer);
|
---|
| 83 | optimizerHiveTask.Task.Priority = Task.Priority;
|
---|
| 84 | this.childHiveTasks.Add(optimizerHiveTask);
|
---|
| 85 | }
|
---|
| 86 | while (this.childHiveTasks.Count > batchRun.Repetitions) {
|
---|
| 87 | this.childHiveTasks.Remove(this.childHiveTasks.Last());
|
---|
| 88 | }
|
---|
[6976] | 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
[8939] | 94 | finally {
|
---|
| 95 | childHiveTasksLock.ExitWriteLock();
|
---|
| 96 | }
|
---|
[6976] | 97 | }
|
---|
| 98 |
|
---|
| 99 | protected override void RegisterItemTaskEvents() {
|
---|
| 100 | base.RegisterItemTaskEvents();
|
---|
| 101 | if (ItemTask != null) {
|
---|
| 102 | if (ItemTask.Item is Optimization.Experiment) {
|
---|
| 103 | Optimization.Experiment experiment = ItemTask.OptimizerAsExperiment;
|
---|
| 104 | experiment.Optimizers.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded);
|
---|
| 105 | experiment.Optimizers.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced);
|
---|
| 106 | experiment.Optimizers.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved);
|
---|
| 107 | experiment.Optimizers.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset);
|
---|
| 108 | } else if (ItemTask.Item is Optimization.BatchRun) {
|
---|
| 109 | Optimization.BatchRun batchRun = ItemTask.OptimizerAsBatchRun;
|
---|
| 110 | batchRun.RepetitionsChanged += new EventHandler(batchRun_RepetitionsChanged);
|
---|
| 111 | batchRun.OptimizerChanged += new EventHandler(batchRun_OptimizerChanged);
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
[9219] | 115 | protected override void DeregisterItemTaskEvents() {
|
---|
| 116 | base.DeregisterItemTaskEvents();
|
---|
[6976] | 117 | if (ItemTask != null) {
|
---|
| 118 | if (ItemTask.Item is Optimization.Experiment) {
|
---|
| 119 | Optimization.Experiment experiment = ItemTask.OptimizerAsExperiment;
|
---|
| 120 | experiment.Optimizers.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded);
|
---|
| 121 | experiment.Optimizers.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced);
|
---|
| 122 | experiment.Optimizers.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved);
|
---|
| 123 | experiment.Optimizers.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset);
|
---|
| 124 | } else if (ItemTask.Item is Optimization.BatchRun) {
|
---|
| 125 | Optimization.BatchRun batchRun = ItemTask.OptimizerAsBatchRun;
|
---|
| 126 | batchRun.RepetitionsChanged -= new EventHandler(batchRun_RepetitionsChanged);
|
---|
| 127 | batchRun.OptimizerChanged -= new EventHandler(batchRun_OptimizerChanged);
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | private void batchRun_OptimizerChanged(object sender, EventArgs e) {
|
---|
| 133 | if (syncTasksWithOptimizers) {
|
---|
| 134 | UpdateChildHiveTasks();
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | private void batchRun_RepetitionsChanged(object sender, EventArgs e) {
|
---|
| 139 | if (syncTasksWithOptimizers) {
|
---|
| 140 | UpdateChildHiveTasks();
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | private void Optimizers_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
|
---|
| 145 | if (syncTasksWithOptimizers && this.ItemTask.ComputeInParallel) {
|
---|
| 146 | childHiveTasksLock.EnterWriteLock();
|
---|
| 147 | try {
|
---|
| 148 | foreach (var item in e.Items) {
|
---|
| 149 | if (GetChildByOptimizer(item.Value) == null && item.Value.Name != "Placeholder") {
|
---|
| 150 | this.childHiveTasks.Add(new OptimizerHiveTask(item.Value));
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
[7222] | 153 | }
|
---|
| 154 | finally { childHiveTasksLock.ExitWriteLock(); }
|
---|
[6976] | 155 | }
|
---|
| 156 | }
|
---|
| 157 | private void Optimizers_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
|
---|
| 158 | if (syncTasksWithOptimizers && this.ItemTask.ComputeInParallel) {
|
---|
| 159 | childHiveTasksLock.EnterWriteLock();
|
---|
| 160 | try {
|
---|
| 161 | foreach (var item in e.OldItems) {
|
---|
| 162 | this.childHiveTasks.Remove(this.GetChildByOptimizer(item.Value));
|
---|
| 163 | }
|
---|
| 164 | foreach (var item in e.Items) {
|
---|
| 165 | if (GetChildByOptimizer(item.Value) == null && item.Value.Name != "Placeholder") {
|
---|
| 166 | this.childHiveTasks.Add(new OptimizerHiveTask(item.Value));
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
[7222] | 169 | }
|
---|
| 170 | finally { childHiveTasksLock.ExitWriteLock(); }
|
---|
[6976] | 171 | }
|
---|
| 172 | }
|
---|
| 173 | private void Optimizers_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
|
---|
| 174 | if (syncTasksWithOptimizers && this.ItemTask.ComputeInParallel) {
|
---|
| 175 | childHiveTasksLock.EnterWriteLock();
|
---|
| 176 | try {
|
---|
| 177 | foreach (var item in e.Items) {
|
---|
| 178 | this.childHiveTasks.Remove(this.GetChildByOptimizer(item.Value));
|
---|
| 179 | }
|
---|
[7222] | 180 | }
|
---|
| 181 | finally { childHiveTasksLock.ExitWriteLock(); }
|
---|
[6976] | 182 | }
|
---|
| 183 | }
|
---|
| 184 | private void Optimizers_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
|
---|
| 185 | if (syncTasksWithOptimizers && this.ItemTask.ComputeInParallel) {
|
---|
| 186 | childHiveTasksLock.EnterWriteLock();
|
---|
| 187 | try {
|
---|
| 188 | foreach (var item in e.Items) {
|
---|
| 189 | this.childHiveTasks.Remove(this.GetChildByOptimizer(item.Value));
|
---|
| 190 | }
|
---|
[7222] | 191 | }
|
---|
| 192 | finally { childHiveTasksLock.ExitWriteLock(); }
|
---|
[6976] | 193 | }
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | /// <summary>
|
---|
| 197 | /// if this.Optimizer is Experiment
|
---|
| 198 | /// replace the child-optimizer in the experiment
|
---|
| 199 | /// if this.Optimizer is BatchRun
|
---|
| 200 | /// add the runs from the optimizerTask to the batchrun and replace the Optimizer
|
---|
| 201 | /// </summary>
|
---|
[7218] | 202 | public override void IntegrateChild(ItemTask task, Guid childTaskId) {
|
---|
[6976] | 203 | var optimizerTask = (OptimizerTask)task;
|
---|
| 204 | syncTasksWithOptimizers = false; // don't sync with optimizers during this method
|
---|
| 205 |
|
---|
| 206 | if (this.ItemTask != null && this.ItemTask.Item != null) {
|
---|
| 207 | if (this.ItemTask.Item is Optimization.Experiment) {
|
---|
| 208 | UpdateOptimizerInExperiment(this.ItemTask.OptimizerAsExperiment, optimizerTask);
|
---|
| 209 | } else if (this.ItemTask.Item is Optimization.BatchRun) {
|
---|
| 210 | UpdateOptimizerInBatchRun(this.ItemTask.OptimizerAsBatchRun, optimizerTask);
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[7222] | 214 | IEnumerable<HiveTask> childs = this.ChildHiveTasks.Where(j => j.Task.Id == childTaskId);
|
---|
| 215 | //TODO: in very rare cases childs is empty. This shouldn't be the case and should be further investigated.
|
---|
| 216 | if (childs.Count() > 0) {
|
---|
| 217 | OptimizerHiveTask child = childs.First() as OptimizerHiveTask;
|
---|
| 218 |
|
---|
| 219 | if (child != null && !optimizerTask.ComputeInParallel) {
|
---|
[6976] | 220 | child.syncTasksWithOptimizers = false;
|
---|
| 221 | child.ItemTask = optimizerTask;
|
---|
| 222 | child.syncTasksWithOptimizers = true;
|
---|
| 223 | }
|
---|
[7222] | 224 | }
|
---|
| 225 |
|
---|
[6976] | 226 | syncTasksWithOptimizers = true;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | /// <summary>
|
---|
| 230 | /// Adds the runs from the optimizerTask to the batchrun and replaces the Optimizer
|
---|
| 231 | /// Sideeffect: the optimizerTask.Optimizer will be prepared (scopes are deleted and executionstate will be reset)
|
---|
| 232 | /// </summary>
|
---|
| 233 | private void UpdateOptimizerInBatchRun(BatchRun batchRun, OptimizerTask optimizerTask) {
|
---|
[8939] | 234 | itemTaskLock.EnterWriteLock();
|
---|
| 235 | try {
|
---|
[12041] | 236 | if (optimizerTask.Item is IAlgorithm) {
|
---|
| 237 | // only set the first optimizer as Optimizer. if every time the Optimizer would be set, the runs would be cleared each time
|
---|
| 238 | if (batchRun.Optimizer == null) {
|
---|
| 239 | batchRun.Optimizer = (IOptimizer)optimizerTask.Item.Clone();
|
---|
| 240 | batchRun.Optimizer.Runs.Clear();
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | foreach (IRun run in optimizerTask.Item.Runs) {
|
---|
[7782] | 244 | run.Name = GetNewRunName(run, batchRun.Runs);
|
---|
[12041] | 245 | batchRun.Optimizer.Runs.Add(run);
|
---|
| 246 | }
|
---|
| 247 | } else {
|
---|
| 248 | // only set the first optimizer as Optimizer. if every time the Optimizer would be set, the runs would be cleared each time
|
---|
| 249 | if (batchRun.Optimizer == null) {
|
---|
| 250 | batchRun.Optimizer = optimizerTask.Item;
|
---|
| 251 | }
|
---|
| 252 | foreach (IRun run in optimizerTask.Item.Runs) {
|
---|
| 253 | if (batchRun.Runs.Contains(run)) continue;
|
---|
| 254 | run.Name = GetNewRunName(run, batchRun.Runs);
|
---|
[7782] | 255 | batchRun.Runs.Add(run);
|
---|
| 256 | }
|
---|
[6976] | 257 | }
|
---|
| 258 | }
|
---|
[8939] | 259 | finally {
|
---|
| 260 | itemTaskLock.ExitWriteLock();
|
---|
| 261 | }
|
---|
[6976] | 262 | }
|
---|
| 263 |
|
---|
| 264 | /// <summary>
|
---|
| 265 | /// replace the child-optimizer in the experiment
|
---|
| 266 | /// Sideeffect: the optimizerTask.Optimizer will be prepared (scopes are deleted and executionstate will be reset)
|
---|
| 267 | /// </summary>
|
---|
| 268 | private void UpdateOptimizerInExperiment(Optimization.Experiment experiment, OptimizerTask optimizerTask) {
|
---|
[8939] | 269 | itemTaskLock.EnterWriteLock();
|
---|
| 270 | try {
|
---|
| 271 | if (optimizerTask.IndexInParentOptimizerList < 0)
|
---|
| 272 | throw new IndexOutOfRangeException("IndexInParentOptimizerList must be equal or greater than zero! The Task is invalid and the optimizer-tree cannot be reassembled.");
|
---|
[6976] | 273 |
|
---|
[8939] | 274 | while (experiment.Optimizers.Count < optimizerTask.IndexInParentOptimizerList) {
|
---|
| 275 | experiment.Optimizers.Add(new UserDefinedAlgorithm("Placeholder")); // add dummy-entries to Optimizers so that its possible to insert the optimizerTask at the correct position
|
---|
[6976] | 276 | }
|
---|
[8939] | 277 | if (experiment.Optimizers.Count < optimizerTask.IndexInParentOptimizerList + 1) {
|
---|
| 278 | experiment.Optimizers.Add(optimizerTask.Item);
|
---|
| 279 | } else {
|
---|
| 280 | // if ComputeInParallel==true, don't replace the optimizer (except it is still a Placeholder)
|
---|
| 281 | // this is because Jobs with ComputeInParallel get submitted to hive with their child-optimizers deleted
|
---|
| 282 | if (!optimizerTask.ComputeInParallel || experiment.Optimizers[optimizerTask.IndexInParentOptimizerList].Name == "Placeholder") {
|
---|
| 283 | experiment.Optimizers[optimizerTask.IndexInParentOptimizerList] = optimizerTask.Item;
|
---|
| 284 | }
|
---|
| 285 | }
|
---|
[6976] | 286 | }
|
---|
[8939] | 287 | finally {
|
---|
| 288 | itemTaskLock.ExitWriteLock();
|
---|
| 289 | }
|
---|
[6976] | 290 | }
|
---|
| 291 |
|
---|
| 292 | /// <summary>
|
---|
| 293 | /// Sets the IndexInParentOptimizerList property of the OptimizerJob
|
---|
| 294 | /// according to the position in the OptimizerList of the parentHiveTask.Task
|
---|
| 295 | /// Recursively updates all the child-jobs as well
|
---|
| 296 | /// </summary>
|
---|
| 297 | internal void SetIndexInParentOptimizerList(OptimizerHiveTask parentHiveTask) {
|
---|
| 298 | if (parentHiveTask != null) {
|
---|
| 299 | if (parentHiveTask.ItemTask.Item is Optimization.Experiment) {
|
---|
| 300 | this.ItemTask.IndexInParentOptimizerList = parentHiveTask.ItemTask.OptimizerAsExperiment.Optimizers.IndexOf(this.ItemTask.Item);
|
---|
| 301 | } else if (parentHiveTask.ItemTask.Item is Optimization.BatchRun) {
|
---|
| 302 | this.ItemTask.IndexInParentOptimizerList = 0;
|
---|
| 303 | } else {
|
---|
| 304 | throw new NotSupportedException("Only Experiment and BatchRuns are supported");
|
---|
| 305 | }
|
---|
| 306 | }
|
---|
| 307 | childHiveTasksLock.EnterReadLock();
|
---|
| 308 | try {
|
---|
| 309 | foreach (OptimizerHiveTask child in childHiveTasks) {
|
---|
| 310 | child.SetIndexInParentOptimizerList(this);
|
---|
| 311 | }
|
---|
[7222] | 312 | }
|
---|
| 313 | finally { childHiveTasksLock.ExitReadLock(); }
|
---|
[6976] | 314 | }
|
---|
| 315 |
|
---|
[7125] | 316 | public override void AddChildHiveTask(HiveTask hiveTask) {
|
---|
| 317 | base.AddChildHiveTask(hiveTask);
|
---|
[15121] | 318 | var optimizerHiveTask = (OptimizerHiveTask)hiveTask;
|
---|
[6976] | 319 | syncTasksWithOptimizers = false;
|
---|
[15121] | 320 | if (this.ItemTask != null && optimizerHiveTask.ItemTask != null) {
|
---|
| 321 | OptimizerTask optimizerTaskClone = (OptimizerTask)optimizerHiveTask.ItemTask.Clone();
|
---|
[6976] | 322 |
|
---|
| 323 | if (this.ItemTask.Item is Optimization.Experiment) {
|
---|
[15121] | 324 | if (!this.ItemTask.OptimizerAsExperiment.Optimizers.Contains(optimizerHiveTask.ItemTask.Item)) {
|
---|
| 325 | UpdateOptimizerInExperiment(this.ItemTask.OptimizerAsExperiment, optimizerHiveTask.ItemTask);
|
---|
[6976] | 326 | }
|
---|
| 327 | } else if (this.ItemTask.Item is Optimization.BatchRun) {
|
---|
[15121] | 328 | UpdateOptimizerInBatchRun(this.ItemTask.OptimizerAsBatchRun, optimizerHiveTask.ItemTask);
|
---|
[6976] | 329 | }
|
---|
| 330 |
|
---|
[15121] | 331 | optimizerHiveTask.syncTasksWithOptimizers = false;
|
---|
| 332 | optimizerHiveTask.ItemTask = optimizerTaskClone;
|
---|
| 333 | optimizerHiveTask.syncTasksWithOptimizers = true;
|
---|
[6976] | 334 | }
|
---|
| 335 | syncTasksWithOptimizers = true;
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | /// <summary>
|
---|
| 339 | /// Creates a TaskData object containing the Task and the IJob-Object as byte[]
|
---|
| 340 | /// </summary>
|
---|
| 341 | /// <param name="withoutChildOptimizers">
|
---|
| 342 | /// if true the Child-Optimizers will not be serialized (if the task contains an Experiment)
|
---|
| 343 | /// </param>
|
---|
| 344 | public override TaskData GetAsTaskData(bool withoutChildOptimizers, out List<IPluginDescription> plugins) {
|
---|
[12621] | 345 | if (ItemTask == null) {
|
---|
| 346 | plugins = new List<IPluginDescription>();
|
---|
[6976] | 347 | return null;
|
---|
[12621] | 348 | }
|
---|
[6976] | 349 |
|
---|
| 350 | IEnumerable<Type> usedTypes;
|
---|
| 351 | byte[] jobByteArray;
|
---|
[12621] | 352 | if (withoutChildOptimizers && ItemTask.Item is Optimization.Experiment) {
|
---|
| 353 | OptimizerTask clonedJob = (OptimizerTask)ItemTask.Clone(); // use a cloned task, so that the childHiveJob don't get confused
|
---|
[6976] | 354 | clonedJob.OptimizerAsExperiment.Optimizers.Clear();
|
---|
| 355 | jobByteArray = PersistenceUtil.Serialize(clonedJob, out usedTypes);
|
---|
[12621] | 356 | } else if (withoutChildOptimizers && ItemTask.Item is Optimization.BatchRun) {
|
---|
| 357 | OptimizerTask clonedJob = (OptimizerTask)ItemTask.Clone();
|
---|
[6976] | 358 | clonedJob.OptimizerAsBatchRun.Optimizer = null;
|
---|
| 359 | jobByteArray = PersistenceUtil.Serialize(clonedJob, out usedTypes);
|
---|
[12621] | 360 | } else if (ItemTask.Item is IAlgorithm) {
|
---|
| 361 | ((IAlgorithm)ItemTask.Item).StoreAlgorithmInEachRun = false; // avoid storing the algorithm in runs to reduce size
|
---|
| 362 | jobByteArray = PersistenceUtil.Serialize(ItemTask, out usedTypes);
|
---|
[6976] | 363 | } else {
|
---|
[12621] | 364 | jobByteArray = PersistenceUtil.Serialize(ItemTask, out usedTypes);
|
---|
[6976] | 365 | }
|
---|
| 366 |
|
---|
| 367 | TaskData jobData = new TaskData() { TaskId = task.Id, Data = jobByteArray };
|
---|
[12621] | 368 | plugins = PluginUtil.GetPluginsForTask(usedTypes, ItemTask);
|
---|
[6976] | 369 | return jobData;
|
---|
| 370 | }
|
---|
| 371 |
|
---|
[7218] | 372 | public OptimizerHiveTask GetChildByOptimizerTask(OptimizerTask optimizerTask) {
|
---|
[6976] | 373 | childHiveTasksLock.EnterReadLock();
|
---|
| 374 | try {
|
---|
| 375 | foreach (OptimizerHiveTask child in childHiveTasks) {
|
---|
[7218] | 376 | if (child.ItemTask == optimizerTask)
|
---|
[6976] | 377 | return child;
|
---|
| 378 | }
|
---|
| 379 | return null;
|
---|
[7222] | 380 | }
|
---|
| 381 | finally { childHiveTasksLock.ExitReadLock(); }
|
---|
[6976] | 382 | }
|
---|
| 383 |
|
---|
| 384 | public HiveTask<OptimizerTask> GetChildByOptimizer(IOptimizer optimizer) {
|
---|
| 385 | childHiveTasksLock.EnterReadLock();
|
---|
| 386 | try {
|
---|
| 387 | foreach (OptimizerHiveTask child in childHiveTasks) {
|
---|
| 388 | if (child.ItemTask.Item == optimizer)
|
---|
| 389 | return child;
|
---|
| 390 | }
|
---|
| 391 | return null;
|
---|
[7222] | 392 | }
|
---|
| 393 | finally { childHiveTasksLock.ExitReadLock(); }
|
---|
[6976] | 394 | }
|
---|
| 395 |
|
---|
[8939] | 396 | public void ExecuteReadActionOnItemTask(Action action) {
|
---|
| 397 | itemTaskLock.EnterReadLock();
|
---|
| 398 | try {
|
---|
| 399 | action();
|
---|
| 400 | }
|
---|
| 401 | finally {
|
---|
| 402 | itemTaskLock.ExitReadLock();
|
---|
| 403 | }
|
---|
| 404 | }
|
---|
| 405 |
|
---|
[6976] | 406 | #region Helpers
|
---|
| 407 | /// <summary>
|
---|
| 408 | /// Parses the run numbers out of runs and renames the run to the next number
|
---|
| 409 | /// </summary>
|
---|
| 410 | private static string GetNewRunName(IRun run, RunCollection runs) {
|
---|
| 411 | int idx = run.Name.IndexOf("Run ") + 4;
|
---|
| 412 |
|
---|
[7142] | 413 | if (idx == 3 || runs.Count == 0)
|
---|
[6976] | 414 | return run.Name;
|
---|
| 415 |
|
---|
| 416 | int maxRunNumber = int.MinValue;
|
---|
| 417 | foreach (IRun r in runs) {
|
---|
| 418 | int number = GetRunNumber(r.Name);
|
---|
| 419 | maxRunNumber = Math.Max(maxRunNumber, number);
|
---|
| 420 | }
|
---|
| 421 |
|
---|
| 422 | return run.Name.Substring(0, idx) + (maxRunNumber + 1).ToString();
|
---|
| 423 | }
|
---|
| 424 |
|
---|
| 425 | /// <summary>
|
---|
| 426 | /// Parses the number of a Run out of its name. Example "Genetic Algorithm Run 3" -> 3
|
---|
| 427 | /// </summary>
|
---|
| 428 | private static int GetRunNumber(string runName) {
|
---|
| 429 | int idx = runName.IndexOf("Run ") + 4;
|
---|
[7142] | 430 | if (idx == 3) {
|
---|
[6976] | 431 | return 0;
|
---|
| 432 | } else {
|
---|
| 433 | return int.Parse(runName.Substring(idx, runName.Length - idx));
|
---|
| 434 | }
|
---|
| 435 | }
|
---|
| 436 | #endregion
|
---|
| 437 | }
|
---|
| 438 | }
|
---|