[11519] | 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.Data;
|
---|
| 25 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[11520] | 27 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
[11519] | 28 | using System.Linq;
|
---|
| 29 |
|
---|
| 30 | namespace 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) { }
|
---|
[11520] | 36 | protected KSPTSPConnector(KSPTSPConnector original, Cloner cloner)
|
---|
| 37 | : base(original, cloner) {
|
---|
| 38 | RegisterEvents();
|
---|
| 39 | }
|
---|
[11519] | 40 | public KSPTSPConnector()
|
---|
| 41 | : base("KSPTSPConnector") {
|
---|
| 42 | Initialize();
|
---|
[11520] | 43 | RegisterEvents();
|
---|
[11519] | 44 | }
|
---|
| 45 | public KSPTSPConnector(string name)
|
---|
| 46 | : base(name) {
|
---|
| 47 | Initialize();
|
---|
[11520] | 48 | RegisterEvents();
|
---|
[11519] | 49 | }
|
---|
| 50 | public KSPTSPConnector(string name, string description)
|
---|
| 51 | : base(name, description) {
|
---|
| 52 | Initialize();
|
---|
[11520] | 53 | RegisterEvents();
|
---|
[11519] | 54 | }
|
---|
| 55 |
|
---|
[11520] | 56 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 57 | private void AfterDeserialization() {
|
---|
| 58 | RegisterEvents();
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[11519] | 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 });
|
---|
[11520] | 76 | ksp.Parameters.Add(new PortParameter<PathTSPTour>("Route") { Type = PortParameterType.Output });
|
---|
[11519] | 77 |
|
---|
| 78 | var tsp = new GenericPort("TSP Connector");
|
---|
| 79 | Ports.Add(tsp);
|
---|
| 80 | tsp.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") { Type = PortParameterType.Output });
|
---|
[11520] | 81 | tsp.Parameters.Add(new PortParameter<PathTSPTour>("Best TSP Solution") { Type = PortParameterType.Input });
|
---|
| 82 | tsp.Parameters.Add(new PortParameter<DoubleValue>("BestQuality") { Type = PortParameterType.Input });
|
---|
[11519] | 83 | }
|
---|
| 84 |
|
---|
[11520] | 85 | protected virtual void RegisterEvents() {
|
---|
| 86 | var ksp = (IGenericPort)Ports["KSP Connector"];
|
---|
| 87 | ksp.MessageReceived += Knapsack_MessageReceived;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[11519] | 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);
|
---|
[11520] | 117 | var tspSolution = (PathTSPTour)tspMsg["Best TSP Solution"];
|
---|
[11519] | 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);
|
---|
[11520] | 123 | kspMsg["Route"] = tspSolution;
|
---|
[11519] | 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|