1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Threading;
|
---|
5 | using HeuristicLab.Common;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Core.Networks;
|
---|
8 | using HeuristicLab.Data;
|
---|
9 | using HeuristicLab.Operators;
|
---|
10 | using HeuristicLab.Optimization;
|
---|
11 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Networks.IntegratedOptimization {
|
---|
14 | [Item("OrchestratedAlgorithmNode", "A node of a network which contains a HeuristicLab algorithm and can be orchestrated by an orchestrator.")]
|
---|
15 | [StorableClass]
|
---|
16 | public class OrchestratedAlgorithmNode : AlgorithmNode {
|
---|
17 | #region Constants
|
---|
18 | private const string OrchestrationPortName = "OrchestrationPort";
|
---|
19 | private const string EvaluationPortName = "EvaluationPort";
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | private bool sendResultMessage = true;
|
---|
23 |
|
---|
24 | #region Ports
|
---|
25 | public IConfigurationPort OrchestrationPort {
|
---|
26 | get { return (IConfigurationPort)Ports[OrchestrationPortName]; }
|
---|
27 | }
|
---|
28 | public IConfigurationPort EvaluationPort {
|
---|
29 | get { return (IConfigurationPort)Ports[EvaluationPortName]; }
|
---|
30 | }
|
---|
31 | #endregion
|
---|
32 |
|
---|
33 | [Storable]
|
---|
34 | private IHookOperator evalHook;
|
---|
35 | public IHookOperator EvalHook {
|
---|
36 | get { return evalHook; }
|
---|
37 | set {
|
---|
38 | if (evalHook == value) return;
|
---|
39 | evalHook = value;
|
---|
40 | evalHook.Port = EvaluationPort;
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | [Storable]
|
---|
45 | public bool CloneAlgorithm { get; set; }
|
---|
46 |
|
---|
47 | [StorableConstructor]
|
---|
48 | protected OrchestratedAlgorithmNode(bool deserializing) : base(deserializing) { }
|
---|
49 | protected OrchestratedAlgorithmNode(OrchestratedAlgorithmNode original, Cloner cloner) : base(original, cloner) {
|
---|
50 | CloneAlgorithm = original.CloneAlgorithm;
|
---|
51 | EvalHook = cloner.Clone(original.EvalHook);
|
---|
52 |
|
---|
53 | OrchestrationPort.MessageReceived += OrchestrationPort_MessageReceived;
|
---|
54 | }
|
---|
55 | public OrchestratedAlgorithmNode() : this("OrchestratedAlgorithmNode") { }
|
---|
56 | public OrchestratedAlgorithmNode(string name) : base(name) {
|
---|
57 | #region Configure Ports
|
---|
58 | #region OrchestrationPort
|
---|
59 | var orchestrationPort = new ConfigurationPort(OrchestrationPortName);
|
---|
60 | orchestrationPort.MessageReceived += OrchestrationPort_MessageReceived;
|
---|
61 | Ports.Add(orchestrationPort);
|
---|
62 | #endregion
|
---|
63 |
|
---|
64 | #region EvaluationPort
|
---|
65 | var evaluationPort = new ConfigurationPort(EvaluationPortName);
|
---|
66 | Ports.Add(evaluationPort);
|
---|
67 | #endregion
|
---|
68 | #endregion
|
---|
69 | }
|
---|
70 |
|
---|
71 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
72 | return new OrchestratedAlgorithmNode(this, cloner);
|
---|
73 | }
|
---|
74 |
|
---|
75 | [StorableHook(HookType.AfterDeserialization)]
|
---|
76 | private void AfterDeserialization() {
|
---|
77 | OrchestrationPort.MessageReceived += OrchestrationPort_MessageReceived;
|
---|
78 | }
|
---|
79 |
|
---|
80 | #region Port Events
|
---|
81 | private void OrchestrationPort_MessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) {
|
---|
82 | var message = ((EnumValue<OrchestrationMessage>)e.Value["OrchestrationMessage"]).Value;
|
---|
83 | IAlgorithm alg = null;
|
---|
84 |
|
---|
85 | if (message.HasFlag(OrchestrationMessage.Prepare)) {
|
---|
86 | // TODO: what if CloneAlgorithm == true?
|
---|
87 | IMessageValue problemMsgVal;
|
---|
88 | if (e.Value.Values.TryGetValue("Problem", out problemMsgVal)) {
|
---|
89 | alg = CloneAlgorithm ? (IAlgorithm)Algorithm.Clone() : Algorithm;
|
---|
90 | var prob = (IHeuristicOptimizationProblem)problemMsgVal.Value.Clone();
|
---|
91 |
|
---|
92 | if (message.HasFlag(OrchestrationMessage.QualityAdaption)) {
|
---|
93 | var instEval = prob.Evaluator as InstrumentedOperator;
|
---|
94 | if (instEval != null && EvalHook != null) instEval.AfterExecutionOperators.Add(EvalHook);
|
---|
95 | }
|
---|
96 |
|
---|
97 | var parameters = new Dictionary<string, IItem>();
|
---|
98 | alg.CollectParameterValues(parameters);
|
---|
99 | var entry = parameters.SingleOrDefault(x => x.Value is IMultiAnalyzer);
|
---|
100 | var checkedStates = new Dictionary<Type, bool>();
|
---|
101 | if (entry.Key != null) {
|
---|
102 | var multiAnalyzer = entry.Value as IMultiAnalyzer;
|
---|
103 | foreach (var analyzer in multiAnalyzer.Operators)
|
---|
104 | checkedStates.Add(analyzer.GetType(), multiAnalyzer.Operators.ItemChecked(analyzer));
|
---|
105 | }
|
---|
106 |
|
---|
107 | alg.Problem = prob;
|
---|
108 |
|
---|
109 | if (entry.Key != null) {
|
---|
110 | var multiAnalyzer = entry.Value as IMultiAnalyzer;
|
---|
111 | foreach (var state in checkedStates) {
|
---|
112 | var analyzer = multiAnalyzer.Operators.Single(x => x.GetType() == state.Key);
|
---|
113 | multiAnalyzer.Operators.SetItemCheckedState(analyzer, state.Value);
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | alg.Prepare();
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (message.HasFlag(OrchestrationMessage.Start)) {
|
---|
122 | // TODO: what if CloneAlgorithm == true?
|
---|
123 | StartAlgorithm(alg ?? (CloneAlgorithm ? (IAlgorithm)Algorithm.Clone() : Algorithm));
|
---|
124 |
|
---|
125 | if (sendResultMessage) {
|
---|
126 | var msg = OrchestrationPort.PrepareMessage();
|
---|
127 | msg["Results"] = (ResultCollection)alg.Results.Clone();
|
---|
128 | OrchestrationPort.SendMessage(msg);
|
---|
129 | } else sendResultMessage = true;
|
---|
130 | }
|
---|
131 |
|
---|
132 | if (message.HasFlag(OrchestrationMessage.Pause)) {
|
---|
133 | // TODO: what if CloneAlgorithm == true?
|
---|
134 | sendResultMessage = false;
|
---|
135 | (alg ?? (CloneAlgorithm ? (IAlgorithm)Algorithm.Clone() : Algorithm)).Pause();
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (message.HasFlag(OrchestrationMessage.Stop)) {
|
---|
139 | // TODO: what if CloneAlgorithm == true?
|
---|
140 | (alg ?? (CloneAlgorithm ? (IAlgorithm)Algorithm.Clone() : Algorithm)).Stop();
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | private void StartAlgorithm(IAlgorithm a) {
|
---|
145 | var trigger = new ManualResetEvent(false);
|
---|
146 | Exception ex = null;
|
---|
147 | a.Stopped += (src, e) => { trigger.Set(); };
|
---|
148 | a.ExceptionOccurred += (src, e) => { ex = e.Value; trigger.Set(); };
|
---|
149 | a.Prepare();
|
---|
150 | a.Start();
|
---|
151 | trigger.WaitOne();
|
---|
152 | if (ex != null) throw ex;
|
---|
153 | }
|
---|
154 | #endregion
|
---|
155 | }
|
---|
156 | }
|
---|