Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14586 was 14586, checked in by jkarder, 7 years ago

#2205: worked on optimization networks

  • added projects for integrated optimization (orchestration)
File size: 6.4 KB
Line 
1using System.Threading;
2using System.Threading.Tasks;
3using HeuristicLab.Collections;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Core.Networks;
7using HeuristicLab.Data;
8using HeuristicLab.Optimization;
9using HeuristicLab.Parameters;
10using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
11
12namespace HeuristicLab.Networks.IntegratedOptimization {
13  [Item("OrchestratorNode", "An orchestrator node.")]
14  [StorableClass]
15  public abstract class OrchestratorNode : Node, IOrchestratorNode {
16    #region Constants
17    private const string OrchestrationMessageParameterName = "OrchestrationMessage";
18    protected const string OrchestrationPortNameSuffix = "OrchestrationPort";
19    protected const string EvaluationPortNameSuffix = "EvaluationPort";
20    #endregion
21
22    [Storable]
23    private ParameterCollection parameters;
24    protected ParameterCollection Parameters {
25      get { return parameters; }
26    }
27    private ReadOnlyKeyedItemCollection<string, IParameter> readOnlyParameters;
28    IKeyedItemCollection<string, IParameter> IOrchestratorNode.Parameters {
29      get {
30        if (readOnlyParameters == null) readOnlyParameters = parameters.AsReadOnly();
31        return readOnlyParameters;
32      }
33    }
34
35    [Storable]
36    private ResultCollection results;
37    public ResultCollection Results {
38      get { return results; }
39    }
40
41    #region Parameters
42    //public IValueParameter<EnumValue<OrchestrationMessage>> OrchestrationMessageParameter {
43    //  get { return (IValueParameter<EnumValue<OrchestrationMessage>>)Parameters[OrchestrationMessageParameterName]; }
44    //}
45    #endregion
46
47    [StorableConstructor]
48    protected OrchestratorNode(bool deserializing) : base(deserializing) { }
49    protected OrchestratorNode(OrchestratorNode original, Cloner cloner) : base(original, cloner) {
50      results = cloner.Clone(original.results);
51      parameters = cloner.Clone(original.parameters);
52      readOnlyParameters = null;
53    }
54    protected OrchestratorNode() : base("OrchestratorNode") {
55      results = new ResultCollection();
56      parameters = new ParameterCollection();
57      Parameters.Add(new ValueParameter<EnumValue<OrchestrationMessage>>("OrchestrationMessage", new EnumValue<OrchestrationMessage>()));
58      readOnlyParameters = null;
59    }
60    protected OrchestratorNode(string name) : base(name) {
61      results = new ResultCollection();
62      parameters = new ParameterCollection();
63      Parameters.Add(new ValueParameter<EnumValue<OrchestrationMessage>>("OrchestrationMessage", new EnumValue<OrchestrationMessage>()));
64      readOnlyParameters = null;
65    }
66    protected OrchestratorNode(string name, string description) : base(name, description) {
67      results = new ResultCollection();
68      parameters = new ParameterCollection();
69      Parameters.Add(new ValueParameter<EnumValue<OrchestrationMessage>>("OrchestrationMessage", new EnumValue<OrchestrationMessage>()));
70      readOnlyParameters = null;
71    }
72
73    protected void AddOrchestrationPort<T>(string solverName)
74        where T : class, IProblem {
75      var orchestrationPort = new ConfigurationPort(solverName + OrchestrationPortNameSuffix);
76      orchestrationPort.Parameters.Add(new PortParameter<EnumValue<OrchestrationMessage>>("OrchestrationMessage") {
77        Type = PortParameterType.Output
78      });
79      orchestrationPort.Parameters.Add(new PortParameter<T>("Problem") {
80        Type = PortParameterType.Output
81      });
82      orchestrationPort.Parameters.Add(new PortParameter<ResultCollection>("Results") {
83        Type = PortParameterType.Input
84      });
85      Ports.Add(orchestrationPort);
86    }
87
88    protected void AddEvaluationPort<T>(string solverName, string solutionName, string qualityName) where T : class, IItem {
89      var evaluationPort = new ConfigurationPort(solverName + EvaluationPortNameSuffix);
90      evaluationPort.Parameters.Add(new PortParameter<T>(solutionName) {
91        Type = PortParameterType.Input
92      });
93      evaluationPort.Parameters.Add(new PortParameter<DoubleValue>(qualityName) {
94        Type = PortParameterType.Input | PortParameterType.Output
95      });
96      Ports.Add(evaluationPort);
97    }
98
99    protected virtual void ProcessMessage(IMessage message, IMessagePort port, CancellationToken token) { }
100
101    public abstract void Prepare();
102    public async Task PrepareAsync() { await Task.Run(() => Prepare()); }
103    public abstract void Start();
104    public async Task StartAsync() { await Task.Run(() => Start()); }
105    public abstract void Pause();
106    public async Task PauseAsync() { await Task.Run(() => Pause()); }
107    public abstract void Stop();
108    public async Task StopAsync() { await Task.Run(() => Stop()); }
109
110    #region Events
111    protected override void Ports_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IPort> e) {
112      base.Ports_ItemsAdded(sender, e);
113      foreach (var p in e.Items)
114        RegisterPortEvents(p);
115    }
116    protected override void Ports_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IPort> e) {
117      base.Ports_ItemsRemoved(sender, e);
118      foreach (var p in e.Items)
119        DeregisterPortEvents(p);
120    }
121    protected override void Ports_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IPort> e) {
122      base.Ports_ItemsReplaced(sender, e);
123      foreach (var p in e.OldItems)
124        DeregisterPortEvents(p);
125      foreach (var p in e.Items)
126        RegisterPortEvents(p);
127    }
128    protected override void Ports_CollectionReset(object sender, CollectionItemsChangedEventArgs<IPort> e) {
129      base.Ports_CollectionReset(sender, e);
130      foreach (var p in e.OldItems)
131        DeregisterPortEvents(p);
132      foreach (var p in e.Items)
133        RegisterPortEvents(p);
134    }
135    private void MessagePort_MessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) {
136      ProcessMessage(e.Value, (IMessagePort)sender, e.Value2);
137    }
138    #endregion
139
140    #region Port Events
141    protected virtual void RegisterPortEvents(IPort port) {
142      var mp = port as IMessagePort;
143      if (mp != null)
144        mp.MessageReceived += MessagePort_MessageReceived;
145    }
146    protected virtual void DeregisterPortEvents(IPort port) {
147      var mp = port as IMessagePort;
148      if (mp != null)
149        mp.MessageReceived -= MessagePort_MessageReceived;
150    }
151    #endregion
152  }
153}
Note: See TracBrowser for help on using the repository browser.