How to ask turtles to wait x number of ticks? - netlogo

I want to ask if I want to ask female to hatch after X number of ticks.
the female and male mated in March or April and give birth in January or February.
at the begining I made the code that they mate and have children in january and february
the code
to-report parents-here?
report any? turtles-here with [gender = "male"]
and
any? turtles-here with [gender = "female"]
end
to go
if ticks mod 12 <= 2 [
ask patches with [parents-here?] [
ask one-of turtles-here with [gender = "female"] [
hatch 1 [
set gender one-of ["male" "female"]
]
]
]
tick
end
but how can I ask them if parents here now so after exact number of ticks this female can hatch (pregnancy time)
the problem that when I make gestaton 0 then + 1 they still count even after hatching.
Thanks in advance for the help and sorry for my bad English

This may not be exactly what you're asking for, but you might be able to achieve the desired result by having a turtles own variable that you use as a gestation countdown:
turtles-own [ gestation ]
to-report parents-here?
report any? turtles-here with [gender = "male"]
and
any? turtles-here with [gender = "female"]
end
to go
if ticks mod 12 <= 2 [
ask patches with [parents-here?] [
ask one-of turtles-here with [gender = "female"] [
set gestation 10
]
]
]
ask turtles with [gestation > 0] [
if [gestation = 1] [
hatch 1 [ set gender one-of ["male" "female"] ]
]
set gestation (gestation - 1)
]
tick
end

Related

Is there a possibility to have an ifelse statement with two conditions (ticks mod and probability)?

I'm fairly new to Netlogo but I want to build a model where an agent (a car driver) will leave his home at a certain hour with a certain probability. Let's say he leaves Monday morning at 1 am (if have linked the ticks to the time so one tick is one hour).
I tried to work with ifelse-statements combined with a second statement which has to be verified in order for the entire statement to become true. In the example below the car / agent should leave with a probability of 7.7% its house and drive to a patch called underway-patches. Since one week has 168 hours, I tried to link the hour via the mod ticks (hence, mod ticks = 1 is equal to 1 am on a Monday morning).
This alone works:
ifelse ticks mod 168 = 1 and random-float 100.0 < 7.7
[ ask turtles [ move-to one-of underway-patches ] ]
[ ask turtles [ move-to one-of home-patches] ]
This works fine. So I have always about 7 out of 100 turtles moving to the underway-patch.
But If I now add the second hour, so 2 am, the first function does not work anymore (there are no turtles moving at all at 1 am - only at 2 am). I expect about 7 out of 100 turtles to move at 1 am to the underway-patch and then I expect about 5 out of 100 turtles to move at 2 am to the underway-patch (and the other 7 of the first hour should go back to the home-patches).
This does not work anymore:
; Monday, 1 am
ifelse ticks mod 168 = 1 and random-float 100.0 < 7.7
[ ask turtles [ move-to one-of underway-patches ] ]
[ ask turtles [ move-to one-of home-patches] ]
; Monday, 2 am
ifelse ticks mod 168 = 2 and random-float 100.0 < 5.1
[ ask turtles [ move-to one-of underway-patches] ]
[ ask turtles [move-to one-of home-patches] ]
I appreciate every help! Thanks in advance.
First, congratulations on an extremely clear question despite your newness to the site.
The problem is not that you have multiple ifelse statements, that is fine. The issue is that your ifelse statement is applying a single test to all turtles. Just look at the first example with one statement:
ifelse ticks mod 168 = 1 and random-float 100.0 < 7.7
[ ask turtles [ move-to one-of underway-patches ] ]
[ ask turtles [ move-to one-of home-patches] ]
Imagine that it is tick number 1. The computer runs the random number generator and gets 2. Great, the condition is true so the first block gets run. That will have ALL turtles move to the underway-patches. Similarly, if the random number generator returns 10, then the condition is false and ALL turtles move to home-patches.
You probably want something more like (you don't have to do the brackets on multiple lines, I did it so you can see the logical blocks of the structure):
ifelse ticks mod 168 = 1
[ ask turtles-on home-patches
[ if random-float 100.0 < 7.7
[ move-to one-of underway-patches
]
]
]
[ ask turtles-on underway-patches [ move-to one-of home-patches] ]
Or if you want exactly the correct proportion of turtles to move:
ifelse ticks mod 168 = 1
[ let num-to-move 0.077 * count turtles-on home-patches
ask n-of num-to-move turtles-on home-patches
[ move-to one-of underway-patches
]
]
[ ask turtles-on underway-patches [ move-to one-of home-patches] ]
Just a general observation, if you are going to code this for every possible starting time, you are going to have a lot of code that is identical except for the tick and the proportion. You state that you are new to NetLogo, so I don't want to jump too quickly to more advanced concepts, but come back when you're a little further along with having thought through your model and we can probably help you create a procedure that reduces the need to duplicate code.
UPDATE: One approach to reusing the code
This isn't quite right because I'm not really clear what sort of movement you want, but here is a complete model where the proportions are stored in a list and ticks is used to identify the correct item in the list. That proportion is then passed to a piece of code that moves turtles.
globals
[ home-patches
underway-patches
proportions
]
to setup
clear-all
set proportions [0 0.077 0.05 0.15]
set home-patches patches with [abs pxcor <= 3 and abs pycor <= 3]
ask home-patches [ set pcolor white ]
set underway-patches patches with [not member? self home-patches]
ask underway-patches [ set pcolor yellow ]
create-turtles 100
[ set color red
]
reset-ticks
end
to go
move-turtles item (1 + ticks mod 3) proportions
tick
end
to move-turtles [#prop]
print #prop
ask turtles
[ ifelse member? patch-here home-patches
[ if random-float 1 < #prop
[ move-to one-of underway-patches
]
]
[ move-to one-of home-patches
]
]
end

My agents are producing too many children

I'm working on a model where there is sexual reproduction of offspring, so there are two agent types, males and females. I ask the agents to reproduce once they reach a certain age: 400 ticks and continue to do so every 400th tick.
Females should only produce one child provided there are males. The model works for the first few generations but then the population explodes. With a starting population of one female and one male the numbers proceed as follows: 2, 3, 7, 19, 575. I don't know why it suddenly increases from 19 to 575.
It looks like some of the female offspring reproduce immediately after birth despite having an age = 0 i.e. they're not following this command:
ask females [
if age > 0 and age mod 400 = 0 [
reproduce
]
Here's the full model:
turtles-own [age]
breed[males male]
breed[females female]
females-own [ mates max-mate-count mate-count availa-males mother father]
to setup
clear-all
crt 2 [
ifelse random 2 = 1 [set breed males] [set breed females]
]
ask females [set color grey
setxy random-xcor random-ycor
]
ask males [set color red
setxy random-xcor random-ycor
]
reset-ticks
end
to go
ask turtles [increment-age]
ask females [
if age > 0 and age mod 400 = 0 [
choose-mates
]
]
ask females [
if age > 0 and age mod 400 = 0 [
reproduce
]
]
tick
end
to increment-age
set age (1 + age)
end
to choose-mates
ask females [
set mates males in-radius 100 with [age >= 400]
]
end
to reproduce
ask females with [count mates > 0 ] [
hatch 1 [
set mother myself
set father one-of [mates] of mother
ifelse random 2 = 1 [set breed males
set color red
move-to one-of patches with [pcolor = black]
set age 0
]
[set breed females
set color grey
move-to one-of patches with [pcolor = black]
set mate-count 0
set age 0
]]]
end
Hope you can help!
Don't ask females in the reproduce proc. See below. I've made some other suggestions as well.
turtles-own [age]
breed[males male]
breed[females female]
females-own [ mates max-mate-count mate-count availa-males mother father]
to setup
clear-all
create-males 1 [init-male]
create-females 1 [init-female]
reset-ticks
end
to init
set age 0
move-to one-of patches with [pcolor = black]
ifelse (breed = males) [init-male][init-female]
end
to init-male
set color red
end
to init-female
set color gray
set mate-count 0
end
to-report fertile
report (age > 0 and age mod 400 = 0)
end
to go
ask turtles [increment-age]
let _fertile (females with [fertile])
ask _fertile [choose-mates]
ask _fertile [reproduce]
tick
end
to increment-age
set age (1 + age)
end
to choose-mates
;ask females [ ;DONT DO THIS!
set mates (males in-radius 100 with [age >= 400])
;]
end
to reproduce ;female proc
;ask females with [count mates > 0 ] [ ;DON'T DO THIS!!
if (count mates > 0) [ ;DO THIS INSTEAD
hatch 1 [
set mother myself
set father one-of [mates] of mother
set breed one-of (list males females)
init
]
]
end

How do I choose from an agent-set that depends on an attribute?

I have a model where the male turtles get classified according to a threshold as follows:
ifelse(condition > threshold) [set status 0 ] [set status 1]
I want to count the potential male mates of each of the females in the population who meet this threshold. I've coded it so the females are limited to having a 10-patch radius of detection. If there are no males that meet this threshold then FR should be 0 otherwise it should be a proportion.
ask turtles with [sex = "female"] [if any? turtles with [sex = "male"]
in-radius 10 [ set potentialMates turtles with [sex = "male"] in-radius 10]
ifelse any? potentialMates with [anadromousM = 1]
[set FR count potentialMates with [anadromousM = 1] / count potentialMates ]
[set FR 0]]
When I run this I get the error for the second any?
ANY? expected input to be an agentset but got the number 0 instead.
Where am I going wrong? Hope you can help.
This is easier to see with different indenting. The following is your code (wrapped in a procedure name).
to find-mate
ask turtles with [sex = "female"]
[ if any? turtles with [sex = "male"] in-radius 10
[ set potentialMates turtles with [sex = "male"] in-radius 10
]
ifelse any? potentialMates with [anadromousM = 1]
[ set FR count potentialMates with [anadromousM = 1] / count potentialMates ]
[ set FR 0]
]
end
As you can see, the ifelse is tested (for each female turtle) regardless of the outcome of if any? turtles with [sex = "male"] in-radius 10. Imagine this first if is false, then the potentialMates agentset never gets created with the male turtles. Hence the error. You haven't shown us the code where pontentialMates is first instantiated - assuming it is a global variable, then it will have the value 0.
I think you want FR to be 0 in the case where there are no males in the radius at all. In that case, try this.
to find-mate
ask turtles with [sex = "female"]
[ ifelse any? turtles with [sex = "male"] in-radius 10
[ set potentialMates turtles with [sex = "male"] in-radius 10
set FR count potentialMates with [anadromousM = 1] / count potentialMates
]
[ set FR 0]
]
end
If there's no potentialMates with anadromousM = 1 then the numerator is 0 anyway, and 0/N is 0.

How can I create territories for several groups of agents in netlogo?

I'm very new to Netlogo, and this is my very first post in a forum.
I need to create animal agent groups which move in habitats or territories. The territories can overlap. More precisely I need male agents that move in different territories and several groups of female agents which move in other territories.
This is what I did so far. I created a territory just for one group of females and one group of males. Thats not exactly what I need.
globals [fragments]
breed [preys prey]
breed [femaletigers femaletiger]
breed [kids kid]
breed [maletigers maletiger]
turtles-own
[
energy
age
gender
territory
]
to setup
clear-all
setup-fragments
;setup-habitats
setup-turtles
reset-ticks
end
to setup-fragments
ask patches[set pcolor 67]
repeat 50
[
ask one-of patches
[
set pcolor brown
repeat 30
[
let a random 360
let b random 5
ask patch-at-heading-and-distance a b
[
ask neighbors [ set pcolor brown]
set pcolor brown
]
]
]
]
end
to setup-turtles
set-default-shape femaletigers "default" ; default shape (dreieck)
create-femaletigers 10
[
set color red
set size 1.5
set energy 100
set age random 20
set gender "female"
set territory patches-in-territory patch 10 6 15
move-to one-of territory with [pcolor = 67 ] ; tigers start in territory but not on fragmented areas
]
set-default-shape maletigers "default" ; default shape (dreieck)
create-maletigers 10
[
set color blue
set size 1.5
set energy random 100
set age random 20
set gender "male"
set territory patches-in-territory patch 40 15 10
move-to one-of territory with [pcolor = 67 ] ; tigers start in territory but not on fragmented areas
]
set-default-shape preys "circle"
create-preys 100
[
move-to one-of patches with [pcolor = 67] ;preys don't start in fragmented areas
set color 114
set size 0.75
]
end
to-report patches-in-territory [Center rd]
let ptr []
ask Center [set ptr patches in-radius 20]
report ptr
end
I'm thankful for any help.
Maria
welcome to Stack Overflow. When posting on here, in general you will be better served by restricting your code to just the bare minimum needed to demonstrate your issue (see the MCVE guidelines here); for example you could remove your to-report, prey breeds, etc just to make it very obvious what you're trying to solve.
I'm not following your code exactly as I'm not sure of some of your overall goal- instead I give an alternative example that will hopefully illustrate one way to accomplish what you're after. I'll show an example of territories that can overlap for females in this example and of territories that cannot overlap, for males in this example. Using these variables and setup:
breed [ femaletigers femaletiger ]
breed [ maletigers maletiger ]
turtles-own [ territory ]
patches-own [ maleclaimed? ]
to setup
ca
ask patches [
set maleclaimed? false
]
create-maletigers 3 [
set shape "triangle"
set size 1.5
move-to one-of patches with [ maleclaimed? = false ]
pd
set territory patches in-radius 5 with [ maleclaimed? = false ]
ask territory [
set maleclaimed? true
]
]
create-femaletigers 3 [
set shape "square"
setxy random-xcor random-ycor
pd
set territory patches in-radius 3
hatch 1 + random 3 [
rt random 360
fd 1
]
]
reset-ticks
end
In the setup, the patches-own boolean indicates if a patch has been claimed by a male- set it to false to start so that a male can check, as it sets up its territory, whether a patch is already claimed or not. Tigers then set up their territories similar to how you did, except males will not select territory from another male. Females set up their territory then hatch a few more females that will share the territory of the "mother".
To move within their territory, just restrict turtles such that they can only move to one of their territory patches- here is one way to do that:
to go
ask turtles [
let target one-of territory in-radius 1.5
if target != nobody [
face target
fd 1
]
]
tick
end
If you want to confirm that the turtles stay within a territory, run something like:
to territory-check
ask turtles [
let col color + 2 + random 3
ask territory [
set pcolor col
]
]
end
Thanks again. What is when the tigers reproduce and I want their kids to create a new territoy and the territories should not overlap?
to move-male
ask maletigers [
let target1 one-of territory in-radius 2
if target1 != nobody [
face target1
fd 1]]
end
to reproduce-tiger
if age >= 48 and any? maletigers-on patch-here and pcolor = 67 [
hatch random 1 + random (5 - 1) [
set gender one-of [ "male" "female" ]
set breed kids
fd random-float 2 ]]
end
to search-territory-male
ask maletigers with [ territory = nobody ] [
if maleclaimed? = false [
set territory patches in-radius 18 with [ maleclaimed? = false ]
ask territory [ set maleclaimed? true ]]]
end

Netlogo: simulation of cellular differentiation pattern?

I am trying to simulate the differentiation pattern of a simple organism. This is how I'd like my breeds and variables to work:
Breeds: vegetatives and heterocysts. Vegetatives can divide, heterocysts can't. Vegetatives can become heterocysts. Ideally, once a heterocyst is formed, the closer a vegetative is to it, the less likely it is for it to become a heterocyst in turn.
Variables:
age: + 1 per tick, - 1 for newly-hatched turtles
bump: A means by which to displace all the turtles located 'ahead' of a newly-hatched turtle, so they don't overlap. I imagined the system a bit like a Newton's Cradle (http://s.hswstatic.com/gif/newtons-cradle-1.jpg)
pro: promoter. Accumulates partially randomly. Once it reaches a certain value ('concentration'), a vegetative would change breed to become a heterocyst. Value decreased by inh.
proL: label for pro, with rounded values.
inh: inhibitor. Ideally this value should form a 'gradient' (highest in turtles near heterocysts, lowest further away).
The obvious problem that I can see is that I get a lot of contiguous heterocysts. Which is sort of what I've been trying to avoid. But I can't see what went wrong...Please help?
to setup
clear-all
setup-turtles
reset-ticks
ask turtles [ set size 1 ]
end
to setup-turtles
create-vegetatives 1
ask turtles [
setxy random-xcor random-ycor
set shape "circle"
set color 65]
end
to go
divide
add-age
move
differentiate
tick
end
turtles-own [age
bump
inh
pro
proL]
breed [vegetatives vegetative]
breed [heterocysts heterocyst]
to add-age
ask turtles [
set age age + 1
ifelse show-age?
[ set label age ]
[ set label "" ]
]
end
to divide
ask vegetatives [
if random 100 < 2 [
hatch 1[
set bump 1
set age age - 1
set inh 0
]
]]
end
;;Trying to get only one turtle per patch, making the others move
to move
ask turtles[
while [bump = 1] [
ifelse not any? turtles-on patch-right-and-ahead 180 1[
rt 180
fd 1
set bump 0
if any? other turtles-here[
ask other turtles-here
[set bump 1]
]
]
[fd 1
set bump 0
if any? other turtles-here[
ask other turtles-here[
set bump 1]
]
]
]]
end
to differentiate
ask turtles[
set pro (pro - inh + (random 2))
set proL round pro
ifelse show-proL?
[ set label proL ]
[ set label "" ]
create-links-with other turtles-on patch-ahead 1
create-links-with other turtles-on patch-right-and-ahead 180 1
if breed = vegetatives [
if any? link-neighbors[
ifelse any? link-neighbors with [breed = heterocysts]
[]
[set inh mean [inh] of link-neighbors]
]
if any? vegetatives with [pro > 50]
[ask vegetatives with [pro > 50]
[set breed heterocysts
set color brown
set shape "circle"
if any? link-neighbors[
ask link-neighbors with [breed != heterocysts]
[set inh 2]]
]]
]]
clear-links
end