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.
Related
I'm want to calculate the index of dissimilarity in NetLogo. I have a world divided into different regions and want to examine how evenly species are distributed around the world.
Consider this example: A world is divided into 16 different regions. The world is populated with two types of ants, red and blue. It looks like this:
The world in the picture is produced with the following code:
globals[indexdissimilarity] ; where I want the index of dissimilarity to be stored.
to setup
ca
;Setting world.
resize-world 0 19 0 19
set-patch-size 15
;Creating regions.
let x 5
let y 5
let col 45
while [y <= max-pycor + 1 ][
while [x <= max-pxcor + 1][
ask patches with [pxcor < x and pxcor >= x - 5 and pycor < y and pycor >= y - 5][
set pcolor col
]
set x x + 5
set col col + 3
]
set x 5
set y y + 5
]
ask n-of (count patches * 0.85) patches[sprout 1[
set shape "bug"
set color red]]
ask n-of (count turtles * 0.50) turtles [set color blue]
dissimilarity
end
; Here is where I want to calculate the index of dissimilarity.
to dissimilarity
let tot_red (count turtles with [color = red])
let tot_blue (count turtles with [color = blue])
; set indexdissimilarity
end
My main issue is how to iterate parts of the calculations over each neighborhood.
Thanks!
I think I managed to solve it. Please, let me know if it looks correct. Here is the full updated code.
globals[indexdissimilarity
dis
]
patches-own [reg]
to setup
ca
;Setting world.
resize-world 0 19 0 19
set-patch-size 15
;Creating regions.
let x 5
let y 5
let col 45
while [y <= max-pycor + 1 ][
while [x <= max-pxcor + 1][
ask patches with [pxcor < x and pxcor >= x - 5 and pycor < y and pycor >= y - 5][
set pcolor col
]
set x x + 5
set col col + 3
]
set x 5
set y y + 5
]
ask patches [set reg [pcolor] of self]
ask n-of (count patches * 0.85) patches[sprout 1[
set shape "bug"
set color red]]
ask n-of (count turtles * 0.7) turtles [set color blue]
update
end
to update
;Dissimilarity index.
let tot_red (count turtles with [color = red])
let tot_blue (count turtles with [color = blue])
let neighb1 [reg] of turtles
let neighb remove-duplicates neighb1
set dis []
foreach neighb [i -> set dis lput abs((count turtles with [reg = i and color = red] / tot_red) - (count turtles with [reg = i and color = blue] / tot_blue)) dis]
set indexdissimilarity sum(dis) / 2
print(indexdissimilarity)
end
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
Obviously, any shape drawable by other means can be drawn by a turtle. Circles and squares are easy
rt 1 fd .0
and
if ticks mod 100 = 0 [rt 90]
fd 1
Super-ellipses not so much. (regular ellipses are not trivial either.)
The Wikipedia article on super-ellipses if you need to be refreshed on the topic.
Any input is appreciated.
Using a pendown turtle is there way to make a super-ellipse that emerges from turtle movement?
I have 1/4 of it, I suppose you could piece-wise put the other three together. Other values of n are not tested here. (using the Wiki notation, plus phi as an angle of rotating the whole thing.) And the placement of reset-ticks, pen-down, is sloppy, I know.
to go2
clear-all
reset-ticks
let a 6
let b 5
let phi 0
let n 3.5
create-turtles 1 [
let iNdx 1
repeat 90 [
show iNdx
show cos(iNdx)
if cos(iNdx) > 0 and sin(iNdx) > 0 [
let tx (a * (cos(iNdx) ^ (2 / n)))
let ty (b * (sin(iNdx) ^ (2 / n)))
let tx2 tx * cos(phi) - ty * sin(phi)
let ty2 tx * sin(phi) + ty * cos(phi)
setxy tx2 ty2
]
pen-down
set iNdx iNdx + 1
]
]
end
The ellipse looks simpler, but you be the judge
to go
clear-all
reset-ticks
let a 6
let b 5
let phi 45
create-turtles 1 [
let iNdx 1
repeat 360 [
let tx (a * cos(iNdx))
let ty (b * sin(iNdx))
let tx2 tx * cos(phi) - ty * sin(phi)
let ty2 tx * sin(phi) + ty * cos(phi)
setxy tx2 ty2
pen-down
set iNdx iNdx + 1
]
]
end
a generalization and simplification as a procedure.
to Super-ellipse [x y a b m n]
create-turtles 1 [
let iNdx 1
repeat 360 [
setxy (x + (abs cos iNdx)^(2 / m) * a * (sgn cos iNdx))
(y + (abs sin iNdx)^(2 / n) * b * (sgn sin iNdx))
pendown
set iNdx iNdx + 1]
]
end
The generalized form of another answer seems to produce the sort of thing I was thinking of. the closer the pen starts to one of the foci the closer the drawing is to a square. the n<1 super-ellipses are not achieved.
globals[c]
breed [pens pen]
breed [foci focus]
foci-own [dist distx disty]
to setup
ca
create-pens 1 [set heading 45 fd 10 pendown set C self]
;create-foci 1 [setxy (random-xcor / 2) (random-ycor / 2)]
create-foci 1 [setxy 10 10]
create-foci 1 [setxy 10 -10]
create-foci 1 [setxy -10 -10]
create-foci 1 [setxy -10 10]
end
to go
repeat 5100
[
ask foci [
set dist distance c
set distx xcor - [xcor] of c
set disty ycor - [ycor] of c
]
ask c
[
set heading 90 + atan ( sum [distx / dist] of foci / sum [dist] of foci)
( sum [disty / dist] of foci / sum [dist] of foci)
FD .0125
]
]
end
This is an answer to a question posed in the comments in my possibly poorly worded question about super-ellipses.
in Netlogo it is natural to draw geometric shapes in ways that may seem strange in other languages.
ask turtle 1 [pendown
let d (pi * distance turtle 2) / 360
repeat 360 [face turtle 2 rt 90 fd d]
]
for instance inscribes makes turtle 1 draw a circle [360-gon] around turtle 2. I did not invoke any of the standard circle formulas but still get a circle.
Is it possible to draw an ellipse in this same vernacular with say one turtle drawing an ellipse (or super-ellipse)round two other turtles using them as the foci?
Essentially to make an ellipse you set the turtles heading to the weighted mean heading of the foci and update each step. It could be done in one line but that would be one ugly line.
globals [a b c]
to setup
ca
crt 1 [set heading 90 fd 10 pendown set C self]
crt 1 [setxy 5 10 set A self]
crt 1 [setxy 0 -10 set B self]
end
to go
repeat 5100 ;; ad hoc number
[
ask c
[
let Ax [xcor] of A - xcor
let Ay [ycor] of A - ycor
let Bx [xcor] of B - xcor
let By [ycor] of B - ycor
let da 1 / distance a
let db 1 / distance B
set heading 90 + atan ((ax * da + bx * dB) / (da + db))
((ay * da + by * db) / (da + db))
FD .0125 ;;
]
]
end