1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Analysis.SolutionCaching {
|
---|
30 | public class RunCollectionModifierHiveTask : HiveTask<RunCollectionModifierTask> {
|
---|
31 | #region Constructors and Cloning
|
---|
32 | public RunCollectionModifierHiveTask(RunCollectionModifierTask task)
|
---|
33 | : base() {
|
---|
34 | ItemTask = task;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public RunCollectionModifierHiveTask(RunCollectionModifierExecutable executable)
|
---|
38 | : base() {
|
---|
39 | ItemTask = new RunCollectionModifierTask(executable);
|
---|
40 | }
|
---|
41 |
|
---|
42 | protected RunCollectionModifierHiveTask(RunCollectionModifierHiveTask original, Cloner cloner)
|
---|
43 | : base(original, cloner) {
|
---|
44 | }
|
---|
45 |
|
---|
46 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
47 | return new RunCollectionModifierHiveTask(this, cloner);
|
---|
48 | }
|
---|
49 | #endregion
|
---|
50 |
|
---|
51 | public override void IntegrateChild(ItemTask task, Guid childTaskId) {
|
---|
52 | var optimizerTask = (RunCollectionModifierTask)task;
|
---|
53 | syncTasksWithOptimizers = false; // don't sync with optimizers during this method
|
---|
54 |
|
---|
55 | if (this.ItemTask != null && this.ItemTask.Item != null) {
|
---|
56 | itemTaskLock.EnterWriteLock();
|
---|
57 | try {
|
---|
58 | this.ItemTask.Item.RunCollection.AddRange(((RunCollectionModifierExecutable)task.Item).RunCollection);
|
---|
59 | }
|
---|
60 | finally {
|
---|
61 | itemTaskLock.ExitWriteLock();
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | IEnumerable<HiveTask> childs = this.ChildHiveTasks.Where(j => j.Task.Id == childTaskId);
|
---|
66 | //TODO: in very rare cases childs is empty. This shouldn't be the case and should be further investigated.
|
---|
67 | if (childs.Count() > 0) {
|
---|
68 | RunCollectionModifierHiveTask child = childs.First() as RunCollectionModifierHiveTask;
|
---|
69 | if (child != null && !optimizerTask.ComputeInParallel) {
|
---|
70 | child.ItemTask = optimizerTask;
|
---|
71 | }
|
---|
72 | }
|
---|
73 | syncTasksWithOptimizers = true;
|
---|
74 | }
|
---|
75 |
|
---|
76 | protected override void UpdateChildHiveTasks() {
|
---|
77 | childHiveTasksLock.EnterWriteLock();
|
---|
78 | try {
|
---|
79 | if (Task != null && syncTasksWithOptimizers) {
|
---|
80 | if (!ItemTask.ComputeInParallel) {
|
---|
81 | if (childHiveTasks.Any()) {
|
---|
82 | var exec = (RunCollectionModifierExecutable)ItemTask.Item;
|
---|
83 | //compute in parallel was deactivated, copy runs back
|
---|
84 | foreach (var childHiveTask in childHiveTasks) {
|
---|
85 | exec.RunCollection.AddRange(((RunCollectionModifierExecutable)childHiveTask.ItemTask.Item).RunCollection);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | childHiveTasks.Clear();
|
---|
89 | } else {
|
---|
90 | var runs = ItemTask.Item.RunCollection;
|
---|
91 | foreach (var run in runs) {
|
---|
92 | var exec = (RunCollectionModifierExecutable)ItemTask.Item.Clone(new Cloner());
|
---|
93 | //TODO: this could be more efficient, create new RCME instead of cloning
|
---|
94 | exec.RunCollection.Clear();
|
---|
95 | exec.RunCollection.Add(run);
|
---|
96 |
|
---|
97 | var rcmHiveTask = new RunCollectionModifierHiveTask(exec);
|
---|
98 | rcmHiveTask.Task.Priority = Task.Priority;
|
---|
99 | childHiveTasks.Add(rcmHiveTask);
|
---|
100 | }
|
---|
101 | ItemTask.Item.RunCollection.Clear();
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 | finally {
|
---|
106 | childHiveTasksLock.ExitWriteLock();
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | } |
---|