Netlogo I want to convert turtles-own variable to global's variable, and run them as "counter" of turtles-own variable without any error - counter

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

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

Related

General questions regarding Netlogo

Im very new to Netlogo and trying to learn the basic. Therefore, I'm trying to extend an example code Netlogo provided. Im trying to make the pollution rate dependent upon the number of people from the Urban Site Pollution example.
Is there also a way to introduce reinforced learning (Q-learning) to improve the simulation?
Sincerly,
Victor
Do I need to create a new function that updates pollution when population increased?
Below is the example code:
breed [ people person ]
breed [ trees tree ]
turtles-own [ health ]
patches-own [
pollution
is-power-plant?
]
to setup
clear-all
set-default-shape people "person"
set-default-shape trees "tree"
ask patches [
set pollution 0
set is-power-plant? false
]
create-power-plants
ask patches [ pollute ]
create-people initial-population [
set color black
setxy random-pxcor random-pycor
set health 5
]
reset-ticks
end
to go
if not any? people [ stop ]
ask people [
wander
reproduce
maybe-plant
eat-pollution
maybe-die
]
diffuse pollution 0.8
ask patches [ pollute ]
ask trees [
cleanup
maybe-die
]
tick
end
to create-power-plants
ask n-of power-plants patches [
set is-power-plant? true
]
end
to pollute ;; patch procedure
if is-power-plant? [
set pcolor red
set pollution polluting-rate
]
set pcolor scale-color red (pollution - .1) 5 0
end
to cleanup ;; tree procedure
set pcolor green + 3
set pollution max (list 0 (pollution - 1))
ask neighbors [
set pollution max (list 0 (pollution - .5))
]
set health health - 0.1
end
to wander ;; person procedure
rt random-float 50
lt random-float 50
fd 1
set health health - 0.1
end
to reproduce ;; person procedure
if health > 4 and random-float 1 < birth-rate [
hatch-people 1 [
set health 5
]
]
end
to maybe-plant ;; person procedure
if random-float 1 < planting-rate [
hatch-trees 1 [
set health 5
set color green
]
]
end
to eat-pollution ;; person procedure
if pollution > 0.5 [
set health (health - (pollution / 10))
]
end
to maybe-die ;; die if you run out of health
if health <= 0 [ die ]
end
; Copyright 2007 Uri Wilensky.
; See Info tab for full copyright and license.

How to set a probability of death for each tick

