You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NetLogo报错求助:DISTANCE函数预期输入为Agent却获数值0

Fixing "DISTANCE expected input to be an agent but got the number 0 instead" in NetLogo Tick Movement Model

Hey there! Let's break down why you're hitting that error and how to fix it quickly.

The Root Cause

The error pops up because your raccoons-move and mice-move procedures use distance home-patch, but you never initialized the home-patch variable for your new agents. In NetLogo, uninitialized variables default to the number 0, and the distance command expects an agent (like a patch or turtle) as input—not a number. That's exactly why you're seeing this message.

Step-by-Step Fix

1. Define the home-patch variable

First, you need to declare that your host species have a home-patch attribute. Add this at the very top of your code (before any to procedures):

2. Set home-patch when sprouting new agents

When you create new mice, deer, or raccoons, you need to assign their home-patch to the patch they're sprouted from. Use myself inside the sprout block to reference the patch that's creating the agent (this way, even if you move the agent immediately, their home patch stays set to their birth location).

Here's how to update your sprouting code:

Mice Sprouting:

Deer Sprouting:

Raccoons Sprouting:

Quick Note on Order

Notice we set home-patch before setxy random-xcor random-ycor. If you reversed these lines, patch-here would refer to the new random location instead of the birth patch. Using myself avoids this confusion entirely, since it always points to the patch that initiated the sprout command.

Verify the Fix

After making these changes, your raccoons-move and mice-move procedures will have a valid patch to calculate distance from, so the distance command will work as expected. You won't get that "got the number 0 instead" error anymore!

内容的提问来源于stack exchange,提问作者JRF89

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:30:24