Netlogo 我想将 turtles-own 变量转换为全局变量,并且 运行 它们作为 turtles-own 变量的 "counter" 而没有任何错误
Netlogo I want to convert turtles-own variable to global's variable, and run them as "counter" of turtles-own variable without any error
我想将 turtles-own 的变量 (nb-of-turtles, nb-dead) 转换为全局变量 (number-of-turtles, number-dead) 以便使用 BehaviorSpace 进行编译。 nb-of-turtles 是一个递减计数器(在模型的开始,我用它作为递增计数器。计数器计算路上乌龟的数量。这个计数器不算作累积值。因此我把 "set nb-of-turtles nb-of-turtles - 1"). nb-dead 是increment-counter(这个计数器需要统计死海龟总数的累计值)。但是,这些计数器计数不佳。当路尽头的乌龟死亡时,将 nb-dead 加一。同样,当路尽头的海龟死亡时,它会将 nb-of-turtles 减一。在我的模型中,当乌龟死在路的尽头时,我使用标志(onend)。以下是示例代码。请指教。(部分代码已经参考讨论,然后在下面解决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
您可能混淆了 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
我想将 turtles-own 的变量 (nb-of-turtles, nb-dead) 转换为全局变量 (number-of-turtles, number-dead) 以便使用 BehaviorSpace 进行编译。 nb-of-turtles 是一个递减计数器(在模型的开始,我用它作为递增计数器。计数器计算路上乌龟的数量。这个计数器不算作累积值。因此我把 "set nb-of-turtles nb-of-turtles - 1"). nb-dead 是increment-counter(这个计数器需要统计死海龟总数的累计值)。但是,这些计数器计数不佳。当路尽头的乌龟死亡时,将 nb-dead 加一。同样,当路尽头的海龟死亡时,它会将 nb-of-turtles 减一。在我的模型中,当乌龟死在路的尽头时,我使用标志(onend)。以下是示例代码。请指教。(部分代码已经参考讨论,然后在下面解决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
您可能混淆了 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