How to restrict Old vs. Young: in netlogo? - 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

Related

How do you fix a problem with convert probability on a zombie apocalypse simulation on Netlogo [duplicate]

This question already has an answer here:
How do you go about making a convert probability chance for an agent in netlogo?
(1 answer)
Closed 1 year ago.
currently I'm working on a zombie apocalypse simulation on Netlogo, and I'm almost done except that the convertion function is not working. when the zombies collide with humans the humans they either die or convert the human to a zombie according to the global variable convert_probability. For example, if convert probability is set to 80 the human would have an 80% chance of winning the fight and killing the zombie and the zombie would have a 20% chance of converting the human to a zombie.
Also, zombies and humans are not allowed to pass over the brown patches and the function I wrote for that is change_direction, but had to comment those parts because I run to an error.
I have spent hours but couldnt come up with a solution so I would really appreciate any help;
breed [humans human]
humans-own [ humans_speed]
turtles-own [
infected?
]
patches-own [
block
]
breed [zombies zombie]
zombies-own [ zombies_speed]
globals [
student_id
username
nzomb
%infected
]
to setup_world
clear-all
reset-ticks
set student_id 19009670
ask patches [set pcolor green]
set-default-shape zombies "person"
set-default-shape humans "person"
create-zombies 5 [
set color red
set size 4
set zombies_speed 0.5
setxy random-pxcor random-pycor
]
ask zombies[
move-to one-of patches with [pcolor = green]
]
create-humans 15 [
set color blue
set size 4
set humans_speed 1 + random-float 0.1
setxy random-pxcor random-pycor
]
ask humans[
move-to one-of patches with [pcolor = green]
]
set %infected (count turtles with [color = red ] / 20 ) * 100
set nzomb 0
draw_blocks
end
to draw_blocks
ask patches [
set block false
]
ask n-of 100 patches with [pcolor = green][
set pcolor brown
set block true
]
end
to change_direction
if[block] of patch-ahead 2 = true [
right 180
face one-of patches in-radius 2 with [not block]
]
end
to run_model
if ticks >= 10000 [ stop ]
tick
move-turtles
convert
if not any? humans [stop]
end
to move-turtles
ask humans [move-humans]
ask zombies [move-zombies]
end
to move-humans
ask humans [
if any? zombies in-radius (10 + random 20) [rt 180]
set infected? false
set heading (heading + 45 - (random 90))
forward 1 + random-float 1.1
zombiedeath
convert
;;change_direction
]
end
to move-zombies
ask zombies [
chasehuman
convert
zombiedeath
set %infected (count turtles with [color = red ] / 20 ) * 100
set infected? true
set heading (heading + 45 - (random 90))
forward 0.5
; change-direction
]
end
to chasehuman
if any? humans in-radius 10 [
set heading towards one-of humans in-radius 10]
; change_direction
end
to runaway
face min-one-of zombies [distance myself]
rt 180
;change_direction
end
to convert
ask turtles with [ breed = zombies ]
[
ask humans-on patch-here
[
if random 100 < convert_probability [set breed zombies]]
ask humans-on patch-here [if random 100 < convert_probability
[set color red]]
]
end
to zombiedeath
ask zombies-on patch-here [if random 100 > convert_probability
[die]]
end
Okay, the only procedure relevant to this question is this one (same code as above, formatted so you can see the structure logic}:
to convert
ask turtles with [ breed = zombies ]
[ ask humans-on patch-here
[ if random 100 < convert_probability
[ set breed zombies
]
]
ask humans-on patch-here
[ if random 100 < convert_probability
[ set color red
]
]
]
end
You haven't actually told us what the problem is - "convertion function is not working" does not say whether it is generating an error, or what behaviour it is doing compared to what is expected for example. But, look at what this code actually does. First it finds all the zombies then, for each zombie, it:
gets the humans on the same patch to convert to zombies with some probability
gets the humans on the same patch to change colour to red with some probability
Your first problem is that you haven't linked the turning red with becoming a zombie. Is that the problem you were trying to solve? If so, here is some cleaned up code that links zombie and red:
to convert
ask zombies ; note, you can just directly use the breed name
[ ask humans-here ; again, can use the breed name
[ if random 100 < convert_probability
[ set breed zombies
set color red
]
]
]
end

