Node degree in a network - netlogo

I have a landscape that includes a road network as in the below figure.
I would like to calculate an average number of roads (or white line in the figure) that link each color polygon (for example a black polygon) between them.
In a network, a link corresponds to a road, a node corresponds to a color polygon and I think that to calculate an average number of roads between color polygons means to calculate the average node degree of the network. For example, in the figure, the two black polygons are linked by two roads. So, the degree of a black polygon is 2. Is it possible to calculate a node degree with the extension Network of Netlogo ?
Thanks in advance for your help.

Normal NetLogo contains link agents that link turtles even without an extension. Calculating degree is usually just a matter of doing [ count my-links ] of node where node is the turtle you want to know the degree of. However, in NetLogo, turtles can't be connected by more than one link. The typical workaround for this is create a links-own variable (just like turtles-own or patches-own variable) . This variable is often called weight, but you can call it whatever you want. In that case, you would do [ sum [ weight ] of my-links ] of node to calculate the degree.
This is all assuming you have a network representation of your roadways, which it doesn't sound like you do. Furthermore, I'm not sure what you're trying to represent is a network, since (as shown in your picture) roads branch at intersections. Thus more than two polygons could be connected by a single (for some definition of "single") road. This is often called a hypernetwork or hypergraph. However, this is probably a heavier weight concept than what you want.
Now, I'm not entirely sure what you're really trying to calculate. Is it:
...the number of roads connected to a polygon? The lower polygon has 4 roads connected to it, the upper has 3 (visible).
...the number of polygons directly connected to the polygon? Both polygons are connected to 1 other (visible) polygon, though I assume in the larger picture there are more.
The number of roads connected to a polygon would be pretty easy to calculate, assuming each road is 1 pixel wide. You could just do:
count (patch-set [ neighbors4 with [ is-road? ] ] of polygon)
where polygon is a patch set containing the patches of the polygon and is-road? is a reporter that returns true for road patches and false for non-road patches (this could be something pcolor = white). Note that this will break if roads are wider than 1 patch or if the same road can touch a polygon in other places. Let me know if that's the case and I'll expand this into something that can that into account.
The number of polygons directly connected to the polygon is more difficult. The basic idea would be to follow the roads out until you hit other polygons and count the number that you hit. Code for this is somewhat tricky. I think the best way to go about it would be to have two patch-sets, frontier and explored and list of found polygons. frontier should initialize to every road patch touch the polygon. Each iteration, get the polygons touching the frontier and add them to the list of found polygons if they're not already in there. Add the frontier to explored. Get all the road patches touching the frontier that are not in explored. Set the frontier to this new set of patches. Keep going until frontier is empty. This is a version of breadth-first search. There could be a better way to do this.

Related

Calculating distance and determining units of measure in Netlogo using GIS extension

I'm using GIS extension in Netlogo to import shapefiles, then creating turtles from attributes of the shapefile. I'm using the distance primitive to calculate the distance between turtles. I know Netlogo does converts the shapefile to netlogo space, so the distances wouldn't be reported the same b/w GIS and Netlogo. For example, in GIS distance between turtle A and B is 49550 meters. In Netlogo, it's 0.2038. Is there a way to determine what the units of measure are in Netlogo? Is the conversion always the same or does it depend on the projection? Thanks for any assistance.
The distance in a NetLogo model is entirely arbitrary. You are deciding the unit of distance implicitly when you import a GIS real-world structure into whatever number of patches there are in the NetLogo world. In your case, the easiest way to work out the conversion factor is likely to be to find a few points on your GIS and get their xcor and ycor. From your question, haven't you already done this? You have said that 0.2038 is 49550m.
There will also be problems coming out of the projection - if your model covers a large real world area, then the mismatch between the surface of a sphere and a flat model will mean the distance conversion is different at different locations.
Maybe this helps. I found a model here, which is also trying to work with space.
I quote: "Grid cell size does not represent an absolute spatial unit (e.g. meters); instead, the size of grid cells is only meaningful with respect to the step size of individuals which can vary with user input."

NetLogo: Measure total area covered by turtles

After my NetLogo simulation, I would like to measure the total area covered by only the turtles. Is there a simple way to implement this in NetLogo, or will I have to do this in another program?
My simulation has clusters that form and I would eventually like to calculate the cluster agent density. 1
NetLogo is not aware of where the edge of the turtles’ shapes are. So, if you are trying to work out what proportion of the screen has shapes on it, you need to do some complicated programming to calculate where the edges of the shapes are, how they overlap and so on. However, if all you care about is where the turtles are, then it's much easier. For example, to work out the proportion of patches where there are at least one turtle, you could do:
count patches with [any? turtles-here] / count patches

Plotting variables of multiple agents

I am trying to plot a graph that shows the variables of all agents of one breed. Although the number of agents is rather small, I think there must be a more elegant way than creating a pen for every agent. Is there?
Elegance, here, depends on what exactly you want to do. It can be perfectly legitimate to create one plot pen per turtle, using create-temporary-plot-pen. Among advantages, it means that you can give each plot pen a name and show a legend on your plot.
That being said, it's also very easy to use a single pen for all your agents. Suppose you have multiple turtles, of different colors, and you want to plot their sizes. You can put something like this in a pen's "update commands":
ask turtles [ set-plot-pen-color color plotxy ticks size ]
This will change the pen to the color of each turtle and plot a single point for that turtle. Just be sure to set your pen to "point" mode in the advanced pen options.
Also notice the use of plotxy instead of just plot, allowing you to specify an x coordinate instead of relying on the "auto advance" behavior of plot.

