Truck routing

Truck routing preview image

1 collaborator

Gil Alvaro Gil (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 5.0 • Viewed 594 times • Downloaded 32 times • Run 0 times
Download the 'Truck routing' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Info tab cannot be displayed because of an encoding error

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

extensions [ gis table]
globals [ cities-dataset countries-dataset arr_e]
breed [cities city]
breed [trucks truck]
patches-own [population country-name]

cities-own [key name pop]

to setup
  file-close-all
  ca
  set arr_e []
  set-default-shape trucks "truck"
  ; Load all of our datasets
  set cities-dataset gis:load-dataset "USCAN/cities.shp"
  set countries-dataset gis:load-dataset "USCAN/countries.shp"
  gis:set-world-envelope (gis:envelope-union-of (gis:envelope-of cities-dataset)
                                                (gis:envelope-of countries-dataset))
  display-countries
  create-towns
end 

to create-towns
  foreach gis:feature-list-of cities-dataset [
    let location gis:location-of (first (first (gis:vertex-lists-of ?)))
    if not empty? location
      [ create-cities 1
        [ set xcor item 0 location
          set ycor item 1 location
          set size 2
          set shape "pentagon"
          set color black
          if label-cities [set label gis:property-value ? "NAME"]
          set key gis:property-value ? "KEY" 
          set name gis:property-value ? "NAME" 
          set pop gis:property-value ? "POPULATION" 
          ] ]
    ]
end 

to create-electric-network
  ask turtles [create-links-with other turtles]
  set arr_e []
  ask links [set hidden? true]
  let l [] 
  foreach [self] of links [
    set l lput ([link-length] of ?) l] 
  set l sort l
  ;first pair
  ask first([self] of links with [link-length = first(l)])
  [set color 25
    set hidden? false
    set thickness 0.3 
    let e1 [who] of end1
    let e2 [who] of end2
    let tempar []
    set tempar lput e1 tempar
    set tempar lput e2 tempar
    set arr_e lput tempar arr_e]
  set l remove-item 0 l
  while [length(l) > 0] [
   let li first([self] of links with [link-length = first(l)])
   let e1 [who] of [end1] of li
   let e2 [who] of [end2] of li
   let i 0
   let flag 0
   let p1 -1
   let p2 -1
   while [i < length(arr_e)] [
     if (member? e1 item i arr_e)
      [set p1 i]
      if (member? e2 item i arr_e)
      [set p2 i]
      set i i + 1]
   let case 0
   while [flag = 0] [
     let tempar []
     if p1 != -1 and p1 = p2 [ ;Case1: Both belong to the same array
       set case 1
       set flag 2]
     if p1 = -1 and p1 = p2 [  ;Case2: Nobody belongs 
      set tempar lput e1 tempar
      set tempar lput e2 tempar
      set arr_e lput tempar arr_e
      set case 2
      set flag 1]
     if p1 != -1 and p2 = -1 [ ;Case 3: Only e1 exists in one group
       set tempar item p1 arr_e
       set arr_e remove tempar arr_e
       set tempar lput e2 tempar
       set arr_e lput tempar arr_e
       set case 3
       set flag 1]
     if p1 = -1 and p2 != -1 [ ;Case 4: Only e2 exists in one group
       set tempar item p2 arr_e
       set arr_e remove tempar arr_e
       set tempar lput e1 tempar
       set arr_e lput tempar arr_e
       set case 4
       set flag 1]
     if p1 != -1 and p2 != -1 and flag = 0 [ ;Case 5: Both already belong to different groups
       let g1 item p1 arr_e
       let g2 item p2 arr_e
       set tempar sentence g1 g2
       set arr_e remove g1 arr_e
       set arr_e remove g2 arr_e
       set arr_e lput tempar arr_e
       set case 5
       set flag 1]
   ]
   if flag = 1
   [ask li [
     set color 25
     set hidden? false
    set thickness 0.3 ]
   ;print (word "ADD link" li ". Case " case)
   ;show arr_e
   ]
   set l remove-item 0 l
   ]
  set arr_e remove-duplicates arr_e
  ;show arr_e
  ask links with [hidden? = true] [die]
end 

; Drawing polygon data from a shapefile, and optionally loading some
; of the data into turtles, if label-countries is true

to display-countries
  gis:set-drawing-color white
  gis:draw countries-dataset 1
  gis:apply-coverage countries-dataset "CNTRY_NAME" country-name
  ask patches [
    ifelse country-name = "United States" [set pcolor 135]
    [ifelse country-name = "Canada" [set pcolor 64]
    [set pcolor 88]]
    ]
end 

to import-road-network
  let dest []
  file-open "Road.txt"
  let l file-read-characters 1
  let i 1
  while [i <= count(turtles)] [
    set dest lput(file-read) dest
    set i i + 1]
  set i 1
  let j 1
  while [i <= length(dest)] [
    set j 1
    while [j <= length(dest)] [
      if j = 1 [set l file-read]
      let value file-read
      let fro item (i - 1) dest
      let too item (j - 1) dest
      if value != 0 [;print (word "From " fro " to " too)
        ask cities with [key = fro] [create-link-with one-of (cities with [key = too])]
        ]
      set j j + 1]
    set i i + 1]
  file-close
end 

to-report dijkstra
  let tempar []
  ask turtle Origin [
    let r []
    let distan []
    let l sort([who] of other turtles)
    let from_array l
    let index table:make 
    let t 0
    while [t < length(l)] [
      set r lput([]) r
      set distan lput 999999 distan
      table:put index (item t l) (position (item t l) l)
      set t t + 1]
    ;First turn
    let next_index []
    let j 0
    while [j < length(l)] [
      let de item j l
      if is-link? link who de
        [let dd 0
          set next_index lput(de) next_index
          ask link who de [set dd link-length]
          let temproute item j r
          set temproute lput de temproute
          set r replace-item j r temproute
          set distan replace-item j distan dd]
      set j j + 1]
    while [length(from_array) > 0] [
      foreach next_index [
        let temp_from ?
        let posic_orig table:get index temp_from
        let k 0
        while [k < length(from_array)] [
          let temp_dest item k from_array
          if temp_dest != temp_from [
            if is-link? link temp_dest temp_from [
              let dd 0
              ask link temp_dest temp_from [set dd link-length]
              let posic_dest table:get index temp_dest
              if item posic_dest distan > (dd + item posic_orig distan) [
                set distan replace-item posic_dest distan (dd + item posic_orig distan)
                let temp_route (item posic_orig r)
                set temp_route lput(temp_dest) temp_route
                set r replace-item posic_dest r temp_route]
            ]]
          set k k + 1]
        set from_array remove temp_from from_array]
      set next_index []
      let i 0
      while [i < length(r)]
        [ if item i r != [] and member? (item i l) from_array [set next_index lput(item i l) next_index]
          set i i + 1]]
    let final_route item (table:get index Destin) r
    set final_route fput(Origin) final_route
    let total_distance item (table:get index Destin) distan
    set tempar lput final_route tempar
    set tempar lput total_distance tempar]
  report tempar
end 

to create-shortest-path
  ask turtles [set color 85]
  ask links [
    set color gray
    set thickness 0]
if Origin = Destin [user-message "Please select two different nodes" stop]
ask turtle Origin [set color Red]
ask turtle Destin [set color Red]
let tempar dijkstra
let final_route item 0 dijkstra
let total_distance item 1 dijkstra
print (word "The shortest path between Origin and Destination nodes is " final_route " for " total_distance)
let i 0
while [i < length(final_route) - 1] [
  if (item (i + 1) final_route) != Destin [ask turtle (item (i + 1) final_route) [set color 27]]
  ask link (item i final_route) (item (i + 1) final_route) [
    set color red
    set thickness 0.3] 
  set i i + 1]
end 

to send-truck
  ask trucks [die]
  ask turtles [set color black set size 2
    set label ""]
  ask links [
    set color gray
    set thickness 0]
  if Origin = Destin [user-message "Please select two different nodes" stop]
  ask turtle Origin [set color Red set size 3 set label Name]
  ask turtle Destin [set color Red set size 3 set label Name]
  let tempar dijkstra
  let final_route item 0 dijkstra
  let total_distance item 1 dijkstra
  let fr_name []
  foreach final_route [set fr_name lput([Name] of turtle ?) fr_name]
  print "The shortest path between Origin and Destination nodes is "
  print fr_name
  print (word "with a total distance of " (total_distance * 86.5671974577573) " km")
let i 0
while [i < length(final_route) - 1] [
  if (item (i + 1) final_route) != Destin [ask turtle (item (i + 1) final_route) [set color 27 set label Name]]
  ask link (item i final_route) (item (i + 1) final_route) [
    set color red
    set thickness 0.3] 
  set i i + 1]
create-trucks 1 [
  set xcor [xcor] of city Origin
  set ycor [ycor] of city Origin
  set size 3
  set color blue
  set label (word "Truck " who)
  set label-color 25]
let c0 first([who] of trucks)
watch truck c0
set i 0
while [i < length(final_route) - 1] [
let c1 item (i + 1) final_route
move-from-to c0 c1 speed
set i i + 1]
reset-perspective
end 

to reset-labels
  ask cities [set color black set size 2 set label Name]
  ask links [set color gray]
end 

to move-from-to [a b c] ; from turtle a ==> to turtle b, with speed c
  ask turtle a
  [set heading towards turtle b]
  let coordfrom []
  let coordto []
  set coordfrom lput([xcor] of turtle a) coordfrom
  set coordfrom lput([ycor] of turtle a) coordfrom
  set coordto lput([xcor] of turtle b) coordto
  set coordto lput([ycor] of turtle b) coordto
  let stepx ((item 0 coordfrom) - (item 0 coordto)) / 20
  let stepy ((item 1 coordfrom) - (item 1 coordto)) / 20
  while [(abs((item 0 coordfrom) - (item 0 coordto))  > 0.5) or (abs((item 1 coordfrom) - (item 1 coordto))  > 0.5)]
  [ask turtle a 
    [set xcor (xcor - stepx)
      set ycor (ycor - stepy)
      wait (1 / c)]
  set coordfrom replace-item 0 coordfrom ([xcor] of turtle a) 
  set coordfrom replace-item 1 coordfrom ([ycor] of turtle a) 
  ]
end 

There is only one version of this model, created almost 10 years ago by Alvaro Gil.

Attached files

File Type Description Last updated
Mod5-6-GIS.zip extension Model, extensions and data almost 10 years ago, by Alvaro Gil Download
Truck routing.png preview Preview for 'Truck routing' almost 10 years ago, by Alvaro Gil Download

This model does not have any ancestors.

This model does not have any descendants.