Reputation: 418
I want to convert turtles-own's variable (nb-of-turtles, nb-dead) to global's variable (number-of-turtles, number-dead) in order to compile with BehaviorSpace. nb-of-turtles is a decrement-counter (In the beginning of the model, I use this as increment counter. The counter counts the number of turtle in the road. This counter does not count as cumulative value. Therefore I puts "set nb-of-turtles nb-of-turtles - 1"). nb-dead are increment- counter (This counter needs to count as cumulative value of number of dead turtles totally). However, these counters do not count well. When the turtle at the end of the road dies, increment nb-dead by one. Similarly, when the turtle at the end of the road dies, it decrements nb-of-turtles by one. In my model, when the turtle dies at the end of the road, I use the flag (onend). The following is sample code. Please give me advice.(Some codes have already been consulted and discussed, then solved in the following link.the link Thank you very much.
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
Upvotes: 0
Views: 430
Reputation: 10336
You may be mixing up the use of global
and turtles-own
variables. It doesn't really make sense in this context to use a turtles-own
variable as a counter, since every new turtle created will have its "nb-dead" or "nb-of-turtles" variable start at 0. It's better in this case to have the turtles access the global counter directly when they die, for example. Additionally, you can just use count turtles
to get the current number of turtles- no need to have the turtles manually add to that value. For an example, please see below:
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
Upvotes: 4