Spatial Autocorrelation in NetLogo

Is there any straight-forward way to adjust spatial autocorrelation for three different patch colors? I am trying to control both the number of red patches and how spatially autocorrelated (how close same colored patches are to each other). I can control the proportion of red patches, but don't know how to setup the autocorrelation.
Here is my code so far:
to setup-patches
resize-world 0 15 0 15
set-patch-size 30
ask patches [
set pcolor one-of [ green brown ]
]
ask patches [
let close-patches patches with [pcolor != red]
ask n-of ((proportion-red-plants * count patches) - count patches with [pcolor = red]) close-patches
[set pcolor red]
]
end
proportion-red-plants is a slider in the interface
If you know that AC of 0 means pick a patch that has no red neighbours, and that AC of 1 means pick a neighbour of any red patch, then all that is required is to choose AC=1 method with the given probability and the AC=0 method otherwise. This is what I meant by a design issue, you need to work out the steps required before trying to code those steps.
Here is an almost solution. I haven't bothered to do things like make sure the patches being turned red aren't already red so the counts will be incorrect.
to setup
clear-all
let prop-red 0.1
let AC 0
ask one-of patches [set pcolor red]
ask n-of (prop-red * count patches) patches
[ ifelse random-float 1 < AC
[ ask one-of patches with [ pcolor = red ]
[ ask one-of neighbors [ set pcolor red ]
]
]
[ let candidates patches with [not any? neighbors with [pcolor = red] ]
if any? candidates
[ ask one-of candidates [ set pcolor red ]
]
]
]
end
Thank you JenB again for helping. This is the code I ended up using which proved to get the job done (while keeping counts of red patches correct)
to setup-patches
resize-world 0 15 0 15
set-patch-size 30
ask patches [set pcolor one-of [green brown]]
let first-patch one-of patches
ask first-patch [set pcolor red]
repeat (proportion-red-plants * count patches - 1) [ask one-of patches [assign]]
end
to assign
ifelse random-float 1 < AC
[let candds patches with [any? (neighbors with [pcolor = red])]
ask one-of candds [set pcolor red]]
[ask one-of patches [set pcolor red]]
end

How can i make my model count ticks faster vs the actions they are doing using NetLogo?

