如何在NetLogo中设置初始位置位于pxcor = -15左侧的随机海龟?
NetLogo海龟初始位置设置问题修复
你当前代码里的set xcor random-xcor > -15是错误的——这行代码会生成一个布尔值(true/false)并赋值给xcor,导致海龟位置异常,无法实现让海龟位于pxcor = -15左侧的需求。
下面是两种可行的修复方案:
方案一:直接移动到目标区域的空补丁(推荐)
这种方式结合了你代码里的move-to逻辑,既满足位置要求,又避免海龟重叠:
to setup-turtles create-turtles num-turtles [ set shape "circle" set size 0.5 set color red set metabolism 1 + random-float 6 ;; 代谢为0的智能体是永生的 set vision random-float 4 set wealth random-float 20 ;; 移动到pxcor小于-15的空补丁上 move-to one-of patches with [pxcor < -15 and not any? turtles-here] ] end
- 移除了错误的
set xcor和set ycor语句,因为move-to会直接设置海龟的坐标 - 通过
pxcor < -15限定了目标区域,确保海龟初始位置在要求的左侧区域
方案二:单独生成符合范围的随机坐标
如果你不想依赖补丁,只想直接设置海龟坐标,可以修改xcor的赋值逻辑:
to setup-turtles create-turtles num-turtles [ set shape "circle" set size 0.5 set xcor min-pxcor + random-float (-15 - min-pxcor) ;; 生成min-pxcor到-15之间的随机x值 set ycor random-ycor set color red set metabolism 1 + random-float 6 ;; 代谢为0的智能体是永生的 set vision random-float 4 set wealth random-float 20 move-to one-of patches with [not any? turtles-here] ] end
min-pxcor是你NetLogo世界的最左侧x坐标,-15 - min-pxcor计算出目标区域的宽度random-float生成0到该宽度之间的随机数,加上min-pxcor就得到了符合要求的x坐标
内容的提问来源于stack exchange,提问作者Jessie
相关产品推荐
相关产品推荐

