Spatial Autocorrelation in NetLogo - 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

Related

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

How to make turtles move in one color in Netlogo

I start developping with Netlogo and I face the problem that I want to make all Turtles moving in one way which has the Black Color. How can I do that ? I tried with patch-ahead but I didn't success.
Anyone have a solution ? I will be grateful.
to setup
clear-all
import-drawing "patch.png"
create-turtles 10 [set xcor -10 set ycor -13]
ask turtles [set color white]
ask turtles [set shape "bug"]
reset-ticks
end
to bouge
ask turtles[
fd 1
]
end
to go
bouge
ask turtles [if [pcolor] of patch-ahead 6 != black [set heading heading - 100] ]
end
enter image description here
You should use something like these:
set black-patches patches with [pcolor = black]
ask turtles [
face one-of black-patches
]
Bests, Ervin

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

Define home area-turtles?

I am very new to netlogo. I have searched every question here before I posted this.
I have the following code which sprouts a given number of horses:
ask n-of Number-horses patches with [grass? = "Yes"]
[sprout-horses 1 [set color 25 ]]
The person can change the number of horses using the slider but I would like each horse to have its own area/range/radius.
They can only move within this radius/area and they cannot meet each other.
From what I've read it's got something to do with the distance function?
You can find a similar problem here which has examples too :
Spacing agents in NetLogo based on territory size
There are several ways that you can assign a territory zone to each horse, but all methods that I know have two steps, first step is in order to make sure initial home area of horses are separated from each other , So we need to create horses only in patches which has a certain distance from another patch which has a horse on it,I did not follow your method that asked patches to sprout horses and instead I created them without asking patches.
I was not sure how you defined grass? Variable for each patch but I have assigned a number of patches with grass? = true and others false.
Second step is to set home-area property of each horse. If initially you moved them far away from each other they will have separate territories.
I have included a few examples here :
First to use in-radius for both steps:
Breed [Horses horse]
Horses-own [home-area]
patches-own [grass?]
globals [Zone-Radius]
to setup
clear-all
reset-ticks
set Zone-Radius 2
ask patches
[
ifelse pxcor mod 5 = 3
[ set Grass? true ]
[ set Grass? false ]
]
create-horses Number-horses
[ Move-to one-of patches with [Grass? and not any? other horses in-radius (Zone-Radius + 1)]
set home-area patches in-radius Zone-Radius
set color 25
]
end
to go
ask horses [
ifelse member? patch-ahead 1 home-area
[rt random 10 fd 1 ] ; move if next patch is in their zone
[rt random 180]
]
tick
end
In this example horses only move in the patches in their radius 2. But you can change that base on your model requirements.
In the second method you can use distance for the first step (finding empty patches with enough distance to current patch) and radius for second one (assigning home-area to each horse).
Move-to one-of patches with [Grass? and not any? other horses with [distance myself < (Zone-Radius + 1)]]
set home-area patches in-radius Zone-Radius
If you use higher distance for finding empty patches you will have completely seprated zones. Finally , you can use distance for both steps:
Move-to one-of patches with [Grass? and not any? other horses with [distance myself < (Zone-Radius + 1)]]
set home-area patches with [distance myself < Zone-Radius]
I just did it another way:
Breed [Horses horse]
Horses-own [home-area]
patches-own [ concession? forest? parks?]
globals [Zone-Radius]
to setup
clear-all
reset-ticks
set Zone-Radius 2
ask n-of 500 patches [ set concession? "No" ]
ask n-of 500 patches[ set forest? "Yes" ]
ask n-of 500 patches[ set parks? "Yes"]
let i 0
while [i < Number-horses]
[
ask one-of patches with [(concession? = "No" or forest? = "YES" or parks? = "YES" ) and (not any? horses in-radius (Zone-Radius + 2) )]
[
sprout-horses 1 [
set home-area patches with [distance myself < Zone-Radius]
let w who
ask home-area [set pcolor red]
set color 25 ]
]
set i (i + 1)
]
end
to go
ask horses [
ifelse member? patch-ahead 1 home-area [rt random 10 fd 1 ] [rt random 180]
]
tick
end
As you can see I used while and a condition to ask patches one by one, I might be mistaken but when I ask all the n-of Number-of-horses patches with [YourCondition][...] I get the wrong results and distance between horses is not effective, maybe they are created all at the same time and therefore upon creating a horse there was no horse nearby!? I am new to these concepts and might be wrong.
This is the code and view for the one which asks patches to create horses at once here :
Breed [Horses horse]
Horses-own [home-area]
patches-own [ concession? forest? parks?]
globals [Zone-Radius]
to setup
clear-all
reset-ticks
set Zone-Radius 2
ask n-of 500 patches [ set concession? "No" ]
ask n-of 500 patches[ set forest? "Yes" ]
ask n-of 500 patches[ set parks? "Yes"]
ask n-of number-horses patches with [(concession? = "No" or forest? = "YES" or parks? = "YES" ) and (not any? horses in-radius (Zone-Radius + 2) )]
[
sprout-horses 1 [
set home-area patches with [distance myself < Zone-Radius]
let w who
ask home-area [set pcolor red]
set color 25 ]
]
end
to go
ask horses [
ifelse member? patch-ahead 1 home-area [rt random 10 fd 1 ] [rt random 180]
]
tick
end