Copy the link breed variable in the patch below - netlogo

I have a network of nodes and links. This figure
is a capture of the world. The graph represents streets of a city. I have imported a shapefile with the gis extension. The gray lines are links, black dots are nodes and red dots represent people. The people move heading to the next node. In a street corner, the red dot chooses next street by examining the variable popularity owned by the link.
The links breed has a variable, popularity, whose value I would like to copy in the patches that are below.
If I try, for example, something like this to access patches under links will produce an error
ask links [show [(list pxcor pycor)] of patch-here]
Another approach can be to access links variable popularity from patches, but I do not know how to do it.
The reason why I want this is because I want to write in a file a matrix of popularity values and its position in the matrix should correspond with the position of the link in the world. Thus, the patches below the links would give me the matrix form. I have a procedure that for each patch writes the value of the patch in a file. However, I do not know how to pass the popularityvalue from the link to the patch below it.
Is there any way to copy a link owned variable to its patch?
Regards

If someone has a better way of doing this (or can simplify my code), feel free. Here is a complete working example. Copy it into an empty NetLogo model and run it to see it work.
The setup procedure just creates some nodes and links with appropriate test values and then calls the transfer-link-values procedure, which does what I think you want. The setup procedure then puts the values into the patch labels to display them and see the results.
The way the transfer-link-values procedure works is to create a turtle at one end of the link, and that turtle moves toward the other end of the link transferring the value as it goes. When it gets to the other end, the turtle dies.
patches-own [patch-popularity]
links-own [link-popularity]
to setup
clear-all
create-turtles 10 [ setxy random-xcor random-ycor]
while [ any? turtles with [not any? my-links] ]
[ let to-pair turtles with [not any? my-links]
let thisNode one-of to-pair
ask thisNode
[ create-link-with one-of other to-pair
[ set link-popularity 5 + random 5 ]
]
]
transfer-link-values
ask patches [ if patch-popularity != 0 [set plabel patch-popularity ] ]
end
to transfer-link-values
ask links
[ let start-node one-of both-ends
let this-link self
let end-node nobody
ask start-node [ set end-node [other-end] of this-link ]
let transfer-value link-popularity
ask start-node
[ hatch 1
[ face end-node
if transfer-value > patch-popularity
[ ask patch-here [ set patch-popularity transfer-value ] ]
while [ not member? end-node turtles-here ]
[ forward 1
if transfer-value > patch-popularity
[ ask patch-here [ set patch-popularity transfer-value ] ]
]
if transfer-value > patch-popularity
[ ask patch-here [ set patch-popularity transfer-value ] ]
die
]
]
]
end

Related

Getting turtles to spend energy while moving on a least cost path in a network environment

I’m creating a model in which turtles choose and traverse a least cost path in a network environment, one node at a time.
I’m using the nw:turtles-on-weighted-path-to primitive from the NW extension to let my turtles choose a path. Relevant snippets of my code (which so far works fine) are below:
undirected-link-breed [ routes route ]
undirected-link-breed [ tracts tract ]
breed [ stalkers stalker ]
stalkers-own [ energy ]
links-own [ difficulty ]
to setup-network
ask routes [
set thickness 2
set color white
set difficulty 1
]
ask tracts [
set thickness 2
set color black
set difficulty 2
]
end
to enter-the-zone
ask stalkers [
let path nobody
let target one-of nodes-on patch -10 42
ask nodes-on patch-here [ set path but-first nw:turtles-on-weighted-path-to one-of nodes-on patch -10 42 difficulty ]
ifelse patch-here != ( patch -10 42 )
[
face first path
move-to first path
]
[ return_to_base
]
]
tick
end
to return_to_base
move-to patch -258 -231
end
However, I would like to add an “energy” attribute to my turtles that depletes a little every time they reach a new node. The catch is: I would like this loss of energy to be equal to the difficulty of the link between the node they are now and the node in which they just were.
What would be the best way to implement that?
Thanks in advance for the help.

NetLogo: calculate perimeter of a patch-set

