#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("TtpOrchestratorNode1", "Orchestrator for TTP optimization network version 1.")] [StorableClass] public sealed class TtpOrchestratorNode1 : TtpOrchestratorNode { [StorableConstructor] private TtpOrchestratorNode1(bool deserializing) : base(deserializing) { } private TtpOrchestratorNode1(TtpOrchestratorNode1 original, Cloner cloner) : base(original, cloner) { } public TtpOrchestratorNode1() : this("TtpOrchestratorNode1") { } public TtpOrchestratorNode1(string name) : base(name) { } public override IDeepCloneable Clone(Cloner cloner) { return new TtpOrchestratorNode1(this, cloner); } #region MetaSolver Message Handling protected override void MetaSolverEvaluationPortMessage(IMessage message) { var factors = (RealVector)message["RealVector"]; var ksp = (BinaryKnapsackProblem)KspParameter.Value.Clone(); for (int i = 0; i < ksp.Values.Length; i++) ksp.Values[i] = (int)Math.Ceiling(ksp.Values[i] * factors[i]); 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 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 } }