How to randomly remove block side in a grid? - netlogo

Following on from How to control square size in a grid from square area?, I would like to randomly remove block side in a grid. Here my idea:
%% In a first time, I randomly select a white patch in the grid (figure below):
let random-patch one-of patches with [pcolor = white]
%% Then, I draw red patches to identify intersections between white roads (figure below):
ask patches with [ (pxcor mod (side + 1) = 0 ) and (pycor mod (side + 1) = 0 ) ] [set pcolor red]
%% Finally, I color in brown all white patches that are situated between two red patches and on the same side than the random patch. To identify these white patches, have I to calculate distance between the random patch and the nearest red patches and to color all white patches situated along these distances ?
Thanks in advance for your help.

An alternative way to think about your problem is in terms of finding clusters of white patches: you're picking a white patch at random, and you want to turn all the contiguous white patches to brown.
You can look at the "Patch Clusters Example" in the Code Examples section of NetLogo's model library to see one way to do this.
Here is how I would do it. Let's start by defining a grow-cluster reporter:
to-report grow-cluster [ current-cluster new-patch ]
let patches-to-add [
neighbors4 with [
pcolor = [ pcolor ] of myself and
not member? self current-cluster
]
] of new-patch
let new-cluster (patch-set current-cluster new-patch)
report ifelse-value any? patches-to-add
[ reduce grow-cluster fput new-cluster sort patches-to-add ]
[ new-cluster ]
end
The code may be hard to understand if you're not used to functional programming because it uses recursion within a call to reduce. Still, in a nutshell, it:
takes an existing patch cluster and a new patch to add to this cluster
looks for neighbors of that new patch that should also be added to the cluster, i.e., those that are the same color but not already part of the cluster
calls itself for these new patches to add so that their neighbors can be added to the cluster (and those neighbors' neighbors, etc.) until no new patches are found.
Once you have grow-cluster, you can use it to accomplish exactly what you want by seeding it with an empty cluster and the random patch that you selected:
to remove-random-side
let random-patch one-of patches with [pcolor = white]
let side grow-cluster no-patches random-patch
ask side [ set pcolor brown ]
end
Caveat: for this to work, world wrapping has to be disabled in your model settings.

Since you're making a uniform grid, you might consider just doing math on pxcor and pycor instead of taking the Patch Clusters Example approach. That approach is best suited to dealing with irregular shapes.
To set up your grid, you can just do:
ask patches [
set pcolor brown
let horizontal-street? pycor mod (side + 1) = 0
let vertical-street? pxcor mod (side + 1) = 0
if horizontal-street? or vertical-street?
[ set pcolor white ]
if horizontal-street? and vertical-street?
[ set pcolor red ]
]

Related

Divide regions accordingly to physical features

I'm working on a smaller project and got stuck on an issue, I'm not really sure if it's possible to solve it in NetLogo but I want to give StackOverflow a go!
I got a model that divides the world into different parts and randomly add physical features (such as rivers). If a feature goes through the whole region, I want it to separate the region and make into two regions. As an example, in the picture below, I want to separate the purple region into two unique regions accordingly to the physical feature (black).
The code I used to generate the picture above, can be found below.
to setup
ca
;Setting world.
resize-world 0 19 0 19
;Creating regions.
let x 5
let y 5
let col 45
while [y <= max-pycor + 1][
while [x <= max-pxcor + 1 ][
ask patches with [pxcor < x and pxcor >= x - 5 and pycor < y and pycor >= y - 5][
set pcolor col
]
set x x + 5
set col col + 10
]
set x 5
set y y + 5
]
;Generating physical features.
ask n-of 5 patches[ sprout 1[
set pcolor black]
]
let i 0
while [ i < (max-pycor * 2 )][
ask turtles [
fd 1
set pcolor black
ifelse (random 20 <= 1)
[
rt one-of [-90 0 90]
forward 1
]
[
fd 1
set pcolor black
fd 1
set pcolor black
]
set pcolor black
set i i + 1]
]
ask turtles [die]
end
My strategy for handling this is to realize that all we really need to do is "flood" a patch out by color and tag all the found adjacent patches, then repeat for any un-tagged, non-black patches until they are all done.
NetLogo does not have a "flood" command to get all patches adjacent to a patch meeting a criteria, so we make a special reporter of our own to handle it, patches-adjacent. Then it's just easy to ask those patches-adjacent to set their region to the currently chosen region.
I don't love this code, it's a little finicky and would be prone to infinite loops if tweaked incorrectly, but it should work. I bet there is a cleaner way to do this that I'm not thinking of at the moment.
; add a variable to track the different regions
; the default value will be `0` for each patch when `clear-all` is called
patches-own [ region ]
to set-regions
let current-region 1
; only act on non-black patches that haven't yet been assigned a region
let untagged patches with [ region = 0 and pcolor != black ]
while [any? untagged] [
ask one-of untagged [
ask patches-adjacent [
set region current-region
]
]
; update the region and the untagged patches we have left to process
set current-region current-region + 1
set untagged patches with [ region = 0 and pcolor != black ]
]
; this is just to get a view of the regions to quickly see if our code worked, it can be removed
ask patches [ set plabel region ]
end
to-report patches-adjacent
report patches-adjacent-ex (patch-set self) pcolor
end
to-report patches-adjacent-ex [found pc]
let newly-found neighbors4 with [ (not member? self found) and pcolor = pc and region = 0 and pcolor != black ]
set found (patch-set found newly-found)
ask newly-found [
; use recursion to find the patches adjacent to each newly-found one
; relying on updating the `found` agentset as we go to avoid duplicates
; or looping forwarder
set found (patches-adjacent-ex found pc)
]
report found
end
I solved this by using the Patch Clusters model that can be found in the NetLogo model library.

Adding obstacles and targets from shapefile in netlogo

I am new to NetLogo, so my apologies in advance if that question is very stupid. I would like to create an Agent-Based Model where animals move around in a complex terrain looking for water sources. Movement should be downhill, constrained by steep slopes (>25°) and targets should be lakes. I am using a real-world example from GIS data for this, and I have already managed to setup a world containing an ASCII elevation grid, a shapefile containing lines that represent slopes steeper 25degrees and a shapefile containing areas representing lakes. I have created animals (cows) and found a code line telling them to move downhill. Now, I would like to tell them
a) to avoid slopes >25° by using the slope shapefiles as obstacles and
b) to go to the lakes by using the lake shapfiles as targets
Can someone help me how to code this?
Many thanks in advance!
Here is the code I have put together so far
breed [ cows cow ]
extensions [ gis ]
patches-own [ elevation ]
globals [
slope-dataset
lake-dataset
elevation-dataset
]
to setup-terrain
clear-all
reset-ticks
set slope-dataset gis:load-dataset "FILENAME.shp" ;;extent of GIS datasets is N42.3-43.4 and W120.0-121.1
set lake-dataset gis:load-dataset "FILENAME.shp"
set elevation-dataset gis:load-dataset "FILENAME.asc"
gis:set-world-envelope gis:envelope-of slope-dataset
gis:set-world-envelope gis:envelope-of lake-dataset
gis:set-world-envelope gis:envelope-of elevation-dataset
end
to display-slopes
gis:set-drawing-color red
gis:draw slope-dataset 0.5
end
to display-lakes
gis:set-drawing-color blue
gis:draw lake-dataset 2
end
to display-elevation-in-patches
gis:apply-raster elevation-dataset elevation
let min-elevation gis:minimum-of elevation-dataset
let max-elevation gis:maximum-of elevation-dataset
ask patches
[ ; note the use of the "<= 0 or >= 0" technique to filter out
; "not a number" values, as discussed in the documentation.
if (elevation <= 0) or (elevation >= 0)
[ set pcolor scale-color black elevation min-elevation max-elevation ] ]
end
to setup-cows
set-default-shape cows "cow"
create-cows 100 [
setxy random-pxcor random-pycor
set size 1
set color white
]
end
to move
move-to patch-here ;; go to patch center
let p min-one-of neighbors [elevation]
if [elevation] of p < elevation [
face p
move-to p ;; makes cows move to the next lower elevation patch, if no lower
elevetion is present, cow doesn't move
]
end
to go
ask cows [
move
]
end
Thanks Seth, I have found the solution using the gis:intersects? primitive you suggested:
to display-slopes
ask patches gis:intersecting slope-dataset
[ set pcolor red ]
end
to go
ask cows
[fd 1
avoid-ostacles]
tick
end
to avoid-obstacles
ask cows [
if [pcolor] of patch-ahead 1 = red
[rt 90 fd 1]]
end
This way I can color the patches in red that intersect with the shapefile containing the slope vector dataset and then ask the cows to avoid and move around red colored patches.
Thanks again!

