Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2205: Continued working on programmable network items

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