Color progression issue in netlogo - 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.

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

expected input to be a number but got the TRUE/FALSE true instead

I want to write an evacuation with leader code in net-logo but i have this error (AND expected input to be a TRUE/FALSE but got the number 0 instead.)the error came from function (follow_leader). my project is that i evacuate an environment and evacuate it with the nearest leader from the crowd . the code is not perfect that because iam knew to netlogo .
this is my code
globals[ goal-x goal-y n number leader
percent_of_leader wall
window
door largecircle]
patches-own [path?
obstacle
goal]
turtles-own [direction
fast?
fear?
leader?
is-leader?
follower
other-nearby]
to setup
clear-all
set-default-shape turtles "person"
;create-turtles 50 [ set color yellow ]
;ask turtles [fd random 13 ]
drwa-walls
ask n-of population patches with [ pcolor = black]
[sprout 1
[ set color white
set size 0.9
set shape "person"
set leader? false
;set follower self
if xcor < 9 and ycor > 9 [set heading 0]
if xcor >= 9 and ycor >= -10[set heading -90]
if xcor > -12 and ycor < -10 [set heading 90]
if xcor <= -8 and ycor > -10 and ycor < 0 [set heading 180]
]]
choose-leaders
end
to choose-leaders
ask turtle 6
[
set leader? true
set color yellow
set size 1
set shape "default"
set leader self
set xcor 3
set ycor 10
set heading 0
]
end
to go
move-for
ask turtle 6[move-to-exit1
fd 1 ]
ask turtles with [shape = "person"][
ifelse (xcor < 9 and ycor > 9 ) [follow_leader] [move-to-exit1]
if xcor >= 9 and ycor >= -10 [move-to-exit2]
if xcor > -12 and ycor < -10 [move-to-exit3]
if xcor < -12 and ycor < -10 [move-to-exit3]
if xcor <= -8 and ycor > -10 and ycor < 0 [move-to-exit4]
;avoid obstcao
; avoid_obstacles
;ifelse [pcolor] of patch-ahead 1 = blue
; [ lt random-float 360 ] ;; We see a blue patch in front of us. Turn a random amount.
;[ fd 1 ] ;; Otherwise, it is safe to move forward.
; ifelse not is-patch? patch-ahead 3 or [ pcolor ] of patch-ahead 3 =
;blue or not is-patch? patch-ahead 2 or [ pcolor ] of patch-ahead 2 =
;blue or not is-patch? patch-ahead 1 or [ pcolor ] of patch-ahead 1 =
;blue
;[lt random-float 360 ]
;[fd 1]
; while [patch-ahead 1 = nobody]
;[
;lt random-float 360
;]
;ifelse [pcolor] of patch-ahead 1 = blue
; [ lt random-float 360 ] ;; We see a blue patch in front of us. Turn a random amount.
;[ fd 1 ]
; ifelse not is-patch? patch-ahead 3 or [ pcolor ] of patch-ahead 3 =
;white or not is-patch? patch-ahead 2 or [ pcolor ] of patch-ahead 2 =
;white or not is-patch? patch-ahead 1 or [ pcolor ] of patch-ahead 1 =
;white
; [lt random-float 360 ]
; [fd 1]
;ifelse not is-patch? patch-ahead 3 or [ pcolor ] of patch-ahead 3 =
;green or not is-patch? patch-ahead 2 or [ pcolor ] of patch-ahead 2 =
;green or not is-patch? patch-ahead 1 or [ pcolor ] of patch-ahead 1 =
;green
;[lt random-float 360]
; [fd 1]
]
reset-ticks
tick
end
to rt-random
while [patch-ahead 3 = nobody]
[
rt random 360
]
end
to avoide
ask turtles [
if [pcolor] of patch-ahead 1 = green
[lt random 360 fd 1]
]
end
to avoid_obstacles ;; all obstacles set as green patches
let i 1
while [[pcolor] of patch-ahead i != blue ]
[set i (i + 1) ]
if ([pcolor] of patch-ahead i = blue)
[
ifelse [pcolor] of patch-at-heading-and-distance (heading - 20) i + 1 = blue
[
ifelse [pcolor] of patch-at-heading-and-distance (heading + 20) i + 1 = blue
[
ifelse random 1 = 0
[ rt 360 ]
[ lt 360]
]
[ rt 360 ]
]
[lt 360]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;
to bounce
if [pcolor] of patch-at dx 8 = blue [
set heading (90)
]
if [pcolor] of patch-at 0 dy = blue [
set heading (-90)
]
end
;;;;;;;;;;;;;;;;;;;;;;;
to move-for
ask turtles with [shape = "person"][fd 1]
end
to drwa-walls
draw-exit1
draw-exit2
draw-exit3
draw-exit4
;rows x
ask patches with [pxcor <= 30 and pxcor >= 0 and pycor = -10]
[set pcolor blue]
ask patches with [pxcor >= -30 and pxcor <= 0 and pycor = 9]
[set pcolor blue]
;coilmn
;ask patches with [pycor <= 9 and pycor >= -10 and pxcor = 0]
;[set pcolor green]
;ask patches with [pycor <= 4 and pycor >= 0 and pxcor = 0]
;[set pcolor green]
;circle shape
ask patch -14 -9 [
set largecircle patches in-radius (2)
]
;set color of largecircle patches green
ask largecircle [
set pcolor green
]
ask patch 16 16 [
set largecircle patches in-radius (2)
]
;set color of largecircle patches green
ask largecircle [
set pcolor white
]
end
to draw-exit1
;exits at top of concourse area, where turtles will leave
set goal-x -1
set goal-y -30
ask patch goal-x goal-y [
sprout 1 [ set pcolor red
set shape "square"
]
]
end
to draw-exit2
set goal-x -1
set goal-y 30
ask patch goal-x goal-y [
sprout 1 [ set pcolor red
set shape "square"
]
]
end
to draw-exit3
set goal-x -30
set goal-y -6
ask patch goal-x goal-y [
sprout 1 [ set pcolor red
set shape "square"
]
]
end
to draw-exit4
set goal-x 30
set goal-y 4
ask patch goal-x goal-y [
sprout 1 [ set pcolor red
set shape "square"
]
]
end
;;;;;;;;;;;;;;;;;;;;;;exit goals;;
to move-to-exit1
facexy -1 30
end
to move-to-exit2
facexy 30 4
end
to move-to-exit3
facexy -1 -30
end
to move-to-exit4
facexy -30 -6
end
;;;;;;;;;;;;;;;;;;;;;;;;;;
;to follow-leader
;if not leader? ;; we only want to ask non-leaders
;[let nearby-leaders turtles with [leader? and distance myself < 3] ;; find nearby leaders
;if any? nearby-leaders ;; to avoid 'nobody'-error, check if there are any first
; [ set heading (towards min-one-of nearby-leaders [distance myself]) ]] ;; then face the one closest to myself
; end
;;;;;;;;;;;;;;;;;;;;;;;;;;
to follow_leader
let nearby-leaders turtles with [is-leader? and distance myself < 10] ;; nearby leaders
if any? nearby-leaders[
face min-one-of nearby-leaders [distance myself]
fd 0.5]
end
In the future, please only provide relevant code. For NetLogo, that is usually the procedure that throws the error (or doesn't work correctly etc) and whichever procedure calls it.
In your case, this is the procedure you mentioned:
to follow_leader
let nearby-leaders turtles with [is-leader? and distance myself < 10]
if any? nearby-leaders[
face min-one-of nearby-leaders [distance myself]
fd 0.5]
end
So the and that is throwing the error must be [is-leader? and distance myself < 10] (also, please state which line if you know it). My guess would be that you haven't initialised the variable is-leader? so it is 0 (the default value) instead of either true or false.
If this is the problem, wherever you creates turtles, initialise with set is-leader? false

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

Changing patch colors except one color

I'm creating a program in Netlogo which has shoppers (turtles) moving through a grocery store layout. When they step on a patch it increases in color and when it has no agent on it, it decreases in color, as this will show the paths shoppers take through a store.
My code is:
ask turtles
[ rt random 360
fd 1
set pcolor pcolor + 1 ]
ask patches with [ (pcolor > 9.9) or (pcolor < 0.1) ]
[set pcolor 0]
ask patches with [ (count turtles-here = 0) and (pcolor <= 9.9) and (pcolor > 0) ]
[ set pcolor pcolor - 0.1 ]
However, as the aisle patches are blue this is turning them back to black as well. I was wondering what code I could use so patches with pcolor = 105 will stay blue and not change to black?
Don't change the color of the patches with pcolor = 105. You'll just need to add an additional condition to anywhere you modify the patch color.
ask turtles
[ rt random 360
fd 1
if pcolor != 105[set pcolor pcolor + 1 ]
]
ask patches with [ pcolor != 105 and ((pcolor > 9.9) or (pcolor < 0.1))]
[set pcolor 0]
ask patches with [pcolor != 105 and (count turtles-here = 0) and (pcolor <= 9.9) and (pcolor > 0) ]
[ set pcolor pcolor - 0.1 ]

Netlogo Sprouting turtles at regular intervals

I want to place turtles on each of the black patches(below Figure) at a specified step size:
Therefore if step size less more turtles will be created/sprouted and more step size will result in less turtles.
Code I use right now:
ask patches with [pcolor = black][sprout-dead-turtles wall-agents [set color red]]
This gives the following result:
Previous question asked on same lines:Netlogo Sprouting turtles spaced at less than one patch
Here:
to fill-wall [ d ]
set d precision d 1 ; make sure d is a multiple of 0.1
let n precision (d / 0.1) 0 ; interval at which to hatch
ask one-of possible-next-patches [
sprout 1 [
hatch 1
let i 0
let next-patch my-next-patch
while [ next-patch != nobody ] [
face next-patch
while [ patch-ahead 0.55 != nobody and [ pcolor ] of patch-ahead 0.55 = black ] [
fd 0.1
setxy precision xcor 1 precision ycor 1 ; avoid floating point imprecisions
set i i + 1
if i mod n = 0 [ hatch 1 ]
]
set next-patch my-next-patch
]
die
]
]
end
to-report possible-next-patches
let empty-black-patches patches with [ pcolor = black and not any? turtles-here ]
report empty-black-patches with [
count neighbors4 with [ member? self empty-black-patches ] = 1
]
end
to-report my-next-patch
report one-of possible-next-patches with [ member? self [ neighbors4 ] of myself ]
end
Here is how you would use it:
to setup
ca
; draw the background:
ask patches with [ abs pxcor != max-pxcor and abs pycor != max-pycor ] [ set pcolor grey ]
ask patches with [ pycor = max-pycor and abs pxcor <= 1 ] [ set pcolor white ]
set-default-shape turtles "circle 2"
fill-wall 0.3
end
Constraints:
d has to be a multiple of 0.1
world wrapping needs to be turned off