Create clusters of patches without changing occurrence

I am writing a Netlogo model which only involves patches. I have managed to create a landscape consisting of patches of 6 different colours (each representing a different vegetation in my project) according to probability. So red patches have a probability of 10% to occur on each patch, yellow 5%, brown 20% and so on.
An example of my code where this probability is set up:
let i random-float 1
ifelse i + random-float 0.1 <= 0.8 ;random 0.1 threshold for environmental noise
[ set pcolor green ]
[ ifelse i + random-float 0.1 <= 0.9
[ set pcolor yellow ]
[ set pcolor blue ] ]
However, this creates a random pattern for each colour. But I would like to create a clustered spatial pattern for one of them.Specifically, in my landscape, I want the proportion of brown patches to be 50%. But if I were to set this 50% probability for every patch, the brown patches will be randomly distributed. How do I get it to occupy 50% of my landscape, but appear in a clustered pattern?
I tried creating the clustered pattern using the Moore neighbourhood, but that obviously changes the proportion of brown patches.
I hope this is somewhat clear. Thanks for any help in advance.
You could seed based on your weights and then grow around the seeds. Here is a different approach: color all patches based on your weights, and then cluster the colors.
extensions [rnd] ;use the rnd extension
globals [threshold]
to setup
ca
set threshold 2
let _cw [[red 10] [yellow 20] [blue 70]] ;colors with weights
ask patches [set pcolor first rnd:weighted-one-of-list _cw [last ?]]
repeat 20 [cluster] ;adjust to taste
end
to cluster
ask patches [
if unhappy? [
swap-pcolor
]
]
end
to swap-pcolor
let _c pcolor
let _p one-of neighbors with [pcolor != [pcolor] of myself]
set pcolor [pcolor] of _p
ask _p [set pcolor _c]
end
to-report unhappy?
let _ct count neighbors with [pcolor = [pcolor] of myself]
report (_ct < threshold)
end

