Merge traffic lanes in NetLogo simulation

喜欢而已 提交于 2019-12-05 07:28:09

问题


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.

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.

My code is:

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

回答1:


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".




回答2:


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
  ]
]



回答3:


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


来源:https://stackoverflow.com/questions/34262738/merge-traffic-lanes-in-netlogo-simulation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!