Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Optimization.Networks.KSPTSP/KSPTSPConnector.cs @ 11530

Last change on this file since 11530 was 11530, checked in by swagner, 9 years ago

#2205: Implemented review comments

  • renamed GenericPort to MessagePort
  • refactored CanConnectToPort
  • refactored PrepareMessage
  • removed IConnectedPort

Additional changes:

  • added UserDefinedMessagePort
  • refactored CloneConnectedPortParameters to CloneParametersFromPort and moved it to ParameterizedPort
  • added ports to NetworkView
File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Core.Networks;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.BinaryVectorEncoding;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.TravelingSalesman;
29using System.Linq;
30
31namespace HeuristicLab.Optimization.Networks.KSPTSP {
32  [Item("KSPTSPConnector", "A node of an optimization network which connects a KSP and a TSP.")]
33  [StorableClass]
34  public class KSPTSPConnector : Node {
35    [StorableConstructor]
36    protected KSPTSPConnector(bool deserializing) : base(deserializing) { }
37    protected KSPTSPConnector(KSPTSPConnector original, Cloner cloner)
38      : base(original, cloner) {
39      RegisterEvents();
40    }
41    public KSPTSPConnector()
42      : base("KSPTSPConnector") {
43      Initialize();
44      RegisterEvents();
45    }
46    public KSPTSPConnector(string name)
47      : base(name) {
48      Initialize();
49      RegisterEvents();
50    }
51    public KSPTSPConnector(string name, string description)
52      : base(name, description) {
53      Initialize();
54      RegisterEvents();
55    }
56
57    [StorableHook(HookType.AfterDeserialization)]
58    private void AfterDeserialization() {
59      RegisterEvents();
60    }
61
62    public override IDeepCloneable Clone(Cloner cloner) {
63      return new KSPTSPConnector(this, cloner);
64    }
65
66    protected virtual void Initialize() {
67      var parameters = new MessagePort("Parameters");
68      Ports.Add(parameters);
69      parameters.Parameters.Add(new PortParameter<DoubleMatrix>("Cities") { Type = PortParameterType.Input });
70      parameters.Parameters.Add(new PortParameter<DoubleValue>("TransportCostsFactor") { Type = PortParameterType.Input });
71
72      var ksp = new MessagePort("KSP Connector");
73      Ports.Add(ksp);
74      ksp.Parameters.Add(new PortParameter<BinaryVector>("KnapsackSolution") { Type = PortParameterType.Input });
75      ksp.Parameters.Add(new PortParameter<DoubleValue>("Quality") { Type = PortParameterType.Input | PortParameterType.Output });
76      ksp.Parameters.Add(new PortParameter<DoubleValue>("TransportCosts") { Type = PortParameterType.Output });
77      ksp.Parameters.Add(new PortParameter<PathTSPTour>("Route") { Type = PortParameterType.Output });
78
79      var tsp = new MessagePort("TSP Connector");
80      Ports.Add(tsp);
81      tsp.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") { Type = PortParameterType.Output });
82      tsp.Parameters.Add(new PortParameter<PathTSPTour>("Best TSP Solution") { Type = PortParameterType.Input });
83      tsp.Parameters.Add(new PortParameter<DoubleValue>("BestQuality") { Type = PortParameterType.Input });
84    }
85
86    protected virtual void RegisterEvents() {
87      var ksp = (IMessagePort)Ports["KSP Connector"];
88      ksp.MessageReceived += Knapsack_MessageReceived;
89    }
90
91    protected virtual void Knapsack_MessageReceived(object sender, EventArgs<IMessage, System.Threading.CancellationToken> e) {
92      // get parameters
93      var parametersPort = (IMessagePort)Ports["Parameters"];
94      var parameters = parametersPort.PrepareMessage();
95      parametersPort.SendMessage(parameters, e.Value2);
96      var cities = (DoubleMatrix)parameters["Cities"];
97      var factor = ((DoubleValue)parameters["TransportCostsFactor"]).Value;
98
99      // build coordinates
100      var kspMsg = e.Value;
101      var kspSolution = (BinaryVector)kspMsg["KnapsackSolution"];
102      var kspQuality = ((DoubleValue)kspMsg["Quality"]).Value;
103      var coords = new DoubleMatrix(kspSolution.Count(x => x), 2);
104      int j = 0;
105      for (int i = 0; i < kspSolution.Length; i++) {
106        if (kspSolution[i]) {
107          coords[j, 0] = cities[i, 0];
108          coords[j, 1] = cities[i, 1];
109          j++;
110        }
111      }
112
113      // solve TSP
114      var tspConnectorPort = (IMessagePort)Ports["TSP Connector"];
115      var tspMsg = tspConnectorPort.PrepareMessage();
116      tspMsg["Coordinates"] = coords;
117      tspConnectorPort.SendMessage(tspMsg, e.Value2);
118      var tspSolution = (PathTSPTour)tspMsg["Best TSP Solution"];
119      var tspQuality = ((DoubleValue)tspMsg["BestQuality"]).Value;
120
121      // calculate transport costs
122      ((DoubleValue)kspMsg["Quality"]).Value = kspQuality - factor * tspQuality;
123      kspMsg["TransportCosts"] = new DoubleValue(factor * tspQuality);
124      kspMsg["Route"] = tspSolution;
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.