How to replace color with another color in a polygon?

I have patches as follows:
I would like to color white patches as in this figure:
Here my code to color white patches in blue:
ask patches with [pcolor = white] [
if any? neighbors with [pcolor = blue] [set pcolor blue] ]
But the problem is that I obtain this figure: .
Thanks in advance for your help.
Alan is right about the cause of your problem, but you don't need to create a next-pcolor patch variable. If you put both conditions inside the with block, NetLogo will first construct the agentset of patches, and then ask these patches to do stuff, thereby avoiding the timing problem that you had. Let's try it. And since you're obviously going to have to do it with both blue and cyan patches, let's build a more general version:
to color-white-patches-v1 [ c ]
ask patches with [ pcolor = white and any? neighbors with [ pcolor = c ]] [
set pcolor c
]
end
That you can then call with:
color-white-patches-v1 cyan
color-white-patches-v1 blue
Here is the result:
But it's not quite what you wanted. That's because neighbors gives you all 8 neighbors of a patch. Let's try with neighbors4 instead:
to color-white-patches-v2 [ c ]
ask patches with [ pcolor = white and any? neighbors4 with [ pcolor = c ]] [
set pcolor c
]
end
Not quite there yet. It think you are going to have to resort to something like patch-at. In this example, I look at only the patch above:
to color-white-patches-v3 [ c ]
ask patches with [ pcolor = white and [ pcolor ] of patch-at 0 1 = c ] [
set pcolor c
]
end
I'm not sure if that was exactly what you wanted and how well that applies to your general problem, but with some combination of patch-at, you should be able to get what you need.
The problem arises because patches are being changed sequentially, so some white patches find they have new blue neighbors by the time they respond to ask.
ask patches with [pcolor = white and any? neighbors with [pcolor = blue]] [set pcolor blue]
Note: this answer is edited in response to Nicholas's observation that with will create the entire agent set before ask is called. However I stuck with neighbors since that's how I read the question.

To build patch clusters at large spatial scales

I used the code from How to create cluster patches that do not overlap between them to build patches as shown in the first figure below.
Here is the code :
to make-cluster
loop [
let cluster [patches in-radius (2 + random-float 2)] of one-of patches
if all? (patch-set [neighbors] of cluster) [pcolor = black] [
ask cluster [ set pcolor green ]
stop ] ]
clear-all repeat 20 [ make-cluster ]
When I use this code in a large spatial extent (i.e. 1000 x 1000 patches with patch size = 1 pixel), green patches are like circles (see the second figure below).
How can I have patches as shown in the first figure ?
Thank you very much for your help.
If your goal is to simply have heterogeneous regions (rather than specifically blocky, symmetric things), you might play around with some of the answers here: Creating a random shape (blob) of a given area in NetLogo
Frank's solution and my first solution will probably run pretty slow on that large of a world. I just added a solution that should scale to a world of your size. I've put it here too for convenience:
to make-blob [ area x y ]
let blob-maker nobody
crt 1 [ set blob-maker self setxy x y ]
let border patch-set [ patch-here ] of blob-maker
repeat area [
ask blob-maker [
ask min-one-of border [ distance myself ] [
set pcolor green
set border (patch-set border neighbors4) with [ pcolor = black ]
]
rt random 360
fd .8
]
]
ask blob-maker [ die ]
end
That said, if you like the blockiness, it's often the case that models with a large number of patches in a blocky formation can be reworked into models with a smaller number of patches that behave quite similarly. For example, one strategy is to scale down the size and movements of the turtles so that the world is still relatively large to them.