How can I create less than one turtle per point with NetLogo GIS extension? - gis

I have a Shapefile consisting of several points, about 50 000 of them. I would like to generate a certain amount of turtles using those points, but not all of them. The number of turtles to create is defined by a global globals [num-of-turtles], which can be modified on the interface.
By now, I have only managed to create one turtle per point and then killing the extra turtles with the following code (after loading the coordinate system, loading datasets, setting world envelope, etc.):
gis:create-turtles-from-points my-point-map turtles [
set shape "house"
set size 0.30
]
ask n-of (count turtles - num-of-turtles) turtles [die]
However, this seems a bit clumsy to me. Does someone come up with a way of creating just the pre-set amount of turtles?

Related

Restrict Turtle Movement to Boundary in NetLogo

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.

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

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

Sketchup 3D Models, DEM and Netlogo GIS Extension

Bit of a challenge here which I've been grappling with for some time. I'll explain my full work flow so you can reproduce if needed.
I'm creating virtual landscapes in Google SketchUp which I ultimately would like to use in Netlogo to examine how turtles interact with them.
My problem is that by the time I get the landscapes into Netlogo the units don't seem to relate to the original 3D model.
Step 1: Create simple hill on a 50m by 50m square in Sketchup using the Toposhaper extension.
Step 2: Export to .dae file and import into Meshlab, ensure the Meshlab model has the same dimensions as the Sketchup model by adjusting the units with the assistance of the measuring tool. Export from meshlab as .xyz file.
Step 3: Import .xyz file into QGis as points by adding a new layer from delimited file. Selecting field_1 and field_2 as X and Y fields.
Step 4: Create raster of points using Raster > Interpolation > Interpolation. Add field_3 as interpolation attribute, set number of columns to 50 by 50 (to correspond to the 50m x 50m 3D model), adjust cell size X and Y to match to ensure Netlogo will read the resulting .asc file.
Step 5: Finally, I setup a model in Netlogo to receive the raster. Firstly, in model settings I set the the min and max pxor and pycor to 0 and 50. Then, using the Gis Extension, I import the raster apply the z-value to a patch variable called elevation:
to load-gis
set elevation gis:load-dataset "cone_50.asc"
gis:set-world-envelope-ds gis:envelope-of elevation
gis:apply-raster elevation target-elev
end
Now, each patch of my 50 by 50 Netlogo world should have an elevation value taken from my 50 by 50 raster. In theory, adding all the elevation values together should (roughly) give me the total volume of the raised area of the hill? The figure I get is higher however and the problem gets worse with larger volumes.
Can anyone help?