I'm modeling a critical situation that's happening in my city (ViƱa del Mar, Chile), where the "metro" (subway)can have serious problems if a tsunami occurs because the evacuation exits are not well prepared nor well managed.
This example is a simple model of the real one, but i was having really big problems trying to make the turtles move from the subway (blue patches), using the door (lime patch) and then jumping into the railways (gray patches). When i finally made them do those actions i found 2 problems: (1) the ticks aren't working the way i need them to work. I need it to run like it was a clock, counting seconds. (2) i create 3 actions called "check-vecinos-azul/lime/gray" where they need to check if there's any turtle arround them (neighbors). If there's an empty space arround a turtle it moves to that empty spot, otherwise it stands until it finds an empty one.
I'd be so glad if anyone of you could give me a help/tip with my model.
Thank you guys,
Kind regards,
Felipe.
breed [pasajeros pasajero]
turtles-own [
distancia
speed
speed-limit
speed-min
evacuacion
]
patches-own
[
pvacio
]
to setup
ca
setup-patches
setup-pasajeros
reset-ticks
end
to setup-patches
ask patches with [pycor <= 0]
[set pcolor blue - 1]
ask patches with [pycor > 0]
[set pcolor gray + 2]
ask patch 0 0 [set pcolor lime]
end
to setup-pasajeros
ask n-of num-pasajeros (patches with [pcolor = blue - 1])
[
sprout-pasajeros 1
[set size 1
set color orange
set speed 0.1 + random-float 0.9
set speed-limit 1
set speed-min 0
set evacuacion 0
]
]
end
to go
ask pasajeros [
ifelse evacuar?
[
go-puerta
salir-puerta
move-chancado
]
[
stop
]
]
tick
end
to go-puerta
ask turtles with [pcolor = blue - 1]
[
set evacuacion 1
if evacuacion = 1
[
check-vecinos-azul
face min-one-of patches with [pcolor = lime][distance myself]
fd speed ;;
]
]
end
to salir-puerta
ask turtles with [pcolor = lime]
[
set evacuacion 1
if evacuacion = 1
[
check-vecinos-lime
face min-one-of patches with [pcolor = gray + 2][distance myself]
fd speed
]
]
end
to move-chancado
ask turtles with [pcolor = gray + 2]
[
set evacuacion 1
check-vecinos-gray
]
end
to check-vecinos-azul
ifelse not any? turtles-on one-of neighbors
[
wait 0
move-to min-one-of patches with [pcolor = blue - 1][distance myself]
fd speed
]
[
wait 0.5
]
end
to check-vecinos-lime
ifelse not any? turtles-on one-of neighbors
[
wait 0
face one-of patches with [pcolor = gray + 2]
fd speed
]
[
wait 0.5
]
end
to check-vecinos-gray
ifelse not any? turtles-on one-of neighbors
[
wait 0
face min-one-of patches with [pcolor = gray + 2][distance myself]
fd speed
]
[
wait 0.5
]
end

How to stop the ticks using a breed in netlogo?

I want to create a Democracy Model. I have created 4 breed for that. One for people who will vote, the rest 2 for parties. In my code, I wand to stop the model when any of the parties reach a total number of 100 votes. I can't figure it out. Please help me. Here's my code:
breed [people p]
breed [party1 p1]
breed [party2 p2]
breed [party3 p3]
party1-own [vote]
party2-own [vote]
party3-own [vote]
to setup
clear-all
setup-patches
setup-people
setup-parties
reset-ticks
end
to setup-patches
ask patches [
ifelse pxcor >= 4 and pycor >= 6
[set pcolor white]
[set pcolor brown]
]
end
to setup-people
set-default-shape people "person"
create-people number-of-people
ask people [setxy random-float -16 random-float -16]
end
to setup-parties
set-default-shape party1 "person"
set-default-shape party2 "person"
set-default-shape party3 "person"
create-party1 1
create-party2 1
create-party3 1
ask party1 [setxy 15 -1]
ask party2 [setxy 15 -3]
ask party3 [setxy 15 -5]
ask party1 [set color blue]
ask party2 [set color green]
ask party3 [set color yellow]
end
to go
start-voting
ask party1 [
if (vote) >= 100 [stop]
]
ask party2 [
if (vote) >= 100 [stop]
]
ask party3 [
if (vote) >= 100 [stop]
]
tick
end
to start-voting
let x random 3
ifelse x = 2
[ask party3 [set vote vote + 1]]
[
ifelse x = 1
[ask party2 [set vote vote + 1]]
[ask party1 [set vote vote + 1]]
]
ifelse show-votes?
[
ask party1 [set label vote]
ask party2 [set label vote]
ask party3 [set label vote]
]
[
ask party1 [set label ""]
ask party2 [set label ""]
ask party3 [set label ""]
]
end
It looks like the problem with your stop procedure is that a turtle can only stop its own role in a procedure. check out the procedure manual regarding buttons for a more thorough explanation, where it is stated:
In a turtle or patch forever button, the button won't stop until every turtle or patch stops -- a single turtle or patch doesn't have the power to stop the whole button
So it looks like what's happening is that your first group is stopping the procedure, but since that happens after voting happens, and the other turtles are not stopped, the votes will continue to be added and the procedure will continue to run. In this case, it's probably better to have a global-level stop condition, as below. Note that when the observer queries a turtle using of, the variable is returned in a list, so item 0 is needed below.
to go
start-voting
if ( item 0 [vote] of party1 > 100 ) or ( item 0 [vote] of party2 > 100 ) or ( item 0 [vote] of party3 > 100 ) [
stop
]
tick
end

