Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.LocationRouting/3.3/LrpOrchestratorNode4.cs @ 14895

Last change on this file since 14895 was 14895, checked in by jkarder, 7 years ago

#2205: worked on optimization networks

  • added lrp networks (4, 5)
  • fixed lrp evaluation
  • updated flp models
  • updated to cplex 12.7
File size: 6.9 KB
Line 
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
22using System;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Core.Networks;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.IntegerVectorEncoding;
29using HeuristicLab.Encodings.RealVectorEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.FacilityLocation;
33using HeuristicLab.Problems.VehicleRouting;
34using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
35
36namespace HeuristicLab.Networks.IntegratedOptimization.LocationRouting {
37  [Item("LrpOrchestratorNode4", "Orchestrator for LRP optimization network version 4.")]
38  [StorableClass]
39  public sealed class LrpOrchestratorNode4 : LrpOrchestratorNode {
40    [StorableConstructor]
41    private LrpOrchestratorNode4(bool deserializing) : base(deserializing) { }
42    private LrpOrchestratorNode4(LrpOrchestratorNode4 original, Cloner cloner) : base(original, cloner) { }
43    public LrpOrchestratorNode4() : this("LrpOrchestratorNode4") { }
44    public LrpOrchestratorNode4(string name) : base(name) {
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);
49    }
50
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new LrpOrchestratorNode4(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);
62      var problem = new MinimizationVariegationProblem<RealVectorEncoding>();
63      problem.Encoding.Length = nrOfDepots;
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[])depotCosts.Clone();
75      for (int i = 0; i < nrOfDepots; i++)
76        dc[i] = dc[i] * factors[i];
77      flp.OpeningCostsParameter.Value = new DoubleArray(dc);
78
79      var flpMsg = FlpSolverOrchestrationPort.PrepareMessage();
80      flpMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
81      flpMsg["Problem"] = flp;
82      FlpSolverOrchestrationPort.SendMessage(flpMsg);
83      cts.Token.ThrowIfCancellationRequested();
84
85      var flpResults = (ResultCollection)flpMsg["Results"];
86      var bestFlpSolution = (IntegerVector)flpResults["Best Solution"].Value;
87      var flpSolution = FlpParameter.Value.GetSolution(bestFlpSolution);
88
89      var depots = bestFlpSolution.Select((x, i) => Tuple.Create(x, i)).GroupBy(x => x.Item1, x => x.Item2);
90      var vrpSolutions = new ResultCollection(depots.Count());
91      foreach (var depot in depots.OrderBy(x => x.Key)) {
92        var depotIdx = depot.Key;
93        var customers = depot.ToArray();
94
95        var vrp = (VehicleRoutingProblem)VrpParameter.Value.Clone();
96        var vrpInstance = (CVRPProblemInstance)vrp.ProblemInstance;
97        var coordinates = LrpUtils.GetVrpCoordinates(depotCoordinates, customerCoordinates, new[] { depotIdx }, customers);
98        var distances = LrpUtils.GetVrpDistances(coordinates, distanceType);
99        vrpInstance.Coordinates = new DoubleMatrix(coordinates);
100        vrpInstance.DistanceMatrix = new DoubleMatrix(distances);
101        vrpInstance.Demand = new DoubleArray(new[] { 0.0 }.Concat(customers.Select(x => customerDemands[x])).ToArray());
102        vrpInstance.Vehicles.Value = customers.Length;
103
104        var vrpMsg = VrpSolverOrchestrationPort.PrepareMessage();
105        vrpMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
106        vrpMsg["Problem"] = vrp;
107        VrpSolverOrchestrationPort.SendMessage(vrpMsg);
108        cts.Token.ThrowIfCancellationRequested();
109
110        var vrpResults = (ResultCollection)vrpMsg["Results"];
111        IResult bestVrpSolutionResult;
112        if (!vrpResults.TryGetValue("Best valid VRP Solution", out bestVrpSolutionResult)
113         && !vrpResults.TryGetValue("Best VRP Solution", out bestVrpSolutionResult)) {
114          // no best solution found ... throw?
115        }
116        var bestVrpSolution = (VRPSolution)bestVrpSolutionResult.Value.Clone();
117        vrpSolutions.Add(new Result("Depot " + depot.Key, bestVrpSolution));
118      }
119
120      #region Analyze
121      double objectiveValue = LrpUtils.Evaluate(flpSolution, vrpSolutions.Select(x => (VRPSolution)x.Value).ToArray());
122      ((DoubleValue)message["Quality"]).Value = objectiveValue;
123
124      IResult bestQuality;
125      if (!Results.TryGetValue("Best LRP Quality", out bestQuality)) {
126        Results.Add(new Result("Best LRP Quality", new DoubleValue(objectiveValue)));
127        Results.Add(new Result("Best FLP Solution", flpSolution));
128        Results.Add(new Result("Best VRP Solutions", vrpSolutions));
129      } else if (objectiveValue < ((DoubleValue)bestQuality.Value).Value) {
130        ((DoubleValue)bestQuality.Value).Value = objectiveValue;
131        Results["Best FLP Solution"].Value = flpSolution;
132        Results["Best VRP Solutions"].Value = vrpSolutions;
133      }
134      #endregion
135    }
136    #endregion
137  }
138}
Note: See TracBrowser for help on using the repository browser.