[14610] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2017 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 System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Threading;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Core.Networks;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
| 30 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 31 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 32 | using HeuristicLab.Operators;
|
---|
| 33 | using HeuristicLab.Optimization;
|
---|
| 34 | using HeuristicLab.Parameters;
|
---|
| 35 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 36 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
| 37 |
|
---|
| 38 | namespace HeuristicLab.Networks.IntegratedOptimization.TravelingThief {
|
---|
| 39 | [Item("TtpOrchestratorNode", "An abstract base class for orchestrators used in TTP optimization networks.")]
|
---|
| 40 | [StorableClass]
|
---|
| 41 | public abstract class TtpOrchestratorNode : OrchestratorNode {
|
---|
| 42 | #region Constants
|
---|
| 43 | private const string InstanceParameterName = "Instance";
|
---|
| 44 | private const string TspParameterName = "TSP";
|
---|
| 45 | private const string KspParameterName = "KSP";
|
---|
| 46 | private const string AvailabilityParameterName = "Availability";
|
---|
| 47 | private const string MinSpeedParameterName = "MinSpeed";
|
---|
| 48 | private const string MaxSpeedParameterName = "MaxSpeed";
|
---|
| 49 | private const string RentingRatioParameterName = "RentingRatio";
|
---|
| 50 | private const string MetaSolverName = "MetaSolver";
|
---|
| 51 | private const string TspSolverName = "TspSolver";
|
---|
| 52 | private const string KspSolverName = "KspSolver";
|
---|
| 53 | #endregion
|
---|
| 54 |
|
---|
| 55 | protected CancellationTokenSource cts;
|
---|
| 56 | protected ResultCollection tspResults, kspResults;
|
---|
| 57 |
|
---|
| 58 | [Storable]
|
---|
| 59 | protected double[,] tspCoordinates = new double[0, 0];
|
---|
| 60 | [Storable]
|
---|
| 61 | protected TtpUtils.DistanceType distanceType;
|
---|
| 62 | [Storable]
|
---|
| 63 | protected int kspCapacity;
|
---|
| 64 | [Storable]
|
---|
| 65 | protected int[] kspItemWeights = new int[0], kspItemValues = new int[0], ttpAvailability = new int[0];
|
---|
| 66 | [Storable]
|
---|
| 67 | protected double ttpMinSpeed, ttpMaxSpeed, ttpRentingRatio;
|
---|
| 68 |
|
---|
| 69 | #region Parameters
|
---|
| 70 | public IFixedValueParameter<TextFileValue> InstanceParameter {
|
---|
| 71 | get { return (IFixedValueParameter<TextFileValue>)Parameters[InstanceParameterName]; }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | public IValueParameter<TravelingSalesmanProblem> TspParameter {
|
---|
| 75 | get { return (IValueParameter<TravelingSalesmanProblem>)Parameters[TspParameterName]; }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | public IValueParameter<BinaryKnapsackProblem> KspParameter {
|
---|
| 79 | get { return (IValueParameter<BinaryKnapsackProblem>)Parameters[KspParameterName]; }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | public IValueParameter<IntArray> AvailabilityParameter {
|
---|
| 83 | get { return (IValueParameter<IntArray>)Parameters[AvailabilityParameterName]; }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | public IValueParameter<DoubleValue> MinSpeedParameter {
|
---|
| 87 | get { return (IValueParameter<DoubleValue>)Parameters[MinSpeedParameterName]; }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | public IValueParameter<DoubleValue> MaxSpeedParameter {
|
---|
| 91 | get { return (IValueParameter<DoubleValue>)Parameters[MaxSpeedParameterName]; }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | public IValueParameter<DoubleValue> RentingRatioParameter {
|
---|
| 95 | get { return (IValueParameter<DoubleValue>)Parameters[RentingRatioParameterName]; }
|
---|
| 96 | }
|
---|
| 97 | #endregion
|
---|
| 98 |
|
---|
| 99 | #region Ports
|
---|
| 100 | public IMessagePort MetaSolverOrchestrationPort {
|
---|
| 101 | get { return (IMessagePort)Ports[MetaSolverName + OrchestrationPortNameSuffix]; }
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | public IMessagePort MetaSolverEvaluationPort {
|
---|
| 105 | get { return (IMessagePort)Ports[MetaSolverName + EvaluationPortNameSuffix]; }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | public IMessagePort TspSolverOrchestrationPort {
|
---|
| 109 | get { return (IMessagePort)Ports[TspSolverName + OrchestrationPortNameSuffix]; }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | public IMessagePort TspSolverEvaluationPort {
|
---|
| 113 | get { return (IMessagePort)Ports[TspSolverName + EvaluationPortNameSuffix]; }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | public IMessagePort KspSolverOrchestrationPort {
|
---|
| 117 | get { return (IMessagePort)Ports[KspSolverName + OrchestrationPortNameSuffix]; }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | public IMessagePort KspSolverEvaluationPort {
|
---|
| 121 | get { return (IMessagePort)Ports[KspSolverName + EvaluationPortNameSuffix]; }
|
---|
| 122 | }
|
---|
| 123 | #endregion
|
---|
| 124 |
|
---|
| 125 | [StorableConstructor]
|
---|
| 126 | protected TtpOrchestratorNode(bool deserializing) : base(deserializing) { }
|
---|
| 127 | protected TtpOrchestratorNode(TtpOrchestratorNode original, Cloner cloner) : base(original, cloner) {
|
---|
| 128 | tspCoordinates = (double[,])original.tspCoordinates.Clone();
|
---|
| 129 | kspCapacity = original.kspCapacity;
|
---|
| 130 | kspItemWeights = (int[])original.kspItemWeights.Clone();
|
---|
| 131 | kspItemValues = (int[])original.kspItemValues.Clone();
|
---|
| 132 | ttpAvailability = (int[])original.ttpAvailability.Clone();
|
---|
| 133 | ttpMinSpeed = original.ttpMinSpeed;
|
---|
| 134 | ttpMaxSpeed = original.ttpMaxSpeed;
|
---|
| 135 | ttpRentingRatio = original.ttpRentingRatio;
|
---|
| 136 |
|
---|
| 137 | RegisterEvents();
|
---|
| 138 | }
|
---|
| 139 | protected TtpOrchestratorNode() : this("TtpOrchestratorNode") { }
|
---|
| 140 | protected TtpOrchestratorNode(string name) : base(name) {
|
---|
| 141 | #region Configure Parameters
|
---|
| 142 | Parameters.Add(new FixedValueParameter<TextFileValue>(InstanceParameterName));
|
---|
| 143 | Parameters.Add(new ValueParameter<TravelingSalesmanProblem>(TspParameterName, new TravelingSalesmanProblem()));
|
---|
| 144 | Parameters.Add(new ValueParameter<BinaryKnapsackProblem>(KspParameterName, new BinaryKnapsackProblem()));
|
---|
| 145 | Parameters.Add(new ValueParameter<IntArray>(AvailabilityParameterName));
|
---|
| 146 | Parameters.Add(new ValueParameter<DoubleValue>(MinSpeedParameterName, new DoubleValue(0.1)));
|
---|
| 147 | Parameters.Add(new ValueParameter<DoubleValue>(MaxSpeedParameterName, new DoubleValue(1.0)));
|
---|
| 148 | Parameters.Add(new ValueParameter<DoubleValue>(RentingRatioParameterName, new DoubleValue(0.5)));
|
---|
| 149 | #endregion
|
---|
| 150 |
|
---|
| 151 | #region Configure Ports
|
---|
| 152 | AddOrchestrationPort<VariegationProblem>(MetaSolverName);
|
---|
| 153 | AddEvaluationPort<RealVector>(MetaSolverName, "RealVector", "Quality");
|
---|
| 154 | AddOrchestrationPort<TourProfitProblem>(TspSolverName);
|
---|
| 155 | AddEvaluationPort<Permutation>(TspSolverName, "TSPTour", "TSPTourLength");
|
---|
| 156 | AddOrchestrationPort<BinaryKnapsackProblem>(KspSolverName);
|
---|
| 157 | AddEvaluationPort<BinaryVector>(KspSolverName, "KnapsackSolution", "Quality");
|
---|
| 158 |
|
---|
| 159 | RegisterEvents();
|
---|
| 160 | #endregion
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 164 | private void AfterDeserialization() {
|
---|
| 165 | RegisterEvents();
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | protected virtual void RegisterEvents() {
|
---|
| 169 | InstanceParameter.Value.ToStringChanged += InstanceParameter_Value_ToStringChanged;
|
---|
| 170 | MetaSolverOrchestrationPort.ConnectedPortChanged += MetaSolverOrchestrationPort_ConnectedPortChanged;
|
---|
| 171 | TspSolverOrchestrationPort.ConnectedPortChanged += TspSolverOrchestrationPort_ConnectedPortChanged;
|
---|
| 172 | KspSolverOrchestrationPort.ConnectedPortChanged += KspSolverOrchestrationPort_ConnectedPortChanged;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | private void InstanceParameter_Value_ToStringChanged(object sender, EventArgs e) {
|
---|
| 176 | string filePath = InstanceParameter.Value.Value;
|
---|
| 177 | TtpUtils.Import(filePath, out tspCoordinates, out distanceType,
|
---|
| 178 | out kspCapacity, out kspItemValues, out kspItemWeights,
|
---|
| 179 | out ttpAvailability, out ttpMinSpeed, out ttpMaxSpeed, out ttpRentingRatio);
|
---|
| 180 |
|
---|
| 181 | var tsp = TspParameter.Value;
|
---|
| 182 | tsp.Coordinates = new DoubleMatrix(tspCoordinates);
|
---|
| 183 |
|
---|
| 184 | var ksp = KspParameter.Value;
|
---|
| 185 | ksp.KnapsackCapacity.Value = kspCapacity;
|
---|
| 186 | ksp.Encoding.Length = kspItemValues.Length;
|
---|
| 187 | ksp.Values = new IntArray(kspItemValues);
|
---|
| 188 | ksp.Weights = new IntArray(kspItemWeights);
|
---|
| 189 |
|
---|
| 190 | AvailabilityParameter.Value = new IntArray(ttpAvailability);
|
---|
| 191 | MinSpeedParameter.Value.Value = ttpMinSpeed;
|
---|
| 192 | MaxSpeedParameter.Value.Value = ttpMaxSpeed;
|
---|
| 193 | RentingRatioParameter.Value.Value = ttpRentingRatio;
|
---|
| 194 |
|
---|
| 195 | Prepare();
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | public override void Prepare(bool clearRuns = false) {
|
---|
| 199 | Results.Clear();
|
---|
| 200 |
|
---|
| 201 | var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
|
---|
| 202 | var msgFlags = OrchestrationMessage.Prepare | OrchestrationMessage.SetEvalHook;
|
---|
| 203 | if (clearRuns) msgFlags |= OrchestrationMessage.ClearRuns;
|
---|
| 204 | metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(msgFlags);
|
---|
| 205 | var problem = new VariegationProblem();
|
---|
| 206 | problem.Encoding.Length = KspParameter.Value.Length;
|
---|
| 207 | problem.Encoding.Bounds = new DoubleMatrix(new[,] { { -1.0, 1.0 } });
|
---|
| 208 | metaMsg["Problem"] = problem;
|
---|
| 209 | MetaSolverOrchestrationPort.SendMessage(metaMsg);
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | public override void Start() {
|
---|
| 213 | cts = new CancellationTokenSource();
|
---|
| 214 |
|
---|
| 215 | var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
|
---|
| 216 | metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Start);
|
---|
| 217 | MetaSolverOrchestrationPort.SendMessage(metaMsg);
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | public override void Pause() {
|
---|
| 221 | cts.Cancel();
|
---|
| 222 |
|
---|
| 223 | var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
|
---|
| 224 | metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Pause);
|
---|
| 225 | MetaSolverOrchestrationPort.SendMessage(metaMsg);
|
---|
| 226 |
|
---|
| 227 | var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
|
---|
| 228 | tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
|
---|
| 229 | TspSolverOrchestrationPort.SendMessage(tspMsg);
|
---|
| 230 |
|
---|
| 231 | var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
|
---|
| 232 | kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
|
---|
| 233 | KspSolverOrchestrationPort.SendMessage(kspMsg);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | public override void Stop() {
|
---|
| 237 | cts.Cancel();
|
---|
| 238 |
|
---|
| 239 | var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
|
---|
| 240 | metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
|
---|
| 241 | MetaSolverOrchestrationPort.SendMessage(metaMsg);
|
---|
| 242 |
|
---|
| 243 | var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
|
---|
| 244 | tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
|
---|
| 245 | TspSolverOrchestrationPort.SendMessage(tspMsg);
|
---|
| 246 |
|
---|
| 247 | var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
|
---|
| 248 | kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
|
---|
| 249 | KspSolverOrchestrationPort.SendMessage(kspMsg);
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | protected override void ProcessMessage(IMessage message, IMessagePort port, CancellationToken token) {
|
---|
| 253 | var messageActions = new Dictionary<IMessagePort, Action<IMessage>>();
|
---|
| 254 | messageActions.Add(MetaSolverOrchestrationPort, MetaSolverOrchestrationPortMessage);
|
---|
| 255 | messageActions.Add(MetaSolverEvaluationPort, MetaSolverEvaluationPortMessage);
|
---|
| 256 | messageActions.Add(TspSolverOrchestrationPort, TspSolverOrchestrationPortMessage);
|
---|
| 257 | messageActions.Add(TspSolverEvaluationPort, TspSolverEvaluationPortMessage);
|
---|
| 258 | messageActions.Add(KspSolverOrchestrationPort, KspSolverOrchestrationPortMessage);
|
---|
| 259 | messageActions.Add(KspSolverEvaluationPort, KspSolverEvaluationPortMessage);
|
---|
| 260 |
|
---|
| 261 | messageActions[port](message);
|
---|
| 262 |
|
---|
| 263 | base.ProcessMessage(message, port, token);
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | #region MetaSolver Message Handling
|
---|
| 267 | protected virtual void MetaSolverOrchestrationPortMessage(IMessage message) { }
|
---|
| 268 |
|
---|
| 269 | protected virtual void MetaSolverEvaluationPortMessage(IMessage message) { }
|
---|
| 270 | #endregion
|
---|
| 271 |
|
---|
| 272 | #region TspSolver Message Handling
|
---|
| 273 | protected virtual void TspSolverOrchestrationPortMessage(IMessage message) {
|
---|
| 274 | var results = (ResultCollection)message["Results"];
|
---|
| 275 | if (results.ContainsKey("Best TSP Solution")) {
|
---|
| 276 | tspResults = results;
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | protected virtual void TspSolverEvaluationPortMessage(IMessage message) { }
|
---|
| 281 | #endregion
|
---|
| 282 |
|
---|
| 283 | #region KspSolver Message Handling
|
---|
| 284 | protected virtual void KspSolverOrchestrationPortMessage(IMessage message) {
|
---|
| 285 | var results = (ResultCollection)message["Results"];
|
---|
| 286 | if (results.ContainsKey("Best Solution")) {
|
---|
| 287 | kspResults = results;
|
---|
| 288 | }
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | private void KspSolverEvaluationPortMessage(IMessage message) { }
|
---|
| 292 | #endregion
|
---|
| 293 |
|
---|
| 294 | #region Event Handlers
|
---|
| 295 | private void MetaSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
|
---|
| 296 | if (MetaSolverOrchestrationPort.ConnectedPort == null) return;
|
---|
| 297 |
|
---|
| 298 | var node = MetaSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
|
---|
| 299 | if (node == null) return;
|
---|
| 300 |
|
---|
| 301 | var hook = new HookOperator { Name = "Meta Eval Hook" };
|
---|
| 302 | hook.Parameters.Add(new LookupParameter<RealVector>("RealVector") { Hidden = true });
|
---|
| 303 | hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = true });
|
---|
| 304 | node.EvalHook = hook;
|
---|
| 305 |
|
---|
| 306 | node.OrchestrationPort.CloneParametersFromPort(MetaSolverOrchestrationPort);
|
---|
| 307 | node.EvaluationPort.CloneParametersFromPort(MetaSolverEvaluationPort);
|
---|
| 308 | node.EvaluationPort.ConnectedPort = MetaSolverEvaluationPort;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | private void TspSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
|
---|
| 312 | if (TspSolverOrchestrationPort.ConnectedPort == null) return;
|
---|
| 313 |
|
---|
| 314 | var node = TspSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
|
---|
| 315 | if (node == null) return;
|
---|
| 316 |
|
---|
| 317 | var hook = new HookOperator { Name = "TSP Eval Hook" };
|
---|
| 318 | hook.Parameters.Add(new LookupParameter<Permutation>("TSPTour") { Hidden = true });
|
---|
| 319 | hook.Parameters.Add(new LookupParameter<DoubleValue>("TSPTourLength") { Hidden = true });
|
---|
| 320 | node.EvalHook = hook;
|
---|
| 321 |
|
---|
| 322 | node.OrchestrationPort.CloneParametersFromPort(TspSolverOrchestrationPort);
|
---|
| 323 | node.EvaluationPort.CloneParametersFromPort(TspSolverEvaluationPort);
|
---|
| 324 | node.EvaluationPort.ConnectedPort = TspSolverEvaluationPort;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | private void KspSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
|
---|
| 328 | if (KspSolverOrchestrationPort.ConnectedPort == null) return;
|
---|
| 329 |
|
---|
| 330 | var node = KspSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
|
---|
| 331 | if (node == null) return;
|
---|
| 332 |
|
---|
| 333 | var hook = new HookOperator { Name = "KSP Eval Hook" };
|
---|
| 334 | hook.Parameters.Add(new LookupParameter<BinaryVector>("KnapsackSolution") { Hidden = true });
|
---|
| 335 | hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = true });
|
---|
| 336 | node.EvalHook = hook;
|
---|
| 337 |
|
---|
| 338 | node.OrchestrationPort.CloneParametersFromPort(KspSolverOrchestrationPort);
|
---|
| 339 | node.EvaluationPort.CloneParametersFromPort(KspSolverEvaluationPort);
|
---|
| 340 | node.EvaluationPort.ConnectedPort = KspSolverEvaluationPort;
|
---|
| 341 | }
|
---|
| 342 | #endregion
|
---|
| 343 | }
|
---|
| 344 | }
|
---|