NetLogo 4.1 - implementation of a motorway ( Problem creating collision of cars )
Posted
by user206019
on Stack Overflow
See other posts from Stack Overflow
or by user206019
Published on 2010-03-31T19:30:50Z
Indexed on
2010/03/31
19:33 UTC
Read the original article
Hit count: 464
Hi there,
I am trying to create a simulation of motorway and the behaviour of the drivers in NetLogo.
I have some questions that I m struggling to solve.
Here is my code:
globals
[
selected-car ;; the currently selected car
average-speed ;; average speed of all the cars
look-ahead
]
turtles-own
[
speed ;; the current speed of the car
speed-limit ;; the maximum speed of the car (different for all cars)
lane ;; the current lane of the car
target-lane ;; the desired lane of the car
change? ;; true if the car wants to change lanes
patience ;; the driver's current patience
max-patience ;; the driver's maximum patience
]
to setup
ca
import-drawing "my_road3.png"
set-default-shape turtles "car"
crt number_of_cars
[ setup-cars ]
end
to setup-cars
set color blue
set size .9
set lane (random 3)
set target-lane (lane + 1)
setxy round random-xcor (lane + 1)
set heading 90
set speed 0.1 + random 9.9
set speed-limit (((random 11) / 10) + 1)
set change? false
set max-patience ((random 50) + 10)
set patience (max-patience - (random 10))
;; make sure no two cars are on the same patch
loop
[
ifelse any? other turtles-here
[ fd 1 ]
[ stop ]
;if count turtles-here > 1
; fd 0.1
;if
;
;ifelse (any? turtles-on neighbors) or (count turtles-here > 1)
;[
; ifelse (count turtles-here = 1)
; [ if any? turtles-on neighbors
; [
; if distance min-one-of turtles-on neighbors [distance myself] > 0.9
; [stop]
; ]
; ]
; [ fd 0.1 ]
;]
;[ stop ]
]
end
to go
drive
end
to drive
;; first determine average speed of the cars
set average-speed ((sum [speed] of turtles) / number_of_cars)
;set-current-plot "Car Speeds"
;set-current-plot-pen "average"
;plot average-speed
;set-current-plot-pen "max"
;plot (max [speed] of turtles)
;set-current-plot-pen "min"
;plot (abs (min [speed] of turtles) )
;set-current-plot-pen "selected-car"
;plot ([speed] of selected-car)
ask turtles
[
ifelse (any? turtles-at 1 0)
[
set speed ([speed] of (one-of (turtles-at 1 0)))
decelerate
]
[
ifelse (look-ahead = 2)
[
ifelse (any? turtles-at 2 0)
[
set speed ([speed] of (one-of turtles-at 2 0))
decelerate
]
[
accelerate
if count turtles-at 0 1 = 0 and ycor < 2.5
[lt 90
fd 1
rt 90]
]
]
[accelerate
if count turtles-at 0 1 = 0 and ycor < 2.5
[lt 90
fd 1
rt 90]
]
]
if (speed < 0.01)
[ set speed 0.01 ]
if (speed > speed-limit)
[ set speed speed-limit ]
ifelse (change? = false)
[ signal ]
[ change-lanes ]
;; Control for making sure no one crashes.
ifelse (any? turtles-at 1 0) and (xcor != min-pxcor - .5)
[ set speed [speed] of (one-of turtles-at 1 0) ]
[
ifelse ((any? turtles-at 2 0) and (speed > 1.0))
[
set speed ([speed] of (one-of turtles-at 2 0))
fd 1
]
[jump speed]
]
]
tick
end
;; increase speed of cars
to accelerate ;; turtle procedure
set speed (speed + (speed-up / 1000))
end
;; reduce speed of cars
to decelerate ;; turtle procedure
set speed (speed - (slow-down / 1000))
end
to signal
ifelse (any? turtles-at 1 0)
[
if ([speed] of (one-of (turtles-at 1 0))) < (speed)
[ set change? true ]
]
[ set change? false ]
end
;; undergoes search algorithms
to change-lanes ;; turtle procedure
show ycor
ifelse (patience <= 0)
[
ifelse (max-patience <= 1)
[ set max-patience (random 10) + 1 ]
[ set max-patience (max-patience - (random 5)) ]
set patience max-patience
ifelse (target-lane = 0)
[
set target-lane 1
set lane 0
]
[
set target-lane 0
set lane 1
]
]
[ set patience (patience - 1) ]
ifelse (target-lane = lane)
[
ifelse (target-lane = 0)
[
set target-lane 1
set change? false
]
[
set target-lane 0
set change? false
]
]
[
ifelse (target-lane = 1)
[
ifelse (pycor = 2)
[
set lane 1
set change? false
]
[
ifelse (not any? turtles-at 0 1)
[ set ycor (ycor + 1) ]
[
ifelse (not any? turtles-at 1 0)
[ set xcor (xcor + 1) ]
[
decelerate
if (speed <= 0)
[ set speed 0.1 ]
]
]
]
]
[
ifelse (pycor = -2)
[
set lane 0
set change? false
]
[
ifelse (not any? turtles-at 0 -1)
[ set ycor (ycor - 1) ]
[
ifelse (not any? turtles-at 1 0)
[ set xcor (xcor + 1) ]
[
decelerate
if (speed <= 0)
[ set speed 0.1 ]
]
]
]
]
]
end
I know its a bit messy because I am using code from other models from the library.
I want to know how to create the collision of the cars. I can't think of any idea. As you notice my agent has almost the same size as the patch (I set it to 0.9 so that you can distinguish the space between 2 cars when they are set next to each other and I round the coordinates so that they are set to the centre of the patch).
In my accelerate procedure I set my agent to turn left, move 1, turn right in a loop. I want to know if there's a command that lets me make the agent jump from one lane to the other (to the patch next to it on its left) without making it turn and move.
And last, if you notice the code i created the car checks the patch that is next to it on the lane on its left and the patch in front of it and the back of it. So if the 3 patches on its left are empty then it can change lane. The fuzzy part is that when i run the setup and I press Go sometimes (not always) the car goes out of the 3 basic lanes.
To understand this I have 7 lanes. The middle one which I don't use which is lane 0. Then there are 3 lanes on top of lane 0 and 3 below it. So the code I am using refers to the upper 3 lanes where I set the cars but for some reason some of the cars change lane and go to lane -3 then -2 and so forth.
If someone can give me a tip I would really appreciate it.
Thank you in advance.
Tip: if you want to try this code in netlogo keep in mind that on interface tab I have 2 buttons one setup and one go as well as 3 sliders with names: number_of_cars , speed-up , slow-down.
© Stack Overflow or respective owner