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

NetLogo模型grow-grass函数报错:+运算期望数值却获布尔值true

Let's dig into this error you're seeing—it's a classic typo that's turning a numeric variable into a boolean, which breaks the grow-grass procedure. Here's what's going wrong and how to fix it:

1. The Root Cause: A Typo in setup-patches

The error happens because one of your variables (grass-energy) is being assigned a boolean true/false value instead of a number. Look at this line in your setup-patches procedure:

This line has two critical issues:

  • An accidental space between grass- and energy creates invalid variable syntax, and NetLogo tries to interpret this as a boolean comparison (grass- energy = 0) instead of a value assignment.
  • You’re using a comparison operator (=) instead of setting a value directly. Even if the variable name was correct, grass-energy = 0 returns true or false, not the number 0.

Fix this line to properly set grass-energy to 0 for brown patches:

2. Missing Global Variable Declarations

Your code references several global variables (like regrow-energy, green-patches, birth-energy) but doesn’t declare them. NetLogo may treat these as undefined, which could cause additional errors. Add a globals block at the top of your code to define all shared variables:

Make sure you set values for these variables (e.g., in your setup procedure) so they aren’t undefined when the model runs.

3. Minor Cleanup Fixes

  • You have two identical grow-grass procedures defined—remove one to avoid confusion.
  • In setup-turtles, set energy energy + birth-energy won’t work for new turtles (they start with energy as undefined). Replace it with set energy birth-energy.

Corrected Full Code

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 09:26:50