I'm modeling territory selection in NetLogo, and would like my turtles to calculate the perimeter of their territory once established. I've been trying to come up with ideas for how to do this, but haven't found a good means yet. Any ideas?
patches-own
[ owner ] ;; patches know who owns them
turtles-own
[ territory ;; agentset of patches I own
food ;; food acquired in my territory
threshold ] ;; food required, will build territory until meet this
to go
tick
ask turtles [ build-territory ]
end
to build-territory
if food > threshold [ calculate-perimeter ] ;; stop building when enough food
pick-patch ;; keep picking patches until meet threshold.
end
to calculate-perimeter
;; what could I use to add up the perimeter of the territory?
end
Thanks in advance for any suggestions!
A modification of my last answer to you:
to setup
ca
ask patches with [pxcor > 0 ] [
set pcolor white
]
crt 1
end
to go
ask turtles [
let blacklist patches with [ pcolor = black ]
let northpatches patches with [ pycor > 0 ]
let northred ( northpatches with [ member? self blacklist = false ] )
ask northred [ set pcolor red ]
let border northred with [ any? neighbors4 with [ pcolor != red ] ]
ask border [
set pcolor blue
]
print count border
]
end
You can designate border/perimeter patches as any of you territory patches with neighbors that are not territory. For you it might look something like:
ask turtles [
print count territory with [ any? neighbors4 with [owner != myself ]
]
]
Again, I can't test it without your setup so you would have to modify.
Edited below
To count the edges of patches that are on the border, you could have them count their neighbors4 that belong to another turtle. Then, they can add them to that turtle's perimeter length. For example:
to assess-perimeter ;;; must be called by a turtle
print ("Assessing perimeter")
let current-turtle who
let temp-per-len 0
let border-patches patches with [ owner = current-turtle and any? neighbors4 with [ owner != current-turtle ] ]
show (word "I have " count border-patches " border patches")
ask border-patches [
;; One way to get each border patch to check each of its neighbors
let nobodies 4 - count neighbors4 ;; if any patches are on the edge of the world, returns the number of those edges
let non-territory-edges count neighbors4 with [ owner != current-turtle ]
let border-edges nobodies + non-territory-edges
set temp-per-len temp-per-len + border-edges
]
show (word "My perimeter length: " temp-per-len )
set perimeter-length temp-per-len
end
If that is called after all turtles have chosen their entire home range, the idea is that each turtle assesses the border of its home range. Then, it has each of those border patches count its neighbors4 that have a different owner. I used "temp-per-len" as a summing variable within the loop, which is then used to set the turtles-own "perimeter-length". Full model code, including setup and definitions, here. Note- you'll have to download or copy the code, the model is too bulky to run well in the HTML format.
Also, I didn't actually count to make sure this worked perfectly- I did a quick version and crossed my fingers, but I think the idea makes sense and hopefully gets you started.

How to choose the not previously used links by an agent

I have breed [walkers walker] which walk roads in a road map represented by links in Netlogo.
The links-own [ guiri-ids ] which I intend to be an turtle-set of walkers that have already walked for the link.
I would like to use this guiri-ids to select, from the set of possible next links, which I call nextlinks, the links that the walker has not been walked before (the new ones).
If all possible links have been walked before then choose one of them.
How could obtain the set of next links which guiri-ids set does not contain myself (the walker) ?.
I am trying this line
let new-nextlinks nextlinks with [ guiri-ids != myself ]
but the keeps taking old paths.
Thank your very much for your help
breed [walkers walker]
links-own [ guiri-ids ]
to test
ca
crt 25 [setxy random-xcor random-ycor]
ask turtles [
create-link-with one-of other turtles
]
create-walkers 10
ask links [set guiri-ids n-of 3 walkers]
ask walkers [walk]
end
to walk
let _next one-of links with [not member? myself guiri-ids]
ifelse (_next != nobody) [
ask _next [set guiri-ids (turtle-set myself guiri-ids)]
] [
;do whatever you wish in this case
]
end

Netlogo: ask turtle to reproduce after it completes a procedure, and ask new turtle to repeat that procedure for itself

