Restrict Turtle Movement to Boundary in NetLogo - gis

In NetLogo, I'm looking to
have turtles sprout randomly within their boundaries on an imported shapefile and
restrict their movement to these boundaries
Currently, I have them sprouting at the centroid, but quickly they spread across the map. How can I incorporate the shapefile into the turtle's movement? Ideally, they would travel slightly outside their boundary, but I'm sure that's easy to manipulate after I restrict them. The turtles need to be able to interact with eachother across borders, just not travel across the whole map.
See this image for reference:
MSOA File
A separate solution could be to restrict them within a radius of where they sprouted, but unsure of how to do that too.
I've considered:
creating different breeds, but it's much too tedious considering there must be a simpler solution
creating different colored patches and restricting their movement to their assigned-color patch, but that then limits their interaction (and again way too tedious)

What you want to do is to give patches two variables:
One that identifies which region they belong to (I assume this already exists in your code, given that you import a shapefile);
Another one that identifies the patch's own region + regions that are close enough that a turtle from the other region might move there.
I'll call the first variable region and it will be an integer, the second variable allowed and it will be a list of integers.
The idea is that turtles, each of which has its own my-region value based on where it originated from, will look at the allowed patch-variable (and not at region) to see where they can move. That way, each turtle will be able to move to any patch belonging to its own region + to those patches that are close enough to its region (according to a value that you specify, which here I called buffer-range).
This is the logic.
The code below implements it, the relevant part being to create-buffers - which I commented in-code.
Then, to go uses this new information to determine the potential patches where the turtle can move, including those outside its region but close enough (you did not share how your turtles move, but it should be easy to apply the same condition to whatever procedure you are implementing).
; Untick the 'World wraps horizontally' box in Interface > Settings.
globals [
buffer-range
]
patches-own [
region
allowed
]
turtles-own [
my-region
]
to setup
clear-all
create-regions
create-buffers
populate-world
end
to create-regions
ask patches [
ifelse (pxcor < 0)
[set region 1
set allowed (list region)
set pcolor 43]
[set region 2
set allowed (list region)
set pcolor 63]
]
end
to create-buffers
set buffer-range 3
; First, each patch targets the patches that belong to another region which is near enough according to
; the buffer value, and it also creates a local empty list that will be used by those patches.
; Then the patch ask those target patches, if any, to check if their region has not been recorded already
; as a region allowed for movement or as such a candidate. If that's the case, that region is added to
; the allowed-candidates list.
ask patches [
let target-patches (patches with [region != [region] of myself] in-radius buffer-range)
let allowed-candidates (list)
if (any? target-patches) [
ask target-patches [
if (not member? region [allowed] of myself) and (not member? region allowed-candidates) [
set allowed-candidates (lput region allowed-candidates)
]
]
]
; Now, each element of the allowed-candidates list is added to the allowed list.
foreach allowed-candidates [x -> set allowed (lput x allowed)]
]
end
to populate-world
create-turtles 10 [
setxy random-xcor random-ycor
set my-region region
set color pcolor + 2
]
end
to go
ask turtles [
move-to one-of patches with [member? [my-region] of myself allowed]
]
end
For future questions, please share what you have / what you did (check here and here)!
Ideally, you should provide an example of your problem that is like the code part of my answer: you can copy-paste it in NetLogo and you have a workable example.

Related

How to create a network of neighbors?

