How can use NetLogo to simulate the outcome of a function - netlogo

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

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.

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.

How do you generate a histogram in NetLogo?

I have a breed called players. Each player-owns a player-strategy. There are 6 possible player-strategies. When I write
histogram [player-strategy] of players
nothing appears on my plot.
I have one plot with one pen -- and don't set either the plot or penname. plot-name returns the plot. pen-mode is bar. interval is 1.0
I don't know how to ask for the current penname. But since the plot has only one pen it shouldn't matter.
The histogram command is in both plot-update and pen-update. I've tried it in both together or one at a time. I also tried in code. Makes no difference.
What do I have to do to get a histogram to appear?
OK, here is what I think you are after.
If you create the following plot:
And write the following code:
extensions [table]
breed [players player]
players-own [player-strategy]
to setup
clear-all
create-players 100 [ set player-strategy one-of ["a" "b" "c" "d" "e"] ]
reset-ticks
end
to update-strategy-plot
set-current-plot "Player strategies"
clear-plot
let counts table:counts [ player-strategy ] of players
let strategies sort table:keys counts
let n length strategies
set-plot-x-range 0 n
let step 0.05 ; tweak this to leave no gaps
(foreach strategies range n [ [s i] ->
let y table:get counts s
let c hsb (i * 360 / n) 50 75
create-temporary-plot-pen s
set-plot-pen-mode 1 ; bar mode
set-plot-pen-color c
foreach (range 0 y step) [ _y -> plotxy i _y ]
set-plot-pen-color black
plotxy i y
set-plot-pen-color c ; to get the right color in the legend
])
end
You should get the following plot:
I believe this should answer your previous question as well.
(Note that I've put the plotting code in a code tab procedure, but you could just as well stick it in the "Plot update commands" field.)
I know it is a very old question, but in case anyone lands here on a search. The current version of NetLogo comes with example code for histogram usage: look in the file menu under 'Models Library'; see 'Code Examples'/'Histogram Example'.

Netlogo create a periodic function

I would create a periodic function in Netlogo based on ticks and a price variable.
I would like a function where price is maintained between 2 rang (0 and 1) and I will be great if I can choose the periodicity.
my small reproducible example :
patches-own[
price
]
to setup
clear-all
ask patches [
set price 0.1
]
reset-ticks
end
to go
ask patches [
set price (sin ticks) + price
]
tick
end
Thank you in advance
It sounds like you just want a mod based cycle.
to test
ca
reset-ticks
print map price n-values 100 [?]
print map [price02 0 1 ?] n-values 100 [?]
end
to-report price [#ticks]
let _cycle 10 ;cycle length
let _first 3 ;length of first cycle component
report ifelse-value (#ticks mod _cycle < _first) [
0.1
][
0.2
]
end
Edit:
Alternatively, you may simply be trying to scale a sine wave.
to-report price02 [#min #max #ticks]
let _cycle 10
let _sin sin (#ticks * 360 / _cycle)
let _scaledsin (1 + _sin) / 2
report #min + (#max - #min) * _scaledsin
end

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.