[14604] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.IO;
|
---|
| 4 | using System.Linq;
|
---|
| 5 | using HeuristicLab.Problems.FacilityLocation;
|
---|
| 6 | using HeuristicLab.Problems.VehicleRouting;
|
---|
| 7 |
|
---|
| 8 | namespace HeuristicLab.Networks.IntegratedOptimization.LocationRouting {
|
---|
| 9 | public static class LrpUtils {
|
---|
| 10 | public enum DistanceType {
|
---|
| 11 | Unknown = int.MinValue,
|
---|
| 12 | CEIL_2D = 0,
|
---|
| 13 | EUC_2D = 1,
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 | public static readonly Dictionary<DistanceType, Func<double[,], int, double[,], int, double>> DistanceFuncs;
|
---|
| 17 |
|
---|
| 18 | static LrpUtils() {
|
---|
| 19 | DistanceFuncs = new Dictionary<DistanceType, Func<double[,], int, double[,], int, double>>();
|
---|
| 20 | DistanceFuncs.Add(DistanceType.EUC_2D, (d, i, c, j) => Math.Sqrt(Math.Pow(d[i, 0] - c[j, 0], 2.0) + Math.Pow(d[i, 1] - c[j, 1], 2.0)));
|
---|
[14649] | 21 | DistanceFuncs.Add(DistanceType.CEIL_2D, (d, i, c, j) => Math.Ceiling(DistanceFuncs[DistanceType.EUC_2D](d, i, c, j) * 100));
|
---|
[14604] | 22 | }
|
---|
| 23 |
|
---|
| 24 | public static void Import(string filePath, out int nrOfDepots,
|
---|
| 25 | out int nrOfCustomers,
|
---|
| 26 | out double[,] depotCoordinates,
|
---|
| 27 | out double[,] customerCoordinates,
|
---|
| 28 | out DistanceType distanceType,
|
---|
| 29 | out double[] depotCapacities,
|
---|
| 30 | out double[] customerDemands,
|
---|
| 31 | out double[] depotCosts,
|
---|
| 32 | out double vehicleCapacity,
|
---|
| 33 | out double vehicleCost) {
|
---|
| 34 | nrOfDepots = 0; nrOfCustomers = 0;
|
---|
| 35 | depotCoordinates = new double[0, 0]; customerCoordinates = new double[0, 0];
|
---|
| 36 | distanceType = DistanceType.Unknown;
|
---|
| 37 | depotCapacities = new double[0]; customerDemands = new double[0]; depotCosts = new double[0];
|
---|
| 38 | vehicleCapacity = 0.0; vehicleCost = 0.0;
|
---|
| 39 |
|
---|
| 40 | int dataIndex = 0;
|
---|
| 41 |
|
---|
| 42 | using (var fs = new FileStream(filePath, FileMode.Open))
|
---|
| 43 | using (var sr = new StreamReader(fs)) {
|
---|
| 44 | string input;
|
---|
| 45 | while ((input = sr.ReadLine()) != null) {
|
---|
| 46 | int commentCol = input.IndexOf("//");
|
---|
| 47 | input = input.Substring(0, commentCol >= 0 ? commentCol : input.Length);
|
---|
| 48 |
|
---|
| 49 | if (string.IsNullOrEmpty(input)) continue;
|
---|
| 50 |
|
---|
| 51 | switch (dataIndex) {
|
---|
| 52 | case 0:
|
---|
| 53 | int.TryParse(input, out nrOfCustomers);
|
---|
| 54 | customerCoordinates = new double[nrOfCustomers, 2];
|
---|
| 55 | customerDemands = new double[nrOfCustomers];
|
---|
| 56 | ++dataIndex;
|
---|
| 57 | break;
|
---|
| 58 | case 1:
|
---|
| 59 | int.TryParse(input, out nrOfDepots);
|
---|
| 60 | depotCoordinates = new double[nrOfDepots, 2];
|
---|
| 61 | depotCapacities = new double[nrOfDepots];
|
---|
| 62 | depotCosts = new double[nrOfDepots];
|
---|
| 63 | ++dataIndex;
|
---|
| 64 | break;
|
---|
| 65 | case 2:
|
---|
| 66 | for (int i = 0; i < nrOfDepots; i++) {
|
---|
| 67 | string[] coords = input.Split('\t');
|
---|
| 68 | double x; double.TryParse(coords[0], out x);
|
---|
| 69 | double y; double.TryParse(coords[1], out y);
|
---|
| 70 | depotCoordinates[i, 0] = x;
|
---|
| 71 | depotCoordinates[i, 1] = y;
|
---|
| 72 | if (i < nrOfDepots - 1)
|
---|
| 73 | input = sr.ReadLine();
|
---|
| 74 | }
|
---|
| 75 | ++dataIndex;
|
---|
| 76 | break;
|
---|
| 77 | case 3:
|
---|
| 78 | for (int i = 0; i < nrOfCustomers; i++) {
|
---|
| 79 | string[] coords = input.Split('\t');
|
---|
| 80 | double x; double.TryParse(coords[0], out x);
|
---|
| 81 | double y; double.TryParse(coords[1], out y);
|
---|
| 82 | customerCoordinates[i, 0] = x;
|
---|
| 83 | customerCoordinates[i, 1] = y;
|
---|
| 84 | if (i < nrOfCustomers - 1)
|
---|
| 85 | input = sr.ReadLine();
|
---|
| 86 | }
|
---|
| 87 | ++dataIndex;
|
---|
| 88 | break;
|
---|
| 89 | case 4:
|
---|
| 90 | double.TryParse(input, out vehicleCapacity); ++dataIndex;
|
---|
| 91 | break;
|
---|
| 92 | case 5:
|
---|
| 93 | for (int i = 0; i < nrOfDepots; i++) {
|
---|
| 94 | double capacity; double.TryParse(input, out capacity);
|
---|
| 95 | depotCapacities[i] = capacity;
|
---|
| 96 | if (i < nrOfDepots - 1)
|
---|
| 97 | input = sr.ReadLine();
|
---|
| 98 | }
|
---|
| 99 | ++dataIndex;
|
---|
| 100 | break;
|
---|
| 101 | case 6:
|
---|
| 102 | for (int i = 0; i < nrOfCustomers; i++) {
|
---|
| 103 | double demand; double.TryParse(input, out demand);
|
---|
| 104 | customerDemands[i] = demand;
|
---|
| 105 | if (i < nrOfCustomers - 1)
|
---|
| 106 | input = sr.ReadLine();
|
---|
| 107 | }
|
---|
| 108 | ++dataIndex;
|
---|
| 109 | break;
|
---|
| 110 | case 7:
|
---|
| 111 | for (int i = 0; i < nrOfDepots; i++) {
|
---|
| 112 | double cost; double.TryParse(input, out cost);
|
---|
| 113 | depotCosts[i] = cost;
|
---|
| 114 | if (i < nrOfDepots - 1)
|
---|
| 115 | input = sr.ReadLine();
|
---|
| 116 | }
|
---|
| 117 | ++dataIndex;
|
---|
| 118 | break;
|
---|
| 119 | case 8: double.TryParse(input, out vehicleCost); ++dataIndex; break;
|
---|
| 120 | case 9: Enum.TryParse(input, out distanceType); break;
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | public static double[,] GetFlpDeliveryCosts(double[,] depotCoordinates, double[,] customerCoordinates, DistanceType distanceType) {
|
---|
| 127 | int nrOfDepots = depotCoordinates.GetLength(0), nrOfCustomers = customerCoordinates.GetLength(0);
|
---|
| 128 | var deliveryCosts = new double[nrOfDepots, nrOfCustomers];
|
---|
| 129 |
|
---|
| 130 | for (int i = 0; i < nrOfDepots; i++)
|
---|
| 131 | for (int j = 0; j < nrOfCustomers; j++)
|
---|
| 132 | deliveryCosts[i, j] = DistanceFuncs[distanceType](depotCoordinates, i, customerCoordinates, j);
|
---|
| 133 |
|
---|
| 134 | return deliveryCosts;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[14649] | 137 | public static double[,] GetVrpCoordinates(double[,] depotCoordinates, double[,] customerCoordinates, int[] depots, int[] customers) {
|
---|
| 138 | var coordinates = new double[depots.Length + customers.Length, 2];
|
---|
[14604] | 139 |
|
---|
[14649] | 140 | for (int i = 0; i < depots.Length; i++) {
|
---|
| 141 | coordinates[i, 0] = depotCoordinates[depots[i], 0];
|
---|
| 142 | coordinates[i, 1] = depotCoordinates[depots[i], 1];
|
---|
| 143 | }
|
---|
[14604] | 144 | for (int i = 0; i < customers.Length; i++) {
|
---|
[14649] | 145 | coordinates[i + depots.Length, 0] = customerCoordinates[customers[i], 0];
|
---|
| 146 | coordinates[i + depots.Length, 1] = customerCoordinates[customers[i], 1];
|
---|
[14604] | 147 | }
|
---|
| 148 |
|
---|
| 149 | return coordinates;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[14649] | 152 | public static double[,] GetVrpDistances(double[,] depotCoordinates, double[,] customerCoordinates, int[] depots, int[] customers, DistanceType distanceType) {
|
---|
| 153 | var coordinates = GetVrpCoordinates(depotCoordinates, customerCoordinates, depots, customers);
|
---|
[14604] | 154 | return GetVrpDistances(coordinates, distanceType);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | public static double[,] GetVrpDistances(double[,] coordinates, DistanceType distanceType) {
|
---|
| 158 | int nrOfNodes = coordinates.GetLength(0);
|
---|
| 159 | var distances = new double[nrOfNodes, nrOfNodes];
|
---|
| 160 |
|
---|
| 161 | for (int i = 1; i < nrOfNodes; i++)
|
---|
| 162 | for (int j = 0; j < i; j++)
|
---|
| 163 | distances[i, j] = distances[j, i] = DistanceFuncs[distanceType](coordinates, i, coordinates, j);
|
---|
| 164 |
|
---|
| 165 | return distances;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | public static double Evaluate(FacilityLocationSolution flpSolution, VRPSolution[] vrpSolutions) {
|
---|
[14895] | 169 | if (flpSolution.TotalOverbookedCapacityParameter.Value.Value > 0.0)
|
---|
| 170 | return flpSolution.FitnessValueParameter.Value.Value + vrpSolutions.Sum(x => x.Quality.Value);
|
---|
| 171 | else
|
---|
| 172 | return flpSolution.TotalOpeningCostsParameter.Value.Value + vrpSolutions.Sum(x => x.Quality.Value);
|
---|
[14604] | 173 | }
|
---|
| 174 | }
|
---|
| 175 | }
|
---|