Class: Arcgis_Vrps
- Inherits:
-
Object
- Object
- Arcgis_Vrps
- Defined in:
- lib/arcgis_vrps.rb
Instance Method Summary collapse
- #getJobStatus ⇒ Object
- #getOutputOrders(jobId) ⇒ Object
- #getOutputRoutes(jobId) ⇒ Object
- #getToken ⇒ Object
-
#initialize(client_id, client_secret) ⇒ Arcgis_Vrps
constructor
# getOutputRoutes(“jf163f4d4f7874fd69b4f6f90e2d5d9aa”) # getOutputOrders(“jf163f4d4f7874fd69b4f6f90e2d5d9aa”) end.
- #loadSampleData(loadFromFile) ⇒ Object
-
#mockCase1(numberOfVehicles) ⇒ Object
Routes without any start & end time (Basic routing).
-
#mockCase2(numberOfVehicles) ⇒ Object
9 locations with set times that vehicles must reach (no overlapping time).
-
#mockCase3(numberOfVehicles) ⇒ Object
8 locations with set times that vehicles must reach (with overlapping time).
-
#mockCase4(numberOfVehicles) ⇒ Object
Same as use case 3 but with more overlapping times than routes available.
-
#submitJob(ordersObj, depotsObj, routesObj, timer) ⇒ Object
TODO: Check to decide whether to use synchronous or async.
- #writeCoordinateToFile ⇒ Object
- #writeToFile(filename, contents) ⇒ Object
Constructor Details
#initialize(client_id, client_secret) ⇒ Arcgis_Vrps
# getOutputRoutes(“jf163f4d4f7874fd69b4f6f90e2d5d9aa”) # getOutputOrders(“jf163f4d4f7874fd69b4f6f90e2d5d9aa”) end
31 32 33 34 35 36 37 38 39 |
# File 'lib/arcgis_vrps.rb', line 31 def initialize(client_id, client_secret) auth = Auth.new(client_id, client_secret) @token = auth.generateToken # puts # puts "Token generated #{@token}" # puts end |
Instance Method Details
#getJobStatus ⇒ Object
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 |
# File 'lib/arcgis_vrps.rb', line 157 def getJobStatus url = "https://logistics.arcgis.com/arcgis/rest/services/World/VehicleRoutingProblem/GPServer/SolveVehicleRoutingProblem/jobs/"+@jobId escaped_url = URI.encode(url) uri = URI.parse(escaped_url) req = Net::HTTP::Post.new(uri.to_s) req.set_form_data( token: @token, returnMessages: true, f: 'json' ) http = Net::HTTP.new(uri.hostname, uri.port) http.use_ssl = true res = http.request(req) status = JSON.parse(res.body)['jobStatus'] if status == "esriJobSucceeded" return res.body elsif status == "esriJobFailed" raise "\nJob failed with the following message stack:\n #{res.body}" else return false end end |
#getOutputOrders(jobId) ⇒ Object
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 |
# File 'lib/arcgis_vrps.rb', line 218 def getOutputOrders(jobId) unless jobId.nil? @jobId = jobId end url = 'https://logistics.arcgis.com/arcgis/rest/services/World/VehicleRoutingProblem/GPServer/SolveVehicleRoutingProblem/jobs/'+@jobId+'/results/out_stops' escaped_url = URI.encode(url) uri = URI.parse(escaped_url) req = Net::HTTP::Post.new(uri.to_s) req.set_form_data( token: @token, f: 'json' ) http = Net::HTTP.new(uri.hostname, uri.port) http.use_ssl = true res = http.request(req) # We return the entire string is because it have meta data about the geometry type, spatial reference, etc ordersFeatures = JSON.parse(res.body) # ordersFeatures = JSON.parse(res.body)["value"]["features"] # puts " ================================================== " # puts "ordersFeatures" # puts ordersFeatures # puts " ================================================== " if ordersFeatures.nil? ordersFeatures = res.body end return ordersFeatures end |
#getOutputRoutes(jobId) ⇒ Object
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 |
# File 'lib/arcgis_vrps.rb', line 186 def getOutputRoutes(jobId) unless jobId.nil? @jobId = jobId end url = 'https://logistics.arcgis.com/arcgis/rest/services/World/VehicleRoutingProblem/GPServer/SolveVehicleRoutingProblem/jobs/'+@jobId+'/results/out_routes' escaped_url = URI.encode(url) uri = URI.parse(escaped_url) req = Net::HTTP::Post.new(uri.to_s) req.set_form_data( token: @token, f: 'json' ) http = Net::HTTP.new(uri.hostname, uri.port) http.use_ssl = true res = http.request(req) # We return the entire string is because it have meta data about the geometry type, spatial reference, etc routesFeatures = JSON.parse(res.body) # JSON.parse(res.body)["value"]["features"]["geometry"]["paths"] will return an array of coordinates in the following format: [longitude, latitude] # routesFeatures = JSON.parse(res.body)["value"]["features"] if routesFeatures.nil? routesFeatures = res.body end return routesFeatures end |
#getToken ⇒ Object
41 42 43 |
# File 'lib/arcgis_vrps.rb', line 41 def getToken return @token end |
#loadSampleData(loadFromFile) ⇒ Object
254 255 256 257 258 259 260 261 |
# File 'lib/arcgis_vrps.rb', line 254 def loadSampleData(loadFromFile) @loadData = Data_Load.new() if loadFromFile @loadData.getCoordinateFromMapSynq() @loadData.writeCoordinateToFile() end end |
#mockCase1(numberOfVehicles) ⇒ Object
Routes without any start & end time (Basic routing)
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 |
# File 'lib/arcgis_vrps.rb', line 263 def mockCase1(numberOfVehicles) # Routes without any start & end time (Basic routing) puts "Setting up orders" @loadData.getCoordinateFromFile() coordinate = @loadData.coordinateArrayFromFile ord = Orders.new() i = 0 coordinate.each do |coor| store_name = "Store_"+i.to_s orderAttributeObj = ord.getBasicOrderAttributeObj(store_name) ord.addOrders(coor[0], coor[1], orderAttributeObj) if i == 50 break end i += 1 end ordersObj = ord.getOrderObj(nil) puts "Setting up depots" dep = Depots.new() deportAttributeObj = dep.getDepotAttributeObj("depot_1") dep.addDepots(103.848427, 1.277751, deportAttributeObj) depotsObj = dep.getDepotObj(nil) puts "Setting up routes" route = Routes.new() startDateTime = (Time.now.to_f * 1000).to_i for i in 1..numberOfVehicles truck_name = "Truck_#{i}" route.addRoutes(truck_name, "depot_1", startDateTime, startDateTime) end routesObj = route.getRouteObj() begin contents = submitJob(ordersObj, depotsObj, routesObj, nil) writeToFile("Mock Case 1 - #{numberOfVehicles}.json", contents) rescue Exception => e puts puts e puts end end |
#mockCase2(numberOfVehicles) ⇒ Object
9 locations with set times that vehicles must reach (no overlapping time)
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 |
# File 'lib/arcgis_vrps.rb', line 319 def mockCase2(numberOfVehicles) # 9 locations with set times that vehicles must reach (no overlapping time) puts "Setting up orders" @loadData.getCoordinateFromFile() coordinate = @loadData.coordinateArrayFromFile ord = Orders.new() i = 0 minutesInSeconds = 1800000 # 30 minutes in milliseconds timeWindowStart1 = 1441933200000 # Default date time: 11th Sept 2015, 9:00:00 AM in milliseconds timeWindowEnd1 = timeWindowStart1 + minutesInSeconds # Add 30 minutes every run coordinate.each do |coor| store_name = "Store_"+i.to_s serviceTime = 30 if i < 9 && i >= 0 if i < 5 && i >= 1 # Can reach later than the set time here timeWindowStart1 += (minutesInSeconds*2) # Add 1 hour every run timeWindowEnd1 = timeWindowStart1 + minutesInSeconds # Add 30 minutes every run maxViolationTime1 = nil elsif i < 9 && i >= 5 # Can reach later than the set time based on the x minute set by MaxViolationTime1 timeWindowStart1 += (minutesInSeconds*2) # Add 1 hour every run timeWindowEnd1 = timeWindowStart1 + minutesInSeconds # Add 30 minutes every run maxViolationTime1 = 0 end orderAttributeObj = ord.getOrderAttributeObj(store_name, serviceTime, timeWindowStart1, timeWindowEnd1, maxViolationTime1) elsif i >= 9 timeWindowStart1 = nil timeWindowEnd1 = nil maxViolationTime1 = nil orderAttributeObj = ord.getBasicOrderAttributeObj(store_name) end ord.addOrders(coor[0], coor[1], orderAttributeObj) if i == 49 break end i += 1 end ordersObj = ord.getOrderObj(nil) puts "Setting up depots" dep = Depots.new() deportAttributeObj = dep.getDepotAttributeObj("depot_1") dep.addDepots(103.848427, 1.277751, deportAttributeObj) depotsObj = dep.getDepotObj(nil) puts "Setting up routes" route = Routes.new() startDateTime = 1441933200000 for i in 1..numberOfVehicles truck_name = "Truck_#{i}" route.addRoutes(truck_name, "depot_1", startDateTime, startDateTime) end routesObj = route.getRouteObj() # puts # puts ordersObj.to_json # puts # puts depotsObj.to_json # puts # puts routesObj.to_json # puts begin contents = submitJob(ordersObj, depotsObj, routesObj, nil) writeToFile("Mock Case 2 - #{numberOfVehicles}.json", contents) rescue Exception => e puts puts e puts end end |
#mockCase3(numberOfVehicles) ⇒ Object
8 locations with set times that vehicles must reach (with overlapping time)
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 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'lib/arcgis_vrps.rb', line 405 def mockCase3(numberOfVehicles) # 8 locations with set times that vehicles must reach (with overlapping time) puts "Setting up orders" @loadData.getCoordinateFromFile() coordinate = @loadData.coordinateArrayFromFile ord = Orders.new() i = 0 minutesInSeconds = 1800000 # 30 minutes in milliseconds timeWindowStart1 = 1441933200000 # Default date time: 11th Sept 2015, 9:00:00 AM in milliseconds timeWindowEnd1 = timeWindowStart1 + minutesInSeconds # Add 30 minutes every run countThree = 0 countFive = 0 coordinate.each do |coor| store_name = "Store_"+i.to_s serviceTime = 30 if ((i%3 == 0 || i%5 == 0) && (countThree < 4 || countFive < 4)) if timeWindowStart1.nil? || timeWindowStart1 >= 1441944000000 timeWindowStart1 = 1441933200000 end if i%3 == 0 && countThree < 4 timeWindowStart1 += (minutesInSeconds*2) # Add 1 hour every run timeWindowEnd1 = timeWindowStart1 + minutesInSeconds # Add 30 minutes every run maxViolationTime1 = nil countThree += 1 elsif i%5 == 0 && countFive < 4 timeWindowStart1 += (minutesInSeconds*2) # Add 1 hour every run timeWindowEnd1 = timeWindowStart1 + minutesInSeconds # Add 30 minutes every run maxViolationTime1 = 0 countFive += 1 end orderAttributeObj = ord.getOrderAttributeObj(store_name, serviceTime, timeWindowStart1, timeWindowEnd1, maxViolationTime1) else timeWindowStart1 = nil timeWindowEnd1 = nil maxViolationTime1 = nil orderAttributeObj = ord.getBasicOrderAttributeObj(store_name) end ord.addOrders(coor[0], coor[1], orderAttributeObj) if i == 49 break end i += 1 end ordersObj = ord.getOrderObj(nil) puts "Setting up depots" dep = Depots.new() deportAttributeObj = dep.getDepotAttributeObj("depot_1") dep.addDepots(103.848427, 1.277751, deportAttributeObj) depotsObj = dep.getDepotObj(nil) puts "Setting up routes" route = Routes.new() startDateTime = 1441933200000 for i in 1..numberOfVehicles truck_name = "Truck_#{i}" route.addRoutes(truck_name, "depot_1", startDateTime, startDateTime) end routesObj = route.getRouteObj() # puts # puts ordersObj.to_json # puts # puts depotsObj.to_json # puts # puts routesObj.to_json # puts begin contents = submitJob(ordersObj, depotsObj, routesObj, nil) writeToFile("Mock Case 3 - #{numberOfVehicles}.json", contents) rescue Exception => e puts puts e puts end end |
#mockCase4(numberOfVehicles) ⇒ Object
Same as use case 3 but with more overlapping times than routes available
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 |
# File 'lib/arcgis_vrps.rb', line 500 def mockCase4(numberOfVehicles) # Same as use case 3 but with more overlapping times than routes available puts "Setting up orders" @loadData.getCoordinateFromFile() coordinate = @loadData.coordinateArrayFromFile ord = Orders.new() i = 0 minutesInSeconds = 1800000 # 30 minutes in milliseconds timeWindowStart1 = 1441933200000 # Default date time: 11th Sept 2015, 9:00:00 AM in milliseconds timeWindowEnd1 = timeWindowStart1 + minutesInSeconds # Add 30 minutes every run countThree = 0 countFive = 0 coordinate.each do |coor| store_name = "Store_"+i.to_s serviceTime = 30 if i < 6 # This if block is to set 3 with MaxViolationTime1 and 3 without # if i < 3 && i >= 0 # maxViolationTime1 = nil # elsif i < 6 && i >= 3 # maxViolationTime1 = 0 # end maxViolationTime1 = 0 # This is to set all 6 with MaxViolationTime1 orderAttributeObj = ord.getOrderAttributeObj(store_name, serviceTime, timeWindowStart1, timeWindowEnd1, maxViolationTime1) else timeWindowStart1 = nil timeWindowEnd1 = nil maxViolationTime1 = nil orderAttributeObj = ord.getBasicOrderAttributeObj(store_name) end ord.addOrders(coor[0], coor[1], orderAttributeObj) if i == 49 break end i += 1 end ordersObj = ord.getOrderObj(nil) puts "Setting up depots" dep = Depots.new() deportAttributeObj = dep.getDepotAttributeObj("depot_1") dep.addDepots(103.848427, 1.277751, deportAttributeObj) depotsObj = dep.getDepotObj(nil) puts "Setting up routes" route = Routes.new() startDateTime = 1441933200000 for i in 1..numberOfVehicles truck_name = "Truck_#{i}" route.addRoutes(truck_name, "depot_1", startDateTime, startDateTime) end routesObj = route.getRouteObj() # puts # puts ordersObj.to_json # puts # puts depotsObj.to_json # puts # puts routesObj.to_json # puts begin contents = submitJob(ordersObj, depotsObj, routesObj, nil) writeToFile("Mock Case 4 - location - 6.json", contents) rescue Exception => e puts puts e puts end end |
#submitJob(ordersObj, depotsObj, routesObj, timer) ⇒ Object
TODO: Check to decide whether to use synchronous or async
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 |
# File 'lib/arcgis_vrps.rb', line 45 def submitJob(ordersObj, depotsObj, routesObj, timer) # TODO: Check to decide whether to use synchronous or async puts "Submitting job" jobStatusNoOfCalls = 0 t1 = Time.now.to_i url = "https://logistics.arcgis.com/arcgis/rest/services/World/VehicleRoutingProblem/GPServer/SolveVehicleRoutingProblem/submitJob" escaped_url = URI.encode(url) uri = URI.parse(escaped_url) req = Net::HTTP::Post.new(uri.to_s) # Expiration is set to 20160 minutes = 2 weeks req.set_form_data( orders: ordersObj.to_json, depots: depotsObj.to_json, routes: routesObj.to_json, # default_date: Time.now.to_f, # This must be in milliseconds default_date: 1453600108000, token: @token, f: 'json', time_units: 'Seconds', distance_units: 'Meters' ) http = Net::HTTP.new(uri.hostname, uri.port) http.use_ssl = true res = http.request(req) # print " ======================================================== " # print "req" # print ordersObj.to_json # print " ----- " # print depotsObj.to_json # print " ----- " # print routesObj.to_json # print " ----- " # print Time.now.to_i # print " ----- " # print @token # print " ======================================================== " @jobId = JSON.parse(res.body)['jobId'] if @jobId.nil? raise "Error! #{res.body}" else puts "Received job ID: #{@jobId}" print "Getting job status..." if timer.nil? timer = 1 # Timer of 500ms to constantly check for updates end start_time = Time.now end_time = start_time+timer completed = false begin while completed == false print "." jobStatusNoOfCalls += 1 while Time.now < end_time end completed = getJobStatus() end print "Completed!\n" puts # puts completed.to_s # puts t2 = Time.now.to_i time_jobStatusIsComplete = t2-t1 puts "Time taken for job to be completed: #{time_jobStatusIsComplete}" puts routesReceived = getOutputRoutes(nil) # routesReceived = "Routes received: \n #{routesReceived}" ordersReceived = getOutputOrders(nil) # ordersReceived = "Orders received: \n #{ordersReceived}" t3 = Time.now.to_i time_fullJobComplete = t3-t1 # extraContent = "Total time taken for getting routes and orders: #{time_fullJobComplete} \nNumber of calls to jobStatus: #{jobStatusNoOfCalls} \n\n" extraContent = { :time_job_status_complete => time_jobStatusIsComplete, :time_full_job_complete => time_fullJobComplete, :no_of_job_status_call => jobStatusNoOfCalls } # puts extraContent # puts return extraContent, routesReceived, ordersReceived rescue Exception => e puts puts e puts end end end |
#writeCoordinateToFile ⇒ Object
587 588 589 590 591 |
# File 'lib/arcgis_vrps.rb', line 587 def writeCoordinateToFile @loadData.getCoordinateFromFile(true) coordinate = @loadData.coordinateArrayFromFile writeToFile("coordinateArray", coordinate.to_json); end |
#writeToFile(filename, contents) ⇒ Object
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 |
# File 'lib/arcgis_vrps.rb', line 593 def writeToFile(filename, contents) puts "Writing to file #{filename}" File.open("lib/test/sample_data/#{filename}", "w") do |file| if contents.kind_of?(Array) contents.each do |content| file.puts content file.puts end else file.puts contents file.puts end end puts "Done writing to file #{filename}" puts end |