dũng bùi
dũng bùi

Reputation: 55

Merge traffic lanes in NetLogo simulation

I want to write a NetLogo program to merge automobile lanes. Vehicles are in 4 lanes, separated by 3.5 meters (with each patch representing 1m). The center coordinates of each lane are at ycor values of -3.75, -7.25, -10.75 and -14.25.

Vehicles have random xcor values with ycor values at the center of one of the lanes, and are heading to the right. I want the traffic to merge so that cars driving toward the center of the map (distancexy 0 0 <50) all move to the same lane at ycor = -14.25 as pictured. So the car already in that lane continues forward, but the cars in other lanes turn right 45 degrees to switch lanes and then turn left by 45 degrees when they reach the pycor = -14.25 lane.

What I want

The cars turn right. However, the conditions I have set to turn the car left again when it reaches ycor = -14.25 are not working. Instead, the car continues straight ahead, crossing the lane as in the next figure.

What is actually happening

My code is:

ifelse ycor = -14.25
[ fd speed ]
[ rt 45
  fd speed
  ifelse ycor = -14.25
  [ lt 45
    fd speed ]
  [ fd speed ]
]
]

Upvotes: 1

Views: 600

Answers (3)

JenB
JenB

Reputation: 17678

I think your problem is that ycor never exactly equals -14.25 unless it starts at -14.25. This is because the car moves forward and only checks its position after the movement, so it might move to -14.5 or -14.0 or some other value that is not -14.25. In that case, you want it turn left whenever it gets close to the -14.25 lane. Try something like this:

ifelse ycor = -14.25
[ fd speed ]
[ if heading = 90 [ rt 45 ]
  fd speed
  if ycor <= -12.5
  [ set heading
    set ycor -14.25
  ]
]

Upvotes: 2

B&#249;i Dũng
B&#249;i Dũng

Reputation: 79

 ifelse ycor = -14.25 [
    fd speed
    ]
    [
    rt 45
    fd speed
    ifelse ycor = -14.25
       [
       lt 45
       fd speed
       ]
       [
       fd speed
       ]
    ]
   ]

Upvotes: 0

Seth Tisue
Seth Tisue

Reputation: 30508

You wrote:

if ycor = -10.75
[
  rt 45
  fd speed 
  ;;;fd 5.1
  ifelse ycor = -14.25
  [
    lt 45
    fd speed 
  ]
  [
    fd speed 
  ]
]

If I leave out some things that don't matter, that's:

if ycor = -10.75
[
  ...
  ifelse ycor = -14.25
  [
    ...

The ifelse is inside the if, so it only runs if ycor is -10.75. But how can ycor be equal to -10.75, and equal to -14.25? It can't, so the second condition never triggers.

Perhaps the structure you intended is:

ifelse ycor = -10.75
[
  ...
]
[
  ifelse ycor = -14.25
  [
    ...

this is how you express "if ycor is -10.75, do this; but if ycor is -14.25, do that instead".

Upvotes: 2

Related Questions