edge to edge distance between polygons that are situated within a buffer - netlogo

I would like to calculate edge to edge distance between a polygon in which is situated a turtle and each polygon that is situated in a radius of 2 km around the polygon in which is situated the turtle. The polygons are represented by different IDs and each polygon is composed of several patches. I obtain a error message with the following code "FOREACH expected this input to be a list, but got an agentset instead". I don't understand why "patches with [plabel = ID-polygon])" is not a list ? In fact, I would like to select all patches of which label is equal to label of the polygon.
Thank you for your help.
to-report create-edge-turtles [ID-polygon]
let edge-turtles nobody
ask ID-polygon [
foreach (patches with [plabel = ID-polygon]) [
foreach sort neighbors [
sprout 1 [
if [plabel] of neighbors != ID-polygon [
face ?
fd distance ? / 2
set edge-turtles (turtle-set edge-turtles self)] ] ] ] ]
report edge-turtles
end code here
to-report edge-distance-between-polygons-in-buffer [indvidual]
ask individual [
set list-ID-polygon-in-buffer ([plabel] of patch-here in-radius 2)
set list-ID-polygon-in-buffer remove ([plabel] of patch-here) list-ID-polygon-in-buffer
foreach list-ID-polygon-in-buffer [
let ID-polygon-with-individual ([plabel] of patch-here)
let ID-polygon-in-buffer ?
let edges-polygon-with-individual create-edge-turtles ID-polygon-with-individual
let edges-polygon-in-buffer create-edge-turtles ID-polygon-in_buffer
set distance-patches min [ min [ distance myself ] of edges-polygon-in-buffer ] of edges-polygon-with-individual
ask edges-polygon-with-individual [ die ]
ask edges-polygon-in-buffer [ die ] ] ]
report distance-patches
end

The command
patches with [plabel = ID-polygon]
returns an agentset, not a list. To turn an agentset into list simply use the sort keyword, as such
sort patches with [plabel = ID-polygon]

Related

How to find the distance between two patches?

I'm trying to have agents go the most efficient possible route to their destination (randomly selected from a patchset of 10 patches). The problem is, distance seems to be an agent-only command, and the only code I've found that does anything similar seems to work by running the distance command with patches, which gives an error.
This is the troublesome procedure:
to-report best-route
let visible-patches patches in-radius turtles-vision-dist
let visible-routes visible-patches with [ pcolor = gray ]
let routes-that-take-me-closer visible-routes with [
;;THIS IS THE PROBLEM LINE RIGHT BELOW HERE
[ distance visible-routes] of destination < [ distance destination - 1 ] of myself
]
In the example I had found, Paths, this was the code:
to-report best-way-to [ destination ]
let visible-patches patches in-radius walker-vision-dist
let visible-routes visible-patches with [ pcolor = gray ]
let routes-that-take-me-closer visible-routes with [
distance destination < [ distance destination - 1 ] of myself
]
ifelse any? routes-that-take-me-closer [
; from those route patches, choose the one that is the closest to me
report min-one-of routes-that-take-me-closer [ distance self ]
] [
; if there are no nearby routes to my destination
report destination
]
end
I had originally had something more similar, but it wasn't working, so I've been playing around with it with no luck, as have a classmate and my professor.
I can't figure out how your paths algorithm is supposed to work since it doesn't look like you are restricting movement to grey patches. However, assuming your destination is stored as a patch, then you can can calculate distance to that patch directly with distance destination, like this:
turtles-own [ destination ]
to testme
clear-all
create-turtles 1
[ set destination one-of patches
ask destination [ set pcolor blue ]
]
ask one-of turtles
[ print distance destination
]
end

How to find the average distance from turtles that fulfill a certain condition?

