Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.Hive/3.3/HiveJobs/OptimizerHiveTask.cs @ 6976

Last change on this file since 6976 was 6976, checked in by ascheibe, 12 years ago

#1672 integrate the Hive client projects into trunk (Hive Job Manager and Administrator)

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