Selecting the first 20 turtles out of 1000 turtles - netlogo

turtles-own [wages]
to setup
clear-all
setup-patches
setup-turtles
reset-ticks
end
to go
move-turtles
get-employed
tick
end
to move-turtles
ask turtles [
right random 360
forward 1
]
end
to get-employed
ask turtles [
if pcolor = blue [
set color green
set wages wages + 10
]
ifelse show-wages?
[ set label wages ]
[ set label " " ]
]
end
to setup-patches
ask patches [set pcolor pink ]
patch 0 0 [ set pcolor blue ]
end
to setup-turtles
create-turtles 80
ask turtles [ setxy random-xcor random-ycor ]
ask turtles [ set color red]
end
I want to add the code to select the first 20 percent of the 80 turtles who comes in contact with the patch having color blue.

In your question is not clear how you will use the first 20% of turtles who get the blue patch so i assume you just want to store them in order to use this information later.
I would add a turtles-own called is-first-20-percent? set to false for every turtle.
Then, at the end of the go procedure, before the tick i would execute the check-20 procedure as following:
to check-20
if count turtles with [color = green] = (count turtles * 20 / 100) [
ask turtles with [color = green] [set is-first-20-percent? true]
]
end
In every moment you can retrieve the first 20% of turtles who reached the blu zone with the command:
ask turtles with [is-first-20-percent?] [ ... do something ... ]
This code is working because 20% of 80 is an integer number (16) but if you plan to modify the starting number of turtles i suggest to modify the check-20 procedure as following:
to check-20
if (count turtles with [color = green] >= (count turtles * 20 / 100)
and count turtles with [is-first-20-percent?] = 0) [
ask turtles with [color = green] [set is-first-20-percent? true]
]
end

Related

How to restrict Old vs. Young: in netlogo?

