Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.LocationRouting/3.3/LrpUtils.cs @ 14610

Last change on this file since 14610 was 14604, checked in by jkarder, 8 years ago

#2205: worked on optimization networks

  • updated ttp networks (1, 2, 3)
  • added lrp network (1)
  • fixed plugin dependencies
File size: 7.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using HeuristicLab.Problems.FacilityLocation;
6using HeuristicLab.Problems.VehicleRouting;
7
8namespace 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)));
21      DistanceFuncs.Add(DistanceType.CEIL_2D, (d, i, c, j) => Math.Ceiling(DistanceFuncs[DistanceType.EUC_2D](d, i, c, j)) * 100);
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
137    public static double[,] GetVrpCoordinates(double[,] depotCoordinates, double[,] customerCoordinates, int depotIdx, int[] customers) {
138      var coordinates = new double[customers.Length + 1, 2];
139
140      coordinates[0, 0] = depotCoordinates[depotIdx, 0];
141      coordinates[0, 1] = depotCoordinates[depotIdx, 1];
142      for (int i = 0; i < customers.Length; i++) {
143        coordinates[i + 1, 0] = customerCoordinates[customers[i], 0];
144        coordinates[i + 1, 1] = customerCoordinates[customers[i], 1];
145      }
146
147      return coordinates;
148    }
149
150    public static double[,] GetVrpDistances(double[,] depotCoordinates, double[,] customerCoordinates, int depotIdx, int[] customers, DistanceType distanceType) {
151      var coordinates = GetVrpCoordinates(depotCoordinates, customerCoordinates, depotIdx, customers);
152      return GetVrpDistances(coordinates, distanceType);
153    }
154
155    public static double[,] GetVrpDistances(double[,] coordinates, DistanceType distanceType) {
156      int nrOfNodes = coordinates.GetLength(0);
157      var distances = new double[nrOfNodes, nrOfNodes];
158
159      for (int i = 1; i < nrOfNodes; i++)
160        for (int j = 0; j < i; j++)
161          distances[i, j] = distances[j, i] = DistanceFuncs[distanceType](coordinates, i, coordinates, j);
162
163      return distances;
164    }
165
166    public static double Evaluate(FacilityLocationSolution flpSolution, VRPSolution[] vrpSolutions) {
167      return flpSolution.TotalOpeningCostsParameter.Value.Value + vrpSolutions.Sum(x => x.Quality.Value);
168    }
169  }
170}
Note: See TracBrowser for help on using the repository browser.