#pragma warning disable 436
#region License Information
/* HeuristicLab
* Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Core.Networks;
using HeuristicLab.Data;
using HeuristicLab.Encodings.BinaryVectorEncoding;
using HeuristicLab.Networks.Programmable;
using HeuristicLab.Problems.TravelingSalesman;
using System.Linq;
using System.Threading;
namespace HeuristicLab.Networks {
[Item("KSPTSPConnector", "A node of an optimization network which connects a KSP and a TSP.")]
public class CompiledKSPTSPConnector : ProgrammableNode.CompiledProgrammableNode {
protected CompiledKSPTSPConnector(CompiledKSPTSPConnector original, Cloner cloner) : base(original, cloner) { }
public CompiledKSPTSPConnector(ProgrammableNode context)
: base(context) {
if (Ports.Count == 0)
Initialize();
}
public override IDeepCloneable Clone(Cloner cloner) {
return new CompiledKSPTSPConnector(this, cloner);
}
public override void Initialize() {
base.Initialize();
var parameters = new MessagePort("Parameters");
Ports.Add(parameters);
parameters.Parameters.Add(new PortParameter("Cities") { Type = PortParameterType.Input });
parameters.Parameters.Add(new PortParameter("TransportCostsFactor") { Type = PortParameterType.Input });
var ksp = new MessagePort("KSP Connector");
Ports.Add(ksp);
ksp.Parameters.Add(new PortParameter("KnapsackSolution") { Type = PortParameterType.Input });
ksp.Parameters.Add(new PortParameter("Quality") { Type = PortParameterType.Input | PortParameterType.Output });
ksp.Parameters.Add(new PortParameter("TransportCosts") { Type = PortParameterType.Output });
ksp.Parameters.Add(new PortParameter("Route") { Type = PortParameterType.Output });
var tsp = new MessagePort("TSP Connector");
Ports.Add(tsp);
tsp.Parameters.Add(new PortParameter("Coordinates") { Type = PortParameterType.Output });
tsp.Parameters.Add(new PortParameter("Best TSP Solution") { Type = PortParameterType.Input });
tsp.Parameters.Add(new PortParameter("BestQuality") { Type = PortParameterType.Input });
}
public override void RegisterEvents() {
base.RegisterEvents();
var ksp = (IMessagePort)Ports["KSP Connector"];
ksp.MessageReceived += Knapsack_MessageReceived;
}
public override void DeregisterEvents() {
var ksp = (IMessagePort)Ports["KSP Connector"];
ksp.MessageReceived -= Knapsack_MessageReceived;
base.DeregisterEvents();
}
private void Knapsack_MessageReceived(object sender, EventArgs e) {
// get parameters
var parametersPort = (IMessagePort)Ports["Parameters"];
var parameters = parametersPort.PrepareMessage();
parametersPort.SendMessage(parameters, e.Value2);
var cities = (DoubleMatrix)parameters["Cities"];
var factor = ((DoubleValue)parameters["TransportCostsFactor"]).Value;
// build coordinates
var kspMsg = e.Value;
var kspSolution = (BinaryVector)kspMsg["KnapsackSolution"];
var kspQuality = ((DoubleValue)kspMsg["Quality"]).Value;
var coords = new DoubleMatrix(kspSolution.Count(x => x), 2);
int j = 0;
for (int i = 0; i < kspSolution.Length; i++) {
if (kspSolution[i]) {
coords[j, 0] = cities[i, 0];
coords[j, 1] = cities[i, 1];
j++;
}
}
// solve TSP
var tspConnectorPort = (IMessagePort)Ports["TSP Connector"];
var tspMsg = tspConnectorPort.PrepareMessage();
tspMsg["Coordinates"] = coords;
tspConnectorPort.SendMessage(tspMsg, e.Value2);
var tspSolution = (PathTSPTour)tspMsg["Best TSP Solution"];
var tspQuality = ((DoubleValue)tspMsg["BestQuality"]).Value;
// calculate transport costs
((DoubleValue)kspMsg["Quality"]).Value = kspQuality - factor * tspQuality;
kspMsg["TransportCosts"] = new DoubleValue(factor * tspQuality);
kspMsg["Route"] = tspSolution;
}
}
}