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