How can i make my model count ticks faster vs the actions they are doing using NetLogo? - 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

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

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

Netlogo: find next target discounting previous targets

I'm trying to get my agents to calculate the next patch target once they have reached their first target. A target is defined as the (highest patch influence value / distance to agent). The next target needs to be calculated using this same procedure, but also discounting any previous targets from the equation.
I have attempted to create a patch-set to include all targets, although I'm not sure if it works.
My issue is how to create a to-report function that calculates the next highest-influence-value. I've gotten completely stuck with this final part of my procedure.
My initial reporter function is:
to-report highest-influence
let available-target patches with [influence > 0] ;and not any? patches with [pcolor = green]
report max-one-of available-target [influence / distance myself]
end
But I'm not sure how to have the next reporter function use the same parameters while also discounting any previous targets
to-report next-highest-influence
; patches with [influence > 0] and not part of patch set
end
Any help would be greatly appreciated. I've provided the full code.
breed [walkers walker]
walkers-own [traveled? ;; parameter to keep track of if a walker has traveled to a target yet or not
target ]
patches-own [influence influence-set] ;highest-influence
to setup
clear-all
reset-ticks
define-patches
create-walkers num-walkers
ask walkers [
setxy 0 0
set heading 90
let streets neighbors with [pcolor = black]
ifelse any? streets in-cone 1 25
[fd 1]
[move-to one-of streets in-radius 1]
set traveled? false ;; so that walkers know they have not yet moved to a target
]
ask walkers [
define-target ]
end
to define-patches
ask n-of 100 patches [
set pcolor white
]
ask n-of 40 patches with [pcolor = white] [set influence random 5 set pcolor blue set plabel influence]
ask patches [set influence-set patches with [pcolor = green] ]
end
to define-target ;; this defines the initial destination of walkers
if traveled? = false [
set target highest-influence
ask target [set pcolor green]
face target ]
end
to new-target ;; this defines subsequent desinations after walkers have reached inital target
if traveled? = true [
set target next-highest-influence
ask target [set pcolor green + 2]
face target ]
end
;;;;;;;;;;;;run-time procedure;;;;;;;;;;
to go
ask walkers [
if distance target > 1
[ travel-walkers
leave-a-trail
set traveled? true ]
if distance target = 1
[ stop
new-target
travel-walkers
leave-a-trail
]
]
tick
end
to travel-walkers
ask walkers [
move-towards-target
]
end
to move-towards-target
ask walkers [
ifelse [pcolor] of patch-ahead 1 != white or any? other turtles-here
[ Move-Function ]
[ Avoid-Function ]
]
end
to Move-Function
let t target
face min-one-of all-possible-moves [distance t]
fd 1
end
to Avoid-Function
let t target
face min-one-of all-possible-moves [distance t]
end
to leave-a-trail
ask patch-here [set pcolor grey]
end
;;;;;; reporter calculations ;;;;;;
to-report highest-influence
let available-target patches with [influence > 0] ;and not any? patches with [pcolor = green]
report max-one-of available-target [influence / distance myself]
end
to-report next-highest-influence
; patches with [influence > 0] and not part of patch set
end
to-report all-possible-moves
report patches in-radius 1 with [pcolor != white and distance myself <= 1 and distance myself > 0 and plabel = "" ]
end
You can make use of the member? primitive to do that. Check out the simplified toy model below (I didn't use your code as I'm not sure how you'd want to implement and there are some interface pieces i would need to set up to run it. Check out the MCVE guidelines).
turtles-own [ visited-list ]
patches-own [ influence ]
to setup-member?
ca
crt 1 [ set visited-list [] ]
ask patches [ set influence 0 ]
ask n-of 5 patches [ set influence 5 + random 50 ]
reset-ticks
end
to go-member?
ask turtles [
let target highest-influence
ifelse target != nobody [
face target
ifelse distance target > 1 [
fd 1
] [
move-to target
ask target [ set pcolor red]
set visited-list lput target visited-list
]
] [ print "No targets remaining." ]
]
tick
end
to-report highest-influence
let to-visit patches with [
influence > 0 and
not member? self [visited-list] of myself ]
report max-one-of to-visit [ influence / ( distance myself )]
end
The reported returns the patch with the highest influence/distance value that is not a member of the turtle's visited-list. Initially, no patches are a member of the list, but as the turtle visits each target, it adds the target to the visited-list so that patch is no longer considered.

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

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