Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization/3.3/OrchestratorNode.cs @ 14624

Last change on this file since 14624 was 14624, checked in by mkommend, 7 years ago

#2205: Refactored messages in orchestrated algorithm node.

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