Please help me - - Two lane traffic simulation

113 views
Skip to first unread message

DuyThoi Do

unread,
Oct 11, 2023, 12:26:50 PM10/11/23
to netlogo-users
Hello Everyone!
Im a new member
I'm developing a model with 2 lane traffic in Netlogo. I develop based on the 2-lane traffic model in the Netlogo Library.

My model target is a mixed lane with 2 types of vehicles ( cars and buses). And i'd like write code so that the sizes of the two types of vehicles are proportional to the sizes of the patches. For example, the size of each patch is 1x1m, the size of a car is 5 x 3m, and the size of a bus is 9 x 3m. 

And I encountered 2 problems as follows:
1/ I have written simulations for 2 types of vehicles but when i set up and go, i got a error like this: 
"TOWARDS expected input to be an agent but got NOBODY instead.
error while turtle 45 running TOWARDS
  called by procedure MOVE-TO-TARGET-LANE
  called by procedure GO
  called by Button 'go'"

2/I still don't know how to write the size of the two vehicles proportional to the size of the patch

I SINCERELY HOPE THE EXPERTS ON THE FORUM CAN SPEND YOUR SOME TIME TO HELP ME FIX IT. THANK YOU SO MUCH!screenshot_1697041482.pngscreenshot_1697041511.png

My code like this:
globals [
  selected-car   ; the currently selected car
  selected-bus ; the currently selected bus
  lanes          ; a list of the y coordinates of different lanes
]

turtles-own [
  speed         ; the current speed of the car
  top-speed     ; the maximum speed of the car (different for all cars)
  target-lane   ; the desired lane of the car
  patience      ; the driver's current level of patience
]

to setup
  clear-all
  draw-road
  create-or-remove-cars
  set selected-car one-of turtles
  ask selected-car [ set color red ]

  create-or-remove-buses
  set selected-bus one-of turtles
  ask selected-bus [ set color pink ]
  reset-ticks
end

to create-or-remove-cars

  ; make sure we don't have too many cars for the room we have on the road
  let road-patches patches with [ member? pycor lanes ]
  if number-of-cars > count road-patches [
    set number-of-cars count road-patches
  ]

  create-turtles (number-of-cars - count turtles) [
    set color car-color
    move-to one-of free road-patches
    set target-lane pycor
    set heading 90
    set top-speed 0.5 + random-float 0.5
    set speed 0.5
    set size 1
    set patience random max-patience
    set shape "car"

  ]

  if count turtles > number-of-cars [
    let n count turtles - number-of-cars
    ask n-of n [ other turtles ] of selected-car [ die ]
  ]

end

to create-or-remove-buses

  ; make sure we don't have too many cars for the room we have on the road
  let road-patches patches with [ member? pycor lanes ]
  if number-of-buses > count road-patches [
    set number-of-buses count road-patches
  ]
  create-turtles (number-of-buses - count turtles) [
    set color bus-color
    move-to one-of free road-patches
    set target-lane pycor
    set heading 90
    set top-speed 0.5 + random-float 0.5
    set speed 0.3
    set size 1
    set patience random max-patience
    set shape "cow"
  ]

end

to-report free [ road-patches ] ; turtle procedure
  let this-car self
  report road-patches with [
    not any? turtles-here with [ self != this-car ]
  ]
end

to draw-road
  ask patches [
    ; the road is surrounded by green grass of varying shades
    set pcolor green - random-float 0.5
  ]
  set lanes n-values number-of-lanes [ n -> number-of-lanes - (n * 2) - 1 ]
  ask patches with [ abs pycor <= number-of-lanes ] [
    ; the road itself is varying shades of grey
    set pcolor grey - 2.5 + random-float 0.25
  ]
  draw-road-lines
end

to draw-road-lines
  let y (last lanes) - 1 ; start below the "lowest" lane
  while [ y <= first lanes + 1 ] [
    if not member? y lanes [
      ; draw lines on road patches that are not part of a lane
      ifelse abs y = number-of-lanes
        [ draw-line y yellow 0 ]  ; yellow for the sides of the road
        [ draw-line y white 0.5 ] ; dashed white between lanes
    ]
    set y y + 1 ; move up one patch
  ]
end

to draw-line [ y line-color gap ]
  ; We use a temporary turtle to draw the line:
  ; - with a gap of zero, we get a continuous line;
  ; - with a gap greater than zero, we get a dasshed line.
  create-turtles 1 [
    setxy (min-pxcor - 0.5) y
    hide-turtle
    set color line-color
    set heading 90
    repeat world-width [
      pen-up
      forward gap
      pen-down
      forward (1 - gap)
    ]
    die
  ]
end

to go
  create-or-remove-cars
  create-or-remove-buses
  ask turtles [ move-forward ]
  ask turtles with [ patience <= 0 ] [ choose-new-lane ]
  ask turtles with [ ycor != target-lane ] [ move-to-target-lane ]
  tick
