Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2205_OptimizationNetworks/HeuristicLab.Networks/3.3/KSPTSPConnectorCode.cs @ 17169

Last change on this file since 17169 was 12945, checked in by jkarder, 9 years ago

#2205: disabled compiler warning cs0436 for all code resources

File size: 5.0 KB
Line 
1#pragma warning disable 436
2
3#region License Information
4/* HeuristicLab
5 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
6 *
7 * This file is part of HeuristicLab.
8 *
9 * HeuristicLab is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * HeuristicLab is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
21 */
22#endregion
23
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Core.Networks;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.BinaryVectorEncoding;
29using HeuristicLab.Networks.Programmable;
30using HeuristicLab.Problems.TravelingSalesman;
31using System.Linq;
32using System.Threading;
33
34namespace HeuristicLab.Networks {
35  [Item("KSPTSPConnector", "A node of an optimization network which connects a KSP and a TSP.")]
36  public class CompiledKSPTSPConnector : ProgrammableNode.CompiledProgrammableNode {
37    protected CompiledKSPTSPConnector(CompiledKSPTSPConnector original, Cloner cloner) : base(original, cloner) { }
38    public CompiledKSPTSPConnector(ProgrammableNode context)
39      : base(context) {
40      if (Ports.Count == 0)
41        Initialize();
42    }
43
44    public override IDeepCloneable Clone(Cloner cloner) {
45      return new CompiledKSPTSPConnector(this, cloner);
46    }
47
48    public override void Initialize() {
49      base.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, 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.