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