In Netlogo, I use choosers to pick a strategy and its associated outcome given an event. In my case, building codes are the strategy, fire damage is the outcome, and a fire is the event). A random variable denotes whether a fire event occurs. However, my results are not valid (I sometimes get fire damage > 0 resulting from a p > 0.40). Thank you so much for any insight regarding the problem.
to fire?
ask patches [
if (strategy = "updated-building-codes")
[set p random-float 1.00
if p > 0.40 [ set fire-level 0 ]
if p > 0.01 and p <= 0.40 [ set fire-level 1 ]
if p > 0.002 and p <= 0.01 [ set fire-level 2 ]
if p <= 0.002 [ set fire-level 3 ]
]
if (strategy = "no-updated-building-codes")
[set p random-float 1.00
if p > 0.40 [ set fire-level 0 ]
if p > 0.01 and p <= 0.40 [ set fire-level 5 ]
if p > 0.002 and p <= 0.01 [ set fire-level 6 ]
if p <= 0.002 [ set fire-level 7 ]
]
]
end
Related
I have a turtle who can eat different amount of food for every tick, updating its stomach content every time. I would like to round the stomach content value so that it belongs to a range x.
this is the forage function that updates the stomach-content :
to forage
;; item=0.064g
set patch-n random-float 100
if patch-n <= 8 [set stomach-content (stomach-content + 0.00) ] ; does not find any item
if patch-n > 8 and patch-n <= 99 [set stomach-content (stomach-content + 0.192) ] ; finds 3 items
if patch-n > 99 [set stomach-content 0.4 ]; full stomach
ifelse stomach-content >= 0.132
[set fat-reserves (fat-reserves + 0.132 ) set stomach-content (stomach-content - 0.132)]
[set fat-reserves (fat-reserves + (stomach-content * 1)) set stomach-content 0] ;;
set fat-reserves (fat-reserves - (8 * bmr)) ; metabolic rate removes fat from fat reserves
end
the range I would like the stomach-content to belong to is
set x (range 0 0.4 0.04)
Is there a way to make my stomach-content value to be in this finite range of 11 values?
Something like to round stomach-content to the nearest value with mod=0.04 in the interval (0 , 0.4)
You could do something fancy with the mod reporter, but if you don't need it to be super fast, the following is easy enough, and also more flexible, as it would work with any list of values:
to-report nearest-in-list [ the-value the-list ]
report first sort-by [ [a b] ->
abs (a - the-value) < abs (b - the-value)
] the-list
end
You can then use it like this:
observer> show nearest-in-list 0.11 (range 0 0.4 0.04)
observer: 0.12
observer> show nearest-in-list 0.021 (range 0 0.4 0.04)
observer: 0.04
observer> show nearest-in-list 0.02 (range 0 0.4 0.04)
observer: 0
Note that, in case of ties (like with 0.02 in the example, which is halfway between 0 and 0.04) it gives you the lowest value.
I want x% of turtles, called pholders, to change their choice from a good 1 to a good 2.
The code is as follows:
ask pholders [ifelse random-float 1 <= probkauf
[ask (n-of (count pholders with [choice-num = 1] * 0.01) pholders with[choice-num = 1]) [set choice-num 2]]
[ifelse random-float 1 < 0.5[imitation set typeofchoice 1][beratung set typeofchoice 4]]
]
Initially 100% of the pholders chose good 1. The Problem is as follows: When i rise the number of pholders above something between 102 and 108 n-of doesn't calculate a 1%-fraction anymore, it calculates 10%. The higher the number of pholders the bigger the fraction: for 200 pholders the code calculates 60%. When i leave the number of pholders constant and below 108 but change the percentage from 0.01 to 0.02 it calculates something like 55% or 58%. Is the problem probably coming from ask n-of in an ask environment?
Thank you very much in advance.
Your problem is that you are running the probabilistic code multiple times. Your code has this structure:
ask pholders
[ ifelse random-float 1 <= probkauf
[ ask (n-of (count pholders with [choice-num = 1] * 0.01) pholders with [choice-num = 1])
[ set choice-num 2]
]
[ <do something else> ]
]
If you have 500 pholders, then there will be 500 times that a pholder selects a random number and, if the number is lower than your value probkauf, it instructs a number of pholders with choice-num of 1 to change it to choice-num 2. 500 potential occasions of 1% conversion is why you have so many being converted.
Based on the description in your comments, I think you want this:
globals [probkauf]
turtles-own [choice-num]
to setup
clear-all
set probkauf 0.5
create-turtles 1000
[ setxy random-xcor random-ycor
set color blue
set choice-num 1
]
reset-ticks
end
to go
update-choices
tick
end
to update-choices
ifelse random-float 1 < probkauf
[ ask turtles with [choice-num = 1]
[ if random-float 1 < 0.01
[ set choice-num 2
set color red
]
]
]
[ ; whatever happens with other part of probability
]
end
I am trying to run a NetLogo program developed in version 4.0.3 in NetLogo Version 5.1.0. The program itself runs fine in the older version of NetLogo but when I try to run it in Version 5.1.0, I receive the following error message:
" this code can't be run by a patch, only the observer
error while patch 19 32 running TICK
called by procedure GO
called by Button 'go'"
I tried making some of the obvious adjustments offered in the transition guide but was unable to debug the program. Any thoughts or suggestions would be greatly appreciated. Code for the whole program is below:
globals [z diver clrrand simp vviolet oorange bbrown yyellow bblue llime total sumdiver matrix-list yellowtrans p
redtrans browntrans violettrans bluetrans orangetrans limetrans]
to setup
;; (for this model to work with NetLogo's new plotting features,
;; __clear-all-and-reset-ticks should be replaced with clear-all at
;; the beginning of your setup procedure and reset-ticks at the end
;; of the procedure.)
__clear-all-and-reset-ticks
setup-patches
set sumdiver 0
set redtrans (list 0.0 0.9 0.1 0.0 0.0 0.0 0.0)
set yellowtrans (list 0.0 0.8 0.1 0.1 0.0 0.0 0.0)
set browntrans (list 0.0 0.0 0.8 0.1 0.1 0.0 0.0)
set violettrans (list 0.0 0.0 0.0 0.8 0.1 0.1 0.0)
set bluetrans (list 0.0 0.0 0.0 0.0 0.8 0.1 0.1)
set orangetrans (list 0.0 0.0 0.0 0.0 0.0 0.8 0.2)
set limetrans (list 0.0 0.0 0.0 0.0 0.0 0.0 1.0)
end
to setup-patches
ask patches [
set z random 100
if z <= 100 [ set pcolor yellow ]
]
end
to go
ignite
fire_spread
succession
do_plots
tick
end
to ignite
ask patches [if pcolor != black
[if random-float 1 < start
[set pcolor red]
]
]
end
to fire_spread
while [any? patches with [pcolor = red]] [ask patches [
if pcolor = red [set pcolor 14
ask neighbors4 [
if (random-float 1) < p_fire_spread and pcolor != 14 and pcolor != black
[ set pcolor red]
]
]
]
]
end
to succession
ask patches [
if pcolor = 14 [
set clrrand random-float 1
set p 0
(foreach redtrans [ 14 yellow brown violet blue orange lime] [
if clrrand > p and clrrand <= (p + ?1) [set pcolor ?2 stop]
set p (p + ?1)
])
stop
]
if pcolor = yellow [
set clrrand random-float 1
set p 0
(foreach yellowtrans [ 14 yellow brown violet blue orange lime] [
if clrrand > p and clrrand <= (p + ?1) [set pcolor ?2 stop]
set p (p + ?1)
])
stop
]
if pcolor = brown [
set clrrand random-float 1
set p 0
(foreach browntrans [ 14 yellow brown violet blue orange lime] [
if clrrand > p and clrrand <= (p + ?1) [set pcolor ?2 stop]
set p (p + ?1)
])
stop
]
if pcolor = violet [
set clrrand random-float 1
set p 0
(foreach violettrans [ 14 yellow brown violet blue orange lime] [
if clrrand > p and clrrand <= (p + ?1) [set pcolor ?2 stop]
set p (p + ?1)
])
stop
]
if pcolor = blue [
set clrrand random-float 1
set p 0
(foreach bluetrans [ 14 yellow brown violet blue orange lime] [
if clrrand > p and clrrand <= (p + ?1) [set pcolor ?2 stop]
set p (p + ?1)
])
stop
]
if pcolor = orange [
set clrrand random-float 1
set p 0
(foreach orangetrans [ 14 yellow brown violet blue orange lime] [
if clrrand > p and clrrand <= (p + ?1) [set pcolor ?2 stop]
set p (p + ?1)
])
stop
]
if pcolor = lime [
set clrrand random-float 1
set p 0
(foreach limetrans [ 14 yellow brown violet blue orange lime] [
if clrrand > p and clrrand <= (p + ?1) [set pcolor ?2 stop]
set p (p + ?1)
])
stop
]
]
end
;; yellow is 45, brown is 35, violet is 115, sky is 95, green is 55,lime is 65,
to do_plots
set oorange count patches with [pcolor = orange]
set bbrown count patches with [pcolor = brown]
set vviolet count patches with [pcolor = violet]
set llime count patches with [pcolor = lime]
set yyellow count patches with [pcolor = yellow]
set bblue count patches with [pcolor = blue]
set-current-plot "abundances"
set-current-plot-pen "orange"
plot oorange
set-current-plot-pen "brown"
plot bbrown
set-current-plot-pen "violet"
plot vviolet
set-current-plot-pen "lime"
plot llime
set-current-plot-pen "yellow"
plot yyellow
set-current-plot-pen "blue"
plot bblue
set total bbrown + oorange + vviolet + llime + yyellow + bblue
set diver (1 / ((bbrown / total) ^ 2 + (oorange / total) ^ 2 + (vviolet / total) ^ 2 + (llime / total) ^ 2 + (yyellow / total) ^ 2 + (bblue / total) ^ 2))
set-current-plot "Simpson's diversity index"
plot diver
if ticks > 50 and ticks < 201 [set sumdiver sumdiver + diver]
if ticks = 201 [
set sumdiver sumdiver / 150
output-type "mean = "
output-type precision sumdiver 2
]
end
Thanks!
This is a previously unreported bug in NetLogo. I'm glad you discovered it. I have minimized it and reported it here: https://github.com/NetLogo/NetLogo/issues/683
In order to work around it, you need to find a way to rewrite the code so it avoids using stop inside foreach.
Hi I need help in NetLogo variable settings.
I have turtles that own attributes with range of values, that is the minimum and the maximum.
turtles-own [weight history state-turtles run-duration ek tt cu sp]
to setup-turtles
create- NMAs 2 [
set ek 8 ; range 8 to 9
set tt 5 ;range 5 to 7
set cu 3 ; range 3 to 5
set sp 4 ; range 4 to 7
]
create- NBSs 2 [
set ek 3 ; range 3 to 5
set tt 4 ; range 4 to 7
set cu 3 ; range 3 to 4
set sp 3 ; range 3 to 6
]
to setup-patches
ask patches [ let projects random 4
;setup colours
]
End
to go
tick
ask turtles [
......
]
search-patch
if .....
]
End
to search-patch
if ( [ pcolor ] of patch-here = brown ) [
ifelse (;statement) [
update-turtles
] [
action-turtles
]
]
end
to update-turtles
if pcolor = yellow [
set ek ek + 0.1
set tt tt + 0.5
set cu cu + 0.1
set sp sp + 1 ]
if pcolor = green [
set ek ......
The numbers are kept increasing and I want to set up limits (range) as shown in setup turtles for each attribute Thank you
turtles-own [... ek-max tt-max cu-max sp-max ...]
to setup
...
create-NMAs 2 [
set ek 8
set ek-max 9
...
]
...
end
to update-turtles
...
if ek + 0.1 <= ek-max [ set ek ek + 0.1 ]
if tt + 0.5 <= tt-max [ set tt tt + 0.5 ]
if cu + 0.1 <= cu-max [ set cu cu + 0.1 ]
if sp + 1 <= sp-max [ set sp sp + 1 ]
...
end
As a side note, tick should come at the end of your go procedure, not at the beginning (reference).
I was wondering if by using same random seed, the result of following functions differs significantly in randomness! and which one is better? provided Code is just an example.
let b random 3
if b = 0 [set Output "Output1"]
if b = 1 [set Output "Output2"]
if b = 2 [set Output "Output3"]
Or this one :
set output one-of ["Output1" "Output2" "Output3"]
UPdate:
I just checked it with a simple netlogo program:
turtles-own [X Y A]
to setup
__clear-all-and-reset-ticks
create-turtles 50
[set A random 3000
set y random-normal 0.5 0.1
set x random-normal 0.5 0.1
move-to patch random 20 random 20]
end
to go
ask turtles
[ set A A + 1
fd 1
rt random 10
if A > 4000 [die]
if random-float 1 < 0.003 and a > 1000 and A < 3000 [
let xx x
let yy y
hatch 1
[ set A 0
set x one-of (list (random-normal 0.5 0.1) (xx + 0.1) (XX - 0.1))
let b random 3
if b = 0 [set y random-normal 0.5 0.1]
if b = 1 [set y yy + 0.1]
if b = 2 [set y yy - 0.1 ]
]
]
]
tick
end
The results is as follow:
There is not much difference is using any of these methods :)
There is a bug in your measurement code. Change b = 3 to b = 2 and I predict you will see any difference go away.
Unless there is some weird bug in NetLogo (or the JVM) that no one has discovered yet, it shouldn't make any difference whether you use one-of or random. To my knowledge, there is no such bug.
The relevant source files are:
https://github.com/NetLogo/NetLogo/blob/5.0.x/src/main/org/nlogo/prim/_randomconst.java
https://github.com/NetLogo/NetLogo/blob/5.0.x/src/main/org/nlogo/prim/_oneof.java
The only difference is that the first calls context.job.random.nextLong and the latter context.job.random.nextInt (because the size of a NetLogo list is an int, not a long, while the input to random is a long). But both nextInt and nextLong are of equally high quality; they're just calls into the standard MersenneTwisterFast class which is widely used by people doing simulation work.