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