PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tester.cpp
Go to the documentation of this file.
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 <string>
25 #include <vector>
26 #include "VRP_Solver.h"
27 #include "Utils.h"
28 
29 char buff[1005];
30 
32 
33 // Load orders from the order file. The first order represents the depot.
34 // TODO(someone) : file names are hard coded, it should be changed to commandline argument.
35 void loadOrders() {
36  FILE *fp = fopen("Orders.txt", "rt");
37  if (fp == NULL) {
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  if (strlen(buff) == 0)
44  break;
46  tokenizer.parse(buff, " \t");
47  StringVector vecToken;
48  tokenizer.getTokens(vecToken);
49 
50  if (vecToken.size() != 7)
51  continue;
52 
53  if (!isdigit(vecToken[0][0]))
54  continue;
55 
56  if (!bGotDepot) {
57  // This order represents Deopot
58  CDepotInfo depot;
59  int id = atoi(vecToken[0].c_str());
60  depot.setDepotId(id);
61 
62  Point pt;
63 
64  pt.X = atof(vecToken[1].c_str());
65  pt.Y = atof(vecToken[2].c_str());
66 
67  depot.setDepotLocation(pt);
68 
69  int openTime = atoi(vecToken[4].c_str());
70  depot.setOpenTime(openTime);
71 
72  int closeTime = atoi(vecToken[5].c_str());
73  depot.setCloseTime(closeTime);
74 
75  solver.addDepot(depot);
76  bGotDepot = true;
77  } else {
78  // This is an order
79  COrderInfo order;
80  int id = atoi(vecToken[0].c_str());
81  order.setOrderId(id);
82 
83  Point pt;
84 
85  pt.X = atof(vecToken[1].c_str());
86  pt.Y = atof(vecToken[2].c_str());
87 
88  order.setOrderLocation(pt);
89 
90  int demand = atoi(vecToken[3].c_str());
91  order.setOrderUnit(demand);
92 
93  int openTime = atoi(vecToken[4].c_str());
94  order.setOpenTime(openTime);
95 
96  int closeTime = atoi(vecToken[5].c_str());
97  order.setCloseTime(closeTime);
98 
99  int serviceTime = atoi(vecToken[6].c_str());
100  order.setServiceTime(serviceTime);
101 
102  solver.addOrder(order);
103  }
104  }
105  fclose(fp);
106 }
107 
108 // Load vehicles from vehicle file.
109 // TODO(someone) : file names are hard coded, it should be changed to commandline argument.
110 
111 void loadVehicles() {
112  FILE *fp = fopen("Vehicles.txt", "rt");
113 
114  if (fp == NULL) {
115  fprintf(stderr, "Vehicle file not found!\n");
116  }
117  // VehicleId Capacity
118  // In terms of cost all the vehicle will have default cost of 1 for the first version
119  while (fgets(buff, 1000, fp)) {
120  if (strlen(buff) == 0)
121  break;
123  tokenizer.parse(buff, " \t");
124  StringVector vecToken;
125  tokenizer.getTokens(vecToken);
126 
127  if (vecToken.size() != 2)
128  continue;
129 
130  if (!isdigit(vecToken[0][0]))
131  continue;
132 
133  CVehicleInfo vehicle;
134 
135  int id = atoi(vecToken[0].c_str());
136  vehicle.setId(id);
137 
138  int capcity = atoi(vecToken[1].c_str());
139  vehicle.setCapacity(capcity);
140 
141  vehicle.setCostPerKM(1);
142 
143  solver.addVehicle(vehicle);
144  }
145  fclose(fp);
146 }
147 
148 // Load the cost matrix
149 // TODO(someone) : file names are hard coded, it should be changed to commandline argument.
151  FILE *fp = fopen("Distance.txt", "rt");
152  if (fp == NULL) {
153  fprintf(stderr, "Cost file not found!\n");
154  return;
155  }
156 
157  // From To Cost
158  while (fgets(buff, 1000, fp)) {
159  if (strlen(buff) == 0)
160  break;
162  tokenizer.parse(buff, " \t");
163  StringVector vecToken;
164  tokenizer.getTokens(vecToken);
165 
166  if (vecToken.size() != 3)
167  continue;
168 
169  if (!isdigit(vecToken[0][0]))
170  continue;
171 
172  int fromId = atoi(vecToken[0].c_str());
173  int toId = atoi(vecToken[1].c_str());
174  CostPack cpack;
175  cpack.cost = cpack.distance = atof(vecToken[2].c_str());
176  cpack.traveltime = cpack.cost;
177 
178  if (fromId == 1)
179  solver.addDepotToOrderCost(fromId, toId, cpack);
180  else if (toId == 1)
181  solver.addOrderToDepotCost(fromId, toId, cpack);
182  else
183  solver.addOrderToOrderCost(fromId, toId, cpack);
184  }
185  fclose(fp);
186 }
187 
188 // Print the solution to a file.
189 // TODO(someone) : Currently prints on a fixed file. Later the file name will be taken as a command line argument
190 
191 bool print_solution(std::string strError) {
192  FILE *fp = fopen("result.txt", "wt");
193  if (fp == NULL) {
194  strError = "Could not open file";
195  return false;
196  }
197  CSolutionInfo solution;
198  bool bOK = solver.getSolution(solution, strError);
199  if (bOK == false)
200  return false;
201 
202  int totalRoute = solution.getTourInfoVector().size();
203  CTourInfo ctour;
204 
205  fprintf(fp, "Total Number of Route: %d\n", totalRoute);
206  fprintf(fp, "Total Cost: %.3lf\n", solution.getTotalCost());
207  fprintf(fp, "Total Distance: %.3lf\n", solution.getTotalDistance());
208  fprintf(fp, "Total TravelTime: %.3lf\n", solution.getTotalTravelTime());
209 
210  for (int i = 0; i < totalRoute; i++) {
211  ctour = solution.getTour(i);
212  fprintf(fp, "Route No. %d: \n", i + 1);
213  fprintf(fp, "Vehicle Id: %d\n", ctour.getVehicleId());
214  fprintf(fp, "Starting Depot Id: %d\n", ctour.getStartDepot());
215  fprintf(fp, "End Depot Id: %d\n", ctour.getEndDepot());
216 
217  std::vector<int> vecOrder = ctour.getOrderVector();
218  int totalOrder = vecOrder.size();
219  fprintf(fp, "Visited Order Ids: ");
220  for (int j = 0; j < totalOrder; j++) {
221  if (j > 0)
222  fprintf(fp, " ");
223  fprintf(fp, "%d", vecOrder[j]);
224  if (j < totalOrder - 1)
225  fprintf(fp, ", ");
226  }
227  fprintf(fp, "\n");
228  }
229 
230  fclose(fp);
231  return true;
232 }
233 
234 int main() {
235  loadOrders();
236  loadVehicles();
238 
239  std::string strError;
240  bool bIsOK = solver.solveVRP(strError);
241 
242  if (!bIsOK) {
243  fprintf(stderr, "Error Occurred: %s\n", strError.c_str());
244  } else {
245  print_solution(strError);
246  }
247 
248 
249  return 0;
250 }
void loadDistanceMatrix()
Definition: Tester.cpp:150
std::vector< CTourInfo > getTourInfoVector()
Definition: VRP_Solver.h:273
bool parse(std::string strInput, std::string chDelim)
void setOpenTime(int openTime)
Definition: VRP_Solver.h:141
CTourInfo & getTour(int pos)
Definition: VRP_Solver.h:250
bool getSolution(CSolutionInfo &solution, std::string &strError)
Definition: VRP_Solver.cpp:551
void setServiceTime(int serviceTime)
Definition: VRP_Solver.h:107
void setCloseTime(int closeTime)
Definition: VRP_Solver.h:104
void setOrderUnit(int orderUnit)
Definition: VRP_Solver.h:110
void setDepotId(int id)
Definition: VRP_Solver.h:147
double distance
Definition: VRP_Solver.h:51
void setOpenTime(int openTime)
Definition: VRP_Solver.h:101
bool solveVRP(std::string &strError)
Definition: VRP_Solver.cpp:223
bool addOrder(COrderInfo orderInfo)
Definition: VRP_Solver.cpp:500
double getTotalDistance()
Definition: VRP_Solver.h:263
char buff[1005]
Definition: Tester.cpp:29
void loadOrders()
Definition: Tester.cpp:35
int getEndDepot()
Definition: VRP_Solver.h:183
double X
Definition: VRP_Solver.h:46
void setCloseTime(int closeTime)
Definition: VRP_Solver.h:144
double cost
Definition: VRP_Solver.h:51
bool addVehicle(CVehicleInfo vehicleInfo)
Definition: VRP_Solver.cpp:512
double getTotalTravelTime()
Definition: VRP_Solver.h:264
double Y
Definition: VRP_Solver.h:46
void setDepotLocation(Point location)
Definition: VRP_Solver.h:150
bool addDepot(CDepotInfo depotInfo)
Definition: VRP_Solver.cpp:490
bool getTokens(StringVector &vecTokensRef)
std::vector< int > getOrderVector()
Definition: VRP_Solver.h:203
double getTotalCost()
Definition: VRP_Solver.h:262
std::vector< std::string > StringVector
bool addOrderToOrderCost(int firstOrder, int secondOrder, CostPack cost)
Definition: VRP_Solver.cpp:542
CVRPSolver solver
Definition: Tester.cpp:31
bool addOrderToDepotCost(int depotId, int orderId, CostPack cost)
Definition: VRP_Solver.cpp:533
void setOrderId(int orderId)
Definition: VRP_Solver.h:116
void setId(int id)
Definition: VRP_Solver.h:74
void setCapacity(int capacity)
Definition: VRP_Solver.h:71
double traveltime
Definition: VRP_Solver.h:51
void setOrderLocation(Point location)
Definition: VRP_Solver.h:113
bool addDepotToOrderCost(int depotId, int orderId, CostPack cost)
Definition: VRP_Solver.cpp:524
int getStartDepot()
Definition: VRP_Solver.h:180
void loadVehicles()
Definition: Tester.cpp:111
bool print_solution(std::string strError)
Definition: Tester.cpp:191
void setCostPerKM(double cost)
Definition: VRP_Solver.h:77
int main()
Definition: Tester.cpp:234
int getVehicleId()
Definition: VRP_Solver.h:175