在Netlogo转弯后如何回到路中间?
我的代码是一条路上大约有两辆车,如果两辆车迎头相遇,它们会改变方向(左转),然后回到路中央(pycor=0),海龟不能走到路外 我的问题是,我不知道如何让海龟回到中心,我试图利用群集的凝聚力,但我不知道正确的方法。这是我的密码:在Netlogo转弯后如何回到路中间?,netlogo,Netlogo,我的代码是一条路上大约有两辆车,如果两辆车迎头相遇,它们会改变方向(左转),然后回到路中央(pycor=0),海龟不能走到路外 我的问题是,我不知道如何让海龟回到中心,我试图利用群集的凝聚力,但我不知道正确的方法。这是我的密码: globals [initialHead too-close-distance] turtles-own [speed top-speed] to setup clear-all ask patches [setup-road] setup-cars
globals
[initialHead too-close-distance]
turtles-own
[speed
top-speed]
to setup
clear-all
ask patches [setup-road]
setup-cars
reset-ticks
end
to go
move
end
to setup-road
if pycor < 2 and pycor > -2 [ set pcolor white
]
end
to setup-cars
create-turtles 1
[
setxy -15 0
set color red
set size 1.2
set initialHead 90
set speed 0.5
set top-speed 0.5 + random-float 0.5
]
create-turtles 1
[
setxy 15 -0
set color blue
set size 1.2
set initialHead 270
set speed 0.5
set top-speed 0.5 + random-float 0.5
]
end
to move
ask turtles
[
speed-up-car
avoid
forward speed]
end
to avoid
slow-down-car
let visibility (patches in-cone 7 50)
let center pycor = 0
let too-near one-of other turtles-on visibility
ifelse too-near != nobody
[turn-away ([heading] of too-near) max-separate-turn
[;need to turn back to center]
[ fd speed]
end
to turn-away [new-heading max-turn]
turn-at-most (subtract-headings heading new-heading) max-turn
end
to turn-at-most [turn max-turn] ;; turtle procedure
ifelse abs turn > max-turn
[ ifelse turn > 0
[ rt max-turn ]
[ lt max-turn ] ]
[ rt turn ]
end
to speed-up-car
set speed (speed + acceleration)
if speed > top-speed [ set speed top-speed ]
end
to slow-down-car
set speed (speed - deceleration)
if speed < 0 [ set speed deceleration ]
end
globals
[头太近]
乌龟自己的
[速度
最高速度]
设置
清除所有
询问补丁[设置道路]
设置车辆
重置滴答声
结束
外带
移动
结束
修路
如果pycor<2且pycor>-2[将pcolor设置为白色
]
结束
安装汽车
创造海龟1
[
setxy-15 0
设置颜色为红色
套装尺寸1.2
将首字母设置为90
设定速度0.5
设置最高速度0.5+随机浮动0.5
]
创造海龟1
[
setxy 15-0
设置颜色为蓝色
套装尺寸1.2
设置首字母270
设定速度0.5
设置最高速度0.5+随机浮动0.5
]
结束
移动
问海龟
[
加速汽车
避免
前进速度]
结束
避
减速车
允许可见性(圆锥体中的补片7 50)
设中心pycor=0
在能见度上让其他海龟离得太近
如果离得太近!=没有人
[转向([航向]太近)最大单独转向
[;需要返回中心]
[fd速度]
结束
转向[新航向最大转向]
最多转弯(减去航向新航向)最多转弯
结束
最多转[最大转];;乌龟程序
如果abs转向>最大转向
[If else turn>0
[rt最大转弯次数]
[lt max turn]]
[右转弯]
结束
加速
设定速度(速度+加速度)
如果速度>最高速度[设置速度最高速度]
结束
使汽车减速
设定速度(速度-减速)
如果速度<0[设置速度减速]
结束
以下是一些基本的逻辑,我认为在两辆车朝相反方向行驶的情况下,这些逻辑将适用于超车。如果你有很多车,那么逻辑会变得更加复杂
我将留给您编写NetLogo代码
基本上,你需要一个状态转换计划来跟踪每辆车所处的状态,这样它就可以决定下一步要做什么,以及把它放在什么状态
假设你有状态[“驾驶”、“转弯”、“超车”、“折返”],你知道你是否有冲突的交通。你也知道你的y位置,这样你就知道你离中心有多远
您需要从逻辑上弄清楚所有可能的和相关的转换是什么,以及在给定您当前处于的状态下确定采用哪种转换的规则
将状态建模为节点,将转换建模为链接,这将是一个有趣的(!)过程,但让我们将其留到下一个时间
基本上,你会得到一长串if-else子句,你可以通过计算来决定下一个状态是什么,然后在计算完所有内容后,你可以做任何事情来移动到该状态。与其建立一个完整的动态运动模型,不如简单地说传球需要从原点转向10度我会朝前走一段时间,然后再转30度,再转10度,再转一段时间,然后在愉快地离开公路时回到0度。回到中心线只是一个相反的过程。对于用户来说,这可能是正常的
下面是一些伪代码:
if driving and there's no conflict, keep driving.
if driving [
if there's a conflict
[ set next-state "turning-away"
if off-road by short distance [set offset-angle 10]
if off-road by medium distance [ set offset-angle 30]
if off-road by large distance [ set offset-angle 10]
if off-road entirely [ set offset-angle 0 set state "passing"]
]
otherwise [ set next-state "driving"]
and move forward based on speed and heading.
if in the middle of turning away the conflict goes away, i don't know what you want to do. Probably completing move to the passing location is simplest to code.
if passing and there's still a conflict, keep on passing
if passing and there's no conflict anymore, reverse the moves based on distance off-road to get back on the road.
if in the middle of turning back there is a new conflict, i don't know what you want to do. Probably change to turning-away and pick a heading based on distance from the centerline as above.
无论如何,请告诉我您对该方法的看法。我不知道如何使其更简单。是的,这是我想做的事情,但使用Netlogo编码对我来说是一个挑战,谢谢您让我更清楚:D