How to hatch turtles with probability - netlogo

I'm trying to find a procedure to hatch a turtle based on random probability:
40% for A
30% for B
30% for C
How do you hatch a turtle depending on that probability? What procedure/s should I use?

It looks like A, B, and C are breeds? Then
to weighted-hatch ;; turtle-proc
let p random-float 100
if (p >= 60) [hatch-As 1 [init-A]]
if (if p >= 30 and p < 60) [hatch-Bs 1 [init-B]]
if (p < 30) [hatch-Cs 1 [init-C]]
end
to init-A
;;put any desired initializations here
end
Etc.
You could alternatively use the rnd extension; see NetLogo, weighted random draw from a list: how to use rnd-extension?

Throw a dice mate! : ) Generate a random number, since you have 3 probable options you can use something like random-float 1 this gives a number in [0,1).
Then, if 0>= number <=0.4 hatch A, else if 0.4< number <=0.7 hatch B and so on for C.
Happy coding!

Related

How to plot distributions of two different breeds

I setup two different breeds with the breed command:
breed [breeds1 breed1]
breed [breeds2 breed2]
and in go, I ask to a random turtle to execute an action command, like this:
to go
ask one-of turtles [
action
]
end
where action is defined as
to action
ifelse (breed = breeds1)
[
set q random-float 1
set c q
set potential_1 (1 + d) * (1 - c)^(d)
]
[
set c random-float 1
set potential_2 (1 + (1 / d))*(1 - c)^(1 / d)
]
end
For breeds1, q has value in [0,1] and c takes its value.
For breeds2, q has value equal to 0 and c takes random values in [0,1].
Both breeds have d=3 (fixed value).
c,q, and both potentials are global variables.
What I need to do is to plot the two potentials/distributions.
I used plotxy to plot the distributions in the plot code box:
[![enter image description here][1]][1]
What I would like are the following two distributions in the same plot. Plot 1 shows the distribution for breeds1, plot 2 for breeds2.
If I consider potential_1 and potential_2 as turtles-own (the first one for breeds1, the second one for breeds2) I receive the message that I can't use potential_1 in an observer context, because potential_1 s turtle-only. Same for potential_2.
If I consider q and c as turtles-own as following:
breeds1-own
[ q
c
potential_1
]
breeds2-own
[
q
c
potential_2
]
I receive the following error:
You can't use c in an observer context, because c is turtle-only
So my question is: how could I plot the two distributions?
I hope you can help me.
Okay, I can't answer your question using your code as I can't work out the logic of the ask one-of. So, what I have done instead is created a complete model that does the plotting in the hope that will help you work out what's wrong with your code.
Try this:
globals [d]
turtles-own
[ potential
group
c
]
to setup
clear-all
set d 3
create-turtles 100
[ set group one-of ["type1" "type2"]
action
]
reset-ticks
end
to action
ifelse (group = "type1")
[ let q random-float 1
set c q
set potential (1 + d) * (1 - c)^(d)
]
[ set c random-float 1
set potential (1 + (1 / d))*(1 - c)^(1 / d)
]
end
Then you will need the following as the pens in your plot. You will also need to change the plot settings for each pen to 'point' rather than 'line'.
ask turtles with [group = "type1"] [plotxy c potential]
ask turtles with [group = "type2"] [plotxy c potential]
What I have done is create 100 turtles in two groups, with the values of the variables 'c' and 'potential' calculated for each group using your code. But it's all done in a single pass - each turtle does its calculation and then control moves to the next turtle as they are created. Then I have each pen in the plot just plot the values from one group.
The error message you were getting "You can't use c in an observer context, because c is turtle-only" means that you tried to use the variable c without letting NetLogo know which turtle's value of 'c' you wanted to use.

How can use NetLogo to simulate the outcome of a function

assuming I got a function like Y= 0.5*a+0.23*b+0.52*c+0.3
a is a continuous variable, b and c are categorical variables(like 1 or 0)
I wanna create multiple agents that can hatch the number of Y (round up to an integer) with different b and c
Honestly, I am new to Netlogo and I am not very familiar with coding.
I went through the three tutorials of the user manual, but I still have no clue to do that.
three tutorials of the user manual.
Thank you
This does what you said. But I don't think it's actually what you mean.
to testme
clear-all
let a 5
let b 10
let c 20
let Y ceiling (0.5 * a + 0.23 * b + 0.52 * c + 0.3)
print Y
create-turtles Y
[ setxy random-xcor random-ycor
]
end

NetLogo - no more than 5 % of population has a certain value of variable

