Netlogo编码-IF代码



这是我在Netlogo中面临的问题。我想让一只乌龟检查自己的一个变量和另一只乌龟(不同品种(的另一个变量。基于这两个值,我想为乌龟设定一个奖励。比方说,我有"学生"one_answers"教师"两个品种。学生可能会"作弊"(二进制(,老师可能会抓住(二进制(——因此,根据他们是否作弊并被抓住,将获得适当的奖励。我正试图通过以下代码来整合这一点

if comply? = 1
[ 
ask students [ set gain reward1 ] 
] 
if comply? = 0 and caught? < random-float 1
[
ask students [set gain reward2 ] 
] 
if comply? = 0 and caught? > random-float 1 
[ 
ask suppliers [set gain reward3 ] 
] 
end 

收益是学生自己的变量而被抓住了吗?是教师自己的变量,表示教师抓住学生的机会。

当我运行模型时,在运行Student1运行CAUGHT?时出现错误"STUDENTS bread not own variable CAUGHT">

我想知道是否有人可以分享一些关于这方面的见解?谢谢Deb

学生品种不拥有CAGHT?:洞察力

当所有权错误弹出时,通常情况下,问题是海龟,或者在本例中是学生,引用了一个不属于他们品种的变量。下面是一个示例,说明如何为代码初始化模型,以及一个通过和失败的示例。

breed [ students student]
breed [ teachers teacher]
students-own [ gain comply?]
teachers-own [ caught? ]
... ; initialize
to go
ask students [ set gain 3 ]    ; this passes
ask students [ set caught? 3 ] ; this fails
end

语境的重要性

你的问题很可能与在学生程序中添加冲突变量有关。(以下示例(

to listen-in-class ; student procedure
if comply? = 0 [ set gain 7 ]
; the comply? variable assumes a student is calling the procedure
if gain = 3 [ set gain 4 ] 
; The gain variable assumes a student is calling the procedure
if caught? = 0 [ set gain 2 ] 
; The caught? variable assumes a teacher is calling the procedure
end

由于过程可以调用其他过程,因此每个过程都从变量/过程中假定其环境(上下文(。

to starting-class ; should be a student procedure
ask student [ listen-in-class ]
; "ask student" assumes listen-in-class only takes global or student only variables
end

最有可能的是,为一个过程添加了一组错误的变量。询问往往会根据品种限制变量的范围。

最新更新