Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/23/14 05:32:41 (9 years ago)
Author:
swagner
Message:

#2205: Continued working on programmable network items

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Optimization.Networks.KSPTSP/KSPTSPConnector.cs

    r11530 r11564  
    2323using HeuristicLab.Core;
    2424using HeuristicLab.Core.Networks;
    25 using HeuristicLab.Data;
    26 using HeuristicLab.Encodings.BinaryVectorEncoding;
    2725using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    28 using HeuristicLab.Problems.TravelingSalesman;
    29 using System.Linq;
    3026
    3127namespace HeuristicLab.Optimization.Networks.KSPTSP {
    3228  [Item("KSPTSPConnector", "A node of an optimization network which connects a KSP and a TSP.")]
    3329  [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
    3535    [StorableConstructor]
    3636    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) { }
    6141
    6242    public override IDeepCloneable Clone(Cloner cloner) {
    6343      return new KSPTSPConnector(this, cloner);
    6444    }
    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 parameters
    93       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 coordinates
    100       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 TSP
    114       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 costs
    122       ((DoubleValue)kspMsg["Quality"]).Value = kspQuality - factor * tspQuality;
    123       kspMsg["TransportCosts"] = new DoubleValue(factor * tspQuality);
    124       kspMsg["Route"] = tspSolution;
    125     }
    12645  }
    12746}
Note: See TracChangeset for help on using the changeset viewer.