NetLogo报错:people-movement仅适用于海龟,求代码修复
问题解决:NetLogo "people-movement in a observer context" 错误
错误核心原因
people-movement是海龟(turtle)专属过程(内部使用了self、age等海龟变量),但你在观察者(observer)上下文直接调用了它,比如在go过程里写了people-movement而不是ask people [people-movement]。
下面是完整的代码修正方案,同时解决你代码里的其他逻辑问题:
1. 修正核心调用逻辑
在你的go过程中,必须让观察者命令每个people执行移动过程,而不是直接调用:
to go report-current-phase ; 关键:让每个people执行自己的移动逻辑 ask people [ people-movement ] tick end
2. 修复setup-people的嵌套逻辑与年龄分组错误
原代码嵌套多层ask people会导致重复执行,且年龄范围存在重叠(比如36-75和37-50的冲突),修正后:
to setup-people ask patches [ sprout-people population [ ; 设置性别 if random 100 < percent-female [ set is-female? true ] ; 生成随机数用于年龄分组 let rand-num random 100 ; 修正年龄范围,避免重叠 if rand-num <= 31 [ set age 10 ] ifelse rand-num <= 37 [ set age 25 ] [ ifelse rand-num <= 50 [ set age 35 ] [ ifelse rand-num <= 75 [ set age 45 ] [ ifelse rand-num <= 81 [ set age 55 ] [ ifelse rand-num <= 87 [ set age 65 ] [ set age 75 ] ] ] ] ] ] ] end
3. 修复setup-daily-activies的逻辑与语法错误
原代码存在运算符优先级问题,修正后:
to setup-daily-activies if age >= 18 [ let cbd-pickup roads with [is-school? and not is-intersection?] let commercial-pickup roads with [is-commercial? and not is-intersection?] let residential-pickup roads with [ (is-high-residential? or is-low-residential? or is-mid-residential?) and not is-intersection? ] let cbd-dropoff roads with [is-school? and not is-intersection? and not is-traffic-light?] let commercial-dropoff roads with [ is-commercial? and not is-intersection? and not is-traffic-light? ] let residential-dropoff roads with [ (is-high-residential? or is-low-residential? or is-mid-residential?) and not is-intersection? and not is-traffic-light? ] ; 注意:确保你定义了对应命名的概率变量(比如leave-home-morning) generate-activity (runresult word "leave-home-" current-phase) residential-pickup commercial-dropoff generate-activity (runresult word "leave-work-" current-phase) commercial-dropoff residential-dropoff ] end
4. 修复generate-activity的目标选择逻辑
原代码ask person origin语法错误,修正为选择在出发地patch上的people:
to generate-activity [some-prob origin destination] if random-float 1 < some-prob [ ; 让在出发地patch上的people设置目的地 ask people-on origin [ set destination-patch-passenger one-of destination set d-xcor [pxcor] of destination-patch-passenger set d-ycor [pycor] of destination-patch-passenger ] ] end
5. 优化people-movement过程
添加目的地存在判断,避免空值错误:
to people-movement setup-daily-activies ; 只有当目的地坐标有效时才移动 if not is-number? d-xcor [ stop ] if not is-number? d-ycor [ stop ] setxy d-xcor d-ycor ; 移动后重置目的地,防止重复移动 set d-xcor nobody set d-ycor nobody end
额外必要的变量定义
确保你在代码开头定义了以下变量:
; 全局变量 globals [current-phase all-phases phase-counter daycounter] ; 海龟 breed 与变量 breed [people person] people-own [ is-female? age destination-patch-passenger d-xcor d-ycor ] ; 道路 patch breed 与变量 breed [roads road] roads-own [ is-school? is-intersection? is-commercial? is-high-residential? is-low-residential? is-mid-residential? is-traffic-light? ] ; 滑块变量(在界面定义) sliders [ population ; 每个patch生成的人数 percent-female ; 女性比例(0-100) ]
内容的提问来源于stack exchange,提问作者Phoebe Bloomfield
相关产品推荐
相关产品推荐

