Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2205_OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization/3.3/OrchestratorNode.cs @ 17026

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

#2205: worked on optimization networks

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