Offspring with different gender - netlogo

I hope I submit it this time in easy and simple way, I used this code to create turtles in each patch, then I assinged them as parents ( male is blue and female is pink)
turtles-own [ gender]
to setup
ask patches [
sprout 1
[set size 0.2
set color pink
set gender "female"
]]
ask patches [
sprout 1
[set size 0.2
set color blue
set gender "male"
]]
reset-ticks
end
I want to have offspring from only one female each tick in each group with different gender (males and females ) . I tried to use hatch from one of the females but all the offspring were females and when I use create it said error as it is observer only. {{ the idea is to make groups as families and each family has their father, mather and different gender offspring }}
Please can someone help me to modify this code .
and thank you in advance :)

I'm not sure what you mean by "group". Do all the turtles together on a patch constitute a group, or what? (I'll assume that's what you mean.)
The following code finds all the patches where there is at least one male and one female present, then on each such patch, has one female hatch an offspring turtle of random gender.
to-report parents-here? ;; patch procedure
report any? turtles-here with [gender = "male"]
and
any? turtles-here with [gender = "female"]
end
to go
ask patches with [parents-here?] [
ask one-of turtles-here with [gender = "female"] [
hatch 1 [
set gender one-of ["male" "female"]
]
]
]
tick
end

Related

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.

Creating a directional link to an agent in radius with a particular attribute value

I'm creating a mating simulation whereby "male" agents select the youngest "women" in their immediate vicinity (no offense!). Unfortunately it's throwing an error at the line create-mtf-to min-one-of women [age] in-radius 1. How can I create a directional link to the youngest "woman" agent in a radius 1 around the male agent who is searching?
to mate-with-women
if any? women in-radius 1 [
create-mtf-to min-one-of women [age] in-radius 1
ask mtfs [set color blue]
]
end
This would be a little easier if you told us what the error message says and which line. However, I think this will fix it:
to mate-with-women
if any? women in-radius 1 [
create-mtf-to min-one-of women in-radius 1 [age]
ask mtfs [set color blue]
]
end
The agentset that you want to take the youngest from is women in-radius 1. If that doesn't work, try putting brackets (of the type '()') around women in-radius 1
As you test the agentset for members anyway so need to construct twice, another approach is:
to mate-with-women
let potential-mates women in-radius 1
if any? potential-mates [
create-mtf-to min-one-of potential-mates [age]
ask mtfs [set color blue]
]
end
This cleans up the syntax and may improve efficiency.

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