我想将海龟自己的变量(nb-of-turtles,nb-dead)转换为全局变量(海龟数量,数字死亡),以便使用BehaviorSpace进行编译。 nb-of-turtles是一个递减计数器(在模型的开头,我用它作为增量计数器。计数器计算道路中乌龟的数量。这个计数器不计入累积值。因此我把“设置” nb-of-turtles nb-of-turtles - 1“)。 nb-dead是递增计数器(此计数器需要计算为死亡龟数的累积值)。但是,这些计数器并不算数。当道路尽头的乌龟死亡时,增加nb-dead一个。同样地,当道路尽头的乌龟死亡时,它会将海龟的数量减少一个。在我的模型中,当乌龟在路的尽头死亡时,我使用旗帜(onend)。以下是示例代码。请给我建议。(有些代码已经过咨询和讨论,然后通过以下链接解决。the link非常感谢。
globals [ number-of-turtles number-dead A ]
turtles-own [ onend? nb-of-turtles nb-dead ]
let numle count [turtles-at 0 0] of patch min-pxcor 0
if numle = 0 [
create-car
set number-of-turtles number-of-turtles + 1
]
to create-car
crt 1 [
set onend? FALSE
]
end
ask turtles with [onend?]
[ if gamma-A = 0 [
die
set nb-dead nb-dead + 1 ;;This does not count.
set nb-of-turtles nb-of-turtles - 1 ;;This does not count well.
]
]
ask (turtles-on patch max-pxcor 0) with [not onend?]
[
set number-of-turtles nb-of-turtles
set number-dead nb-dead
set gamma-A precision (random-gamma (α) (β))0
set speed 0
set color red
set onend? TRUE
]
tick
end
答案 0 :(得分:3)
您可能会混淆使用global
和turtles-own
个变量。在这种情况下使用turtles-own
变量作为计数器并不是真的有意义,因为每个创建的新龟都会有它的" nb-dead"或者" nb-of-turtles"变量从0开始。例如,在这种情况下,让龟在死亡时直接访问全局计数器会更好。此外,您只需使用count turtles
即可获得当前的海龟数量 - 无需手动将海龟添加到该值。有关示例,请参阅以下内容:
globals [ number-of-turtles number-dead gamma-A start-patch end-patch]
turtles-own [ onend? speed ]
to setup
ca
reset-ticks
set start-patch patch min-pxcor 0
set end-patch patch max-pxcor 0
set gamma-A random-gamma 10 1
end
to create-car
ask start-patch [
sprout 1 [
set heading 90
set shape "car"
set color green
set onend? false
set speed 1
]
]
end
to go
;; If start patch has no turtles, it has a 10% chance
; of spawning a new turtle.
if [ count turtles-here ] of start-patch = 0 and random 100 < 10 [
create-car
set number-of-turtles number-of-turtles + 1
]
;; Ask any turtles not on the end patch to move fd
; at their speed, if they get to the end-patch
; set their speed to 0
ask turtles with [ not onend? ] [
fd speed
if patch-here = end-patch [
set speed 0
set color red
set onend? true
]
]
;; Decrease the GLOBAL variable of gamma-A by 0.1
set gamma-A gamma-A - 0.1
;; Ask turtles that are at the end,
; if gamma-A is less or equal to 0,
; increase the number-dead variable by one
; and then die
ask turtles with [onend?]
[
if gamma-A <= 0 [
set number-dead number-dead + 1
die
]
]
;; Use count to set the number-of-turtles
set number-of-turtles count turtles
;; If gamma-A has dropped to 0 or below,
; reset it to its new higher value
if gamma-A <= 0 [
set gamma-A random-gamma 10 1
]
tick
end