Color progression issue in netlogo

I have three different turtles: red, white, and green. I am trying to get these turtles to couple up (like people in relationships) and then uncouple. While a red turtle is coupled with a green or white turtle I want the patch color to sometimes randomly change colors, but for some reason the patch color color progression is not working.
to go
ask turtles
[ if coupled? = true
[ set couple-length couple-length + 1 ] ]
ask turtles
[ if coupled? = false
[ move ] ]
ask turtles
[ if coupled? = false and shape = "person righty" and (random-float 10.0 < coupling-tendency)
[ couple ]
let potential-intervener one-of (turtles-on neighbors) with [ green-dotter? = true]
if (coupled? = true and potential-intervener != nobody and "person righty" = true)
[ifelse (random-float 10.0 < resting-tendency)
[;;decides to stay
ask potential-intervener [move-to patch-here] ;; this is possibly what is causing the lack of intervention?
; does not intervene, start the color progression
ifelse (random-float 10.0 < intervening-tendency)
[;; intervenes
uncouple
]
[set couple-length couple-length + 1 ;; increments the couple length by 1
if (couple-length = 2)
[
ask patch-here [set pcolor yellow]
ask patch-at -1 0 [set pcolor yellow] ]
if (couple-length = 4)
[
ask patch-here [set pcolor orange]
ask (patch-at -1 0) [set pcolor orange] ]
if (couple-length = 6)
[
ask patch-here [set pcolor red]
ask (patch-at -1 0) [set pcolor red] ] ]
]
[
;;decides to move
ask potential-intervener [move]
set couple-length couple-length + 1 ;; increments the couple length by 1
if (couple-length = 2)
[
ask patch-here [set pcolor yellow]
ask patch-at -1 0 [set pcolor yellow] ]
if (couple-length = 4)
[
ask patch-here [set pcolor orange]
ask (patch-at -1 0) [set pcolor orange] ]
if (couple-length = 6)
[
ask patch-here [set pcolor red]
ask (patch-at -1 0) [set pcolor red] ] ]
]
; will green dotter intervene?
ifelse (random-float 10.0 < intervening-tendency)
[;; intervenes
uncouple
]
[;; does not intervene, start the color progression
set couple-length couple-length + 1 ;; increments the couple length by 1
if (couple-length = 2)
[
ask patch-here [set pcolor yellow]
ask patch-at -1 0 [set pcolor yellow] ]
if (couple-length = 4)
[
ask patch-here [set pcolor orange]
ask (patch-at -1 0) [set pcolor orange] ]
if (couple-length = 6)
[
ask patch-here [set pcolor red]
ask (patch-at -1 0) [set pcolor red] ] ]
]
check-sliders
tick
end
For one thing, your line if (coupled? = true and potential-intervener != nobody and "person righty" = true) is inside a command block that is only reached through if coupled? = false and shape = "person righty" and (random-float 10.0 < coupling-tendency). That is, it doesn't get reached because coupled? would need to be both true and false. Better formatting would make this immediately clear. I have reformatted the top part of your code so you can see this:
to go
ask turtles
[ ifelse coupled?
[ set couple-length couple-length + 1 ] ]
[ move
if shape = "person righty" and (random-float 10.0 < coupling-tendency)
[ couple ]
let potential-intervener one-of (turtles-on neighbors) with [ green-dotter?]
if (coupled? and potential-intervener != nobody and "person righty")
[ ifelse (random-float 10.0 < resting-tendency)
[;;decides to stay
ask potential-intervener [move-to patch-here]
A couple of other notes. You don't need to say if coupled? = true, it is sufficient to say if coupled?. Since you have the same colouring code three times, it would be better to have it only once as a separate procedure and call it from the three locations, so it is much easier to change.