Netlogo: Ask specific turtle to do something - netlogo

I'm trying to simulate a car factory using robots called carriers. In my go method i'm trying to ask one carriers who is not on a job to find a cutter and go to it.
How do I ask the specific carrier to do something?
This is what i've done so far:
metal-sheets
cut-sheets
standard-skeleton
finished-standard-skeleton
prestige-skeleton
finished-prestige-skeleton
]
breed[carriers carrier]
turtles-own [
on-job?
]
patches-own [
processing-time
machine-type ;;cutter, standard-welder, prestige-welder, riveter
status ;;import, export, pending
]
to setup
set-default-shape carriers "circle"
create-carriers number-of-carriers
[set color grey
set on-job? false]
setup-patches
reset-ticks
end
to setup-patches
ask patches [
if pxcor = 1 and pycor = 1 [set machine-type "cutter"]
if pxcor = 1 and pycor = 5 [set machine-type "standard-welder"]
if pxcor = 5 and pycor = 1 [set machine-type "prestige-welder"]
if pxcor = 5 and pycor = 5 [set machine-type "riveter"]
if machine-type = "cutter"
[set pcolor red
set status "import"]
if machine-type = "standard-welder"
[set pcolor green
set status "import"]
if machine-type = "prestige-welder"
[set pcolor blue
set status "import"]
if machine-type = "riveter"
[set pcolor yellow
set status "import"]
]
end
to Go
let cutter patches with [machine-type = "cutter"]
let standard-welder patches with [machine-type = "standard-welder"]
let prestige-welder patches with [machine-type = "prestige-welder"]
let riveter patches with [machine-type = "riveter"]
let free-carriers carriers with [in-job? false]
ask free-carriers [
;;on-job is a carrier-owned variable
ask cutter [
if status = "import" [
set status "pending"
face cutter ;; I want the carrier to face the cutter and move towards it
fd 1
]
]
]
end```

I'm assuming that you want free-carriers to find a cutter patch with status "import", have that cutter change its status, and then have the carrier move toward it.
ask free-carriers [
;;on-job is a carrier-owned variable
let my-cutter one-of cutter with [status = "import"]
if my-cutter != nobody [
ask my-cutter [ set status "pending" ]
face cutter ;; I want the carrier to face the cutter and move towards it
fd 1
]
]
Since each free-carrier does this in (random) turn, no two free-carriers should go to the same cutter. This will choose an eligible cutter randomly. You could have free-carriers go to the nearest one if you wished.

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

How to force turtles to move only in a half of the world?

I created a world divided in two parts with the command ask patches with [ pxcor < 0] [set pcolor blue] ask patches with [pxcor > 0] [set pcolor green] .
In one of this there're 100 turtles, who 5 are infected and the same in the other part.
My problem is to force the turtles to move only in their part of the world. So I want that turtles with pcolor = blue move randomly in the second and third quadrant (pxcor <0) and turtles with pcolor = green in the first and fourth quadrant (pxcor> 0). how do I do?
This is the code:
turtles-own
[
sick?
sick-time]
to setup
ca
ask patches with [ pxcor < 0 ] [set pcolor blue] ; we want divide the world in two parts: the blue one in the north of Italy
ask patches with [pxcor > 0 ] [set pcolor green]; the white one is the south of Italy
ask patches with [pxcor = 0 ] [set pcolor white ] ; represent the border
create-turtles 200 ; we create a population made up for 200 people
[ set size 1
set shape "person"
set sick-time 0
get-healthy]
ask n-of 100 turtles ; 100 of this one live in north of Italy
[setxy 12 random-ycor ]
ask n-of 100 turtles ; another 100 in the south
[setxy -12 random-ycor
]
ask n-of 5 turtles with [pcolor = blue] ; we want infect 5 people for each world
[get-sick ]
ask n-of 5 turtles with [pcolor = green]
[get-sick ]
reset-ticks
end
to get-healthy
set sick? false
set color white
end
to get-sick
set sick? true
set color yellow
set shape "circle"
set sick-time sick-time + 1
end
to go
ask turtles
[
move ]
tick
end
to move
rt random-float 360
fd 1
end
Your movement procedure looks like:
to move
right random-float 360
forward 1
end
If you want them to just stay where they are if moving would take them into the wrong half, then you can use patch-ahead to test the patch they'd be moving to. I think what you want is that they don't go to a different coloured patch. One way is:
to move
right random-float 360
if [pcolor] of patch-ahead 1 = pcolor [forward 1]
end
[pcolor] of patch-ahead 1 returns the colour of the patch that is one distance unit ahead, so where the turtle is trying to move to. pcolor is the colour of the patch that the turtle is currently standing on.

Netlogo: agentset of patches to single agent

I have a list of turtles (carriers) that i'm trying to narrow down in my go method.
Here is my program:
globals [
metal-sheets
cut-sheets
standard-skeleton
finished-standard-skeleton
prestige-skeleton
finished-prestige-skeleton
]
breed[carriers carrier]
turtles-own [
on-job?
]
patches-own [
processing-time
machine-type ;;cutter, standard-welder, prestige-welder, riveter
status ;;import, export, pending
]
to setup
clear-all
set-default-shape carriers "circle"
create-carriers number-of-carriers ;;number-of-carriers is a slider
[set color grey
set on-job? false
setxy random-xcor random-ycor]
setup-patches
reset-ticks
end
to setup-patches
ask patches [
if pxcor = 1 and pycor = 1 [set machine-type "cutter"]
if pxcor = 1 and pycor = 5 [set machine-type "standard-welder"]
if pxcor = 5 and pycor = 1 [set machine-type "prestige-welder"]
if pxcor = 5 and pycor = 5 [set machine-type "riveter"]
if machine-type = "cutter"
[set pcolor red
set status "import"]
if machine-type = "standard-welder"
[set pcolor green
set status "import"]
if machine-type = "prestige-welder"
[set pcolor blue
set status "import"]
if machine-type = "riveter"
[set pcolor yellow
set status "import"]
]
end
to Go
let cutter patches with [machine-type = "cutter"]
let standard-welder patches with [machine-type = "standard-welder"]
let prestige-welder patches with [machine-type = "prestige-welder"]
let riveter patches with [machine-type = "riveter"]
let free-carriers carriers with [on-job? = false]
let closest-carrier min-one-of free-carriers [distance cutter] ;;Distance expects agent, got agentset
ask closest-carrier [
set color green
]
end
I'm getting an error on the line let closest-carrier min-one-of free-carriers [distance cutter] saying that distance expected a agent but got a agentset although there only is one agent in the set.
The full error message is:
DISTANCE expected input to be an agent but got the agentset (agentset, 1 patch) instead.
How do narrow down the patches to one patch?
It doesn't matter if there happens to be only one agent in the agentset, it is still an agentset. Put a one-of in front, which selects one agent from the agentset and therefore changes the way the code interprets the code

Only observer can ask all turtles - NetLogo error

I am new user for Netlogo,
Although when I checked the code, there are no issues. However, when i run the program a while, it throws error message
Only the observer can ASK the set of all turtles. Error while turtle 0 running ASK. called by procedure EXIT called by procedure Customer, GO and by button 'go'
to go
customer
end
to setup-turtles
create-turtles 1
ask turtles
[
set shape "person"
set size 3
set heading -90
fd 10
setxy 15 -15
set color red
]
end
to customer
ask turtles
[
set products ( products )
rt (random 360)
fd 1
if patch-here = one-of patches with
[
pcolor = green
]
[
set pcolor orange
set products (products + 1)
]
if patch-here = one-of patches with
[
pcolor = gray
]
[exit]
show count patches with
[pcolor = green ]
move-to one-of patches with
[
pcolor = black
]
]
end
to exit
ask turtles
[
ifelse patch-here = one-of patches with
[
pcolor = gray
]
[ifelse count products >= 2
[
die
]
[move-to one-of patches with
[
pcolor = green or pcolor = black
]
]
]
[
die
]
move-to one-of patches with
[pcolor = green
]
move-to one-of patches with
[pcolor = black
]
]
end
In your customer procedure, you have
ask turtles
[ ...
if patch-here = one-of patches with [pcolor = gray]
[ exit ]
...
]
So the exit procedure is being called by any turtle that is on a gray patch. Each turtle that meets that condition enters the exit procedure. As soon as it enters that procedure, the first command (in the exit procedure) is to ask turtles. So a turtle is asking all turtles to do something.
This is explicitly banned by the NetLogo language partly because it is a common source of beginner errors and is generally both unnecessary and inefficient. You have already selected the turtle to exit, what is it that this particular turtle needs to do to actually exit. It is very unlikely that they need to identify all the turtles on gray patches.

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