Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/26/17 14:12:39 (7 years ago)
Author:
jkarder
Message:

#2205: worked on optimization networks

  • added abstract base classes for ttp networks/orchestrators
  • removed ttp networks/orchestrators from HeuristicLab.Networks.IntegratedOptimization
  • runs can now be cleared when preparing OrchestratedAlgorithmNodes
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpOrchestratorNode2.cs

    r14604 r14610  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.Linq;
    25 using System.Threading;
    2624using HeuristicLab.Common;
    2725using HeuristicLab.Core;
     
    2927using HeuristicLab.Data;
    3028using HeuristicLab.Encodings.BinaryVectorEncoding;
    31 using HeuristicLab.Encodings.PermutationEncoding;
    3229using HeuristicLab.Encodings.RealVectorEncoding;
    33 using HeuristicLab.Operators;
    3430using HeuristicLab.Optimization;
    35 using HeuristicLab.Parameters;
    3631using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3732using HeuristicLab.Problems.Knapsack;
     
    4136  [Item("TtpOrchestratorNode2", "Orchestrator for TTP optimization network version 2.")]
    4237  [StorableClass]
    43   public sealed class TtpOrchestratorNode2 : OrchestratorNode {
    44     #region Constants
    45     private const string InstanceParameterName = "Instance";
    46     private const string TspParameterName = "TSP";
    47     private const string KspParameterName = "KSP";
    48     private const string AvailabilityParameterName = "Availability";
    49     private const string MinSpeedParameterName = "MinSpeed";
    50     private const string MaxSpeedParameterName = "MaxSpeed";
    51     private const string RentingRatioParameterName = "RentingRatio";
    52     private const string MetaSolverName = "MetaSolver";
    53     private const string TspSolverName = "TspSolver";
    54     private const string KspSolverName = "KspSolver";
    55     #endregion
    56 
    57     private CancellationTokenSource cts;
    58     private ResultCollection tspResults, kspResults;
    59 
    60     [Storable]
    61     double[,] tspCoordinates;
    62     [Storable]
    63     TtpUtils.DistanceType distanceType;
    64     [Storable]
    65     int kspCapacity;
    66     [Storable]
    67     int[] kspItemWeights, kspItemValues, ttpAvailability;
    68     [Storable]
    69     double ttpMinSpeed, ttpMaxSpeed, ttpRentingRatio;
    70 
    71     #region Parameters
    72     public IFixedValueParameter<TextFileValue> InstanceParameter {
    73       get { return (IFixedValueParameter<TextFileValue>)Parameters[InstanceParameterName]; }
    74     }
    75 
    76     public IValueParameter<TravelingSalesmanProblem> TspParameter {
    77       get { return (IValueParameter<TravelingSalesmanProblem>)Parameters[TspParameterName]; }
    78     }
    79 
    80     public IValueParameter<BinaryKnapsackProblem> KspParameter {
    81       get { return (IValueParameter<BinaryKnapsackProblem>)Parameters[KspParameterName]; }
    82     }
    83 
    84     public IValueParameter<IntArray> AvailabilityParameter {
    85       get { return (IValueParameter<IntArray>)Parameters[AvailabilityParameterName]; }
    86     }
    87 
    88     public IValueParameter<DoubleValue> MinSpeedParameter {
    89       get { return (IValueParameter<DoubleValue>)Parameters[MinSpeedParameterName]; }
    90     }
    91 
    92     public IValueParameter<DoubleValue> MaxSpeedParameter {
    93       get { return (IValueParameter<DoubleValue>)Parameters[MaxSpeedParameterName]; }
    94     }
    95 
    96     public IValueParameter<DoubleValue> RentingRatioParameter {
    97       get { return (IValueParameter<DoubleValue>)Parameters[RentingRatioParameterName]; }
    98     }
    99     #endregion
    100 
    101     #region Ports
    102     public IMessagePort MetaSolverOrchestrationPort {
    103       get { return (IMessagePort)Ports[MetaSolverName + OrchestrationPortNameSuffix]; }
    104     }
    105 
    106     public IMessagePort MetaSolverEvaluationPort {
    107       get { return (IMessagePort)Ports[MetaSolverName + EvaluationPortNameSuffix]; }
    108     }
    109 
    110     public IMessagePort TspSolverOrchestrationPort {
    111       get { return (IMessagePort)Ports[TspSolverName + OrchestrationPortNameSuffix]; }
    112     }
    113 
    114     public IMessagePort TspSolverEvaluationPort {
    115       get { return (IMessagePort)Ports[TspSolverName + EvaluationPortNameSuffix]; }
    116     }
    117 
    118     public IMessagePort KspSolverOrchestrationPort {
    119       get { return (IMessagePort)Ports[KspSolverName + OrchestrationPortNameSuffix]; }
    120     }
    121 
    122     public IMessagePort KspSolverEvaluationPort {
    123       get { return (IMessagePort)Ports[KspSolverName + EvaluationPortNameSuffix]; }
    124     }
    125     #endregion
    126 
     38  public sealed class TtpOrchestratorNode2 : TtpOrchestratorNode {
    12739    [StorableConstructor]
    12840    private TtpOrchestratorNode2(bool deserializing) : base(deserializing) { }
    129     private TtpOrchestratorNode2(TtpOrchestratorNode2 original, Cloner cloner) : base(original, cloner) {
    130       tspCoordinates = (double[,])original.tspCoordinates.Clone();
    131       kspCapacity = original.kspCapacity;
    132       kspItemWeights = (int[])original.kspItemWeights.Clone();
    133       kspItemValues = (int[])original.kspItemValues.Clone();
    134       ttpAvailability = (int[])original.ttpAvailability.Clone();
    135       ttpMinSpeed = original.ttpMinSpeed;
    136       ttpMaxSpeed = original.ttpMaxSpeed;
    137       ttpRentingRatio = original.ttpRentingRatio;
    138 
    139       RegisterEvents();
    140     }
     41    private TtpOrchestratorNode2(TtpOrchestratorNode2 original, Cloner cloner) : base(original, cloner) { }
    14142    public TtpOrchestratorNode2() : this("TtpOrchestratorNode2") { }
    142     public TtpOrchestratorNode2(string name) : base(name) {
    143       #region Configure Parameters
    144       Parameters.Add(new FixedValueParameter<TextFileValue>(InstanceParameterName));
    145       Parameters.Add(new ValueParameter<TravelingSalesmanProblem>(TspParameterName, new TravelingSalesmanProblem()));
    146       Parameters.Add(new ValueParameter<BinaryKnapsackProblem>(KspParameterName, new BinaryKnapsackProblem()));
    147       Parameters.Add(new ValueParameter<IntArray>(AvailabilityParameterName));
    148       Parameters.Add(new ValueParameter<DoubleValue>(MinSpeedParameterName, new DoubleValue(0.1)));
    149       Parameters.Add(new ValueParameter<DoubleValue>(MaxSpeedParameterName, new DoubleValue(1.0)));
    150       Parameters.Add(new ValueParameter<DoubleValue>(RentingRatioParameterName, new DoubleValue(0.5)));
    151       #endregion
    152 
    153       #region Configure Ports
    154       AddOrchestrationPort<VariegationProblem>(MetaSolverName);
    155       AddEvaluationPort<RealVector>(MetaSolverName, "RealVector", "Quality");
    156       AddOrchestrationPort<TravelingSalesmanProblem>(TspSolverName);
    157       AddEvaluationPort<Permutation>(TspSolverName, "TSPTour", "TSPTourLength");
    158       AddOrchestrationPort<LootProfitProblem>(KspSolverName);
    159       AddEvaluationPort<BinaryVector>(KspSolverName, "KnapsackSolution", "Quality");
    160 
    161       RegisterEvents();
    162       #endregion
    163     }
     43    public TtpOrchestratorNode2(string name) : base(name) { }
    16444
    16545    public override IDeepCloneable Clone(Cloner cloner) {
     
    16747    }
    16848
    169     [StorableHook(HookType.AfterDeserialization)]
    170     private void AfterDeserialization() {
    171       RegisterEvents();
    172     }
    173 
    174     private void RegisterEvents() {
    175       InstanceParameter.Value.ToStringChanged += InstanceParameter_Value_ToStringChanged;
    176       MetaSolverOrchestrationPort.ConnectedPortChanged += MetaSolverOrchestrationPort_ConnectedPortChanged;
    177       TspSolverOrchestrationPort.ConnectedPortChanged += TspSolverOrchestrationPort_ConnectedPortChanged;
    178       KspSolverOrchestrationPort.ConnectedPortChanged += KspSolverOrchestrationPort_ConnectedPortChanged;
    179     }
    180 
    181     private void InstanceParameter_Value_ToStringChanged(object sender, EventArgs e) {
    182       string filePath = InstanceParameter.Value.Value;
    183       TtpUtils.Import(filePath, out tspCoordinates, out distanceType,
    184                                 out kspCapacity, out kspItemValues, out kspItemWeights,
    185                                 out ttpAvailability, out ttpMinSpeed, out ttpMaxSpeed, out ttpRentingRatio);
    186 
    187       var tsp = TspParameter.Value;
    188       tsp.Coordinates = new DoubleMatrix(tspCoordinates);
    189 
    190       var ksp = KspParameter.Value;
    191       ksp.KnapsackCapacity.Value = kspCapacity;
    192       ksp.Encoding.Length = kspItemValues.Length;
    193       ksp.Values = new IntArray(kspItemValues);
    194       ksp.Weights = new IntArray(kspItemWeights);
    195 
    196       AvailabilityParameter.Value = new IntArray(ttpAvailability);
    197       MinSpeedParameter.Value.Value = ttpMinSpeed;
    198       MaxSpeedParameter.Value.Value = ttpMaxSpeed;
    199       RentingRatioParameter.Value.Value = ttpRentingRatio;
    200     }
    201 
    202     public override void Prepare() {
    203       Results.Clear();
    204 
    205       var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
    206       metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.QualityAdaption);
    207       var problem = new VariegationProblem();
    208       problem.Encoding.Length = TspParameter.Value.Coordinates.Rows * 2;
    209       problem.Encoding.Bounds = new DoubleMatrix(new[,] { { -1.0, 1.0 } });
    210       metaMsg["Problem"] = problem;
    211       MetaSolverOrchestrationPort.SendMessage(metaMsg);
    212     }
    213 
    214     public override void Start() {
    215       cts = new CancellationTokenSource();
    216 
    217       try {
    218         var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
    219         metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Start);
    220         MetaSolverOrchestrationPort.SendMessage(metaMsg);
    221       } catch (Exception e) { }
    222     }
    223 
    224     public override void Pause() {
    225       cts.Cancel();
    226 
    227       var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
    228       metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Pause);
    229       MetaSolverOrchestrationPort.SendMessage(metaMsg);
    230 
    231       var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
    232       tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
    233       TspSolverOrchestrationPort.SendMessage(tspMsg);
    234 
    235       var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
    236       kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
    237       KspSolverOrchestrationPort.SendMessage(kspMsg);
    238     }
    239 
    240     public override void Stop() {
    241       cts.Cancel();
    242 
    243       var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
    244       metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
    245       MetaSolverOrchestrationPort.SendMessage(metaMsg);
    246 
    247       var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
    248       tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
    249       TspSolverOrchestrationPort.SendMessage(tspMsg);
    250 
    251       var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
    252       kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
    253       KspSolverOrchestrationPort.SendMessage(kspMsg);
    254     }
    255 
    256     protected override void ProcessMessage(IMessage message, IMessagePort port, CancellationToken token) {
    257       var messageActions = new Dictionary<IMessagePort, Action<IMessage>>();
    258       messageActions.Add(MetaSolverOrchestrationPort, MetaSolverOrchestrationPortMessage);
    259       messageActions.Add(MetaSolverEvaluationPort, MetaSolverEvaluationPortMessage);
    260       messageActions.Add(TspSolverOrchestrationPort, TspSolverOrchestrationPortMessage);
    261       messageActions.Add(TspSolverEvaluationPort, TspSolverEvaluationPortMessage);
    262       messageActions.Add(KspSolverOrchestrationPort, KspSolverOrchestrationPortMessage);
    263       messageActions.Add(KspSolverEvaluationPort, KspSolverEvaluationPortMessage);
    264 
    265       messageActions[port](message);
    266 
    267       base.ProcessMessage(message, port, token);
    268     }
    269 
    27049    #region MetaSolver Message Handling
    271     private void MetaSolverOrchestrationPortMessage(IMessage message) { }
    272 
    273     private void MetaSolverEvaluationPortMessage(IMessage message) {
     50    protected override void MetaSolverEvaluationPortMessage(IMessage message) {
    27451      var factors = (RealVector)message["RealVector"];
    27552
     
    28158
    28259      var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
    283       tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.Start);
     60      tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
    28461      tspMsg["Problem"] = tsp;
    28562      TspSolverOrchestrationPort.SendMessage(tspMsg);
     
    29168
    29269      var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
    293       kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.Start);
     70      kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
    29471      var lpp = new LootProfitProblem {
    29572        Tsp = (TravelingSalesmanProblem)TspParameter.Value.Clone(),
     
    325102        Results.Add(new Result("Best Tour", tour));
    326103        Results.Add(new Result("Best Loot", loot));
    327       } else if (((DoubleValue)bestQuality.Value).Value < objectiveValue) {
     104      } else if (objectiveValue > ((DoubleValue)bestQuality.Value).Value) {
    328105        ((DoubleValue)bestQuality.Value).Value = objectiveValue;
    329106        Results["Best Tour"].Value = tour;
     
    333110    }
    334111    #endregion
    335 
    336     #region TspSolver Message Handling
    337     private void TspSolverOrchestrationPortMessage(IMessage message) {
    338       var results = (ResultCollection)message["Results"];
    339       if (results.ContainsKey("Best TSP Solution")) {
    340         tspResults = results;
    341       }
    342     }
    343 
    344     private void TspSolverEvaluationPortMessage(IMessage message) { }
    345     #endregion
    346 
    347     #region KspSolver Message Handling
    348     private void KspSolverOrchestrationPortMessage(IMessage message) {
    349       var results = (ResultCollection)message["Results"];
    350       if (results.ContainsKey("Best Solution")) {
    351         kspResults = results;
    352       }
    353     }
    354 
    355     private void KspSolverEvaluationPortMessage(IMessage message) { }
    356     #endregion
    357 
    358     #region Event Handlers
    359     private void MetaSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
    360       if (MetaSolverOrchestrationPort.ConnectedPort == null) return;
    361 
    362       var node = MetaSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
    363       if (node == null) return;
    364 
    365       var hook = new HookOperator { Name = "Meta Eval Hook" };
    366       hook.Parameters.Add(new LookupParameter<RealVector>("RealVector") { Hidden = true });
    367       hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = true });
    368       node.EvalHook = hook;
    369 
    370       node.OrchestrationPort.CloneParametersFromPort(MetaSolverOrchestrationPort);
    371       node.EvaluationPort.CloneParametersFromPort(MetaSolverEvaluationPort);
    372       node.EvaluationPort.ConnectedPort = MetaSolverEvaluationPort;
    373     }
    374 
    375     private void TspSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
    376       if (TspSolverOrchestrationPort.ConnectedPort == null) return;
    377 
    378       var node = TspSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
    379       if (node == null) return;
    380 
    381       var hook = new HookOperator { Name = "TSP Eval Hook" };
    382       hook.Parameters.Add(new LookupParameter<Permutation>("TSPTour") { Hidden = true });
    383       hook.Parameters.Add(new LookupParameter<DoubleValue>("TSPTourLength") { Hidden = true });
    384       node.EvalHook = hook;
    385 
    386       node.OrchestrationPort.CloneParametersFromPort(TspSolverOrchestrationPort);
    387       node.EvaluationPort.CloneParametersFromPort(TspSolverEvaluationPort);
    388       node.EvaluationPort.ConnectedPort = TspSolverEvaluationPort;
    389     }
    390 
    391     private void KspSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
    392       if (KspSolverOrchestrationPort.ConnectedPort == null) return;
    393 
    394       var node = KspSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
    395       if (node == null) return;
    396 
    397       var hook = new HookOperator { Name = "KSP Eval Hook" };
    398       hook.Parameters.Add(new LookupParameter<BinaryVector>("KnapsackSolution") { Hidden = true });
    399       hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = true });
    400       node.EvalHook = hook;
    401 
    402       node.OrchestrationPort.CloneParametersFromPort(KspSolverOrchestrationPort);
    403       node.EvaluationPort.CloneParametersFromPort(KspSolverEvaluationPort);
    404       node.EvaluationPort.ConnectedPort = KspSolverEvaluationPort;
    405     }
    406     #endregion
    407112  }
    408113}
Note: See TracChangeset for help on using the changeset viewer.