[14601] | 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.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Core.Networks;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
| 29 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 30 | using HeuristicLab.Optimization;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 | using HeuristicLab.Problems.Knapsack;
|
---|
| 33 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
| 34 |
|
---|
| 35 | namespace HeuristicLab.Networks.IntegratedOptimization.TravelingThief {
|
---|
| 36 | [Item("TtpOrchestratorNode2", "Orchestrator for TTP optimization network version 2.")]
|
---|
| 37 | [StorableClass]
|
---|
[14610] | 38 | public sealed class TtpOrchestratorNode2 : TtpOrchestratorNode {
|
---|
[14601] | 39 | [StorableConstructor]
|
---|
| 40 | private TtpOrchestratorNode2(bool deserializing) : base(deserializing) { }
|
---|
[14610] | 41 | private TtpOrchestratorNode2(TtpOrchestratorNode2 original, Cloner cloner) : base(original, cloner) { }
|
---|
[14601] | 42 | public TtpOrchestratorNode2() : this("TtpOrchestratorNode2") { }
|
---|
[14610] | 43 | public TtpOrchestratorNode2(string name) : base(name) { }
|
---|
[14601] | 44 |
|
---|
| 45 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 46 | return new TtpOrchestratorNode2(this, cloner);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | #region MetaSolver Message Handling
|
---|
[14610] | 50 | protected override void MetaSolverEvaluationPortMessage(IMessage message) {
|
---|
[14601] | 51 | var factors = (RealVector)message["RealVector"];
|
---|
| 52 |
|
---|
| 53 | var tsp = (TravelingSalesmanProblem)TspParameter.Value.Clone();
|
---|
[14604] | 54 | for (int i = 0; i < tsp.Coordinates.Rows; i++) {
|
---|
[14601] | 55 | tsp.Coordinates[i, 0] = (int)Math.Ceiling(tsp.Coordinates[i, 0] * factors[i * 2]);
|
---|
| 56 | tsp.Coordinates[i, 1] = (int)Math.Ceiling(tsp.Coordinates[i, 1] * factors[i * 2 + 1]);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
|
---|
[14610] | 60 | tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
|
---|
[14601] | 61 | tspMsg["Problem"] = tsp;
|
---|
| 62 | TspSolverOrchestrationPort.SendMessage(tspMsg);
|
---|
| 63 | cts.Token.ThrowIfCancellationRequested();
|
---|
| 64 |
|
---|
[14604] | 65 | var bestTspSolution = (PathTSPTour)tspResults["Best TSP Solution"].Value.Clone();
|
---|
[14601] | 66 | var coordinates = (DoubleMatrix)TspParameter.Value.Coordinates.Clone();
|
---|
| 67 | var tour = new PathTSPTour(coordinates, bestTspSolution.Permutation, new DoubleValue(TSPCoordinatesPathEvaluator.Apply(new TSPEuclideanPathEvaluator(), coordinates, bestTspSolution.Permutation)));
|
---|
| 68 |
|
---|
| 69 | var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
|
---|
[14610] | 70 | kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
|
---|
[14601] | 71 | var lpp = new LootProfitProblem {
|
---|
| 72 | Tsp = (TravelingSalesmanProblem)TspParameter.Value.Clone(),
|
---|
| 73 | Ksp = (BinaryKnapsackProblem)KspParameter.Value.Clone(),
|
---|
| 74 | FixedTspSolution = bestTspSolution.Permutation,
|
---|
| 75 | Availability = AvailabilityParameter.Value.ToArray(),
|
---|
| 76 | RentingRatio = RentingRatioParameter.Value.Value,
|
---|
| 77 | MinSpeed = MinSpeedParameter.Value.Value,
|
---|
[14604] | 78 | MaxSpeed = MaxSpeedParameter.Value.Value,
|
---|
| 79 | DistanceType = distanceType
|
---|
[14601] | 80 | };
|
---|
| 81 | lpp.Encoding.Length = KspParameter.Value.Length;
|
---|
| 82 | kspMsg["Problem"] = lpp;
|
---|
| 83 | KspSolverOrchestrationPort.SendMessage(kspMsg);
|
---|
| 84 | cts.Token.ThrowIfCancellationRequested();
|
---|
| 85 |
|
---|
[14604] | 86 | var bestKspSolution = (BinaryVector)kspResults["Best Solution"].Value.Clone();
|
---|
[14601] | 87 | var kspCapacity = (IntValue)KspParameter.Value.KnapsackCapacity.Clone();
|
---|
| 88 | var kspPenalty = new DoubleValue(0.0);
|
---|
| 89 | var kspWeights = (IntArray)KspParameter.Value.Weights.Clone();
|
---|
| 90 | var kspValues = (IntArray)KspParameter.Value.Values.Clone();
|
---|
| 91 | var bestKspQuality = KnapsackEvaluator.Apply(bestKspSolution, kspCapacity, kspPenalty, kspWeights, kspValues).Quality;
|
---|
| 92 | var loot = new KnapsackSolution(bestKspSolution, bestKspQuality, kspCapacity, kspWeights, kspValues);
|
---|
| 93 |
|
---|
| 94 | #region Analyze
|
---|
[14604] | 95 | double objectiveValue = TtpUtils.Evaluate(TspParameter.Value, tour.Permutation.ToArray(), KspParameter.Value, loot.BinaryVector.ToArray(),
|
---|
| 96 | AvailabilityParameter.Value.ToArray(), RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value, distanceType);
|
---|
[14601] | 97 | ((DoubleValue)message["Quality"]).Value = objectiveValue;
|
---|
| 98 |
|
---|
| 99 | IResult bestQuality;
|
---|
| 100 | if (!Results.TryGetValue("Best TTP Quality", out bestQuality)) {
|
---|
| 101 | Results.Add(new Result("Best TTP Quality", new DoubleValue(objectiveValue)));
|
---|
| 102 | Results.Add(new Result("Best Tour", tour));
|
---|
| 103 | Results.Add(new Result("Best Loot", loot));
|
---|
[14610] | 104 | } else if (objectiveValue > ((DoubleValue)bestQuality.Value).Value) {
|
---|
[14601] | 105 | ((DoubleValue)bestQuality.Value).Value = objectiveValue;
|
---|
| 106 | Results["Best Tour"].Value = tour;
|
---|
| 107 | Results["Best Loot"].Value = loot;
|
---|
| 108 | }
|
---|
| 109 | #endregion
|
---|
| 110 | }
|
---|
| 111 | #endregion
|
---|
| 112 | }
|
---|
| 113 | }
|
---|