1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2017 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.Threading;
|
---|
23 | using System.Threading.Tasks;
|
---|
24 | using HeuristicLab.Collections;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Core.Networks;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Networks.IntegratedOptimization {
|
---|
34 | [Item("OrchestratorNode", "An orchestrator node.")]
|
---|
35 | [StorableClass]
|
---|
36 | public abstract class OrchestratorNode : Node, IOrchestratorNode {
|
---|
37 | #region Constants
|
---|
38 | private const string OrchestrationMessageParameterName = "OrchestrationMessage";
|
---|
39 | protected const string OrchestrationPortNameSuffix = "OrchestrationPort";
|
---|
40 | protected const string EvaluationPortNameSuffix = "EvaluationPort";
|
---|
41 | #endregion
|
---|
42 |
|
---|
43 | [Storable]
|
---|
44 | private ParameterCollection parameters;
|
---|
45 | protected ParameterCollection Parameters {
|
---|
46 | get { return parameters; }
|
---|
47 | }
|
---|
48 | private ReadOnlyKeyedItemCollection<string, IParameter> readOnlyParameters;
|
---|
49 | IKeyedItemCollection<string, IParameter> IOrchestratorNode.Parameters {
|
---|
50 | get {
|
---|
51 | if (readOnlyParameters == null) readOnlyParameters = parameters.AsReadOnly();
|
---|
52 | return readOnlyParameters;
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | [Storable]
|
---|
57 | private ResultCollection results;
|
---|
58 | public ResultCollection Results {
|
---|
59 | get { return results; }
|
---|
60 | }
|
---|
61 |
|
---|
62 | [StorableConstructor]
|
---|
63 | protected OrchestratorNode(bool deserializing) : base(deserializing) { }
|
---|
64 | protected OrchestratorNode(OrchestratorNode original, Cloner cloner) : base(original, cloner) {
|
---|
65 | results = cloner.Clone(original.results);
|
---|
66 | parameters = cloner.Clone(original.parameters);
|
---|
67 | readOnlyParameters = null;
|
---|
68 | RegisterEvents();
|
---|
69 | }
|
---|
70 |
|
---|
71 | protected OrchestratorNode() : base("OrchestratorNode") {
|
---|
72 | results = new ResultCollection();
|
---|
73 | parameters = new ParameterCollection();
|
---|
74 | Parameters.Add(new ValueParameter<EnumValue<OrchestrationMessage>>("OrchestrationMessage", new EnumValue<OrchestrationMessage>()));
|
---|
75 | readOnlyParameters = null;
|
---|
76 | }
|
---|
77 | protected OrchestratorNode(string name) : base(name) {
|
---|
78 | results = new ResultCollection();
|
---|
79 | parameters = new ParameterCollection();
|
---|
80 | Parameters.Add(new ValueParameter<EnumValue<OrchestrationMessage>>("OrchestrationMessage", new EnumValue<OrchestrationMessage>()));
|
---|
81 | readOnlyParameters = null;
|
---|
82 | }
|
---|
83 | protected OrchestratorNode(string name, string description) : base(name, description) {
|
---|
84 | results = new ResultCollection();
|
---|
85 | parameters = new ParameterCollection();
|
---|
86 | Parameters.Add(new ValueParameter<EnumValue<OrchestrationMessage>>("OrchestrationMessage", new EnumValue<OrchestrationMessage>()));
|
---|
87 | readOnlyParameters = null;
|
---|
88 | }
|
---|
89 |
|
---|
90 | [StorableHook(HookType.AfterDeserialization)]
|
---|
91 | private void AfterDeserialization() {
|
---|
92 | RegisterEvents();
|
---|
93 | }
|
---|
94 |
|
---|
95 | private void RegisterEvents() {
|
---|
96 | foreach (var p in Ports) RegisterPortEvents(p);
|
---|
97 | }
|
---|
98 |
|
---|
99 | protected void AddOrchestrationPort<T>(string solverName)
|
---|
100 | where T : class, IProblem {
|
---|
101 | var orchestrationPort = new MessagePort(solverName + OrchestrationPortNameSuffix);
|
---|
102 | orchestrationPort.Parameters.Add(new PortParameter<EnumValue<OrchestrationMessage>>("OrchestrationMessage") {
|
---|
103 | Type = PortParameterType.Output
|
---|
104 | });
|
---|
105 | orchestrationPort.Parameters.Add(new PortParameter<T>("Problem") {
|
---|
106 | Type = PortParameterType.Output
|
---|
107 | });
|
---|
108 | orchestrationPort.Parameters.Add(new PortParameter<ResultCollection>("Results") {
|
---|
109 | Type = PortParameterType.Input
|
---|
110 | });
|
---|
111 | Ports.Add(orchestrationPort);
|
---|
112 | }
|
---|
113 |
|
---|
114 | protected void AddEvaluationPort<T>(string solverName, string solutionName, string qualityName) where T : class, IItem {
|
---|
115 | var evaluationPort = new MessagePort(solverName + EvaluationPortNameSuffix);
|
---|
116 | evaluationPort.Parameters.Add(new PortParameter<T>(solutionName) {
|
---|
117 | Type = PortParameterType.Input
|
---|
118 | });
|
---|
119 | evaluationPort.Parameters.Add(new PortParameter<DoubleValue>(qualityName) {
|
---|
120 | Type = PortParameterType.Input | PortParameterType.Output
|
---|
121 | });
|
---|
122 | Ports.Add(evaluationPort);
|
---|
123 | }
|
---|
124 |
|
---|
125 | protected virtual void ProcessMessage(IMessage message, IMessagePort port, CancellationToken token) { }
|
---|
126 |
|
---|
127 | public abstract void Prepare(bool clearRuns = false);
|
---|
128 | public async Task PrepareAsync(bool clearRuns = false) { await Task.Run(() => Prepare(clearRuns)); }
|
---|
129 | public abstract void Start();
|
---|
130 | public async Task StartAsync() { await Task.Run(() => Start()); }
|
---|
131 | public abstract void Pause();
|
---|
132 | public async Task PauseAsync() { await Task.Run(() => Pause()); }
|
---|
133 | public abstract void Stop();
|
---|
134 | public async Task StopAsync() { await Task.Run(() => Stop()); }
|
---|
135 |
|
---|
136 | #region Events
|
---|
137 | protected override void Ports_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IPort> e) {
|
---|
138 | base.Ports_ItemsAdded(sender, e);
|
---|
139 | foreach (var p in e.Items)
|
---|
140 | RegisterPortEvents(p);
|
---|
141 | }
|
---|
142 | protected override void Ports_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IPort> e) {
|
---|
143 | base.Ports_ItemsRemoved(sender, e);
|
---|
144 | foreach (var p in e.Items)
|
---|
145 | DeregisterPortEvents(p);
|
---|
146 | }
|
---|
147 | protected override void Ports_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IPort> e) {
|
---|
148 | base.Ports_ItemsReplaced(sender, e);
|
---|
149 | foreach (var p in e.OldItems)
|
---|
150 | DeregisterPortEvents(p);
|
---|
151 | foreach (var p in e.Items)
|
---|
152 | RegisterPortEvents(p);
|
---|
153 | }
|
---|
154 | protected override void Ports_CollectionReset(object sender, CollectionItemsChangedEventArgs<IPort> e) {
|
---|
155 | base.Ports_CollectionReset(sender, e);
|
---|
156 | foreach (var p in e.OldItems)
|
---|
157 | DeregisterPortEvents(p);
|
---|
158 | foreach (var p in e.Items)
|
---|
159 | RegisterPortEvents(p);
|
---|
160 | }
|
---|
161 | private void MessagePort_MessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) {
|
---|
162 | ProcessMessage(e.Value, (IMessagePort)sender, e.Value2);
|
---|
163 | }
|
---|
164 | #endregion
|
---|
165 |
|
---|
166 | #region Port Events
|
---|
167 | protected virtual void RegisterPortEvents(IPort port) {
|
---|
168 | var mp = port as IMessagePort;
|
---|
169 | if (mp != null)
|
---|
170 | mp.MessageReceived += MessagePort_MessageReceived;
|
---|
171 | }
|
---|
172 | protected virtual void DeregisterPortEvents(IPort port) {
|
---|
173 | var mp = port as IMessagePort;
|
---|
174 | if (mp != null)
|
---|
175 | mp.MessageReceived -= MessagePort_MessageReceived;
|
---|
176 | }
|
---|
177 | #endregion
|
---|
178 | }
|
---|
179 | }
|
---|