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:
set grass-energy grass- energy = 0
This line has two critical issues:
- An accidental space between
grass-andenergycreates 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 = 0returnstrueorfalse, not the number 0.
Fix this line to properly set grass-energy to 0 for brown patches:
set grass-energy 0
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:
globals [ regrow-energy green-patches grass-alive number birth-energy move-energy energy-from-grass reproduce-energy time-limit show-energy? ]
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-grassprocedures defined—remove one to avoid confusion. - In
setup-turtles,set energy energy + birth-energywon’t work for new turtles (they start withenergyas undefined). Replace it withset energy birth-energy.
Corrected Full Code
globals [ regrow-energy green-patches grass-alive number birth-energy move-energy energy-from-grass reproduce-energy time-limit show-energy? ] turtles-own [energy] patches-own [grass-energy] to setup clear-all setup-patches setup-turtles reset-ticks end to scatter clear-turtles setup-turtles end to go if ticks >= time-limit [ stop ] move-turtles check-patches eat-grass grow-grass reproduce check-death tick end to move-turtles ask turtles [ ifelse move-left? [ left random 45 forward 1 set energy energy - move-energy ] [ right random 360 forward 1 set energy energy - move-energy ] ] end to eat-grass ask turtles [ if pcolor = green [ set energy energy + energy-from-grass set grass-energy grass-energy - energy-from-grass ] ifelse show-energy? [ set label energy ] [set label "" ] ] end to reproduce ask turtles [ if energy >= reproduce-energy [ set energy energy - birth-energy hatch 1 [ set energy birth-energy ] ] ] end to check-death ask turtles [ if energy <= 0 [ die ] ] end to check-patches ask patches with [count turtles-here >= 2] [ ask one-of turtles-here [ ask other turtles-here[set energy energy - (energy / 2)] ] ] end to grow-grass ask patches [ set grass-energy grass-energy + regrow-energy ] end to setup-patches ask n-of green-patches patches [ set pcolor green set grass-energy grass-energy + grass-alive ] ask patches [ if pcolor = black [ set pcolor brown set grass-energy 0 ] ] end to setup-turtles create-turtles number [ setxy random-xcor random-ycor ] ask turtles [ set energy birth-energy set shape "cow" set size 1.5 ] end
内容的提问来源于stack exchange,提问作者Mason