I want to move the current turtle one step closer to the others that fulfill a certain condition (e.g. have color = green).
I am doing this the hard way (because I don't know any better), by trying to calculate the the average distance of the current turtle from all others that fulfill the condition, and calculate the average from x+1, x-1, y+1, y-1. Then whichever is the smallest would indicate the direction of the move. Not very elegant, I know, and limits movements to horizontal and vertical, but I couldn't come up with anything better (the only other idea that struck me was to calculate average x and y coordinates of all turtles that fulfill the condition and move the current turtle towards that, but that seemed even more ridiculous to me)
Problem is that even with my clumsy solution, I am not getting anywhere, since I am struggling with how to calculate the average distance from the "green" turtles.
If you want to calculate the mean distance, you can have the asking turtle call mean and [distance myself].
With this setup:
to setup
ca
crt 10 [
set color green
move-to one-of patches with [ pxcor < 0 ]
]
crt 1 [
set color red
move-to one-of patches with [ pxcor > 10 ]
]
reset-ticks
end
Calling the function below will have the red turtle print out first all distances between itself and all green turtles, then the mean of those distances:
to calc-mean-distance
ask turtles with [ color = red ] [
print [ distance myself ] of turtles with [ color = green ]
print mean [ distance myself ] of turtles with [ color = green ]
]
end
Beyond that, I'm not 100% sure what you're trying to do- are you hoping to move the asking turtle towards the nearest turtle that meets some condition? If so, this might work for you:
to go
ask turtles with [ color = red ] [
let target min-one-of ( turtles with [ color = green ] ) [ distance myself ]
face target
ifelse distance target > 1 [
fd 1
] [
move-to target
]
]
tick
end
If you want the asking turtle to move instead towards the geographic center of those turtles that meet a condition, you could indeed get the mean x and y coordinates of those turtles as you describe, then have the asking turtle move towards that point:
to go
let central-x mean [ xcor ] of turtles with [ color = green ]
let central-y mean [ ycor ] of turtles with [ color = green ]
ask turtles with [ color = red ] [
facexy central-x central-y
ifelse distancexy central-x central-y > 1 [
fd 1
] [
setxy central-x central-y
]
]
tick
end
If those aren't quite what you're trying to achieve, feel free to leave a comment for clarification!

Placing turtles at edges of patch clusters

I'm looking for a way to rapidly place turtles at edges of patch clusters. I tried this code but I found that it is a little slow when I increase the size of my study area:
to draw-edges [ID-cluster]
ask patches with [cluster != ID-cluster] [
ask neighbors with [cluster = ID-cluster] [
if not any? turtles-here [
sprout 1 [
set shape "x" ] ] ] ]
end
Thanks in advance for your help.
You're currently asking the whole world outside your cluster to check for neighbors that are in the cluster. It should be faster to ask patches that are part of your cluster to check if they are next to an outside patch:
to draw-edges [ ID-cluster ]
ask patches with [
cluster = ID-cluster and
any? neighbors with [cluster != ID-cluster] and
not any? turtles-here
] [
sprout 1
set shape "x"
]
end

Find the nearest polygon patches from edge to edge distance calculated between two polygons

My objective is to find the nearest polygon patches from edge to edge distance calculated between two polygons. From edge to edge distance between patches with netlogo, here is my code to find the nearest polygon edges:
to-report calculate-distance [ID-polygon-A ID-polygon-B]
let polygon-distance nobody
let edges-of-polygon-A create-edge-turtles ID-polygon-A
let edges-of-polygon-B create-edge-turtles ID-polygon-B
set polygon-distance min [ min [ distance myself ] of edges-of-polygon-B ] of edges-of-polygon-A
## find the nearest edge in the polygon A
let nearest-edge-in-polygon-A edges-of-polygon-A with-min [ min [ distance myself ] of edges-of-polygon-B]
ask nearest-edge-in-polygon-A [
set color red
set shape "x"
set size 2 ]
## find the nearest edge in the polygon B
let nearest-edge-in-polygon-B edges-of-polygon-B with-min [ min [ distance myself ] of edges-of-polygon-A]
ask nearest-edge-in-polygon-B [
set color red
set shape "x"
set size 2 ]
ask edges-of-polygon-A with [ shape = "dot"] [ die ]
ask edges-of-polygon-B with [ shape = "dot"] [ die ]
report polygon-distance
end
From How to find a patch with a specific ID where there is a red turtle?, here is my code to find the nearest patches from the nearest edges:
show patches with [ ID-polygon = [ my-ID-polygon ] of myself and any? (turtles-here with [ color = red and shape = "x" ])
I don't understand why I obtain :
(agentset, 1 patch)
(agentset, 0 patches)
However, the two turtles with shape = "x" are well-visible on the polygons. If my code is not correct, is there another way to find the two nearest polygon patches based on distance ?
Thanks in advance for your help.

NetLogo check if turtles are on same coordinates

I have two turtle breeds who populate each sides of the window and then only move round in there own side.
The problem I am having is that I want to constantly check to see if one singular instance of a turtle from each breed are both on the same y coordinate. And if this returns true i want both of those turtles to stop, but for all other turtles from each breed to carry on moving. I know you can identify a turtle by there unique ID but i don't know how to use this and how to use the correct syntax.
The best way to describe this in pseudo code would be
ask turtles [
if breed1 turtle ycor = breed2 turtle ycor
[ stop breed1 turtle and breed2 turtle ] ]
UPDATE
Tried getting the code to work but still nothing happening. Not sure if it is the way the procedure is wrote or the number I have chosen for the threshold.
to move-turtles
ask turtles [
if not any? turtles with [ breed != [ breed ] of myself and abs (ycor - [ycor] of myself) < 1 ]
[
ask redteam with [pcolor = green - 3] [
right random 360
forward 1
]
ask redteam with [pcolor != green - 3] [
back 1
]
ask blueteam with [pcolor = green - 2] [
right random 360
forward 1
]
ask blueteam with [pcolor != green - 2] [
back 1
]]
]
end
Note that "same coordinate" is actually somewhat ambiguous. If one turtles ycor is 5.0000001 and another's is 5.0000000, are they at the same coordinate? Because of this, you should check to see if their coordinates are within a certain amount of each other.
Also, the best way to stop moving is to simply not move. So, here is a possible go procedure that would do what you want:
to go
ask turtles [
if not any? turtles with [ breed != [ breed ] of myself and abs (ycor - [ ycor ] of myself) < threshold ] [
move ;; replace with your move procedure or code
]
]
end
Here, each turtle checks to see if there are any turtles of a different breed who's ycor is within threshold of their own ycor. If there are not, then it moves. Otherwise, it does nothing.
The myself stuff is the most confusing part here, so I recommend reading the docs.