Draw an arrow for a vector on the top of each of the turtles

I wish to draw an arrow at the top turtle representing a vector denoted by
[x1,y1] . The vector is of unit magnitude and the size of the arrow should not exceed that of the turtle.
The vector is stored in a list with two elements.
I don't wish to use the shape editor in netlogo to shape as arrow and then point the turtle in the heading denoted by the vector. The reason being I could draw one than 1 arrows for each turtle.
Edit:
Desired:
Bryan's answer gives the following:
Edit 2:
Video link : https://www.youtube.com/watch?v=9SVcLg4Oyoc&t=23 for better explanation.
Here's how I'd do it:
Make sure your turtles are all of one breed, say, particles, or whatever they represent. Create another turtles breed called vectors or something. These turtles are going to be the tip of your vectors but you'll use links to actually visualize the vectors. Now, you can create the vectors like so:
ask particles [
hatch-vectors 1 [
create-link-from myself
hide-turtle
]
]
To update the position of the vectors (given that the vector itself is stored in a turtle variable vec), you can do:
ask particles [
let abs-x xcor + first vec
let abs-y ycor + last vec
;; Since the particle is linked to the vector by a directed link, it's an out-link-neighbor
ask out-link-neighbors [ setxy abs-x abs-y ]
]
Edit in response to update:
That's tougher, since link shape editing is more limited than turtle shape editing. One possibility would be to set the shape of the vector turtles to an arrow head (you could either create a new such shape, or the default turtle shape could suffice). Rather than hiding the vectors, you'd then point them in the right direction. This can easily be done by having them face their link-partner and then turn around.
You may also want to switch from directed to undirected links to get rid of the arrow in the link itself. This should only involve minor code changes.

To assign path cost to polygons in a buffer

I would like to build least-cost paths between the polygon (e.g. polygon A) where there is a wolf and all polygons that are situated in a radius of 3 km around the wolf and to found the polygon that has the lowest cost (see also How can I increase speed up simulation of my least-cost path model. Then, the wolf moves toward this polygon (e.g. polygon B). The process is repeated from the polygon B and so forth.
to-report path-cost
ask wolves [
set my-list-of-polygons-in-buffer ( [plabel] of patches in-radius 3 )
set my-list-of-polygons-in-buffer remove-duplicates my-list-of-polygons-in-buffer
set my-list-of-polygons-in-buffer remove [plabel] of patch-here my-list-of-polygons-in-buffer
set my-list-of-polygons-in-buffer remove "" my-list-of-polygons-in-buffer
foreach my-list-of-polygons-in-buffer [
let ID-polygon-in-buffer ?
ask patches with [plabel = ID-polygon-in-buffer] [
set path-cost calculate-LCP [my-ID-polygon] of myself ID-polygon-in-buffer] ] ]
report [plabel] of (min-one-of patches [path-cost])
end
1) From the polygon A, the code works because only the polygons in the buffer have a path cost. But from the polygon B, there is a problem. The code finds the polygon that has the lowest cost among polygons that are situated in the buffer of polygon A and in the buffer of polygon B. The code has to find only the polygon that has the lowest cost among polygons in the buffer of polygon B. How can I resolve this problem? Have I to reset the state variable “path-cost” for each polygon patch before to calculate path cost from polygon B?
ask patches [
set path-cost 0 ]
2) If a same polygon is included in the buffer of both three wolves, how the path cost will be assign to state variable "path-cost" for each patch polygon i.e. is it possible to have 3 x cost value for a same polygon ?
3) In the figure below, why does the least-cost path not follow a straight line? The least-cost path takes the patch diagonal instead of the patch side that is shorter.
Thank you very much for your help.
I'm going to answer your questions in reverse.
3) As Seth mentioned, this is a separate question. That said, here's the answer. By default, all links cost the same amount. That is, diagonal links cost the same amount as horizontal links. Thus, there's no reason to prefer the horizontal links over the diagonal ones. The cost is actually the same. You can use link weights to resolve this. Just make a new link variable (cost or something), set it to the length of the link, and then ask for the shortest weighted path using that variable.
2) Not really. You could set path-cost to a list that contains the values for the different wolves, but I wouldn't recommend. I don't think using a patch variable is the right way to go here.
1) You would have to set path cost to 0, but there's a better way. Also, your procedure only returns the least cost path for the last wolf run (since the wolves keep overwriting the patch variables).
First, I think that you actually want path-cost to be a wolf procedure (I would also name it something like least-cost-polygon as it's actually reporting a polygon). That is just, it just gives the nearest polygon for a wolf. So here is a simplified version that does that and doesn't store anything in any patch variables (thus avoiding collision because nothing is overwritten):
to-report least-cost-polygon
[ plabel ] of min-one-of patches in-radius 3 with [ plabel != [plabel] of myself ] [
calculate-LCP [ my-ID-polygon ] of myself
]
end
Yes
If only one wolf is computing costs at a time, then there's no problem. After one wolf has finished, that wolf's values for path-cost aren't needed anymore, so it's OK for the next wolf to overwrite them. Right? If it's not OK — if you have a need to keep the information around for later — then I'd suggest using links to store it; that's the usual way of storing many-to-many information in NetLogo.
Your questions 1 and 2 are about the same issue, but this is something entirely different. Please ask one question at a time. In any case, it doesn't seem to me like we can possibly debug this for you with the information we have. Apply standard debugging techniques.