#region License Information /* HeuristicLab * Copyright (C) 2002-2017 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 System; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Core.Networks; using HeuristicLab.Data; using HeuristicLab.Encodings.BinaryVectorEncoding; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.Encodings.RealVectorEncoding; using HeuristicLab.Optimization; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.Knapsack; using HeuristicLab.Problems.TravelingSalesman; namespace HeuristicLab.Networks.IntegratedOptimization.TravelingThief { [Item("TtpOrchestratorNode3", "Orchestrator for TTP optimization network version 3.")] [StorableClass] public sealed class TtpOrchestratorNode3 : TtpOrchestratorNode { [StorableConstructor] private TtpOrchestratorNode3(bool deserializing) : base(deserializing) { } private TtpOrchestratorNode3(TtpOrchestratorNode3 original, Cloner cloner) : base(original, cloner) { } public TtpOrchestratorNode3() : this("TtpOrchestratorNode3") { } public TtpOrchestratorNode3(string name) : base(name) { } public override IDeepCloneable Clone(Cloner cloner) { return new TtpOrchestratorNode3(this, cloner); } #region MetaSolver Message Handling protected override void MetaSolverEvaluationPortMessage(IMessage message) { var factors = (RealVector)message["RealVector"]; int fi = 0; var ksp = (BinaryKnapsackProblem)KspParameter.Value.Clone(); while (fi < ksp.Values.Length) { ksp.Values[fi] = (int)Math.Ceiling(ksp.Values[fi] * factors[fi]); ++fi; } var kspMsg = KspSolverOrchestrationPort.PrepareMessage(); kspMsg["OrchestrationMessage"] = new EnumValue(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start); kspMsg["Problem"] = ksp; KspSolverOrchestrationPort.SendMessage(kspMsg); cts.Token.ThrowIfCancellationRequested(); var bestKspSolution = (BinaryVector)kspResults["Best Solution"].Value.Clone(); var kspCapacity = (IntValue)KspParameter.Value.KnapsackCapacity.Clone(); var kspPenalty = new DoubleValue(0.0); var kspWeights = (IntArray)KspParameter.Value.Weights.Clone(); var kspValues = (IntArray)KspParameter.Value.Values.Clone(); var bestKspQuality = KnapsackEvaluator.Apply(bestKspSolution, kspCapacity, kspPenalty, kspWeights, kspValues).Quality; var loot = new KnapsackSolution(bestKspSolution, bestKspQuality, kspCapacity, kspWeights, kspValues); var tsp = (TravelingSalesmanProblem)TspParameter.Value.Clone(); for (int j = 0; j < tsp.Coordinates.Rows; j++) { tsp.Coordinates[j, 0] = (int)Math.Ceiling(tsp.Coordinates[j, 0] * factors[fi + j * 2]); tsp.Coordinates[j, 1] = (int)Math.Ceiling(tsp.Coordinates[j, 1] * factors[fi + j * 2 + 1]); } var tspMsg = TspSolverOrchestrationPort.PrepareMessage(); tspMsg["OrchestrationMessage"] = new EnumValue(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start); var tpp = new TourProfitProblem { Tsp = (TravelingSalesmanProblem)TspParameter.Value.Clone(), Ksp = (BinaryKnapsackProblem)KspParameter.Value.Clone(), FixedKspSolution = bestKspSolution, Availability = AvailabilityParameter.Value.ToArray(), RentingRatio = RentingRatioParameter.Value.Value, MinSpeed = MinSpeedParameter.Value.Value, MaxSpeed = MaxSpeedParameter.Value.Value, DistanceType = distanceType }; tpp.Encoding.Length = TspParameter.Value.Coordinates.Rows; tspMsg["Problem"] = tpp; TspSolverOrchestrationPort.SendMessage(tspMsg); cts.Token.ThrowIfCancellationRequested(); var bestTspSolution = (Permutation)tspResults["Best TSP Solution"].Value.Clone(); var coordinates = (DoubleMatrix)TspParameter.Value.Coordinates.Clone(); var tour = new PathTSPTour(coordinates, bestTspSolution, new DoubleValue(TSPCoordinatesPathEvaluator.Apply(new TSPEuclideanPathEvaluator(), coordinates, bestTspSolution))); #region Analyze double objectiveValue = TtpUtils.Evaluate(TspParameter.Value, tour.Permutation.ToArray(), KspParameter.Value, loot.BinaryVector.ToArray(), AvailabilityParameter.Value.ToArray(), RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value, distanceType); ((DoubleValue)message["Quality"]).Value = objectiveValue; IResult bestQuality; if (!Results.TryGetValue("Best TTP Quality", out bestQuality)) { Results.Add(new Result("Best TTP Quality", new DoubleValue(objectiveValue))); Results.Add(new Result("Best Tour", tour)); Results.Add(new Result("Best Loot", loot)); } else if (objectiveValue > ((DoubleValue)bestQuality.Value).Value) { ((DoubleValue)bestQuality.Value).Value = objectiveValue; Results["Best Tour"].Value = tour; Results["Best Loot"].Value = loot; } #endregion } #endregion } }