I want to set my code so that each forager (a breed of turtle) will have a 10% chance of dying for each tick it is vulnerable. I am building off of a code called Ants in the Netlogo models library.
When I use use [if random 100 > 98 [ die ]] (or anything below 98) nearly all of my turtles will die at the beginning and survive more after a hundred or so ticks have passed. However if I use [if random 100 > 98 [ die ]] no turtles will die. It's very weird.
to go ;; forever button
ask foragers
[ if who >= ticks [ stop ]
ifelse color = red
[ look-for-food ]
[ return-to-nest ]
check-death
wiggle
fd 1 ]
to check-death
ask foragers [
if vulnerable?
[if random 100 > 99
[ die ]]]
end
I expected [if random 100 > 98 [ die ]] to make it so that a vulnerable turtle would only have a 2% chance of dying per tick rather than an immediate wipeout.
The issue here is due to nested ask statements. You have an ask foragers [ ... statement that contains check-death, which contains another ask foragers statement. So, every single forager will be asking all foragers (including itself) to check-death. So, if you have 10 foragers, each forager will be running check-death 10 times per tick.
You should just be able to remove the ask foragers block from within your check-death procedure to solve your issue- have a look at this toy model example:
turtles-own [ vulnerable? ]
to setup
ca
crt 100 [ set vulnerable? one-of [ true false ] ]
reset-ticks
end
to go
ask turtles [
rt random 61 - 30
fd 1
check-death
]
if not any? turtles with [ vulnerable? ] [
print count turtles
stop
]
tick
end
to check-death
if vulnerable? and random-float 1 > 0.90 [
die
]
end
That will randomly assign vulnerable? to the turtles, then have vulnerable turtles die if a randomly generated float value is greater than 0.90 (for the 10% chance mentioned in your question). Once there are no more turtles left, the model will stop.

How to update a proportion used to select among turtles in a loop?

In my model the turtles have two sexes and there are two potential strategies "0" and "1". The females count the number of males in a set radius and choose among that pool based on their strategies.
The females have a limit to their pool of potential mates and they loop through this pool to select the males according to their strategy. This is all in the to-choose procedure.
One issue that a colleague picked up on is that the following line of code should be updated every time a female chooses another mate so that the proportion reflects the remaining potential mates and not the n-max which was set outside of the loop.
set prop_B ( count availa-males with [ strategy = 0 ] ) / n-max
To state the issue another way for clarity if the n-max is 5 and a female sets the prop_B using this value for the first mate then in the next iteration of the loop n-max should deprecate by 1 because there are only 4 remaining males.
So it should be something like: set prop_B ( count availa-males with [ strategy = 0 ] ) / (n-max - count mates-already-chosen)
Please see below for a working example of the model. Hope you can help.
turtles-own [sex availa-males mates mate-count max-mate-count strategy n-max prop_B proba_B]
breed [males male]
breed [females female]
to setup
clear-all
create-males 50
create-females 1
ask turtles [
setxy random-xcor random-ycor
ifelse random 2 = 1 [set strategy 1] [set strategy 0]
]
ask males [set color red]
ask females [set color blue]
reset-ticks
end
to go
ask males [
; fd 1
]
ask turtles [
set mates ( turtle-set )
]
ask females [choose]
tick
end
to choose
; set a cap on possible mates for females; 5, or the number
; available within the radius if less than 5
set availa-males males in-radius 5
set n-max count availa-males
set max-mate-count ifelse-value ( n-max < 5 ) [ n-max ] [ 5 ] ; 5 5
; Until a female has chosen up to her maximum number of mates:
while [ mate-count < max-mate-count ]
[; determine which available males are not already in her 'mates' agentset
set availa-males availa-males with [ not member? self [mates] of myself ]
; assess the proportion of the '0' strategy in remaining available males
set prop_B ( count availa-males with [ strategy = 0 ] ) / n-max
; example probability choice, just meant to choose '0 strategy' males
; with a frequency disproportionate to availability
set proba_B ifelse-value ( prop_B <= 0.1 ) [ 0.8 ] [ 0.2 ]
; use a random float to determine which strategy type is chosen
set mates ( turtle-set mates
ifelse-value ( random-float 1 < proba_B )
[ one-of availa-males with [ strategy = 0] ]
[ one-of availa-males with [ strategy = 1]] )
; count the current mates to break the while loop once
; the maximum number of mates is reached
set mate-count count mates
]
; have the female's males add her to their own mates agentset
ask mates [ set mates ( turtle-set mates myself ) ]
if n-max < count mates [ print "Fewer available males than mates" ]
end
Since you don't need them to be selected sequentially, then one option you should think about is the weighted equivalent of n-of from the rnd extension. The following code is a complete model that uses weighted selection, to show you how it could work. But it won't give quite the same results as your approach. Your mathematics basically forces one choice or the other based on the proportion of each. I thought that might work for you anyway, as the weighting is just a demonstration of disproportional.
extensions [rnd]
turtles-own
[ sex
mates
strategy
]
breed [males male]
breed [females female]
to setup
clear-all
create-males 50 [set color red set sex "M"]
create-females 1 [set color blue set sex "F"]
ask turtles
[ setxy random-xcor random-ycor
set strategy one-of [1 0]
set mates nobody
]
reset-ticks
end
to go
ask males
[ ; fd 1
]
ask females [choose]
tick
end
to choose
let availa-males males in-radius 5
let max-mate-count min (list 5 count availa-males)
if max-mate-count < 5 [ print "Fewer available males than mates" ]
let new-mates rnd:weighted-n-of max-mate-count availa-males [ strategy-weight strategy ]
set mates (turtle-set mates new-mates)
ask new-mates
[ set mates (turtle-set mates myself)
]
end
to-report strategy-weight [ #strategy ]
if #strategy = 1 [ report 0.2 ]
if #strategy = 0 [ report 0.8 ]
report 0
end
You will notice I also removed a bunch of turtle variables. You don't need to have a permanent variable, just create a temporary one with let. I also noticed you have sex as a turtle variable, but you are actually handling sex with different breeds, but I left it in just in case it has some other purpose.

How do I make turtle face in an organized manner like the "cro" command but in turtle context?

I am trying to make a turret that will fire 8 bullets in 8 directions. In the command I have, they all spawn with heading 0 how do I make them face the right direction. Each turtle should face in a multiple of 45. Just like it would with the cro command in observer context.
to fire-tacks
ask ttacks with [alive?] [
set attackSpeed attackSpeed + .5
if any? turtles with [is-bloon?] in-radius 5 and attackSpeed >= 12
[set attackSpeed 0
hatch-btacks 8 [set alive? false set is-turret? false
set size 1 set damage 1 set color black set is-dart? true set bullet-
speed 4
]]]
end
You could use range and foreach to do this (check the links for more detail on how they work). range can generate a sequence of the headings you would like, and foreach can iterate over that sequence to sprout new turtles with each heading. Have a look at this simplified example:
breed [ turrets turret ]
breed [ btacks btack ]
to setup
ca
create-turrets 1 [
setxy random-xcor random-ycor
]
reset-ticks
end
to go
ask turrets [
foreach ( range 0 360 45 ) [
new_heading ->
hatch-btacks 1 [
set heading new_heading
fd 1
]
]
]
end

How can I count dead turtles in Netlogo

I would like to know the numbers of all turtles died in my pseudo model. How can I do that? I would appreciate very much a simply and fast running solution for that problem, like count dead turtles.
I was thinking of a routine like this (but do not know how to implement it):
if turtle is dead % checking all turtles if dead or alive
set death_count death_count + 1 % set counter
tick % go one step ahead in model
This is my code sample (without any check so far):
breed [ humans human ]
humans-own [ age ]
to setup
ca
create-humans(random 100)
[
setxy random-xcor random-ycor
set age (random 51)
]
reset-ticks
end
to death
ask humans [
if floor (ticks mod 1) = 0 [
set age age + 1 ]
if age > 85 [ die ]
]
end
to go
death
tick
if ticks > 20 [ stop ]
end
I'm afraid you have to keep track of it yourself in a global variable. So, add
globals [ number-dead ]
to the top of your model. Then, change death like so:
to death
ask humans [
if floor (ticks mod 1) = 0 [
set age age + 1 ]
if age > 85 [
set number-dead number-dead + 1
die
]
]
end
Then number-dead will always be equal to the number of turtles that have died.
This is really simple:
to setup
let total-population count turtles
end
to go
let current-population count turtles
let dead-people total-population - current-population
ticks
end