I am trying to simulate the following situation. Young and old agents.
The old have few social contacts, slower and at the same time have a greater chance of dying.
Young people have more contacts .
Which group should be vaccinated first ?
I created two groups, assigned an infection algorithm, and assigned a social distance. But at the same time I do not see the relationship, the agents behave identically, where is the error?
I'm a beginner and don't want to complicate the model. please don't answer difficult I'm just learning
my code
;;breed [humans human]
;;humans-own [ old young ]
breed [young p-young]
breed [old p-old]
young-own [
days-after-infected
days-of-sick
]
old-own [
days-after-infected
days-of-sick ]
to setup
clear-all
setup-old
setup-young
setup-patches
reset-ticks
end
to setup-old
create-old population-old
[
setxy random-xcor random-ycor
set color 4
set shape "person"
]
end
to setup-young
create-young population-young
[
setxy random-xcor random-ycor
set shape "person"
set color 96
]
end
to setup-patches
ask patches [set pcolor 61]
end
;:ask old [random 360]
;;ask young [forward 1]
to go
move-old
move-young
transmission-old
transmission-young
incubation
sickness
tick
end
to move-old
ask old [
right random 360
forward 1
]
end
to move-young
ask young [
right random 360
forward 3
]
end
to add-carrier
ask one-of young
[set color orange]
end
to transmission-old
ask young with [color = orange]
[let susceptible-person one-of old in-radius 3 with [color = 4]
if susceptible-person != nobody
[ask susceptible-person
[if random 100 < transmission_rate
[set color orange]
]
]
]
end
to transmission-young
ask young with [color = orange]
[let susceptible-person one-of young in-radius 2 with [color = 96]
if susceptible-person != nobody
[ask susceptible-person
[if random 100 < transmission_rate
[set color orange]
]
]
]
end
to incubation
ask old with [color = orange]
[ ifelse days-after-infected < incubation_period
[set days-after-infected days-after-infected + 1]
[set color red]
]
ask young with [color = orange]
[ ifelse days-after-infected < incubation_period
[set days-after-infected days-after-infected + 1]
[set color red ]
]
end
to sickness
ask old with [ color = red ]
[ifelse days-of-sick < 14
[set days-of-sick days-of-sick + 1]
[ifelse random 100 < mortality
[die]
[ set color blue ]
]
]
ask young with [ color = red ]
[ifelse days-of-sick < 14
[set days-of-sick days-of-sick + 1]
[ifelse random 100 < mortality
[die]
[ set color blue ]
]
]
end
I am trying to infect some agents from others, while I am not interested in the incubation period and trying to link age (less movement and less contact) with morbidity.
But I see that the result is not presentable, there is no dependence on age and morbidity.
The transmission-old is your problem. You copypasted from transmission-young but forgot to replace ask young with ask old
to transmission-old
ask old with [color = orange]
[let susceptible-person one-of old in-radius 3 with [color = 4]
if susceptible-person != nobody
[ask susceptible-person
[if random 100 < transmission_rate
[set color orange]
]
]
]
end
I've been fighting for a week, I changed the concept, I think it's easier. How to describe the last block. If there is one infectious neighbor, he will transmit his infection with faithfulness....
breed [olds humanA]
breed [youngs humanB]
globals
[
humans
]
to setup
clear-all
create-olds humans_number * shareA
[
set color 64
set xcor random-xcor
set ycor random-ycor
set shape "person"
]
create-youngs humans_number * (1 - shareA)
[
set color 45
set xcor random-xcor
set ycor random-ycor
set shape "person"
]
set humans (turtle-set olds youngs)
ask olds
[
create-links-with (up-to-n-of (links_number_old * homophily_old) (other olds))
create-links-with (up-to-n-of (links_number_old * ( 1 - homophily_old)) (other
humans))
]
ask youngs
[
create-links-with (up-to-n-of (links_number_young * homophily_young) (other
youngs))
create-links-with (up-to-n-of (links_number_young * ( 1 - homophily_young))
(other humans))
ask patches
[
]
ask n-of 1 humans
[
set infected 1
set color red
]
reset-ticks
end
*to go
ask humans
[
if any? link-neighbors != red ]
[
set humans ......*
end

Change the color of the patch based on income level

Hi, this is a continuation of my previous question. Basically I'm
trying to differentiate the patches by its income level and once I
have established this I will set a monitor to count them. However,
when I run the model the patches are not updating. Where am I going
wrong? Appreciate your assistance.
turtles-own [wealth]
patches-own [income]
to setup
ca
setup-turtles
setup-patches
reset-ticks
end
to setup-turtles
create-turtles num-turtles
ask turtles
[
set shape "person"
set size 1
setxy random-xcor random-ycor
set wealth 100
]
end
to setup-patches
ask n-of 2000 patches [ set pcolor green ] ;; to identify patches that will accumulate income from turtles
end
to go
if not any? turtles with [wealth > 0] [stop]
move-turtles
spend
tick
end
to move-turtles ;; turtles to stop once they are spend all their wealth
ask turtles [
ifelse wealth > 0
[rt random 360 forward 1]
[stop]
]
end
to color-patches ;; patch colors to change based on their income level
ask patches [
ifelse income > 0 and income < 100 [
set pcolor 15
] [
ifelse income > 100 and income < 200 [
set pcolor 45
] [
ifelse income > 200 [
set pcolor 64
] [
set pcolor green
]
]
]
]
end
to spend
ask turtles with [wealth > 0] [
if pcolor = green [
set wealth wealth - 1
set income income + 1
]
]
end
The short answer is that you created the procedure to update patch colours but you never told NetLogo to run it. You probably want to add a line to your go procedure, I think this order:
to go
if not any? turtles with [wealth > 0] [stop]
move-turtles
spend
color-patches ; this is what you are missing
tick
end
This is not relevant to your question, but I also noticed that you have this code for moving:
to move-turtles ;; turtles to stop once they are spend all their wealth
ask turtles [
ifelse wealth > 0
[rt random 360 forward 1]
[stop]
]
end
You don't need to ask every turtle and then use stop to exit for some of them, instead it is generally easier to filter the turtles using with. So that code could be replaced with:
to move-turtles
ask turtles with [wealth > 0] [
rt random 360
forward 1
]
end

Netlogo increase turtle number by tick

I am trying to build a model where turtle move one patch per tick with random movement. I am looking for a solution to increase the number of turtle per tick based on the percentage. For ex. In the beginning there are 7 turtles, by each tick they should increase by following percentage:
10.72%
10.83%
10.93%
11.03%
11.11%
11.19%
11.27%
11.33%
11.39%
11.45%
Not sure if this is possible? If needed this can be round up to whole number.
If this is not possible, how can I increase the turtle number by 11% each tick for 10 ticks then after 12% each tick for another 10 ticks and so on?
Below is the code that I am using.
to setup
clear-all
setup-turtles
setup-patches
reset-ticks
end
to setup-patches
ask patches [ set pcolor green ]
end
to setup-turtles
create-turtles tourists [setxy random-xcor random-ycor ]
ask turtles [ set shape "person" set size 2 ]
end
to go
if ticks >= 130 [ stop ]
move-turtles
eat
tick
end
to move-turtles
ask turtles [ right random 360 forward 1]
end
to eat
ask turtles [ if pcolor = green [ set pcolor black ] ]
end
Thank you for your support.
Avi
I think your model is very similar to 'Rabbits Grass Weeds' in the Models Library so you might want to have a look at that to get some ideas. Focusing just on the revised question of moving to a green patch, you need with, which will restrict choices. Note that the code below will break if there are no neighbouring patches that are green.
to setup
clear-all
setup-turtles
setup-patches
reset-ticks
end
to setup-patches
ask patches [ set pcolor green ]
end
to setup-turtles
create-turtles 7 [setxy random-xcor random-ycor ]
ask turtles [ set shape "person" set size 2 ]
end
to go
if ticks >= 130 [ stop ]
move-turtles
increase-turtles
tick
end
to move-turtles
ask turtles [ move-to one-of neighbors with [pcolor = green] ]
end
to increase-turtles
ask one-of turtles [ hatch 1 ]
end
I used hatch in this code, which gets one turtle to create another turtle at the same place with the same colour etc. If you want to just create a new turtle in exactly the same way as your original turtles, then you want something more like this.
to setup
clear-all
make-turtles 7
setup-patches
reset-ticks
end
to setup-patches
ask patches [ set pcolor green ]
end
to make-turtles [ num ]
create-turtles num
[ setxy random-xcor random-ycor
set shape "person"
set size 2
]
end
to go
if ticks >= 130 [ stop ]
move-turtles
increase-turtles
tick
end
to move-turtles
ask turtles [ move-to one-of neighbors with [pcolor = green] ]
end
to increase-turtles
make-turtles 1
end
In this case, I have made a new procedure (called make-turtles) that creates as many turtles as you specify, randomly locates them etc. In setup, I call it to make 7 turtles, and then later on just make 1 each time.

How to stop turtles when all patches have been colored

When the turtles have covered the world in patches, I would like the turtles to stop on the last one so that I can record the amount of ticks it took.
Here is my code so far:
globals [marked-patches angle nextangle]
to setup ca ask patches [ set pcolor black ] crt turtle_amount
[set color red
set size 1
setxy (random 20) (random 20)] reset-ticks
end
to go ask turtles [
fd 1
rt random trt_ang
lt random trt_ang
if pcolor = black [set pcolor yellow] ]
tick end
In go, specifically in the turtle command, you can add:
to go
ask turtles [
fd 1
rt random trt_ang
lt random trt_ang
if pcolor = black [
set pcolor yellow
if count patches with [pcolor = black] = 0 [
stop
]
]
]
tick
end

How to get turtle to wait at patch for 10 ticks

I would like my turtles to
1) Stop for ten ticks if the turtle is red and comes across a red patch
2) After ten ticks I would like the turtle to continue on the look-for-food subroutine, which I already have.
An easy way to accomplish this is to use some sort of counter. Here is a full example:
turtles-own [ counter ]
to setup
clear-all
create-turtles 100 [
set counter 0
setxy random-xcor random-ycor
]
ask n-of 25 turtles [ set color red ]
ask n-of 100 patches [ set pcolor red ]
reset-ticks
end
to go
ask turtles [
ifelse counter = 0 [
look-for-food
if color = red and pcolor = red [
set counter 10
]
] [
set counter counter - 1
set label counter ; just to show what's going on
]
]
tick
end
to look-for-food
; your own look-for-food procedure is presumably different
right random 20
left random 20
forward 1
end