end

to move-forward ; turtle procedure
  set heading 90
  speed-up-car ; we tentatively speed up, but might have to slow down
  let blocking-cars other turtles in-cone (1 + speed) 180 with [ y-distance <= 1 ]
  let blocking-car min-one-of blocking-cars [ distance myself ]
  if blocking-car != nobody [
    ; match the speed of the car ahead of you and then slow
    ; down so you are driving a bit slower than that car.
    set speed [ speed ] of blocking-car
    slow-down-car
  ]
  forward speed
  speed-up-bus ; we tentatively speed up, but might have to slow down
  let blocking-buses other turtles in-cone (1 + speed) 180 with [ y-distance <= 1 ]
  let blocking-bus min-one-of blocking-cars [ distance myself ]
  if blocking-bus != nobody [
    ; match the speed of the car ahead of you and then slow
    ; down so you are driving a bit slower than that car.
    set speed [ speed ] of blocking-bus
    slow-down-bus
  ]
  forward speed
end

to slow-down-car ; turtle procedure
  set speed (speed - deceleration1)
  if speed < 0 [ set speed deceleration1 ]
  ; every time you hit the brakes, you loose a little patience
  set patience patience - 1
end

to slow-down-bus ; turtle procedure
  set speed (speed - deceleration2)
  if speed < 0 [ set speed deceleration2 ]
  ; every time you hit the brakes, you loose a little patience
  set patience patience - 1
end

to speed-up-car ; turtle procedure
  set speed (speed + acceleration1)
  if speed > top-speed [ set speed top-speed ]
end

to speed-up-bus ; turtle procedure
  set speed (speed + acceleration2)
  if speed > top-speed [ set speed top-speed ]
end

to choose-new-lane ; turtle procedure
  ; Choose a new lane among those with the minimum
  ; distance to your current lane (i.e., your ycor).
  let other-lanes remove ycor lanes
  if not empty? other-lanes [
    let min-dist min map [ y -> abs (y - ycor) ] other-lanes
    let closest-lanes filter [ y -> abs (y - ycor) = min-dist ] other-lanes
    set target-lane one-of closest-lanes
    set patience max-patience
  ]
end

to move-to-target-lane ; turtle procedure
  set heading ifelse-value target-lane < ycor [ 180 ] [ 0 ]
  let blocking-cars other turtles in-cone (1 + abs (ycor - target-lane)) 180 with [ x-distance <= 1 ]
  let blocking-car min-one-of blocking-cars [ distance myself ]
  ifelse blocking-car = nobody [
    forward 0.2
    set ycor precision ycor 1 ; to avoid floating point errors
  ] [
    ; slow down if the car blocking us is behind, otherwise speed up
    ifelse towards blocking-car <= 180 [ slow-down-car ] [ speed-up-car ]
  ]
  let blocking-buses other turtles in-cone (1 + abs (ycor - target-lane)) 180 with [ x-distance <= 1 ]
  let blocking-bus min-one-of blocking-buses [ distance myself ]
  ifelse blocking-buses = nobody [
    forward 0.2
    set ycor precision ycor 1 ; to avoid floating point errors
  ] [
    ; slow down if the bus blocking us is behind, otherwise speed up
    ifelse towards blocking-bus <= 180 [ slow-down-bus ] [ speed-up-bus ]
  ]
end

to-report x-distance
  report distancexy [ xcor ] of myself ycor
end

to-report y-distance
  report distancexy xcor [ ycor ] of myself
end

to select-car
  ; allow the user to select a different car by clicking on it with the mouse
  if mouse-down? [
    let mx mouse-xcor
    let my mouse-ycor
    if any? turtles-on patch mx my [
      ask selected-car [ set color car-color ]
      set selected-car one-of turtles-on patch mx my
      ask selected-car [ set color red ]
      display
    ]
  ]
end

to select-bus
  ; allow the user to select a different bus by clicking on it with the mouse
  if mouse-down? [
    let mx mouse-xcor
    let my mouse-ycor
    if any? turtles-on patch mx my [
      ask selected-bus [ set color bus-color ]
      set selected-bus one-of turtles-on patch mx my
      ask selected-bus [ set color pink ]
      display
    ]
  ]
end

to-report car-color
  ; give all cars a blueish color, but still make them distinguishable
  report one-of [ blue cyan sky ] + 1.5 + random-float 1.0
end

to-report bus-color
  ; give all cars a blueish color, but still make them distinguishable
  report one-of [ yellow ] + 1.5 + random-float 1.0
end

to-report number-of-lanes
  ; To make the number of lanes easily adjustable, remove this
  ; reporter and create a slider on the interface with the same
  ; name. 8 lanes is the maximum that currently fit in the view.
  report 2
end


; Copyright 1998 Uri Wilensky.
; See Info tab for full copyright and license.



Reply all
Reply to author
Forward
0 new messages