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

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

#2205: Worked on optimization networks

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