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 |
|
---|
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;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.PluginInfrastructure;
|
---|
30 |
|
---|
31 | namespace 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 | foreach (var item in e.Items) {
|
---|
132 | if (GetChildByOptimizer(item.Value) == null && item.Value.Name != "Placeholder") {
|
---|
133 | this.childHiveJobs.Add(new OptimizerHiveJob(item.Value));
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 | private void Optimizers_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
|
---|
139 | if (syncJobsWithOptimizers && this.ItemJob.ComputeInParallel) {
|
---|
140 | foreach (var item in e.OldItems) {
|
---|
141 | this.childHiveJobs.Remove(this.GetChildByOptimizer(item.Value));
|
---|
142 | }
|
---|
143 | foreach (var item in e.Items) {
|
---|
144 | if (GetChildByOptimizer(item.Value) == null && item.Value.Name != "Placeholder") {
|
---|
145 | this.childHiveJobs.Add(new OptimizerHiveJob(item.Value));
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 | private void Optimizers_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
|
---|
151 | if (syncJobsWithOptimizers && this.ItemJob.ComputeInParallel) {
|
---|
152 | foreach (var item in e.Items) {
|
---|
153 | this.childHiveJobs.Remove(this.GetChildByOptimizer(item.Value));
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|
157 | private void Optimizers_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
|
---|
158 | if (syncJobsWithOptimizers && this.ItemJob.ComputeInParallel) {
|
---|
159 | foreach (var item in e.Items) {
|
---|
160 | this.childHiveJobs.Remove(this.GetChildByOptimizer(item.Value));
|
---|
161 | }
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | /// <summary>
|
---|
166 | /// if this.Optimizer is Experiment
|
---|
167 | /// replace the child-optimizer in the experiment
|
---|
168 | /// if this.Optimizer is BatchRun
|
---|
169 | /// add the runs from the optimizerJob to the batchrun and replace the Optimizer
|
---|
170 | /// </summary>
|
---|
171 | public override void IntegrateChild(ItemJob job, Guid childJobId) {
|
---|
172 | var optimizerJob = (OptimizerJob)job;
|
---|
173 | syncJobsWithOptimizers = false; // don't sync with optimizers during this method
|
---|
174 |
|
---|
175 | if (this.ItemJob != null && this.ItemJob.Item != null) {
|
---|
176 | if (this.ItemJob.Item is Optimization.Experiment) {
|
---|
177 | UpdateOptimizerInExperiment(this.ItemJob.OptimizerAsExperiment, optimizerJob);
|
---|
178 | } else if (this.ItemJob.Item is Optimization.BatchRun) {
|
---|
179 | UpdateOptimizerInBatchRun(this.ItemJob.OptimizerAsBatchRun, optimizerJob);
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | OptimizerHiveJob child = (OptimizerHiveJob)this.ChildHiveJobs.Single(j => j.Job.Id == childJobId);
|
---|
184 | if (!optimizerJob.ComputeInParallel) {
|
---|
185 | child.syncJobsWithOptimizers = false;
|
---|
186 | child.ItemJob = optimizerJob;
|
---|
187 | child.syncJobsWithOptimizers = true;
|
---|
188 | }
|
---|
189 | syncJobsWithOptimizers = true;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /// <summary>
|
---|
193 | /// Adds the runs from the optimizerJob to the batchrun and replaces the Optimizer
|
---|
194 | /// Sideeffect: the optimizerJob.Optimizer will be prepared (scopes are deleted and executionstate will be reset)
|
---|
195 | /// </summary>
|
---|
196 | private void UpdateOptimizerInBatchRun(BatchRun batchRun, OptimizerJob optimizerJob) {
|
---|
197 | if (batchRun.Optimizer == null) {
|
---|
198 | 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
|
---|
199 | }
|
---|
200 | foreach (IRun run in optimizerJob.Item.Runs) {
|
---|
201 | if (!batchRun.Runs.Contains(run)) {
|
---|
202 | run.Name = GetNewRunName(run, batchRun.Runs);
|
---|
203 | batchRun.Runs.Add(run);
|
---|
204 | }
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | /// <summary>
|
---|
209 | /// replace the child-optimizer in the experiment
|
---|
210 | /// Sideeffect: the optimizerJob.Optimizer will be prepared (scopes are deleted and executionstate will be reset)
|
---|
211 | /// </summary>
|
---|
212 | private void UpdateOptimizerInExperiment(Optimization.Experiment experiment, OptimizerJob optimizerJob) {
|
---|
213 | if (optimizerJob.IndexInParentOptimizerList < 0)
|
---|
214 | throw new IndexOutOfRangeException("IndexInParentOptimizerList must be equal or greater than zero! The Job is invalid and the optimizer-tree cannot be reassembled.");
|
---|
215 |
|
---|
216 | while (experiment.Optimizers.Count < optimizerJob.IndexInParentOptimizerList) {
|
---|
217 | experiment.Optimizers.Add(new UserDefinedAlgorithm("Placeholder")); // add dummy-entries to Optimizers so that its possible to insert the optimizerJob at the correct position
|
---|
218 | }
|
---|
219 | if (experiment.Optimizers.Count < optimizerJob.IndexInParentOptimizerList + 1) {
|
---|
220 | experiment.Optimizers.Add(optimizerJob.Item);
|
---|
221 | } else {
|
---|
222 | // if ComputeInParallel==true, don't replace the optimizer (except it is still a Placeholder)
|
---|
223 | // this is because Jobs with ComputeInParallel get submitted to hive with their child-optimizers deleted
|
---|
224 | if (!optimizerJob.ComputeInParallel || experiment.Optimizers[optimizerJob.IndexInParentOptimizerList].Name == "Placeholder") {
|
---|
225 | experiment.Optimizers[optimizerJob.IndexInParentOptimizerList] = optimizerJob.Item;
|
---|
226 | }
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | /// <summary>
|
---|
231 | /// Sets the IndexInParentOptimizerList property of the OptimizerJob
|
---|
232 | /// according to the position in the OptimizerList of the parentHiveJob.Job
|
---|
233 | /// Recursively updates all the child-jobs as well
|
---|
234 | /// </summary>
|
---|
235 | internal void SetIndexInParentOptimizerList(OptimizerHiveJob parentHiveJob) {
|
---|
236 | if (parentHiveJob != null) {
|
---|
237 | if (parentHiveJob.ItemJob.Item is Optimization.Experiment) {
|
---|
238 | this.ItemJob.IndexInParentOptimizerList = parentHiveJob.ItemJob.OptimizerAsExperiment.Optimizers.IndexOf(this.ItemJob.Item);
|
---|
239 | } else if (parentHiveJob.ItemJob.Item is Optimization.BatchRun) {
|
---|
240 | this.ItemJob.IndexInParentOptimizerList = 0;
|
---|
241 | } else {
|
---|
242 | throw new NotSupportedException("Only Experiment and BatchRuns are supported");
|
---|
243 | }
|
---|
244 | }
|
---|
245 | foreach (OptimizerHiveJob child in childHiveJobs) {
|
---|
246 | child.SetIndexInParentOptimizerList(this);
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | public override void AddChildHiveJob(HiveJob hiveJob) {
|
---|
251 | base.AddChildHiveJob(hiveJob);
|
---|
252 | var optimizerHiveJob = (OptimizerHiveJob)hiveJob;
|
---|
253 | syncJobsWithOptimizers = false;
|
---|
254 | if (this.ItemJob != null && optimizerHiveJob.ItemJob != null) {
|
---|
255 | // if job is in state Paused, it has to preserve its ResultCollection, which is cleared when a optimizer is added to an experiment
|
---|
256 | OptimizerJob optimizerJobClone = null;
|
---|
257 | if (optimizerHiveJob.Job.State == JobState.Paused) {
|
---|
258 | optimizerJobClone = (OptimizerJob)optimizerHiveJob.ItemJob.Clone();
|
---|
259 | }
|
---|
260 |
|
---|
261 | if (this.ItemJob.Item is Optimization.Experiment) {
|
---|
262 | if (!this.ItemJob.OptimizerAsExperiment.Optimizers.Contains(optimizerHiveJob.ItemJob.Item)) {
|
---|
263 | UpdateOptimizerInExperiment(this.ItemJob.OptimizerAsExperiment, optimizerHiveJob.ItemJob);
|
---|
264 | }
|
---|
265 | } else if (this.ItemJob.Item is Optimization.BatchRun) {
|
---|
266 | UpdateOptimizerInBatchRun(this.ItemJob.OptimizerAsBatchRun, optimizerHiveJob.ItemJob);
|
---|
267 | }
|
---|
268 |
|
---|
269 | if (optimizerHiveJob.Job.State == JobState.Paused) {
|
---|
270 | optimizerHiveJob.ItemJob = optimizerJobClone;
|
---|
271 | }
|
---|
272 | }
|
---|
273 | syncJobsWithOptimizers = true;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /// <summary>
|
---|
277 | /// Creates a JobData object containing the Job and the IJob-Object as byte[]
|
---|
278 | /// </summary>
|
---|
279 | /// <param name="withoutChildOptimizers">
|
---|
280 | /// if true the Child-Optimizers will not be serialized (if the job contains an Experiment)
|
---|
281 | /// </param>
|
---|
282 | public override JobData GetAsJobData(bool withoutChildOptimizers, out List<IPluginDescription> plugins) {
|
---|
283 | plugins = new List<IPluginDescription>();
|
---|
284 | if (this.itemJob == null) // || this.jobItem.Optimizer == null
|
---|
285 | return null;
|
---|
286 |
|
---|
287 | IEnumerable<Type> usedTypes;
|
---|
288 | byte[] jobByteArray;
|
---|
289 | if (withoutChildOptimizers && this.ItemJob.Item is Optimization.Experiment) {
|
---|
290 | OptimizerJob clonedJob = (OptimizerJob)this.ItemJob.Clone(); // use a cloned job, so that the childHiveJob don't get confused
|
---|
291 | clonedJob.OptimizerAsExperiment.Optimizers.Clear();
|
---|
292 | jobByteArray = PersistenceUtil.Serialize(clonedJob, out usedTypes);
|
---|
293 | } else if (withoutChildOptimizers && this.ItemJob.Item is Optimization.BatchRun) {
|
---|
294 | OptimizerJob clonedJob = (OptimizerJob)this.ItemJob.Clone();
|
---|
295 | clonedJob.OptimizerAsBatchRun.Optimizer = null;
|
---|
296 | jobByteArray = PersistenceUtil.Serialize(clonedJob, out usedTypes);
|
---|
297 | } else if (this.ItemJob.Item is IAlgorithm) {
|
---|
298 | ((IAlgorithm)this.ItemJob.Item).StoreAlgorithmInEachRun = false; // avoid storing the algorithm in runs to reduce size
|
---|
299 | jobByteArray = PersistenceUtil.Serialize(this.ItemJob, out usedTypes);
|
---|
300 | } else {
|
---|
301 | jobByteArray = PersistenceUtil.Serialize(this.ItemJob, out usedTypes);
|
---|
302 | }
|
---|
303 |
|
---|
304 | JobData jobData = new JobData() {
|
---|
305 | JobId = job.Id,
|
---|
306 | Data = jobByteArray
|
---|
307 | };
|
---|
308 |
|
---|
309 | PluginUtil.CollectDeclaringPlugins(plugins, usedTypes);
|
---|
310 |
|
---|
311 | return jobData;
|
---|
312 | }
|
---|
313 |
|
---|
314 | public OptimizerHiveJob GetChildByOptimizerJob(OptimizerJob optimizerJob) {
|
---|
315 | foreach (OptimizerHiveJob child in ChildHiveJobs) {
|
---|
316 | if (child.ItemJob == optimizerJob)
|
---|
317 | return child;
|
---|
318 | }
|
---|
319 | return null;
|
---|
320 | }
|
---|
321 |
|
---|
322 | public HiveJob<OptimizerJob> GetChildByOptimizer(IOptimizer optimizer) {
|
---|
323 | foreach (OptimizerHiveJob child in ChildHiveJobs) {
|
---|
324 | if (child.ItemJob.Item == optimizer)
|
---|
325 | return child;
|
---|
326 | }
|
---|
327 | return null;
|
---|
328 | }
|
---|
329 |
|
---|
330 | #region Helpers
|
---|
331 | /// <summary>
|
---|
332 | /// Parses the run numbers out of runs and renames the run to the next number
|
---|
333 | /// </summary>
|
---|
334 | private static string GetNewRunName(IRun run, RunCollection runs) {
|
---|
335 | int idx = run.Name.IndexOf("Run ") + 4;
|
---|
336 |
|
---|
337 | if (idx == -1 || runs.Count == 0)
|
---|
338 | return run.Name;
|
---|
339 |
|
---|
340 | int maxRunNumber = int.MinValue;
|
---|
341 | foreach (IRun r in runs) {
|
---|
342 | int number = GetRunNumber(r.Name);
|
---|
343 | maxRunNumber = Math.Max(maxRunNumber, number);
|
---|
344 | }
|
---|
345 |
|
---|
346 | return run.Name.Substring(0, idx) + (maxRunNumber + 1).ToString();
|
---|
347 | }
|
---|
348 |
|
---|
349 | /// <summary>
|
---|
350 | /// Parses the number of a Run out of its name. Example "Genetic Algorithm Run 3" -> 3
|
---|
351 | /// </summary>
|
---|
352 | private static int GetRunNumber(string runName) {
|
---|
353 | int idx = runName.IndexOf("Run ") + 4;
|
---|
354 | if (idx == -1) {
|
---|
355 | return 0;
|
---|
356 | } else {
|
---|
357 | return int.Parse(runName.Substring(idx, runName.Length - idx));
|
---|
358 | }
|
---|
359 | }
|
---|
360 | #endregion
|
---|
361 | }
|
---|
362 | }
|
---|