如何在NetLogo中设置不同站点海龟(乘客)的登车速度差异?
实现NetLogo不同站点乘客登车速度差异化的解决方案
核心修改思路
要让不同站点乘客登车速度有差异,核心是给不同站点的乘客分配不同的速度区间,同时修正移动和登车逻辑,确保每个站点的乘客按设定速度向公交车移动,登车后正确触发公交车驶离。
具体修改步骤及代码调整
1. 调整各站点乘客的速度区间
根据需求设置速度梯度:上方站点乘客速度最快,中间次之,下方最慢,直接修改setup里的乘客速度赋值语句即可。
2. 修正乘客移动与登车逻辑
原check-in-1的移动判断存在漏洞,修改后确保:
- 只有未登车的乘客会向目标移动
- 乘客到达灰色登车区域即视为完成登车,直接移除该个体
3. 修正公交车驶离的判断条件
原代码存在shape拼写错误,同时优化乘客数量统计逻辑,确保公交车只在对应站点无未登车乘客时驶离。
修改后的完整代码
turtles-own [speed] breed [busses bus] globals [time] to setup clear-all ;; 绘制上方站点 ask patches with [pxcor = 2 and pycor = 5][set pcolor white] ask patches with [pxcor = 2 and pycor = 4][set pcolor white] ask patches with [pxcor = 0 and pycor = 4][set pcolor white] ask patches with [pxcor = 0 and pycor = 5][set pcolor white] ask patches with [pxcor = 1 and pycor = 4][set pcolor white] ask patches with [pxcor = 1 and pycor = 5][set pcolor white] ask patches with [pxcor = 3 and pycor = 4][set pcolor white] ask patches with [pxcor = 3 and pycor = 5][set pcolor white] ask patches with [pxcor = 4 and pycor = 4][set pcolor white] ask patches with [pxcor = 4 and pycor = 5][set pcolor white] ask patches with [pxcor = 5 and pycor = 4][set pcolor white] ask patches with [pxcor = 5 and pycor = 5][set pcolor white] ask patches with [pycor = 6][set pcolor gray] ;; 绘制中间站点 ask patches with [pxcor = 2 and pycor = -6][set pcolor white] ask patches with [pxcor = 2 and pycor = -5][set pcolor white] ask patches with [pxcor = 0 and pycor = -5][set pcolor white] ask patches with [pxcor = 0 and pycor = -6][set pcolor white] ask patches with [pxcor = 1 and pycor = -5][set pcolor white] ask patches with [pxcor = 1 and pycor = -6][set pcolor white] ask patches with [pxcor = 3 and pycor = -5][set pcolor white] ask patches with [pxcor = 3 and pycor = -6][set pcolor white] ask patches with [pxcor = 4 and pycor = -5][set pcolor white] ask patches with [pxcor = 4 and pycor = -6][set pcolor white] ask patches with [pxcor = 5 and pycor = -5][set pcolor white] ask patches with [pxcor = 5 and pycor = -6][set pcolor white] ask patches with [pycor = -4][set pcolor gray] ;; 绘制下方站点 ask patches with [pxcor = 2 and pycor = -15][set pcolor white] ask patches with [pxcor = 2 and pycor = -14][set pcolor white] ask patches with [pxcor = 0 and pycor = -14][set pcolor white] ask patches with [pxcor = 0 and pycor = -15][set pcolor white] ask patches with [pxcor = 1 and pycor = -14][set pcolor white] ask patches with [pxcor = 1 and pycor = -15][set pcolor white] ask patches with [pxcor = 3 and pycor = -14][set pcolor white] ask patches with [pxcor = 3 and pycor = -15][set pcolor white] ask patches with [pxcor = 4 and pycor = -14][set pcolor white] ask patches with [pxcor = 4 and pycor = -15][set pcolor white] ask patches with [pxcor = 5 and pycor = -14][set pcolor white] ask patches with [pxcor = 5 and pycor = -15][set pcolor white] ask patches with [pycor = -13][set pcolor gray] ;; 创建上方公交车 create-busses 1[ set color red set size 7 set xcor 3 set ycor 8 set shape "bus" set heading 90 ] ;; 创建中间公交车 create-busses 1[ set color red set size 7 set xcor 3 set ycor -2 set shape "bus" set heading 90 ] ;; 创建下方公交车 create-busses 1[ set color red set size 7 set xcor 3 set ycor -11 set shape "bus" set heading 90 ] ;; 创建上方乘客(速度最快) create-turtles Nr_Passengers_1[ ;; 滑块控件 setxy one-of (range 0 6) one-of (range 4 6) set color grey set size 1 set shape "person" set heading towards patch 3 7 set speed random-float 0.8 + 0.4 ;; 速度区间0.4~1.2 ] ;; 创建中间乘客(速度中等) create-turtles Nr_Passengers_2[ ;; 滑块控件 setxy one-of (range 0 6) one-of (range -6 -4) set color blue set size 1 set shape "person" set heading towards patch 3 -3 set speed random-float 0.6 + 0.3 ;; 速度区间0.3~0.9 ] ;; 创建下方乘客(速度最慢) create-turtles Nr_Passengers_3[ ;; 滑块控件 setxy one-of (range 0 6) one-of (range -15 -13) set color green set size 1 set shape "person" set heading towards patch 3 -12 set speed random-float 0.4 + 0.1 ;; 速度区间0.1~0.5 ] ask patch 8 10 [set plabel "platform 1"] ask patch 4 0 [set plabel "platform 2"] ask patch 4 -9 [set plabel "platform 3"] reset-ticks end to check-in-1 ;; 处理乘客登车逻辑 ask turtles with [shape = "person"][ ;; 到达灰色登车区域则视为登车,移除该乘客 if pcolor = gray [ die ] ;; 未登车则按自身速度向公交车移动 fd speed ] tick end to drive ;; 下方站点:无未登车乘客时,公交车驶离 let SC_3 count turtles with [pycor >= -15 and pycor <= -10 and shape = "person"] if SC_3 <= 0 [ ask busses with [ycor = -11][ forward 33 ] ask patch 10 -10 [set plabel "20 seconds"] ] ;; 中间站点:无未登车乘客时,公交车驶离 let SC_2 count turtles with [pycor >= -6 and pycor <= -1 and shape = "person"] if SC_2 <= 0[ ask busses with [ycor = -2][ forward 33 ] ] ;; 上方站点:无未登车乘客时,公交车驶离 let SC_1 count turtles with [pycor >= 4 and pycor <= 8 and shape = "person"] if SC_1 <= 0 [ ask busses with [ycor = 8][ forward 33 ] ] end to go check-in-1 drive tick end
关键修改说明
- 速度差异化:通过调整
set speed的数值范围,给每个站点乘客分配了不同的速度区间,直接实现速度差异需求。 - 登车逻辑优化:简化登车判断逻辑,乘客站在灰色patch上即视为完成登车;同时限制只有未登车的乘客会执行移动操作。
- 公交车判断优化:使用
bussesbreed筛选公交车,避免与乘客混淆;修正了shape拼写错误,确保乘客数量统计准确。
内容的提问来源于stack exchange,提问作者Dwayne Johnson
相关产品推荐
相关产品推荐

