Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks/3.3/KSPTSPConnectorCode.cs @ 11577

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

#2205: Restructured solution and projects and switched all projects to .NET 4.5

File size: 5.1 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.Networks.Programmable;
28using HeuristicLab.Problems.TravelingSalesman;
29using System.Drawing;
30using System.Linq;
31using System.Threading;
32
33namespace HeuristicLab.Networks {
34  [Item("KSPTSPConnector", "A node of an optimization network which connects a KSP and a TSP.")]
35  public class CompiledKSPTSPConnector : ProgrammableNode.CompiledProgrammableNode {
36    public static new Image StaticItemImage {
37      get { return HeuristicLab.Common.Resources.VSImageLibrary.RadialChart; }
38    }
39
40    protected CompiledKSPTSPConnector(CompiledKSPTSPConnector original, Cloner cloner) : base(original, cloner) { }
41    public CompiledKSPTSPConnector(ProgrammableNode context)
42      : base(context) {
43      if (Ports.Count == 0)
44        Initialize();
45    }
46
47    public override IDeepCloneable Clone(Cloner cloner) {
48      return new CompiledKSPTSPConnector(this, cloner);
49    }
50
51    public override void Initialize() {
52      base.Initialize();
53      var parameters = new MessagePort("Parameters");
54      Ports.Add(parameters);
55      parameters.Parameters.Add(new PortParameter<DoubleMatrix>("Cities") { Type = PortParameterType.Input });
56      parameters.Parameters.Add(new PortParameter<DoubleValue>("TransportCostsFactor") { Type = PortParameterType.Input });
57
58      var ksp = new MessagePort("KSP Connector");
59      Ports.Add(ksp);
60      ksp.Parameters.Add(new PortParameter<BinaryVector>("KnapsackSolution") { Type = PortParameterType.Input });
61      ksp.Parameters.Add(new PortParameter<DoubleValue>("Quality") { Type = PortParameterType.Input | PortParameterType.Output });
62      ksp.Parameters.Add(new PortParameter<DoubleValue>("TransportCosts") { Type = PortParameterType.Output });
63      ksp.Parameters.Add(new PortParameter<PathTSPTour>("Route") { Type = PortParameterType.Output });
64
65      var tsp = new MessagePort("TSP Connector");
66      Ports.Add(tsp);
67      tsp.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") { Type = PortParameterType.Output });
68      tsp.Parameters.Add(new PortParameter<PathTSPTour>("Best TSP Solution") { Type = PortParameterType.Input });
69      tsp.Parameters.Add(new PortParameter<DoubleValue>("BestQuality") { Type = PortParameterType.Input });
70    }
71
72    public override void RegisterEvents() {
73      base.RegisterEvents();
74      var ksp = (IMessagePort)Ports["KSP Connector"];
75      ksp.MessageReceived += Knapsack_MessageReceived;
76    }
77    public override void DeregisterEvents() {
78      var ksp = (IMessagePort)Ports["KSP Connector"];
79      ksp.MessageReceived -= Knapsack_MessageReceived;
80      base.DeregisterEvents();
81    }
82
83    private void Knapsack_MessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) {
84      // get parameters
85      var parametersPort = (IMessagePort)Ports["Parameters"];
86      var parameters = parametersPort.PrepareMessage();
87      parametersPort.SendMessage(parameters, e.Value2);
88      var cities = (DoubleMatrix)parameters["Cities"];
89      var factor = ((DoubleValue)parameters["TransportCostsFactor"]).Value;
90
91      // build coordinates
92      var kspMsg = e.Value;
93      var kspSolution = (BinaryVector)kspMsg["KnapsackSolution"];
94      var kspQuality = ((DoubleValue)kspMsg["Quality"]).Value;
95      var coords = new DoubleMatrix(kspSolution.Count(x => x), 2);
96      int j = 0;
97      for (int i = 0; i < kspSolution.Length; i++) {
98        if (kspSolution[i]) {
99          coords[j, 0] = cities[i, 0];
100          coords[j, 1] = cities[i, 1];
101          j++;
102        }
103      }
104
105      // solve TSP
106      var tspConnectorPort = (IMessagePort)Ports["TSP Connector"];
107      var tspMsg = tspConnectorPort.PrepareMessage();
108      tspMsg["Coordinates"] = coords;
109      tspConnectorPort.SendMessage(tspMsg, e.Value2);
110      var tspSolution = (PathTSPTour)tspMsg["Best TSP Solution"];
111      var tspQuality = ((DoubleValue)tspMsg["BestQuality"]).Value;
112
113      // calculate transport costs
114      ((DoubleValue)kspMsg["Quality"]).Value = kspQuality - factor * tspQuality;
115      kspMsg["TransportCosts"] = new DoubleValue(factor * tspQuality);
116      kspMsg["Route"] = tspSolution;
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.