Hi I am new to netlogo with no programming background,
I am trying to create a network of "neighbours" , using GIS extension ,
so far I'm using in-radius function but i am not sure if it's the one that is suitable.
since i don't understand the unit of radius in Netlogo
here's the code :
to setup
clear-drawing
clear-all
reset-ticks
; zoom to study area
resize-world 00 45 0 20
set-patch-size 20
; upload city boundries
set mosul-data gis:load-dataset"data/MosulBoundries.shp"
gis:set-world-envelope gis:envelope-of mosul-data
gis:apply-coverage mosul-data "Q_NAME_E" neighbor
to Neighbour-network
;; set 7 neighbour agents inside the city
ask turtles [
let target other turtles in-radius 1
if any? target
[ask one-of target [create-link-with myself]]
]
print count links
I want for each neighberhood neighbor each agent is linked to the 7 nearst neighbors.
my guess is that in the line if any? target something has to change , but all my attempts are useless so far.
Thank in advance
I am unclear how GIS relates to this question and you haven't provided the code for creating the agents so I can't give a complete answer. NetLogo has a coordinate system, automatically built in. Each agent has a position on that coordinate system and each patch occupies the space 1 unit by 1 unit square (centred on integer coordinates). The in-radius and distance primitives are in the distance units.
However, if all you want to do is connect to the 7 nearest turtles, you don't need any of that because NetLogo can simply find those turtles directly by finding those with the minimum distance to the asking turtle. This uses min-n-of to find the given number of turtles with the relevant minimum, and distance [myself] for the thing to minimise. The whole thing, including creating the links with the generated turtleset, can be done in a single line of code.
Here is a complete model to show you what it looks like:
to testme
clear-all
create-turtles 100 [setxy random-xcor random-ycor]
ask n-of 5 turtles
[ create-links-with min-n-of 7 other turtles [distance myself]
]
end
Sarah:
1) This helped me understand the use of 'in-radius' in NetLogo (or the unit of radius): When you use 'in-radius 1' in a patch-context, 5 patches will be selected (patch where the asking turtle is located and four neighbors, not all 8 neighboring patches).
2) Consider using 'min-one-of target [ distance myself ]' instead of 'one-of target'.
min-one-of: http://ccl.northwestern.edu/netlogo/docs/dict/min-one-of.html
distance myself: http://ccl.northwestern.edu/netlogo/docs/dict/distance.html
to Neighbour-network
; set 7 neighbour agents inside the city
ask turtles [
let target other turtles in-radius 1
let counter 0
while [ count target > 0 and counter < 8 ]
[ ask min-one-of target [ distance myself ] [
create-link-with myself
set counter counter + 1
]
]
show my-links
]
3) Consider exploring Nw extension: https://ccl.northwestern.edu/netlogo/docs/nw.html

How to create certain number of turtles on the road shapefile and specific area?