How can I give each tick a random amount of turtles a change in a binary variable (1 or 0), whereas no more than 5 % of the existing population at all times has a value of 0 in that variable?
In other words, I wish to have that the total amount of turtles having a variable value of 0 is between 0 % or 5 % of the total amount of turtles at every tick.
How can I achieve this?
My code is:
to setup
create-turtles 100
set var random 1 (only 5 % max shall have a 0 at start)
end
to start
change
end
to change
let %draw (random 1)
if (%draw < 0) … ; than I do not how to continue
end
The n-of primitive selects the specified number of agents. You want some number up to that, so you also need to randomly generate the number. Something like this:
to setup
create-turtles 100 [ set var 1 ] ; give them all value 1
ask n-of random 6 turtles [ set var 0 ] ; randomly selects 0 to 5 turtles, assigns value 0
end

Create 1000 turtles near each others

I have to create a lot of turtles forming a compact group of any shape, a simple 10x100 rectangle is enough. The important thing is that they must be near each others.
In c i would do something like this:
for(x = 1; x <= rows; x++)
{
for(y = 1; y <= columns; y++)
{
create_turtle(x,y);
}
}
And the equivalent in netlogo would be:
crt 1000
let n 0
let x 1
let y 1
while[y <= 10]
[
set x 1
while[x <= 100]
[
ask turtle n
[move-to patch x y]
set x x + 1
set n n + 1
]
set y y + 1
]
But it's not an elegant solution. Any suggestion?
Edit: More precisely I have to reproduce what has been done in this article: http://science.sciencemag.org/content/345/6198/795.full
Every turtle is a little robot.
And here you can see one way turtles could be positioned turtles schema
I'm using circle turtles like the robots of the article.
One of the trickiest things for programmers from other languages to do when learning NetLogo is getting rid of all the loops. Iterating through the agents or patches is embedded in the ask primitive, you don't need to code the iteration. ask also iterates in a random order so that repeated processes don't lead to any advantage to whichever agent happens to be first in the loop.
Also, when you create turtles, you can immediately give them instructions. You can also place them initially in an arbitrary position rather than move them there. Here is one solution that places them all in a rectangle that is 5 patches to the left/right of centre (0,0) and occupies half the height of the world.
create-turtles 1000 [ setxy random-float 10 - 5 random-ycor * 0.5 ]
From the edit, I think you are wanting them to be created at gridpoints rather than randomly within the space. If that is true, then select the patches you want and ask them to sprout a turtle.
let in-shape patches with [ pxcor >= -10 and pxcor <= 10 and pycor >= -10 and pycor <= 10 ]
ask in-shape [ sprout 1 ]
You will need to work out your own values and make sure they are within the world dimension.

assign spatially autocorrelated variable to turtles

I want to assign a generated autocorrelated variable (0 to 1) to turtles spread over a grid. I can create autocorrelated data in R and then import them into Netlogo, but for sure, there is a more efficient way to do it in Netlogo.
Here a simple example:
turtles-own [
variable
]
to setup
clear-all
create-turtles 30
[
move-to one-of patches with [ not any? turtles-here ]
; I would like to assign a spatially autocorrelated variable
; for now, I am using a uniform variable
set variable random-float 1.0
]
end
Here an example using R:
N <- 16 * 16
p <- 0.07
# generate some points
set.seed(1234)
x.coord <- rep(1:16, 16)
y.coord <- rep(1:16, each = 16)
points <- cbind(x.coord,y.coord)
# distance matrix between points
Dd <- as.matrix(dist(points))
# weights matrix
w <- exp(-p * Dd)
Ww <- chol(w)
# variable
z <- t(Ww) %*% rnorm(N,0,1)
z <- scale(z, center = min(z), scale = max(z) - min(z)) # rescale to 0-1 variable
# plot
require(ggplot2)
df <- data.frame(x = x.coord, y = y.coord, z = z)
ggplot(df, aes(x = x, y = y, col = z)) +
geom_point() +
scale_colour_gradient(low="red", high="white")
Because I get data for each patch, I can create variable from the patch in which turtles are. Anyway, this looks unnecessarily complicated.
Any ideas?
Probably not the best solution, but at least it is close. I use the k-means extension to generate clusters based on distances between turtles. Then, I assign a random number between 0 to 1 + some noise to the turtles of each cluster.
let clusters k-means:cluster-by-xy targets 10 100 0.1
(foreach clusters (n-values 10 [random-float 1.0])
[ ask ?1 [ set variable random-between ?2 0.15 ]] ) ]
random-between is a report:
to-report random-between [number width-interval]
let half-interval width-interval / 2
let random-number (number - half-interval) + random-float width-interval
if random-number < 0 [set random-number 0]
if random-number > 1 [set random-number 1]
report random-number
end
This is not really clear - I don't believe you mean autocorrelated because you haven't mentioned time at all. Based on your R-code, you want to have turtles at regular points (in which case you want to ask patches [ sprout 1 ] rather than create turtles) and for them to have a variable with a value based on its position, in which case you can write a function with xcor and ycor as inputs.