[14621] | 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.IntegerVectorEncoding;
|
---|
| 29 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 30 | using HeuristicLab.Optimization;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 | using HeuristicLab.Problems.FacilityLocation;
|
---|
| 33 | using HeuristicLab.Problems.VehicleRouting;
|
---|
| 34 | using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
|
---|
| 35 |
|
---|
| 36 | namespace HeuristicLab.Networks.IntegratedOptimization.LocationRouting {
|
---|
| 37 | [Item("LrpOrchestratorNode2", "Orchestrator for LRP optimization network version 2.")]
|
---|
| 38 | [StorableClass]
|
---|
| 39 | public sealed class LrpOrchestratorNode2 : LrpOrchestratorNode {
|
---|
| 40 | [StorableConstructor]
|
---|
| 41 | private LrpOrchestratorNode2(bool deserializing) : base(deserializing) { }
|
---|
| 42 | private LrpOrchestratorNode2(LrpOrchestratorNode2 original, Cloner cloner) : base(original, cloner) { }
|
---|
| 43 | public LrpOrchestratorNode2() : this("LrpOrchestratorNode2") { }
|
---|
| 44 | public LrpOrchestratorNode2(string name) : base(name) {
|
---|
[14628] | 45 | MetaSolverOrchestrationPort = CreateOrchestrationPort<MinimizationVariegationProblem<RealVectorEncoding>>(MetaSolverName + OrchestrationPortNameSuffix);
|
---|
| 46 | MetaSolverEvaluationPort = CreateEvaluationPort<RealVector>(MetaSolverName + EvaluationPortNameSuffix, "RealVector", "Quality");
|
---|
| 47 | FlpSolverOrchestrationPort = CreateOrchestrationPort<FacilityLocationProblem>(FlpSolverName + OrchestrationPortNameSuffix);
|
---|
| 48 | VrpSolverOrchestrationPort = CreateOrchestrationPort<VehicleRoutingProblem>(VrpSolverName + OrchestrationPortNameSuffix);
|
---|
[14621] | 49 | }
|
---|
| 50 |
|
---|
| 51 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 52 | return new LrpOrchestratorNode2(this, cloner);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public override void Prepare(bool clearRuns = false) {
|
---|
| 56 | base.Prepare(clearRuns);
|
---|
| 57 |
|
---|
| 58 | var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
|
---|
| 59 | var msgFlags = OrchestrationMessage.Prepare | OrchestrationMessage.SetEvalHook;
|
---|
| 60 | if (clearRuns) msgFlags |= OrchestrationMessage.ClearRuns;
|
---|
| 61 | metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(msgFlags);
|
---|
[14628] | 62 | var problem = new MinimizationVariegationProblem<RealVectorEncoding>();
|
---|
[14621] | 63 | problem.Encoding.Length = nrOfDepots * 2;
|
---|
| 64 | problem.Encoding.Bounds = new DoubleMatrix(new[,] { { -1.0, 1.0 } });
|
---|
| 65 | metaMsg["Problem"] = problem;
|
---|
| 66 | MetaSolverOrchestrationPort.SendMessage(metaMsg);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | #region MetaSolver Message Handling
|
---|
| 70 | protected override void MetaSolverEvaluationPortMessage(IMessage message) {
|
---|
| 71 | var factors = (RealVector)message["RealVector"];
|
---|
| 72 |
|
---|
| 73 | var flp = (FacilityLocationProblem)FlpParameter.Value.Clone();
|
---|
| 74 | var dc = (double[,])depotCoordinates.Clone();
|
---|
| 75 | for (int i = 0; i < nrOfDepots; i++) {
|
---|
| 76 | dc[i, 0] = dc[i, 0] * factors[i * 2];
|
---|
| 77 | dc[i, 1] = dc[i, 1] * factors[i * 2 + 1];
|
---|
| 78 | }
|
---|
| 79 | flp.DeliveryCostsParameter.Value = new DoubleMatrix(LrpUtils.GetFlpDeliveryCosts(dc, customerCoordinates, distanceType));
|
---|
| 80 |
|
---|
| 81 | var flpMsg = FlpSolverOrchestrationPort.PrepareMessage();
|
---|
| 82 | flpMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
|
---|
| 83 | flpMsg["Problem"] = flp;
|
---|
| 84 | FlpSolverOrchestrationPort.SendMessage(flpMsg);
|
---|
| 85 | cts.Token.ThrowIfCancellationRequested();
|
---|
| 86 |
|
---|
[14628] | 87 | var flpResults = (ResultCollection)flpMsg["Results"];
|
---|
[14621] | 88 | var bestFlpSolution = (IntegerVector)flpResults["Best Solution"].Value;
|
---|
| 89 | var flpSolution = FlpParameter.Value.GetSolution(bestFlpSolution);
|
---|
| 90 |
|
---|
| 91 | var depots = bestFlpSolution.Select((x, i) => Tuple.Create(x, i)).GroupBy(x => x.Item1, x => x.Item2);
|
---|
| 92 | var vrpSolutions = new ResultCollection(depots.Count());
|
---|
| 93 | foreach (var depot in depots.OrderBy(x => x.Key)) {
|
---|
| 94 | var depotIdx = depot.Key;
|
---|
| 95 | var customers = depot.ToArray();
|
---|
| 96 |
|
---|
| 97 | var vrp = (VehicleRoutingProblem)VrpParameter.Value.Clone();
|
---|
| 98 | var vrpInstance = (CVRPProblemInstance)vrp.ProblemInstance;
|
---|
[14649] | 99 | var coordinates = LrpUtils.GetVrpCoordinates(depotCoordinates, customerCoordinates, new[] { depotIdx }, customers);
|
---|
[14621] | 100 | var distances = LrpUtils.GetVrpDistances(coordinates, distanceType);
|
---|
| 101 | vrpInstance.Coordinates = new DoubleMatrix(coordinates);
|
---|
| 102 | vrpInstance.DistanceMatrix = new DoubleMatrix(distances);
|
---|
| 103 | vrpInstance.Demand = new DoubleArray(new[] { 0.0 }.Concat(customers.Select(x => customerDemands[x])).ToArray());
|
---|
| 104 | vrpInstance.Vehicles.Value = customers.Length;
|
---|
| 105 |
|
---|
| 106 | var vrpMsg = VrpSolverOrchestrationPort.PrepareMessage();
|
---|
| 107 | vrpMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
|
---|
| 108 | vrpMsg["Problem"] = vrp;
|
---|
| 109 | VrpSolverOrchestrationPort.SendMessage(vrpMsg);
|
---|
| 110 | cts.Token.ThrowIfCancellationRequested();
|
---|
| 111 |
|
---|
[14628] | 112 | var vrpResults = (ResultCollection)vrpMsg["Results"];
|
---|
[14649] | 113 | IResult bestVrpSolutionResult;
|
---|
| 114 | if (!vrpResults.TryGetValue("Best valid VRP Solution", out bestVrpSolutionResult)
|
---|
| 115 | && !vrpResults.TryGetValue("Best VRP Solution", out bestVrpSolutionResult)) {
|
---|
| 116 | // no best solution found ... throw?
|
---|
| 117 | }
|
---|
| 118 | var bestVrpSolution = (VRPSolution)bestVrpSolutionResult.Value.Clone();
|
---|
[14621] | 119 | vrpSolutions.Add(new Result("Depot " + depot.Key, bestVrpSolution));
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | #region Analyze
|
---|
| 123 | double objectiveValue = LrpUtils.Evaluate(flpSolution, vrpSolutions.Select(x => (VRPSolution)x.Value).ToArray());
|
---|
| 124 | ((DoubleValue)message["Quality"]).Value = objectiveValue;
|
---|
| 125 |
|
---|
| 126 | IResult bestQuality;
|
---|
| 127 | if (!Results.TryGetValue("Best LRP Quality", out bestQuality)) {
|
---|
| 128 | Results.Add(new Result("Best LRP Quality", new DoubleValue(objectiveValue)));
|
---|
| 129 | Results.Add(new Result("Best FLP Solution", flpSolution));
|
---|
| 130 | Results.Add(new Result("Best VRP Solutions", vrpSolutions));
|
---|
| 131 | } else if (objectiveValue < ((DoubleValue)bestQuality.Value).Value) {
|
---|
| 132 | ((DoubleValue)bestQuality.Value).Value = objectiveValue;
|
---|
| 133 | Results["Best FLP Solution"].Value = flpSolution;
|
---|
| 134 | Results["Best VRP Solutions"].Value = vrpSolutions;
|
---|
| 135 | }
|
---|
| 136 | #endregion
|
---|
| 137 | }
|
---|
| 138 | #endregion
|
---|
| 139 | }
|
---|
| 140 | }
|
---|