联盟形成模型运行报错求助:THREATS breed未定义ACTIVE?变量
Let's break down what's causing this error and fix it step by step, along with some other tweaks to make your model run smoothly:
Root Cause of the Error
The error pops up because your update-turtles procedure uses ask turtles—which includes both agents and threats—but the active? variable is only defined in agents-own. When NetLogo tries to access active? on a threat turtle, it throws an error because threats don't have that variable assigned to them.
Step-by-Step Fixes
1. Restrict Agent-Specific Logic to agents Only
Change all instances where you're asking turtles to instead target agents when dealing with variables like active?, allied-states, etc.:
- In
go: Replaceif all? turtles [ active? ] [ stop ]withif all? agents [ active? ] [ stop ] - In
move-unhappy-turtles: Replaceask turtles with [ not active? ] [ find-new-spot ]withask agents with [ not active? ] [ find-new-spot ] - In
update-turtles: Replaceask turtles [ ... ]withask agents [ ... ]
2. Fix Global Variable Calculations in update-globals
Globals like percent-similar and percent-unhappy should only calculate values from agents, not all turtles. We also add a check to avoid division by zero:
let similar-neighbors sum [ allied-states ] of agents let total-neighbors sum [ total-nearby ] of agents set percent-similar (similar-neighbors / max (list 1 total-neighbors)) * 100 set percent-unhappy (count agents with [ not active? ]) / max (list 1 count agents) * 100
3. Correct allied-states and perceived-threat to Be Proportions
Your variable comments note these should be 0-1 proportions, but your original code uses raw counts. Update the calculation to get ratios (and exclude threats from neighbor counts):
let same-color-neighbors count (agents-on neighbors) with [ color = [ color ] of myself ] let diff-color-neighbors count (agents-on neighbors) with [ color != [ color ] of myself ] set total-nearby same-color-neighbors + diff-color-neighbors set allied-states same-color-neighbors / max (list 1 total-nearby) set perceived-threat diff-color-neighbors / max (list 1 total-nearby)
4. Improve the find-new-spot Procedure
The original recursive logic can cause stack overflow if empty spots are hard to find. Replace it with a more efficient approach:
to find-new-spot let empty-patches patches with [ not any? turtles-here ] if any? empty-patches [ move-to one-of empty-patches ] end
5. Add Missing Global Variables
Your setup procedure references variables like vision and initial-threats-density that weren't declared. I've added these to the globals list—you'll need to create corresponding sliders in your NetLogo interface for them.
Full Corrected Code
breed [ agents an-agent] breed [ threats threat ] globals [ k ; 攻击概率决定因子 threshold ; 状态分担阈值:D - BS 需比 A 大多少才满足条件 percent-similar ; 平均相似度:海龟邻居中与自身颜色相同的比例,影响结盟倾向 percent-unhappy ; 不满比例:处于不满或感知到威胁的海龟占比 visualization vision ; Add missing vision global initial-threats-density ; Add missing setup globals initial-agent-density movement? alliance-protection conflict-term ] agents-own [ allied-states ; R,智能体生命周期内固定,取值0-1(含),表示海龟邻居中颜色相同的比例是否达到期望相似度 perceived-threat ; T,取值0-1(含),表示颜色不同的邻居海龟数量占比 active? ; 若为true,智能体处于活跃结盟状态;若为false,智能体搭便车 conflict ; 剩余冲突回合数(为0时无冲突) total-nearby ; 邻近海龟总数(allied-states与perceived-threat之和) ] patches-own [ neighborhood ; 视野半径内的周边斑块 ] to setup clear-all ; 设置全局变量(adjust these values or add sliders in the interface) set k 2.3 set threshold 0.1 set vision 5 set initial-threats-density 10 set initial-agent-density 40 set movement? true set alliance-protection 0.5 set conflict-term 5 set visualization "square-x" ask patches [ ; 将背景设为深灰色 set pcolor gray - 1 ; 缓存斑块周边区域 set neighborhood patches in-radius vision ] if initial-threats-density + initial-agent-density > 206 [ user-message (word "The sum of INITIAL-THREATS-DENSITY and INITIAL-AGENT-DENSITY " "should not be greater than 206.") stop ] ; 创建威胁智能体 create-threats round (initial-threats-density * .01 * count patches) [ move-to one-of patches with [ not any? turtles-here ] display-threats ] ; 创建智能体 create-agents round (initial-agent-density * .01 * count patches) [ move-to one-of patches with [ not any? turtles-here ] set heading 0 set allied-states random-float 1.0 set perceived-threat random-float 1.0 set active? false set conflict 0 display-agent ] ; 重置时钟并绘制系统初始状态 reset-ticks end to go if all? agents [ active? ] [ stop ] move-unhappy-turtles update-turtles update-globals end ; 不满的海龟尝试迁移到新位置 to move-unhappy-turtles ask agents with [ not active? ] [ find-new-spot ] end ; 移动至未被占据的斑块 to find-new-spot let empty-patches patches with [ not any? turtles-here ] if any? empty-patches [ move-to one-of empty-patches ] end to update-turtles ask agents [ ; 计算同色和异色邻居的比例 let same-color-neighbors count (agents-on neighbors) with [ color = [ color ] of myself ] let diff-color-neighbors count (agents-on neighbors) with [ color != [ color ] of myself ] set total-nearby same-color-neighbors + diff-color-neighbors set allied-states same-color-neighbors / max (list 1 total-nearby) set perceived-threat diff-color-neighbors / max (list 1 total-nearby) ; 更新活跃状态(调整逻辑匹配比例尺度) set active? allied-states >= (percent-similar / 100) ; 添加可视化设置 if visualization = "old" [ set shape "default" set size 1.3 ] if visualization = "square-x" [ ifelse active? [ set shape "square" ] [ set shape "X" ] ] ] end to update-globals let similar-neighbors sum [ allied-states ] of agents let total-neighbors sum [ total-nearby ] of agents set percent-similar (similar-neighbors / max (list 1 total-neighbors)) * 100 set percent-unhappy (count agents with [ not active? ]) / max (list 1 count agents) * 100 ; 处于冲突中的智能体每回合减少冲突剩余回合数 ask agents [ if conflict > 0 [ set conflict conflict - 1 ] ] ; 更新智能体显示 ask agents [ display-agent ] ask threats [ display-threats ] tick end ; 智能体与威胁智能体行为 ; 移动至空斑块 to move ; 海龟过程 if movement? or breed = threats [ ; 移动至视野内的斑块;候选斑块为空或仅包含处于冲突中的智能体 let targets neighborhood with [ not any? threats-here and all? agents-here [ conflict > 0 ] ] if any? targets [ move-to one-of targets ] ] end ; 智能体行为 to determine-behavior set active? (burden-sharing - allied-states * estimated-conflict-probability > threshold) end to-report burden-sharing report perceived-threat * (1 - alliance-protection) end to-report estimated-conflict-probability let t count (threats-on neighborhood) let a 1 + count (agents-on neighborhood) with [ active? ] ; 有关以下公式的讨论请查看Info标签页 report 1 - exp (- k * floor (t / a)) end to alliance if any? threats [attack] set active? true end ; 威胁智能体行为 to attack if any? (agents-on neighborhood) with [ active? ] [ ; 逮捕目标 let suspect one-of (agents-on neighborhood) with [ active? ] move-to suspect ; 移动至被逮捕智能体的斑块 ask suspect [ set active? false set conflict random conflict-term set color pink ] ] end ; 智能体与威胁智能体可视化 to display-agent ; 智能体过程 if conflict > 0 [ set color pink ] else [ set color cyan ] set shape "triangle" end to display-active? set color pink set shape "triangle" end to display-threats set color red set shape "circle 2" end
Quick Additional Notes
- I adjusted the
display-agentprocedure to check for active conflicts and set the pink color automatically, which aligns with yourattacklogic. - The
active?calculation was updated to match the 0-1 proportional scale ofallied-states.
内容的提问来源于stack exchange,提问作者Cfitzg

