-
Notifications
You must be signed in to change notification settings - Fork 1
/
TwoEchelonModel_IO.R
437 lines (314 loc) · 14.4 KB
/
TwoEchelonModel_IO.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#---------------------------------------------------------
# I/O functions to read and write the inputs of the 2Echelon model and connect with the CO2 models
#------------------------------------------------------------
library(stringr)
library(jsonlite)
read_config <- function(path){
#-----------------------------------------------------------
#read default parameters from config
#----------------------------------------------------------
file_config=paste(path,"/INPUT/config.csv",sep="")
fdconfig=read.csv(file_config,header=T,";")
configUI = as.matrix(fdconfig,nrow=2,ncol=8,byrow=TRUE)
configUI
k= configUI[1,1]
workshift = configUI[1,2]
handlingTimeFirstEchelon = configUI[1,3]
handlingTimeSecondEchelon = configUI[1,4]
stopTimeFirstEchelon = configUI[1,5]
stopTimeSecondEchelon = configUI[1,6]
distanceType = configUI[1,7]
haversineCalibration= configUI[1,8]
config =c(k,workshift, handlingTimeFirstEchelon, handlingTimeSecondEchelon, stopTimeFirstEchelon, stopTimeSecondEchelon, distanceType, haversineCalibration)
return(config)
}
read_facility <- function(path, i, handlingTime){
#---------------------------------------------------------------
#Read data of facilities to serve the consumers. First row of the file_facilities (.csv) with the information of the facilities
#i = leg
#---------------------------------------------------------------
file_facilities=paste(path,"/INPUT/facilities.csv",sep="")
fdFacilities=read.csv(file_facilities,header=F,";")
#FacilityUI = Information in the file (Name Address Number City ZipCode Latitude Longitude HandlingTime (minutes) StartHour EndHour)
facilityUI = as.matrix(fdFacilities,nrow=3,ncol=10,byrow=TRUE)
#Model data input : facility = (name, handling time(h), latitude, longitude)'
#facility first leg in San Fernando = origin of the route
facility = c(facilityUI[i,1], handlingTime, as.double(facilityUI[i,6]),as.double(facilityUI[i,7]))
return (facility)
}
read_vehicle <- function(path, i, stopTime){
#--------------------------------------------------------------------
#Read data of the vehicles to serve the consumer. First row of the #document (.csv) contains the information for the
#first leg and the the second row for the second leg.
#i = leg
#--------------------------------------------------------------------
file_vehicles=paste(path,"/INPUT/vehicles.csv",sep="")
fdVehicles=read.csv(file_vehicles,header=F,";")
vehiclesUI = as.matrix(fdVehicles,nrow=3,ncol=7,byrow=TRUE)
#vehicle = (name, capacity (Porto in boxes), speed (km/h), stop time (h))
vehicle = c(vehiclesUI[i,1], vehiclesUI[i,2], vehiclesUI[i,6], stopTime)
return (vehicle)
}
get_vehicle_attributes<- function(vehicle){
#--------------------------------------------------------------------------------------------
#convert the string with the attributes of the vehicles to a vector with the attributes
#----------------------------------------------------------------------------------------------
temp = strsplit(vehicle, split = ",")
vehicle_attributes = c(unlist(temp), ncol=4, byrow=TRUE)
return(vehicle_attributes)
}
read_services <- function(path){
#read the file services and return the number of delveries and the average size if the file exists
zone_services_fields=c(0,0)
file_services=paste(path,"/INPUT/services.csv",sep="")
fd= read.csv(file_services,header=F,"\t")
zoneAvgOrderSize=(mean(na.omit(fd$V20))+ mean(na.omit(fd$V19)))/2
zoneNOrders=nrow(fd)
zoneAggregatedOrdersSize = sum(na.omit(fd$V20)) + sum(na.omit(fd$V19))
zone_services_fields = c(zoneAvgOrderSize, zoneNOrders)
print( zone_services_fields[1])
return (zone_services_fields)
}
read_services_ErroHandling <- function(code){
#most cases the file with the services will not exist. this code is for handling with the exception.
print("error handling")
zone_services_fields=c(0,0)
zone_services_fields = tryCatch(code,
error = function(c) {
message("error")
zone_services_fields=c(0,0)
},
warning = function(c) {
message("warning")
zone_services_fields=c(0,0)
},
message = function(c) {
message("message")
zone_services_fields=c(0,0)
}
)
return(zone_services_fields)
}
read_deliveryZone <- function(path,i,is_twoEchelon, facility){
#--------------------------------------------------------------------
#read the data of the delivery zone
#--------------------------------------------------------------------
#read zones
file_zones=paste(path,"/INPUT/zones.csv",sep="")
fdZones=read.csv(file_zones,header=T,";")
zonesUI = as.matrix(fdZones,nrow=2,ncol=9,byrow=TRUE)
#number of services and average size will be read from different sources depending on if the services.csv is available
fdServices = read_services_ErroHandling(read_services(path))
if(fdServices[1]==0) {
zoneAvgOrderSize=zonesUI[1,10]
zoneNOrders=zonesUI[1,11]
}else{
zoneAvgOrderSize=fdServices[1]
zoneNOrders=fdServices[2]
}
if(is_twoEchelon && i==1){
totalSize=as.double(zoneAvgOrderSize)*strtoi(zoneNOrders)
zone = c(1,totalSize,0, facility[3], facility[4],1)
}else{
zoneAux= c(zonesUI[1,3], zonesUI[1,4], zonesUI[1,5], zonesUI[1,6])
print(strtoi(zoneAux[1]))
if (strtoi(zoneAux[1])== 1 || strtoi(zoneAux[3]==1)){
if(strtoi(zoneAux[1])== 1) { #the case of Madrid
str_url = zoneAux[2]
str_file_name=paste(path,zoneAux[4],sep="")
Read_url_GeographicData(str_url,path)
}
else{
if (strtoi(zoneAux[3])== 1){
str_file_name=paste(path,zoneAux[4],sep="")
}
}
zoneArea = Read_area(str_file_name)
zoneArea = zoneArea/1000000
zoneCentroid = Read_centroid(str_file_name)
zoneCentroidGeometry = st_geometry(zoneCentroid)
zoneCoordinatesCentroid = st_coordinates(zoneCentroidGeometry)
zoneCentroidX = zoneCoordinatesCentroid[2]
zoneCentroidY = zoneCoordinatesCentroid[1]
}else{
zoneArea = zonesUI[1,7]
zoneCentroidX = zonesUI[1,8]
zoneCentroidY = zonesUI[1,9]
}
zone = c(i,zoneAvgOrderSize,zoneArea, zoneCentroidX, zoneCentroidY,zoneNOrders)
}
return(zone)
}
write_outputJSON<- function(path,dfOutput, filename){
#--------------------------------------------------------------------
#write the output in the output file (.json); only the outputs
#--------------------------------------------------------------
dfOutput_json = toJSON(dfOutput, pretty=TRUE)
file_output=paste(path,"/OUTPUT/", sep="")
file_output=paste(file_output,filename, sep="")
fd=write(dfOutput_json, file_output)
return(fd)
}
twoEchelon_output_to_CO2_Models_outputJSON <- function (path, str_file_in, str_file_out, echelon_index){
#--------------------------------------------------------------------
#This function reads the output produced by the 2Echelon model and creates the file input for the wrapper
#function developed for the LMT optimization model
#--------------------------------------------------------------------
file_input=paste(path,"/OUTPUT/", sep="")
str_file_in=paste(file_input,str_file_in, sep="")
# Passing argument files
input =fromJSON(str_file_in)
# Convert JSON file to dataframe.
df = as.data.frame(input)
#browser()
#extract number of rows
n=nrow(df)
#access to the first vehicle (if one echelon just one row; if two echelon the first row)
vehicle = df[echelon_index,12] #it contains the category, fuel, segment and Eurostandard columns separated by semicolon
vehicle_attributes = get_vehicle_attributes(vehicle)
distance =df[echelon_index,16]
m=df[echelon_index,18]
distance = distance/m #the average distance
dfOutput = data.frame('ResponsePlanId'=0,
'Category'=vehicle_attributes[1],
'Fuel'=vehicle_attributes[2],
'Segment'=vehicle_attributes[3],
'EuroStandard'=vehicle_attributes[4],
'Stock'= m,
'MeanActivity'=distance)
dfOutput_json = toJSON(dfOutput, pretty=TRUE)
fd=write(dfOutput_json, str_file_out)
return(fd)
}
twoEchelon_output_to_COPERT_outputJSON <- function (path, str_file_in, str_file_out, echelon_index){
#--------------------------------------------------------------------
#This function reads the output produced by the 2Echelon model and creates the file input for the wrapper
#function developed for the LMT optimization model
#--------------------------------------------------------------------
file_input=paste(path,"/OUTPUT/", sep="")
str_file_in=paste(file_input,str_file_in, sep="")
#browser()
# Passing argument files
input =fromJSON(str_file_in)
# Convert JSON file to dataframe.
df = as.data.frame(input)
#extract number of rows
n=nrow(df)
#browser()
#access to the first vehicle (if one echelon just one row; if two echelon the first row)
vehicle = df[echelon_index,12] #it contains the category, fuel, segment and Eurostandard columns separated by semicolon
vehicle_attributes = get_vehicle_attributes(vehicle)
distance =df[echelon_index,16]
m=df[echelon_index,18]
distance = distance/m #the average distance
dfOutput = data.frame('ResponsePlanId'=0,
'Category'=vehicle_attributes[1],
'Fuel'=vehicle_attributes[2],
'Segment'=vehicle_attributes[3],
'EuroStandard'=vehicle_attributes[4],
'Stock'= m,
'MeanActivity'=distance)
dfOutput_json = toJSON(dfOutput, pretty=TRUE)
fd=write(dfOutput_json, str_file_out)
return(fd)
}
twoEchelon_output_to_EVCO2_outputJSON <- function (path, str_file_in, str_file_out,echelon_index){
#--------------------------------------------------------------------
#This function reads the output produced by the 2Echelon model and creates the file input for the wrapper
#function developed for the LMT optimization model
#--------------------------------------------------------------------
file_input=paste(path,"/OUTPUT/", sep="")
str_file_in=paste(file_input,str_file_in, sep="")
# Passing argument files
input =fromJSON(str_file_in)
# Convert JSON file to dataframe.
df = as.data.frame(input)
#extract number of rows
n=nrow(df)
#browser()
echelon_index=2
#access to the first vehicle (if one echelon just one row; if two echelon the first row)
vehicle = df[echelon_index,12] #it contains the category, fuel, segment and Eurostandard columns separated by semicolon
vehicle_attributes = get_vehicle_attributes(vehicle)
distance =df[echelon_index,16]
m=df[echelon_index,18]
velocity = df[echelon_index,14]
distance = distance/m #the average distance
dfOutput = data.frame('ResponsePlanId'=0,
'Category'=vehicle_attributes[1],
'Fuel'=vehicle_attributes[2],
'Segment'=vehicle_attributes[3],
'EuroStandard'=vehicle_attributes[4],
'Stock'= m,
'MeanActivity'=distance,
'Velocity'=velocity)
dfOutput_json = toJSON(dfOutput, pretty=TRUE)
fd=write(dfOutput_json, str_file_out)
return(fd)
}
# ----------------------------------------------------------------------------------------------------------------------
create_dir_day<- function (path,name_folder_out){
#leg=1
date=Sys.Date()
today=format(date)
name_dir = str_replace_all(today, "-", "")
name_dir=paste(name_dir,name_folder_out,sep="")
#path=getwd()
setwd(path)
dir.create(name_dir)
name_dir = paste(path,name_dir,sep="/")
return(name_dir)
}
#----------------------------------------------------------------------------------------------------------------
# create_dir_day<- function (path,leg){
# #leg=1
# date=Sys.Date()
# today=format(date)
# name_dir = str_replace_all(today, "-", "")
# name_dir=paste(name_dir,"_LL",sep="")
# name_dir = paste(name_dir,leg,sep="")
# name_dir = paste(name_dir,"_zlc",sep="")
# #path=getwd()
#
# setwd(path)
# dir.create(name_dir)
# name_dir = paste(path,name_dir,sep="/")
#
# return(name_dir)
#
# }
#
LL1_output_path <- function (path_out, file_name_out){
date=Sys.Date()
today=format(date)
setwd(path_out)
dir.create(today)
path_out =paste(path_out,today, sep="/")
file_name_out =paste(path_out,file_name_out, sep="/")
return (file_name_out)
}
LL1_update_services<- function (path,file_in) {
#replace the input file services in the system with the file file_in
file_out=paste(path,"/INPUT/services.csv",sep="")
file.copy(file_in, file_out, overwrite = TRUE,copy.mode = TRUE, copy.date = FALSE)
}
LL1_update_vehicles<- function (path,file_in) {
#replace the input file vehicles in the system with the file file_in
file_out=paste(path,"/INPUT/vehicles.csv",sep="")
file.copy(file_in, file_out, overwrite = TRUE,copy.mode = TRUE, copy.date = FALSE)
}
LL1_update_facilities<- function (path,file_in) {
#replace the input file facilities in the system with the file file_in
file_out=paste(path,"/INPUT/facilities.csv",sep="")
file.copy(file_in, file_out, overwrite = TRUE,copy.mode = TRUE, copy.date = FALSE)
}
LL1_update_zones<- function (path,file_in) {
#replace the input file zones in the system with the file file_in
file_out=paste(path,"/INPUT/zones.csv",sep="")
file.copy(file_in, file_out, overwrite = TRUE,copy.mode = TRUE, copy.date = FALSE)
}
LL1_update_config<- function (path,file_in) {
#replace the input file config in the system with the file file_in
file_out=paste(path,"/INPUT/config.csv",sep="")
file.copy(file_in, file_out, overwrite = TRUE,copy.mode = TRUE, copy.date = FALSE)
}