Turtles in sub-samples which make different actions - netlogo

I would need to let my agents/turtles to make some actions. Specifically I would like to select, let's say, 40 turtles and let make randomly some actions, for instance:
25 turtles action1;
15 turtles action2.
25, 15 should be randomly chosen.
I wrote the following
ask up-to-n-of num_of_turtles_per_tick turtles with [breed = M] [
ifelse random-float 1 < prob
[
action1]
[action2]
]
prob is set to 0.5. I think that my code let 40 turtles to do either action1 or action2, with no possibility to distinguish between two sub-sample of turtles (25,15 in the example, or 20,20, or 12 and 18...).
I should probably add a new parameter to determine this randomly numbers and let them make separately actions.
Could you please give me some advice on how to do this?
Thanks

You want the subsets to be mutually exclusive, so you need some construction that has an if-else type logic. But you can do either as a group or individually.
Individually is easier to understand, so let's start there (not tested so may have syntax errors). Basically you draw a random number and do one action if it's low and the other if it's high.
to testme
clear-all
create-turtles 40 [setxy random-xcor random-ycor]
ask turtles
[ ifelse random-float 1 < 15 / 40
[ set color blue ]
[ set color red ]
]
end
For the group approach, you need some way to remember the group that does the first action so you can identify all the others to be in the group that does the second action. The member? reporter checks whether a turtle is a member of the specified turtleset.
to testme
clear-all
create-turtles 40 [setxy random-xcor random-ycor]
let type1 n-of 15 turtles ; assigns some to a temporary agentset
ask type1 [ set color blue ]
ask turtles with not member? self type1 [ set color red ] ; gets the others
end

Related

How to stop NetLogo simulation

I am trying to create a simulation of Covid19 spread for months: May and June
The code I am using to create turtles and start simulation is showed below:
to gh-month
if month="May"//month is a chooser in interface tabe
[
clear-all
create-turtles 3000
[
setxy random-xcor random-ycor
set shape forma
set color blue
]
set no_infected 106
ask n-of 1 turtles with [color = blue]
[
set infectedTurtle? True
set color red
]
set percentage(no_infected / 3000)* 100
]
if month="June"
[
clear-all
create-turtles 3000
[
setxy random-xcor random-ycor
set shape forma
set color blue
]
set no_infected 2030
ask n-of 1 turtles with [color = blue]
[
set infectedTurtle? True
set color red
]
set percentage(no_infected / 3000)* 100
]
end
And setup and go commands are below
to setup
gh-month
end
to go
reset-ticks ;; netlogo time
ask turtles [
forward 0.005 ;; moving turtles command and speed
]
ask turtles with [color = red] [
ask other turtles-here [
if random 100 < percentage[set color red]
]
]
set %infected (count turtles with [color = red] / count turtles) * 100
if %infected= percentage[stop]//percentage is declared above also is a monitor in interface tab
end
My question after simulation start, why for the month' May' is stopped ate based on given condition(if %infected= percentage[stop]), and for June no, it goes up to 100% and don't stop at given condtion.
Thanks to everyone
You have a basic misunderstanding of how time operates in NetLogo so there are several problems. You have also written way too much code at once. For example, just create the turtles and get that working before having them move, and get that working before trying to have different months.
NetLogo works with a counter or clock. Each tick increments the counter. The reset-ticks is part of the initialisation because it sets the counter to 0 and makes it available. The command tick advances the clock, and is usually the last command in the procedure that contains all the things that happen in a day (or hour or whatever a tick represents). The go procedure therefore needs the command tick, not the command reset-ticks.

Set Radius to find a partner turtle

I'm very new at Netlogo and programming in general.
I want to create a netlogo model, with female and male turtles. Both populations move through the world via random walk. The female population should find a partner and has the property 'radius'. Her own radius should expand if she has not found a partner until the moment she finds one. How can I program a radius around the female turtles, that expand after each time step if she has not found a partner?
Thanks for your help!
First, you need a turtle attribute that stores the value for each turtle. The way to do that is with a turtles-own statement. Then you simply change the value as required. The primitive in-radius looks at everything within the specified distance, and then you can set a condition by whether there are any? suitable mates. Your code would look something like (this is a complete model):
turtles-own
[ search-radius
mate
]
to setup
clear-all
create-turtles 20
[ setxy random-xcor random-ycor
set color blue
set search-radius 1
]
reset-ticks
end
to go
check-for-mate
tick
end
to check-for-mate
ask turtles with [color = blue]
[ let candidates other turtles in-radius search-radius
ifelse any? candidates
[ set mate one-of candidates
set color red
]
[ set search-radius search-radius + 0.5 + random-float 1
]
]
end

How do I assign a gender randomly to turtle breeds in NetLogo?

