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.IntegerVectorEncoding;
|
---|
30 | using HeuristicLab.Operators;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Parameters;
|
---|
33 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
34 | using HeuristicLab.Problems.FacilityLocation;
|
---|
35 | using HeuristicLab.Problems.VehicleRouting;
|
---|
36 | using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
|
---|
37 |
|
---|
38 | namespace HeuristicLab.Networks.IntegratedOptimization.LocationRouting {
|
---|
39 | [Item("LrpOrchestratorNode5", "Orchestrator for LRP optimization network version 5.")]
|
---|
40 | [StorableClass]
|
---|
41 | public sealed class LrpOrchestratorNode5 : LrpOrchestratorNode {
|
---|
42 | [StorableConstructor]
|
---|
43 | private LrpOrchestratorNode5(bool deserializing) : base(deserializing) { }
|
---|
44 | private LrpOrchestratorNode5(LrpOrchestratorNode5 original, Cloner cloner) : base(original, cloner) { }
|
---|
45 | public LrpOrchestratorNode5() : this("LrpOrchestratorNode5") { }
|
---|
46 | public LrpOrchestratorNode5(string name) : base(name) {
|
---|
47 | MetaSolverOrchestrationPort = CreateOrchestrationPort<MinimizationVariegationProblem<BinaryVectorEncoding>>(MetaSolverName + OrchestrationPortNameSuffix);
|
---|
48 | MetaSolverEvaluationPort = CreateEvaluationPort<BinaryVector>(MetaSolverName + EvaluationPortNameSuffix, "BinaryVector", "Quality");
|
---|
49 | FlpSolverOrchestrationPort = CreateOrchestrationPort<FacilityLocationProblem>(FlpSolverName + OrchestrationPortNameSuffix);
|
---|
50 | VrpSolverOrchestrationPort = CreateOrchestrationPort<VehicleRoutingProblem>(VrpSolverName + OrchestrationPortNameSuffix);
|
---|
51 | }
|
---|
52 |
|
---|
53 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
54 | return new LrpOrchestratorNode5(this, cloner);
|
---|
55 | }
|
---|
56 |
|
---|
57 | protected override void MetaSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
|
---|
58 | if (MetaSolverOrchestrationPort.ConnectedPort == null) return;
|
---|
59 |
|
---|
60 | var node = MetaSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
|
---|
61 | if (node == null) return;
|
---|
62 |
|
---|
63 | var hook = new HookOperator { Name = "Meta Eval Hook" };
|
---|
64 | hook.Parameters.Add(new LookupParameter<BinaryVector>("BinaryVector") { Hidden = true });
|
---|
65 | hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = true });
|
---|
66 | node.EvalHook = hook;
|
---|
67 |
|
---|
68 | node.OrchestrationPort.CloneParametersFromPort(MetaSolverOrchestrationPort);
|
---|
69 | node.EvaluationPort.CloneParametersFromPort(MetaSolverEvaluationPort);
|
---|
70 | node.EvaluationPort.ConnectedPort = MetaSolverEvaluationPort;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public override void Prepare(bool clearRuns = false) {
|
---|
74 | base.Prepare(clearRuns);
|
---|
75 |
|
---|
76 | var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
|
---|
77 | var msgFlags = OrchestrationMessage.Prepare | OrchestrationMessage.SetEvalHook;
|
---|
78 | if (clearRuns) msgFlags |= OrchestrationMessage.ClearRuns;
|
---|
79 | metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(msgFlags);
|
---|
80 | var problem = new MinimizationVariegationProblem<BinaryVectorEncoding>();
|
---|
81 | problem.Encoding.Length = nrOfDepots;
|
---|
82 | metaMsg["Problem"] = problem;
|
---|
83 | MetaSolverOrchestrationPort.SendMessage(metaMsg);
|
---|
84 | }
|
---|
85 |
|
---|
86 | #region MetaSolver Message Handling
|
---|
87 | protected override void MetaSolverEvaluationPortMessage(IMessage message) {
|
---|
88 | var selection = (BinaryVector)message["BinaryVector"];
|
---|
89 | var inverse = selection.Select((x, i) => Tuple.Create(x, i)).Where(x => x.Item1).Select(x => x.Item2).ToArray();
|
---|
90 | if (!inverse.Any()) {
|
---|
91 | ((DoubleValue)message["Quality"]).Value = double.MaxValue;
|
---|
92 | return;
|
---|
93 | }
|
---|
94 |
|
---|
95 | var flp = (FacilityLocationProblem)FlpParameter.Value.Clone();
|
---|
96 | double[,] newDepotCoordinates = new double[inverse.Length, 2];
|
---|
97 | double[] newDepotCosts = new double[inverse.Length];
|
---|
98 | double[] newDepotCapacities = new double[inverse.Length];
|
---|
99 |
|
---|
100 | int newIdx = 0;
|
---|
101 | for (int oldIdx = 0; oldIdx < selection.Length; oldIdx++) {
|
---|
102 | if (!selection[oldIdx]) continue;
|
---|
103 | newDepotCoordinates[newIdx, 0] = depotCoordinates[oldIdx, 0];
|
---|
104 | newDepotCoordinates[newIdx, 1] = depotCoordinates[oldIdx, 1];
|
---|
105 | newDepotCosts[newIdx] = depotCosts[oldIdx];
|
---|
106 | newDepotCapacities[newIdx] = depotCapacities[oldIdx];
|
---|
107 | ++newIdx;
|
---|
108 | }
|
---|
109 | flp.OpeningCostsParameter.Value = new DoubleArray(newDepotCosts);
|
---|
110 | flp.DepotCapacitiesParameter.Value = new DoubleArray(newDepotCapacities);
|
---|
111 | flp.DeliveryCostsParameter.Value = new DoubleMatrix(LrpUtils.GetFlpDeliveryCosts(newDepotCoordinates, customerCoordinates, distanceType));
|
---|
112 |
|
---|
113 | var flpMsg = FlpSolverOrchestrationPort.PrepareMessage();
|
---|
114 | flpMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
|
---|
115 | flpMsg["Problem"] = flp;
|
---|
116 | FlpSolverOrchestrationPort.SendMessage(flpMsg);
|
---|
117 | cts.Token.ThrowIfCancellationRequested();
|
---|
118 |
|
---|
119 | var flpResults = (ResultCollection)flpMsg["Results"];
|
---|
120 | var bestFlpSolution = (IntegerVector)flpResults["Best Solution"].Value;
|
---|
121 | var flpSolution = flp.GetSolution(bestFlpSolution);
|
---|
122 |
|
---|
123 | var depots = bestFlpSolution.Select((x, i) => Tuple.Create(inverse[x], i)).GroupBy(x => x.Item1, x => x.Item2);
|
---|
124 | var vrpSolutions = new ResultCollection(depots.Count());
|
---|
125 | foreach (var depot in depots.OrderBy(x => x.Key)) {
|
---|
126 | var depotIdx = depot.Key;
|
---|
127 | var customers = depot.ToArray();
|
---|
128 |
|
---|
129 | var vrp = (VehicleRoutingProblem)VrpParameter.Value.Clone();
|
---|
130 | var vrpInstance = (CVRPProblemInstance)vrp.ProblemInstance;
|
---|
131 | var coordinates = LrpUtils.GetVrpCoordinates(depotCoordinates, customerCoordinates, new[] { depotIdx }, customers);
|
---|
132 | var distances = LrpUtils.GetVrpDistances(coordinates, distanceType);
|
---|
133 | vrpInstance.Coordinates = new DoubleMatrix(coordinates);
|
---|
134 | vrpInstance.DistanceMatrix = new DoubleMatrix(distances);
|
---|
135 | vrpInstance.Demand = new DoubleArray(new[] { 0.0 }.Concat(customers.Select(x => customerDemands[x])).ToArray());
|
---|
136 | vrpInstance.Vehicles.Value = customers.Length;
|
---|
137 |
|
---|
138 | var vrpMsg = VrpSolverOrchestrationPort.PrepareMessage();
|
---|
139 | vrpMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
|
---|
140 | vrpMsg["Problem"] = vrp;
|
---|
141 | VrpSolverOrchestrationPort.SendMessage(vrpMsg);
|
---|
142 | cts.Token.ThrowIfCancellationRequested();
|
---|
143 |
|
---|
144 | var vrpResults = (ResultCollection)vrpMsg["Results"];
|
---|
145 | IResult bestVrpSolutionResult;
|
---|
146 | if (!vrpResults.TryGetValue("Best valid VRP Solution", out bestVrpSolutionResult)
|
---|
147 | && !vrpResults.TryGetValue("Best VRP Solution", out bestVrpSolutionResult)) {
|
---|
148 | // no best solution found ... throw?
|
---|
149 | }
|
---|
150 | var bestVrpSolution = (VRPSolution)bestVrpSolutionResult.Value.Clone();
|
---|
151 | vrpSolutions.Add(new Result("Depot " + depot.Key, bestVrpSolution));
|
---|
152 | }
|
---|
153 |
|
---|
154 | #region Analyze
|
---|
155 | double objectiveValue = LrpUtils.Evaluate(flpSolution, vrpSolutions.Select(x => (VRPSolution)x.Value).ToArray());
|
---|
156 | ((DoubleValue)message["Quality"]).Value = objectiveValue;
|
---|
157 |
|
---|
158 | IResult bestQuality;
|
---|
159 | if (!Results.TryGetValue("Best LRP Quality", out bestQuality)) {
|
---|
160 | Results.Add(new Result("Best LRP Quality", new DoubleValue(objectiveValue)));
|
---|
161 | Results.Add(new Result("Best FLP Solution", flpSolution));
|
---|
162 | Results.Add(new Result("Best VRP Solutions", vrpSolutions));
|
---|
163 | } else if (objectiveValue < ((DoubleValue)bestQuality.Value).Value) {
|
---|
164 | ((DoubleValue)bestQuality.Value).Value = objectiveValue;
|
---|
165 | Results["Best FLP Solution"].Value = flpSolution;
|
---|
166 | Results["Best VRP Solutions"].Value = vrpSolutions;
|
---|
167 | }
|
---|
168 | #endregion
|
---|
169 | }
|
---|
170 | #endregion
|
---|
171 | }
|
---|
172 | }
|
---|