Using link-own variable to transfer resources between specific turtles - netlogo

Say we have two turtles with a link between them, and that link owns a variable that represents the age of the link, "tenure". For turtle 1 to send resources to turtle 2, there is a conversion cost related to the age of the link between them. How do I refer to the tenure value of the specific link between these two turtles?
In context, I have turtles linked into "households" and as they start each day they assess how much energy they have. If they have more than some threshold of energy, they share the surplus with fellow housemates. To elect how they will share the energy, I have them looping through each housemate to determine who has low health. Surplus energy can be transferred to a housemate, but the conversion rate depends upon the tenure of the link between them. A longer tenure results in a better conversion rate of energy into health. But I don't know how to refer to the specific value of the linked-owned variable between each turtle and each of their housemates.
Right now, I have this approach of the shared-link being something like [link = [link] of myself] but that doesn't work:
turtles-own [energy health age]
links-own [tenure]
to go
tick
ask links
[
set tenure tenure + 1]
ask turtles
[
ifelse energy > 3 [supply] [rest]]
end
to supply
let surplus energy - 3
loop
[ask one-of link-neighbors
[
let shared-link link = [link] of myself
if health < 3 [set health health + 1 * [log [tenure] of shared-link]
set surplus surplus-1]
]]
end

Try running this snippet. it illustrates what I think you need.
links-own [ weight ]
to setup
clear-all
random-seed 12345
create-turtles 4 [setxy random-xcor random-ycor set label who set size 3]
ask turtles [ create-links-with other turtles [ set weight random 100 set label weight ]]
ask turtle 1 [ ask link-with turtle 2 [ set color red set thickness 0.5 ]]
ask turtle 1 [
let myweight [weight ] of link-with turtle 2
print ( word "Turtle 1 says, my link weight with turtle 2 is " myweight )
]
ask turtles [ ask link-neighbors [
let myweight [weight ] of link-with myself
print ( word myself " says, my link weight with " self " is " myweight )
]]
reset-ticks
end

Related

how to calculate common friends of two nodes?

Suppose that the more common friends two nodes have, the more closely they are related. I want to find the closest link-neighbor of a node.
But it's difficult for me to find the common friends between one random nodes and its link-neighbors.
I wrote an example to see if my thought works:
turtles-own[CF] ;;common friends
to setup
ca
crt 10 [set size 1 set color gray]
ask turtle 1 [create-links-with other turtles]
ask turtle 2 [create-link-with turtle 3]
ask turtle 2 [create-link-with turtle 4]
layout-circle (sort turtles) 5
reset-ticks
end
to go
ask one-of turtles [
let communicator self
ask link-neighbors [;;setA
let i 0
ask link-neighbors [;;setB
if link-neighbor? communicator[
set i i + 1
]
]
set CF i
]
]
tick
end
In the interface, I set a reporter [CF] of turtle 2.If it works, the answer could be 2
But as it keeps going, the reporter shows sometimes 1 and sometimes 2. I don't know why and I hope sth more simple.
I haven't traced your code's operation, but your core structural problem is that "common friends" is a property of a PAIR of nodes, not one node .
I tweaked your code to set the common friends count ( CFL ) to the LINK between pairs of nodes. So (link 1 2) ends up with CFL = 2 and it's stable. It's not as efficient as it could be but then you can just run all the turtles, not one of the turtles in your go step.
Here's my solution:
turtles-own[CF] ;;common friends
links-own[CFL]
to setup
ca
crt 10 [set size 1 set color gray set shape "circle" set label who ]
ask turtle 1 [create-links-with other turtles set color green]
ask turtle 2 [create-link-with turtle 3 [set color orange set thickness 0.25] ]
ask turtle 2 [create-link-with turtle 4 [set color orange set thickness 0.25] ]
ask turtle 2 [ set color red ]
ask one-of links with [end1 = turtle 1 and end2 = turtle 3] [ set color green set thickness 0.25 ]
ask one-of links with [end1 = turtle 1 and end2 = turtle 4] [ set color green set thickness 0.25 ]
layout-circle (sort turtles) 5
reset-ticks
end
to go
let ln-direct ""
let ln-once-removed ""
ask turtles [
let communicator self
ask link-neighbors [;;setA
set ln-direct self
; print (word "for turtle " myself " one DIRECT link-neighbor is " ln-direct)
let i 0
ask link-neighbors [;;setB
set ln-once-removed self
; print ( word " one neighbor of that neighbor is " ln-once-removed )
if link-neighbor? communicator[
; print (word ",,,,,,,, BINGO - loops back to " communicator )
set i i + 1
]
]
ask one-of links with [
( end1 = communicator and end2 = ln-direct ) or
( end2 = communicator and end1 = ln-direct )
] [set CFL i set label CFL ]
]
]
tick
end

Minimum distance and interference between turtles