I'm new to NetLogo and am attempting to model home range selection of subsequent colonizers. The model should follow simple steps:
Individual 1 picks a home range (a subset of patches).
When individual 1 is done picking its home range, it hatches new
individual 2.
Individual 2 picks a home range, then hatches individual 3.
Individual 3 picks a home range, and so on.
I'm having trouble figuring out how to get this to work. I can get the first turtle to pick a home range. But the offspring do not. Writing the code numerous ways has only accomplished two unintended outcomes. Either endless new individuals are hatched simultaneously, before the first turtle has a home range, and the new turtles fail to pick a home range. Or, the first turtle picks its home range and hatches a new turtle, but that new turtle doesn't pick a home range. Neither outcome is what I want.
How do I set this up to run as intended, so that hatchlings pick home ranges too? Here is one simplified version of my code:
to setup-turtles
crt 1
[setxy random-xcor random-ycor]
end
to go
ask turtles [pick-homerange]
tick
end
to pick-homerange
while [food-mine < food-required] ;; if not enough food, keep picking patches for home range
[;; code to pick home range until it has enough food; this is working okay
]
[;; when enough food, stop picking home range
hatch 1 fd 20 ;; now hatch 1, move new turtle slightly away
]
end
So it is at this last part, once the home range is built, that I want a new turtle to hatch from its parent. I then want that turtle to repeat the pick-homerange procedure. How could that be coded to happen? I've tried writing this every way I can think of; nothing is working. Thanks in advance for any assistance!
One way to do this is to have each patch equal one "food value", and have turtles grow their home range until their home range supplies them with enough food. I would set this up so that patches "know" to which turtle they belong, and so that turtles know how much food they need, which patches are part of their home range, and the food supplied by their homerange. Example patch and turtle variables would then be:
patches-own [
owned_by
]
turtles-own [
food_required
my_homerange
homerange_food
]
Then, your turtles can add patches into their home range until they hit their "food_required", whatever you set that as. For simplicity, in this example I assume that turtles are territorial and so won't "share" home ranges. Further explanation of steps is commented in the code below. This is intended just to get you started- for example, it will hang if you run pick-homerange too many times.
to setup-turtles
crt 1 [
set size 1.5
setxy random-xcor random-ycor
set food_required 5 + random 5
set homerange_food 0
set my_homerange []
]
end
to pick-homerange
ask turtles [
;; Check if the current patch is owned by anyone other than myself
if ( [owned_by] of patch-here != self ) and ( [owned_by] of patch-here != nobody ) [
;; if it is owned by someone else, move to a new patch that is not owned
let target one-of patches in-radius 10 with [ owned_by = nobody ]
if target != nobody [
move-to target
]
]
;; Now add the current patch into my homerange
ask patch-here [
set owned_by myself
]
set my_homerange patches with [ owned_by = myself ]
;; calculate the number of patches currently in my homerange
set homerange_food count patches with [owned_by = myself]
;; Now grow the homerange until there are enough patches in the homerange
;; to fulfill the "food_required" variable
while [ homerange_food < food_required ] [
let expander one-of my_homerange with [ any? neighbors with [ owned_by = nobody ] ]
if expander != nobody [
ask expander [
let expand_to one-of neighbors4 with [ owned_by = nobody ]
if expand_to != nobody[
ask expand_to [
set owned_by [owned_by] of myself
]
]
]
]
;; Reassess homerange food worth
set my_homerange patches with [ owned_by = myself ]
set homerange_food count patches with [owned_by = myself]
]
ask my_homerange [
set pcolor [color] of myself - 2
]
;; Now that my homerange has been defined, I will hatch a new turtle
hatch 1 [
set color ([color] of myself + random 4 - 2)
]
]
end

How to create links in netlogo and ask link-neighbors to execute commands according to link-length?

I've been trying to link turtles from BREED1 (still) to turtles from breed2 (mobile) who are on neighbors of BREED1. I want to do so in order to change a variable according the link-length between BREED1 and breed2.
(you can say that BREED1 represent houses and breed2 represents people, I would like to change the fact that the people are "protected" or not, according to the distance that separates them from their house (BREED1 that they are linked to))
I don't know if this is the best way to do it, but here's my code, I know it's not working because the "protected" variable is always false by default.
to protect
ask n-of total-number-BREED1 BREED1
[ if any? breed2-on neighbors
[ create-link-with [who] of breed2-on neighbors]
ask link-neighbors
[ set protected true]
]
I would also like to add a part concerning the link's length
ask link-neighbors
[ ifelse link-length < 2
[set protected true]
[set protected false]]
Thank you for your help !
Try this to create links with the breed2-on the neighboring patches:
ask BREED1
[
if any? breed2-on neighbors [ create-links-with breed2-on neighbors]
ask link-neighbors [ set protected true]
]
and this, which gets the link-length between the breed1 and it's neighbors
ask BREED1
[
ask link-neighbors
[
if [link-length] of link-with myself < 2 [ do something]
]
]
Note: link-length is called from a link's perspective, so you need to get the link that's connecting two things.