pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
Tester.cpp
1 /*PGR-GNU*****************************************************************
2 
3 Copyright (c) 2013 Khondoker Md. Razequl Islam
4 ziboncsedu@gmail.com
5 
6 ------
7 
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 
22 ********************************************************************PGR-GNU*/
23 
24 #include "VRP_Solver.h"
25 #include "Utils.h"
26 
27 char buff[1005];
28 
29 CVRPSolver solver;
30 
31 // Load orders from the order file. The first order represents the depot.
32 // TODO: file names are hard coded, it should be changed to commandline argument.
33 void loadOrders()
34 {
35  FILE *fp = fopen("Orders.txt", "rt");
36  if(fp == NULL)
37  {
38  fprintf(stderr, "Order file not found!\n");
39  }
40  bool bGotDepot = false;
41  // OrderId XCord YCord Demand StartTime EndTime ServiceTime
42  while(fgets(buff, 1000, fp))
43  {
44  if(strlen(buff) == 0)
45  break;
47  tokenizer.parse(buff, " \t");
48  StringVector vecToken;
49  tokenizer.getTokens(vecToken);
50 
51  if(vecToken.size() != 7)
52  continue;
53 
54  if(!isdigit(vecToken[0][0]))
55  continue;
56 
57  if (!bGotDepot)
58  {
59  // This order represents Deopot
61  int id = atoi(vecToken[0].c_str());
62  depot.setDepotId(id);
63 
64  Point pt;
65 
66  pt.X = atof(vecToken[1].c_str());
67  pt.Y = atof(vecToken[2].c_str());
68 
69  depot.setDepotLocation(pt);
70 
71  int openTime = atoi(vecToken[4].c_str());
72  depot.setOpenTime(openTime);
73 
74  int closeTime = atoi(vecToken[5].c_str());
75  depot.setCloseTime(closeTime);
76 
77  solver.addDepot(depot);
78  bGotDepot = true;
79  }
80  else
81  {
82  // This is an order
83  COrderInfo order;
84  int id = atoi(vecToken[0].c_str());
85  order.setOrderId(id);
86 
87  Point pt;
88 
89  pt.X = atof(vecToken[1].c_str());
90  pt.Y = atof(vecToken[2].c_str());
91 
92  order.setOrderLocation(pt);
93 
94  int demand = atoi(vecToken[3].c_str());
95  order.setOrderUnit(demand);
96 
97  int openTime = atoi(vecToken[4].c_str());
98  order.setOpenTime(openTime);
99 
100  int closeTime = atoi(vecToken[5].c_str());
101  order.setCloseTime(closeTime);
102 
103  int serviceTime = atoi(vecToken[6].c_str());
104  order.setServiceTime(serviceTime);
105 
106  solver.addOrder(order);
107  }
108  }
109  fclose(fp);
110 }
111 
112 // Load vehicles from vehicle file.
113 // TODO: file names are hard coded, it should be changed to commandline argument.
114 
115 void loadVehicles()
116 {
117  FILE *fp = fopen("Vehicles.txt", "rt");
118 
119  if(fp == NULL)
120  {
121  fprintf(stderr, "Vehicle file not found!\n");
122  }
123  // VehicleId Capacity
124  // In terms of cost all the vehicle will have default cost of 1 for the first version
125  while(fgets(buff, 1000, fp))
126  {
127  if(strlen(buff) == 0)
128  break;
129  StringTokenizer tokenizer;
130  tokenizer.parse(buff, " \t");
131  StringVector vecToken;
132  tokenizer.getTokens(vecToken);
133 
134  if(vecToken.size() != 2)
135  continue;
136 
137  if(!isdigit(vecToken[0][0]))
138  continue;
139 
140  CVehicleInfo vehicle;
141 
142  int id = atoi(vecToken[0].c_str());
143  vehicle.setId(id);
144 
145  int capcity = atoi(vecToken[1].c_str());
146  vehicle.setCapacity(capcity);
147 
148  vehicle.setCostPerKM(1);
149 
150  solver.addVehicle(vehicle);
151  }
152  fclose(fp);
153 }
154 
155 // Load the cost matrix
156 // TODO: file names are hard coded, it should be changed to commandline argument.
157 void loadDistanceMatrix()
158 {
159  FILE *fp = fopen("Distance.txt", "rt");
160  if(fp == NULL)
161  {
162  fprintf(stderr, "Cost file not found!\n");
163  return;
164  }
165 
166  // From To Cost
167  while(fgets(buff, 1000, fp))
168  {
169  if(strlen(buff) == 0)
170  break;
171  StringTokenizer tokenizer;
172  tokenizer.parse(buff, " \t");
173  StringVector vecToken;
174  tokenizer.getTokens(vecToken);
175 
176  if(vecToken.size() != 3)
177  continue;
178 
179  if(!isdigit(vecToken[0][0]))
180  continue;
181 
182  int fromId = atoi(vecToken[0].c_str());
183  int toId = atoi(vecToken[1].c_str());
184  CostPack cpack;
185  cpack.cost = cpack.distance = atof(vecToken[2].c_str());
186  cpack.traveltime = cpack.cost;
187 
188  if(fromId == 1)
189  solver.addDepotToOrderCost(fromId, toId, cpack);
190  else if(toId == 1)
191  solver.addOrderToDepotCost(fromId, toId, cpack);
192  else
193  solver.addOrderToOrderCost(fromId, toId, cpack);
194  }
195  fclose(fp);
196 }
197 
198 // Print the solution to a file.
199 // TODO: Currently prints on a fixed file. Later the file name will be taken as a command line argument
200 
201 bool print_solution(std::string strError)
202 {
203  FILE *fp = fopen("result.txt", "wt");
204  if(fp == NULL)
205  {
206  strError = "Could not open file";
207  return false;
208  }
209  CSolutionInfo solution;
210  bool bOK = solver.getSolution(solution, strError);
211  if(bOK == false)
212  return false;
213 
214  int totalRoute = solution.getTourInfoVector().size();
215  CTourInfo ctour;
216 
217  fprintf(fp, "Total Number of Route: %d\n", totalRoute);
218  fprintf(fp, "Total Cost: %.3lf\n", solution.getTotalCost());
219  fprintf(fp, "Total Distance: %.3lf\n", solution.getTotalDistance());
220  fprintf(fp, "Total TravelTime: %.3lf\n", solution.getTotalTravelTime());
221 
222  for(int i = 0; i < totalRoute; i++)
223  {
224  ctour = solution.getTour(i);
225  fprintf(fp, "Route No. %d: \n", i + 1);
226  fprintf(fp, "Vehicle Id: %d\n", ctour.getVehicleId());
227  fprintf(fp, "Starting Depot Id: %d\n", ctour.getStartDepot());
228  fprintf(fp, "End Depot Id: %d\n", ctour.getEndDepot());
229 
230  std::vector<int> vecOrder = ctour.getOrderVector();
231  int totalOrder = vecOrder.size();
232  fprintf(fp, "Visited Order Ids: ");
233  for(int j = 0; j < totalOrder; j++)
234  {
235  if(j > 0)
236  fprintf(fp, " ");
237  fprintf(fp, "%d", vecOrder[j]);
238  if(j < totalOrder - 1)
239  fprintf(fp, ",");
240  }
241  fprintf(fp, "\n");
242  }
243 
244  fclose(fp);
245  return true;
246 }
247 
248 int main()
249 {
250  loadOrders();
251  loadVehicles();
252  loadDistanceMatrix();
253 
254  std::string strError;
255  bool bIsOK = solver.solveVRP(strError);
256 
257  if(!bIsOK)
258  {
259  fprintf(stderr, "Error Occurred: %s\n", strError.c_str());
260  }
261  else
262  {
263  print_solution(strError);
264  }
265 
266 
267  return 0;
268 }
Definition: pdp.hpp:31