Variable specific to turtle and patch - netlogo

In NetLogo, I can create turtle-specific variables with turtles-own, and patch-specific variables with patches-own. But how can I create variables that are specific to a turtle AND a patch?
Specifically, I want to create a preference function - each turtle has a preference to live in a certain patch. The preferences are different for each turtle and patch, for example, turtle 1 has preference 20 to live in patch (1,1) and preference 30 to live in patch (2,2), etc. How can I define this function in NetLogo?

If I understand you correctly, you'd like to have a unique mapping between each turtle and each patch. My first thought is to use the built-in matrix extension via
extensions [matrix]
and have a turtles-own variable "preferences" that is a matrix with the dimensions of your world (e.g. if max-pxcor and max-pycor are both 16 and your origin is centered, you need a 33x33 matrix).
Each element of "preferences" then corresponds to one patch and denotes the assigned value.
See the NetLogo User Manual for documentation on how to fill the matrix with values.

Related

Compute weighted mean of headings

I have each patch holding two lists: directions and magnitudes. Both lists contain values that are computed based on nearby turtles:
directions contains the headings from the patch to the various turtles (so basically it is just the turtle's heading rotated by 180°);
magnitudes contains a value that is directly proportional to the turtles' mass and inversely proportional to the distance between the patch and the turtles;
The items of the two lists are coupled in their order, i.e. the first item of directions and the first item of magnitudes are computed based on the same turtle, the two second items are computed based on another turtle and so on.
What I want to achieve is to come up with two single patch-own values, my-direction and my-magnitude, representing in some way the weighted average of directions, where the weights are magnitudes.
To put it another way, I am thinking of this in terms of vectors: turtles are exerting on the patch a force that can be represented as a vector, always pointing in the direction of the turtle and with a certain intensity (the magnitude). The resulting vector (represented by my-direction and my-magnitude) should be the resulting average of these forces.
I have seen this question. It does not address the issue of a weighted average; however it mentions the concept of circular mean. I've delved into it a bit, but I'm not sure how to apply it to my case and even if to apply it: does it still apply even with the formulation of the problem in terms of vectors?
I've seen this question/answer on SE Mathematics where it is said that the average vector can be found by averaging x- and y-coordinates of the initial vectors. In my case, ideally all the pairs of values in the two lists form a different vector with origin in the patch at issue, with heading found in directions and length found in magnitude. I suspect I can find the coordinates of each vector by multiplying its sine and cosine by its magnitude, however at this point I'd use some guidance as I might be overcomplicating things (either from the maths perspective or the NetLogo perspective).
This one below is a reduced version of the code that brings to the point where target-patches (not focusing on all patches, in order to make it quicker) have the two lists populated.
globals [
target-patches
]
turtles-own [
mass
reach
]
patches-own [
my-direction
my-magnitude
directions-list
magnitudes-list
]
to setup
clear-all
set target-patches n-of 10 patches
ask target-patches [
set directions-list (list)
set magnitudes-list (list)
set pcolor yellow + 3
]
create-turtles 10 [
move-to one-of patches with [(not any? turtles-here) AND (not member? self target-patches)]
set mass (random 11) + 5
set reach (mass * 0.8)
set size (mass / 8)
set shape "circle"
]
populate-lists
end
to populate-lists
ask turtles [
let relevant-targets (target-patches in-radius reach)
ask relevant-targets [
set directions-list lput (towards myself) (directions-list)
set magnitudes-list lput (magnitude-based-on-distance) (magnitudes-list)
]
]
end
to-report magnitude-based-on-distance
report [mass] of myself / (distance myself * 1.2)
end
Your initial instinct is right. You can do something like
LET my-dx mean (map direction magnitude [ theta scalar -> scalar * sin theta ])
(I may have that map and anonymous syntax wrong please edit)
And do the same for my-dy using cos (edit: or maybe negative cos?)
patch-at my-dx my-dy is one way of getting the patch.
You can also do (atan my-dx my-dy) to gets the new direction
And distancexy my-dx my-dy to get the magnitude
Don’t remember if patch can use distancexy or patch-at, but hopefully so. Otherwise you have to use a helper turtle, and do the math yourself.
I think I did a orbital mechanics toy model once that did something like this. It’s hiding on turtlezero.com.

Using only patches to display a letter in Netlogo?

I am new to Netlogo and I wish to understand how to set the patches to show a static letter.
Is it possible for the patches to show a letter (e.g. letter D) without the use of turtles? I was thinking that patches could only have one color, so I am lost when it comes for the patches to display a letter? Is is possible to display a letter by changing the color of some coordinates? Even so, how should I retrieve the correct coordinates to actually be able to draw a real letter?
Thanks!
You are correct that patches must all be one colour - they are squares of environment. So you can either do a very large letter by making it from small squares. Or (more easily) you can use the plabel variable that is owned by each patch. That variable can hold any value like a letter or a word - it is intended as a label for the patch.
If you have the default settings, then the centre patch is patch 0 0. That is both the name of the patch and the co-ordinates. And patch 1 0 is the patch that is one to the right (x direction) of the centre. As well as the label, the variables automatically owned by a patch include the colour (as pcolor), x co-ordinate (pxcor) and y co-ordinate (py-cor).

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.

NetLogo - What is 1 distance unit in NetLogo?

I am working on traffic simulations using NetLogo for a post graduate project.
For turtles to move forward we can specify the number of units it can move e.g. fd 1, which would mean the turtle moves forward by 1. What is this 1 unit? Is it equal to 1 patch or equal to the size of the turtle?
Each step is the same size as the patches.
As Luis said, each step is the size of one patch, but remember that steps are not restricted to integers, eg. fd 0.7 is a valid command and will move a turtle forward seven tenths of the size of a patch.
The patches represent a roadway (if you're discussing a traffic simulation model) and one scales patches in a model appropriately, e.g. 1 patch=25 m....etc.!