For an assignment in one of my classes we need to model disease spread and also have the turtles, whether sick or healthy, reproduce when a male and a female end up on the same patch, and it's also based on a slider with probability of reproduction. We're supposed to have the gender assigned randomly at birth so that the reproduction works properly. Any idea how to do this?
This is my code so far:
to setup
clear-all
ifelse netlogo-web? [set max-turtles 300] [set max-turtles 300]
create-healthy-cows Population-Size [ set shape "cow"
set color lime
set infected? false
set disease? false
ask n-of (random Population-Size) turtles [set gender-male? true]
set size 3
setxy random-xcor random-ycor
set age random 200
set label-color black ]
end
and also:
to check-reproduction
ask turtles [if gender-male? = false [
ask turtles [if any? turtles-here with [gender-male? = true]
[reproduce-cows] ]]]
end
to reproduce-cows
ask turtles [ if max-turtles < 300 [
ask turtles [if age >= 50 [if (random-float 100 < Reproduction-Rate)
[hatch 1
[set color blue] ]]]]]
end
Also I have gender-male? set as a turtles-own.
Have a look at one-of and see if you can come up with a solution. It's important to learn what you're doing and why, and not just ask for the answer, especially when its for a class assignment.
I've never seen or heard of NetLogo and it took me less than 5 minutes to come up with a solution and then find a second, better one.. Especially if this is an assignment you should be googling and researching instead of asking to be spoonfed!

create turtles and move to a position in a radius of another breed agent

I would like to create some turtles in a radius of another breed agent.
I have this example code:
breed [ readers reader ]
undirected-link-breed [ rris rri ]
breed [ tags tag ]
to setup
clear-all
set xy-file "locations.txt"
setup-readers
setup-tags
end
to setup-readers
create-readers num-readers [
set shape "circle"
set color white ; means idle state 'red' is active
setxy random-xcor random-ycor
]
end
to setup-tags
create-tags tag-population [
setxy random-xcor random-ycor in-radius 6 of one-of readers
]
end
The line setxy... to place the tags in a circle of distance 6 from one of the readers it does not work, but I do not know how to fix it. I have also tested move-to primitive without good result.
What I would like is to have a population of tags in a radius of each reader and if possible select the number of tags for each reader to be different.
The sprout primitive might get you what you need- instead of creating your tags and then moving them, just have the appropriate patches spawn them directly. For example, with this setup:
breed [ readers reader ]
breed [ tags tag ]
to setup
clear-all
setup-readers
setup-tags
reset-ticks
end
to setup-readers
create-readers 5 [
set shape "circle"
set color white
setxy random-xcor random-ycor
]
end
You can make a population of patches that are within the radius of readers and have however many you like sprout a tag:
to setup-tags
let radii-patches patch-set []
ask readers [
set radii-patches ( patch-set radii-patches patches in-radius 6 )
]
; Code above defines the patch-set of patches within 6 of readers
; Then, just ask 10 of those radii patches to sprout a tag
ask n-of 10 radii-patches [
sprout-tags 1
]
end
If you are doing this on a per-reader basis and you want each to have a different number of tags, you could try something like:
to setup-tags-reader
let n-tag random 5
ask readers [
ask n-of n-tag patches in-radius 6 [
sprout-tags 1
]
]
end
but then set the n-tag value some other way (for example, from another .csv file as you seem to be setting reader location).
Side note- when you post on here try to strip out any code than cannot be directly copied and pasted into Netlogo by other users (eg the 'xy-file' line, the tag-population and num-readers variables)- it just makes things super simple!
Edit
As per your comment- try this option (using the same setup as above)
to setup-tags
create-tags 10 [
move-to one-of readers
rt random-float 360
fd random-float 6
]
end
This just creates tags, moves them to a reader, has them randomly select a direction, then has them step forward a random amount from 0 to 6.
As to how to assign patches to each reader- just make a readers-own variable then have them assign patches to that variable in their setup (eg, set my-patches patches in-radius 6). You could do a similar thing with tags to define a reader-specific set of tags (eg `set my-tags tags in-radius 6). Note that in both cases you can get overlap where two readers share patches/tags- you will have to account for that.

Hatching turtle of specified color based on other turtle color

I want to hatch a turtle after two different colors of turtles overlap.
to interact
if any? other turtles-here
[
birth
]
;detect interaction
end
to birth
ask turtles
[
hatch random 5 [ fd 1 ]
]
end
I would like to hatch a turtle that was an average color of the two parent turtles that interacted.
something like.
to birth
ask turtles
[ hatch random 5
[ let color be sum of previous turtles color sum / 2
fd 1 ] ]
end
also any tips on what I might be misunderstanding about netlogo syntax might be appreciated.
This might not be exactly what you're looking for, but if the parents are the only ones on that patch when they give birth then this block should do the trick.
to birth
let Q mean [color] of turtles-here
ask one-of turtles-here
[hatch random 5
[
set color Q
fd 1
]
]
end
I'm not sure if you'd need to make the offspring it's own breed though to tell them to change their color and move, or if this will work... If this doesn't work then:
breed[offsprings offspring]
breed[parents parent]
to birth
let Q mean [color] of parents-here
ask one-of parents-here
[hatch-offsprings random 5 ]
ask offsprings-here
[
set color Q
fd 1
]
end