I am doing boarding processes model with anti-Covid measures. I would like to know if any of you could help me.
I would like to enforce that the minimum distance between turtles will be 4 patches.
I would like to identify if there is an interference, that is, that a previous turtle occupies a seat that prevents a new agent from passing through.
I don't know how to insert this in my code:
to go
; stop when all the pessengers have found a seat
if not any? turtles with [seated? = false]
[stop]
set counter counter + 1
(foreach (sort turtles with [seated? = false] ) [a ->
ask a [
; check if we have reached the correct row of seats
ifelse [seat-row] of patch-at 1 -1 = assigned-seat-row
[
; only seat the passenger if he/she has stored the luggage OR if we don't take luggages into account
ifelse luggage-store-ticks = 0 or storing-luggage-takes-time = false
[
let seat patch-at 1 -1
set xcor [pxcor] of seat
set ycor assigned-seat-number
set seated? true
]
[
set luggage-store-ticks luggage-store-ticks - 1
]
]
[
let passenger-ahead one-of turtles-on patch-ahead 1
ifelse passenger-ahead != nobody
[
set speed [speed] of passenger-ahead
if xcor != airplane-door-x-cor
[fd speed]
]
[
set speed default-speed
fd speed
]
]
]
])
end
You could extend your existing patch-ahead 1 to also look at the patches 2, 3 and 4 ahead. But I think it would be easiest to use in-cone 5 10 or similar. That will look ahead in a cone shape, 10 degrees on either side of the heading and a distance of 5. So you could do something like:
let potential-blockers turtles in-cone 5 10
let blocker min-one-of potential-blockers [distance myself]
That should (not tested) find the closest turtle approximately in front and name it "blocker" so that you can do things like check if it's far enough away, match speed (see the basic traffic model in the NetLogo models library)

K on and K off in reverse reaction kinetics

I am trying to code for a reverse reaction in biology - I have already done that by taking inspiration from a sample code pasted below. What I don't understand is how is the association rate constant (Kb in the code) and dissociation constant (Ku in the code) are affecting the forward and backward reactions in this code. Are they affecting the speed of movement of the products after they are formed? Are they affecting the speed with which they are formed? I want the Ku to affect affinity for substrates to form a product, and Ku to affect time to dissociate for products. The product is a complex formation of two substrates.
Also, why do they multiple Kb by 2? To slow the product complex down or speed it up? It should slow down due to increase in mass.
breed [reactants reactant] ;; reactants are green, products are red breed [products product]
to setup clear-all set-default-shape reactants "molecule1" set-default-shape products "molecule2" create-reactants number
[ set color green
setxy random-xcor random-ycor ] reset-ticks end
to go ask turtles
[ rt random-float 10 - random-float 10 ;; wander around randomly
fd 1 ] ask turtles
[ ifelse (breed = reactants)
[ react-forward ] ; reactants
[ react-backward ] ; products
] tick end
to react-forward if (any? other reactants-here) and
;; multiply Kb by 2 because 2 molecules are involved
random-float 1000 < (Kb * 2)
[ ask one-of other reactants-here
[ die ]
set breed products
set color red ] end
to react-backward if (random-float 1000) < Ku
[ set breed reactants ;; change back to reactant
set color green
;; then split into two reactants
hatch 1 [ set heading random 360 ] ] end

NetLogo - turtle to go to the closest concentration of turtles

I would like a turtle to go to the closest patches with most turtles if a threshold of a given variable is met for 5 ticks.
My code is:
to move
let count-tick 5
if var >= 9.5 [
set count-tick count-tick - 1
if count-tick = 0 [
ask turtle [
let nearest-group min-one-of (patches with [sum turtles >= 3] in-radius 3 ) [ distance myself ]
move-to nearest-group ;; go to the biggest crowd near you
ask turtle [ ;; once there do the following
set shape "star"
set color red
]
]
]
]
end
The issue I have is that a) I am unsure how to say the patch with >= 3 turtles closest to you at the given range of 3 (attempted code above) and b) how to say once there, change your shape.
Revised to keep a permanent variable to track whether the variable is high enough 5 times in a row.
turtles-own
[ count-tick
]
; wherever you create the turtles, you need to `set count-tick 5`
to move
ifelse var >= 9.5
[ set count-tick count-tick - 1 ]
[ set count-tick 5 ]
if count-tick = 0
[ let nearest-group min-one-of (patches with [count turtles >= 3] in-radius 3 ) [ distance myself ]
move-to nearest-group ;; go to the biggest crowd near you
set shape "star"
set color red
]
end
First, you are already within an ask turtles code block from the procedure calling this move procedure. So you don't need the additional ask turtles. Look up ask in the NetLogo Dictionary, it iterates through the turtles, running all the code for each turtle in turn.
Second, you need count turtles rather than sum turtles as sum is to add up values.
Note that there is no error checking in this, you may have problems if there are no patches within radius of 3 that have at least 3 turtles.

How to extract a highly linked node from a network

I want to extract a node with highest degree centrality from the network. I don't want to extract a node with max links only. I want to extract the node along with the nodes adjacent to it.
Below is the code. In this code, I have loaded a network using nw extensions.
extensions [nw]
turtles-own [ explored? ]
to setup
ca
crt 25
ask turtles [fd random 15]
load-graph
extract_deg
end
to load-graph
let filename user-file
if (filename != false) [
nw:load-graphml filename [
set shape "circle"
set size 1
]
nw:set-context turtles links
]
end
to extract_deg
let n turtles with [my-links = max [count link-neighbors] of turtles]
ask n [show other turtles network:in-link-radius 1 turtles]
end
to layout
ask turtles [ set size sqrt count my-links ]
layout-spring turtles links 0.5 2 1
ask turtles [
facexy 0 0
fd (distancexy 0 0) / 100 ]
end
The code below will choose one of the nodes with largest degree (just remove one-of if you want all of them), turn it red and make its network neighbours green.
You don't need the expression [my-links = max [count link-neighbors] of turtles], standard NetLogo includes the very useful with-max primitive. However, I think your construction would have worked if you had counted my-links (like let n turtles with [count my-links = max [count link-neighbors] of turtles]). Then you have some syntax errors in the next line (the extension is nw and you don't need the turtles.
to extract_deg
let maxk-set one-of turtles with-max [count my-links]
ask maxk-set
[ set color red
ask other nw:turtles-in-radius 1 [set color green]
]
end