Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Optimization.Networks.KSPTSP/KSPTSPConnectorCode.cs @ 11564

Last change on this file since 11564 was 11564, checked in by swagner, 10 years ago

#2205: Continued working on programmable network items

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