source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Nodes/KSPTSPConnector.cs @ 11520

Last change on this file since 11520 was 11520, checked in by swagner, 8 years ago

#2205: Worked on optimization networks

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