I have imported the road shapefile and polygon shapefile and already intersected with patches. I want to create a certain number of turtles in a specific polygon and turtles allow moving only on the road shapefile.
For example, In polygon A have turtles 20, polygon B have turtles 30 and I want to let the number of these to show up on its own polygon.
What I have done is I have intersected the road and the polygon by using (I mean 2 foreach for road and polygon)
foreach gis:feature-list-of x
[ vector-feature ->
ask patches gis:intersecting vector-feature
[blah blah blah...]
]
Then, I created turtles using this code
to setup
create-walkers Population
ask walkers
[
set wlocation one-of patches with [pcolor = red]
;I have assigned the road shapefile to red color.
move-to wlocation
end
With only patches is RED, it's work,
BUT when I tried to set wlocation one-of patches with [pcolor = red and pcolor = ;another color which is the color of polygon]
Netlogo got an error that
move-to expected input to be an agent but got nobody instead
How could I solve this problem?
Thank you in advance.
You appear to have a basic misunderstanding about patches. In NetLogo, a patch is an area of the world and is a single unit. What that means is that the patch can ONLY have one colour. Try running this code (as a new model)
to testme
ask patches
[ set pcolor random 256
]
end
You can see the patches, each with a randomly allocated colour. Now type inspect one-of patches in the command centre and an inspect window will open for a randomly selected patch. You will see that one of the listed variables is pcolor.
Short answer - a patch can only have one colour, so your request for patches with colour A and colour B is meaningless.

Netlogo - Importing a Shapefile and counting the number of entries on each patch

I have a shapefile containing the location of several thousands of people. I want to import this and for each patch, I'd like to count the amount of people on this exact patch (each person is an entry in the shape file, but multiple people can be located on the exact patch depending on my world size).
I have managed to do so using the following code:
set population-here 0
let population-dataset gis:load-dataset "population_file.shp"
foreach gis:feature-list-of population-dataset [a ->
ask patches gis:intersecting a [
set population-here population-here + 1]
However, it takes several hours to load the dataset in a world of -300 to 300 pixels. Is there a faster way of counting the number of individual entries for each patch?
The population should be placed on an underlying shapefile of an area. This area is imported as following:
let area-dataset gis:load-dataset "area.shp"
ask patches [set world? false]
gis:set-world-envelope gis:envelope-of area-dataset
ask patches gis:intersecting area-dataset
[ set pcolor grey
set world? true
]
Okay, I can't test this and I'm not entirely confident in this answer as I use GIS very rarely. But I suggest you adapt the code in the GIS general examples in the NetLogo model library (see File menu). What you appear to want to do is create a turtle breed for your people, and create a person at each location where there is a person in your population dataset.
breed [people person]
patches-own [population]
to setup
< all the other stuff you have >
let population-dataset gis:load-dataset "population_file.shp"
foreach gis:feature-list-of population-dataset
[ thisFeature ->
[ let location gis:centroid-of (first (first (gis:vertex-lists-of thisFeature )))
create-people 1
[ set xcor item 0 location
set ycor item 1 location
]
]
]
ask patches [ set population count people-here ]
end
You can also import other variables from the population set (eg gender or age group) and have those variables transfer to appropriate attributes of your NetLogo people.
If you haven't found it yet, I recommend this tutorial https://simulatingcomplexity.wordpress.com/2014/08/20/turtles-in-space-integrating-gis-and-netlogo/.
Note that this assumes there is a reason why you want the people in the correct (as defined by the GIS dataset) position for your model rather than simply having some sort of population count (or density) in your GIS file and then create the people in NetLogo on the correct patch.

Netlogo - GIS Movement History of Turtles

I've got a Netlogo model running on a couple of raster layers which I imported using the GIS extension. All good so far
Next, I'd like to record and export the movement history of my turtles (in real world coordinates), along with the turtle number and the tick number.
I've looked at writing out xcor and ycor but that's not much help as I need GIS locations. I've also looked at hatching a separate breed (tracker) at each turtle location to store the locations and then exporting the tracker breed at the end using gis:store-dataset. But this substantially reduces the run speed of the model, even for a relatively small number of turtles, to the extent that it's almost unusable. I also can't figure out how to get the turtle number into the tracker breed.
Does anyone have any bright ideas of alternative faster approaches and which also include the turtle number?
Key elements of existing code are
breed [ tracker trackers ]
trackers-own [ tick_no ]
ask turtles [
my-move-turtles-routine
hatch-trackers 1 [
set hidden? true
set tick_no ticks
]
]
gis:store-dataset gis:turtle-dataset trackers "tracking"
Many thanks
What kind of output are you looking for? Are you writing this out to a csv?
I think this to-report procedure outputs what you need (when called by a turtle) but you will likely have to modify it or split it into multiple pieces depending on your desired output format.
to-report turtle-coords-who-tick
let t_env gis:envelope-of self
let x first t_env
let y last t_env
let me who
report ( list x y me ticks)
end

NetLogo GIS: How to create random points on Map

I have loaded a shapefile of certain city into Netlogo successfully using the GIS extension as shown below.
with the following code
extensions [ gis ]
globals [ countries-dataset min-map max-map]
patches-own [ mapa ]
to setup
reset-ticks
clear-turtles
clear-patches
clear-drawing
clear-all-plots
clear-output
; Note that setting the coordinate system here is optional, as
; long as all of your datasets use the same coordinate system.
; Load all of our datasets
; Load the dataset
set countries-dataset gis:load-dataset "city.shp"
gis:set-world-envelope (gis:envelope-union-of (gis:envelope-of countries-dataset))
gis:set-drawing-color green
gis:draw countries-dataset 1
reset-ticks
end
to match-cells-to-patches
cd
ct
end
to startup
setup
end
How does one create random turtles along roads only?
Do you only want turtles to be along the roads, or you also want them to move along the roads?
For the former, try gis:find-features and gis:vertex-lists-of to get all vertices in the graph, and choose a random one (this will limit the location to vertex only). If you want location to be somewhere on the straight line section, you can calculate a point between two consecutive vertices.