Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.3/HiveJobs/OptimizerHiveJob.cs @ 6721

Last change on this file since 6721 was 6721, checked in by ascheibe, 13 years ago

#1233 Review comments: renamed Job to Task

File size: 17.1 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 OptimizerHiveJob : HiveJob<OptimizerJob> {
33
34    #region Constructors and Cloning
35    public OptimizerHiveJob() { }
36    public OptimizerHiveJob(IOptimizer optimizer)
37      : this() {
38      this.ItemJob = new OptimizerJob(optimizer);
39    }
40    public OptimizerHiveJob(OptimizerJob optimizerJob)
41      : this() {
42      this.ItemJob = optimizerJob;
43    }
44    protected OptimizerHiveJob(OptimizerHiveJob original, Cloner cloner)
45      : base(original, cloner) {
46    }
47    public override IDeepCloneable Clone(Cloner cloner) {
48      return new OptimizerHiveJob(this, cloner);
49    }
50    #endregion
51
52    /// <summary>
53    /// if this.Optimizer is an experiment
54    ///   Uses the child-optimizers of this.HiveJob and creates HiveJob-childs
55    /// if this.Optimizer is a batchrun
56    ///   Creates a number of child-jobs according to repetitions
57    /// </summary>
58    protected override void UpdateChildHiveJobs() {
59      base.UpdateChildHiveJobs();
60      if (Job != null && syncJobsWithOptimizers) {
61        if (!ItemJob.ComputeInParallel) {
62          this.childHiveJobs.Clear();
63        } else {
64          if (ItemJob.Item is Optimization.Experiment) {
65            Optimization.Experiment experiment = (Optimization.Experiment)ItemJob.Item;
66            foreach (IOptimizer childOpt in experiment.Optimizers) {
67              this.childHiveJobs.Add(new OptimizerHiveJob(childOpt));
68            }
69          } else if (ItemJob.Item is Optimization.BatchRun) {
70            Optimization.BatchRun batchRun = ItemJob.OptimizerAsBatchRun;
71            if (batchRun.Optimizer != null) {
72              while (this.childHiveJobs.Count < batchRun.Repetitions) {
73                this.childHiveJobs.Add(new OptimizerHiveJob(batchRun.Optimizer));
74              }
75              while (this.childHiveJobs.Count > batchRun.Repetitions) {
76                this.childHiveJobs.Remove(this.childHiveJobs.Last());
77              }
78            }
79          }
80        }
81      }
82    }
83
84    protected override void RegisterItemJobEvents() {
85      base.RegisterItemJobEvents();
86      if (ItemJob != null) {
87        if (ItemJob.Item is Optimization.Experiment) {
88          Optimization.Experiment experiment = ItemJob.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 (ItemJob.Item is Optimization.BatchRun) {
94          Optimization.BatchRun batchRun = ItemJob.OptimizerAsBatchRun;
95          batchRun.RepetitionsChanged += new EventHandler(batchRun_RepetitionsChanged);
96          batchRun.OptimizerChanged += new EventHandler(batchRun_OptimizerChanged);
97        }
98      }
99    }
100    protected override void DergisterItemJobEvents() {
101      base.DergisterItemJobEvents();
102      if (ItemJob != null) {
103        if (ItemJob.Item is Optimization.Experiment) {
104          Optimization.Experiment experiment = ItemJob.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 (ItemJob.Item is Optimization.BatchRun) {
110          Optimization.BatchRun batchRun = ItemJob.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 (syncJobsWithOptimizers) {
119        UpdateChildHiveJobs();
120      }
121    }
122
123    private void batchRun_RepetitionsChanged(object sender, EventArgs e) {
124      if (syncJobsWithOptimizers) {
125        UpdateChildHiveJobs();
126      }
127    }
128
129    private void Optimizers_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
130      if (syncJobsWithOptimizers && this.ItemJob.ComputeInParallel) {
131        childHiveJobsLock.EnterWriteLock();
132        try {
133          foreach (var item in e.Items) {
134            if (GetChildByOptimizer(item.Value) == null && item.Value.Name != "Placeholder") {
135              this.childHiveJobs.Add(new OptimizerHiveJob(item.Value));
136            }
137          }
138        }
139        finally { childHiveJobsLock.ExitWriteLock(); }
140      }
141    }
142    private void Optimizers_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
143      if (syncJobsWithOptimizers && this.ItemJob.ComputeInParallel) {
144        childHiveJobsLock.EnterWriteLock();
145        try {
146          foreach (var item in e.OldItems) {
147            this.childHiveJobs.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.childHiveJobs.Add(new OptimizerHiveJob(item.Value));
152            }
153          }
154        }
155        finally { childHiveJobsLock.ExitWriteLock(); }
156      }
157    }
158    private void Optimizers_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
159      if (syncJobsWithOptimizers && this.ItemJob.ComputeInParallel) {
160        childHiveJobsLock.EnterWriteLock();
161        try {
162          foreach (var item in e.Items) {
163            this.childHiveJobs.Remove(this.GetChildByOptimizer(item.Value));
164          }
165        }
166        finally { childHiveJobsLock.ExitWriteLock(); }
167      }
168    }
169    private void Optimizers_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
170      if (syncJobsWithOptimizers && this.ItemJob.ComputeInParallel) {
171        childHiveJobsLock.EnterWriteLock();
172        try {
173          foreach (var item in e.Items) {
174            this.childHiveJobs.Remove(this.GetChildByOptimizer(item.Value));
175          }
176        }
177        finally { childHiveJobsLock.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 optimizerJob to the batchrun and replace the Optimizer
186    /// </summary>
187    public override void IntegrateChild(ItemJob job, Guid childJobId) {
188      var optimizerJob = (OptimizerJob)job;
189      syncJobsWithOptimizers = false; // don't sync with optimizers during this method
190
191      if (this.ItemJob != null && this.ItemJob.Item != null) {
192        if (this.ItemJob.Item is Optimization.Experiment) {
193          UpdateOptimizerInExperiment(this.ItemJob.OptimizerAsExperiment, optimizerJob);
194        } else if (this.ItemJob.Item is Optimization.BatchRun) {
195          UpdateOptimizerInBatchRun(this.ItemJob.OptimizerAsBatchRun, optimizerJob);
196        }
197      }
198
199      childHiveJobsLock.EnterReadLock();
200      OptimizerHiveJob child = (OptimizerHiveJob)this.ChildHiveJobs.Single(j => j.Job.Id == childJobId);
201      try {
202        if (!optimizerJob.ComputeInParallel) {
203          child.syncJobsWithOptimizers = false;
204          child.ItemJob = optimizerJob;
205          child.syncJobsWithOptimizers = true;
206        }
207      }
208      finally { childHiveJobsLock.ExitReadLock(); }
209      syncJobsWithOptimizers = true;
210    }
211
212    /// <summary>
213    /// Adds the runs from the optimizerJob to the batchrun and replaces the Optimizer
214    /// Sideeffect: the optimizerJob.Optimizer will be prepared (scopes are deleted and executionstate will be reset)
215    /// </summary>
216    private void UpdateOptimizerInBatchRun(BatchRun batchRun, OptimizerJob optimizerJob) {
217      if (batchRun.Optimizer == null) {
218        batchRun.Optimizer = (IOptimizer)optimizerJob.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 optimizerJob.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 optimizerJob.Optimizer will be prepared (scopes are deleted and executionstate will be reset)
231    /// </summary>
232    private void UpdateOptimizerInExperiment(Optimization.Experiment experiment, OptimizerJob optimizerJob) {
233      if (optimizerJob.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 < optimizerJob.IndexInParentOptimizerList) {
237        experiment.Optimizers.Add(new UserDefinedAlgorithm("Placeholder")); // add dummy-entries to Optimizers so that its possible to insert the optimizerJob at the correct position
238      }
239      if (experiment.Optimizers.Count < optimizerJob.IndexInParentOptimizerList + 1) {
240        experiment.Optimizers.Add(optimizerJob.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 (!optimizerJob.ComputeInParallel || experiment.Optimizers[optimizerJob.IndexInParentOptimizerList].Name == "Placeholder") {
245          experiment.Optimizers[optimizerJob.IndexInParentOptimizerList] = optimizerJob.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 parentHiveJob.Task
253    /// Recursively updates all the child-jobs as well
254    /// </summary>
255    internal void SetIndexInParentOptimizerList(OptimizerHiveJob parentHiveJob) {
256      if (parentHiveJob != null) {
257        if (parentHiveJob.ItemJob.Item is Optimization.Experiment) {
258          this.ItemJob.IndexInParentOptimizerList = parentHiveJob.ItemJob.OptimizerAsExperiment.Optimizers.IndexOf(this.ItemJob.Item);
259        } else if (parentHiveJob.ItemJob.Item is Optimization.BatchRun) {
260          this.ItemJob.IndexInParentOptimizerList = 0;
261        } else {
262          throw new NotSupportedException("Only Experiment and BatchRuns are supported");
263        }
264      }
265      childHiveJobsLock.EnterReadLock();
266      try {
267        foreach (OptimizerHiveJob child in childHiveJobs) {
268          child.SetIndexInParentOptimizerList(this);
269        }
270      }
271      finally { childHiveJobsLock.ExitReadLock(); }
272    }
273
274    public override void AddChildHiveJob(HiveJob hiveJob) {
275      base.AddChildHiveJob(hiveJob);
276      var optimizerHiveJob = (OptimizerHiveJob)hiveJob;
277      syncJobsWithOptimizers = false;
278      if (this.ItemJob != null && optimizerHiveJob.ItemJob != null) {
279        // if job is in state Paused, it has to preserve its ResultCollection, which is cleared when a optimizer is added to an experiment
280        OptimizerJob optimizerJobClone = null;
281        if (optimizerHiveJob.Job.State == TaskState.Paused) {
282          optimizerJobClone = (OptimizerJob)optimizerHiveJob.ItemJob.Clone();
283        }
284
285        if (this.ItemJob.Item is Optimization.Experiment) {
286          if (!this.ItemJob.OptimizerAsExperiment.Optimizers.Contains(optimizerHiveJob.ItemJob.Item)) {
287            UpdateOptimizerInExperiment(this.ItemJob.OptimizerAsExperiment, optimizerHiveJob.ItemJob);
288          }
289        } else if (this.ItemJob.Item is Optimization.BatchRun) {
290          UpdateOptimizerInBatchRun(this.ItemJob.OptimizerAsBatchRun, optimizerHiveJob.ItemJob);
291        }
292
293        if (optimizerHiveJob.Job.State == TaskState.Paused) {
294          optimizerHiveJob.ItemJob = optimizerJobClone;
295        }
296      }
297      syncJobsWithOptimizers = 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 job contains an Experiment)
305    /// </param>
306    public override TaskData GetAsJobData(bool withoutChildOptimizers, out List<IPluginDescription> plugins) {
307      plugins = new List<IPluginDescription>();
308      if (this.itemJob == null) // || this.jobItem.Optimizer == null
309        return null;
310
311      IEnumerable<Type> usedTypes;
312      byte[] jobByteArray;
313      if (withoutChildOptimizers && this.ItemJob.Item is Optimization.Experiment) {
314        OptimizerJob clonedJob = (OptimizerJob)this.ItemJob.Clone(); // use a cloned job, 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.ItemJob.Item is Optimization.BatchRun) {
318        OptimizerJob clonedJob = (OptimizerJob)this.ItemJob.Clone();
319        clonedJob.OptimizerAsBatchRun.Optimizer = null;
320        jobByteArray = PersistenceUtil.Serialize(clonedJob, out usedTypes);
321      } else if (this.ItemJob.Item is IAlgorithm) {
322        ((IAlgorithm)this.ItemJob.Item).StoreAlgorithmInEachRun = false; // avoid storing the algorithm in runs to reduce size
323        jobByteArray = PersistenceUtil.Serialize(this.ItemJob, out usedTypes);
324      } else {
325        jobByteArray = PersistenceUtil.Serialize(this.ItemJob, out usedTypes);
326      }
327
328      TaskData jobData = new TaskData() { TaskId = job.Id, Data = jobByteArray };
329      PluginUtil.CollectDeclaringPlugins(plugins, usedTypes);
330      return jobData;
331    }
332
333    public OptimizerHiveJob GetChildByOptimizerJob(OptimizerJob optimizerJob) {
334      childHiveJobsLock.EnterReadLock();
335      try {
336        foreach (OptimizerHiveJob child in childHiveJobs) {
337          if (child.ItemJob == optimizerJob)
338            return child;
339        }
340        return null;
341      }
342      finally { childHiveJobsLock.ExitReadLock(); }
343    }
344
345    public HiveJob<OptimizerJob> GetChildByOptimizer(IOptimizer optimizer) {
346      childHiveJobsLock.EnterReadLock();
347      try {
348        foreach (OptimizerHiveJob child in childHiveJobs) {
349          if (child.ItemJob.Item == optimizer)
350            return child;
351        }
352        return null;
353      }
354      finally { childHiveJobsLock.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.