Assign a cost to a link - netlogo

I have a landscape where each patch contains a cost value.
I placed a turtle within each patch according to the following code :
to create-turtles
ask neighbors [ sprout 1 [
set shape "dot"
set size 0.5 ] ]
end
Then, I built a link between each turtle according to the following code :
to create-link-turtles
ask turtles [ create-links-with turtles-on neighbors ]
end
As each patch contains a cost value, I would like to assign a cost value to links between turtles.
For example,
If the link intersects two patches (patches 1 and 2) that have two different costs, the link would be equal to cost in patch 1 + cost in patch 2.
If the link intersects two patches (patches 1 and 2) that have the same cost, the link would be equal to cost in patch 1.
How can I assign a cost value to links between turtles in this way ?
After this, I would like to apply the dijkstra' s algorithm.
Thank you for your help.
Have a good day

Assuming that:
your patches have a cost variable
your links have a link-cost variable
turtles are always connected to turtles on neighboring patches (like in the code you posted)
You can simply :
ask links [ set link-cost sum [ cost ] of both-ends ]
This will just add the costs of the two patches under the turtles at both ends of the link. (If you had links traversing more than two patches, this approach would not work and things would get much more complicated.)
For calculating distances afterwards, I'd suggest you take a look at the NW extension. Its weighted-distance-to primitive uses Dijktra's algorithm internally.

Related

Introducing probabilities to patches to replace each-other

I want to create a model which stimulates cell replication in human tissues. To do this I will only be working with patches and not turtles.
A key concept to cell replication is fitness. Fitness in simplified terms is how 'strong' a cell is to replace the cell next to it.
Initially I created a tissue like stimulation where each color is a cell type with a fixed fitness 100. Then I introduced a mutated cell whose fitness ranges from 90 to 110. What I want to do now is introduce probabilities for cell replication based on different fitness values.
So if we have 2 cells next to each other, one with fitness 95 and the other with fitness 100, I want to have a code that says the cell with fitness 100 has a 75% to replace the cell with fitness 95. Of course this should go across the ranges from 90-110 and this probability will depend on what the fitness values of cells next to each other have.
patches-own [ fitness ]
to setup
clear-all
setup-patches
reset-ticks
end
to setup-patches
ask patches ;; randomly set the patches' colors
[ set fitness 100
set pcolor (random colors) * 10 + 5
if pcolor = 75 ;; 75 is too close to another color so change it to 125
[ set pcolor 125 ] ]
end
to go
if (variance [pcolor] of patches) = 0
[ stop ]
ask patches [
;; each patch randomly picks a neighboring patch
;; to copy a color from
set pcolor [pcolor] of one-of neighbors
set fitness [fitness] of one-of neighbors
if fitness > 100
[set pcolor 65]
]
tick
end
to mutate
;let mutateSet [patches with [ pcolor = 125]]
ask patches
[
if ( (random-float 1) < 0.05 ) [
set pcolor 65
set fitness ((random 20) + 90)
]
]
end
This is what I have so far, and I cannot figure out how to introduce this probability parameter accordingly inside the go section. I saw somewhere the rnd function helps with probabilities, but it was using turtles and not patches.
One very important tip I want to give you is to think about the stochasticity and scheduling in your model. Currently your agents take their action one at a time, with the order within each tick being randomised. This means that the order in which the patches change their pcolor has an influence on the outcome.
A way to circumvent this is to ask turtles twice. The first one lets each patch choose whether or not they want to change, the second ask actually does the changing. That way they all choose before any of them change.
The segregation model is a good example of that (it uses turtles but that doesn't make any important difference).
This choosing part (probably a separate procedure that you write) is where the magic happens. You can have each patch check their own fitness and the fitness of all nearby patches ([fitness] of neighbors). When you have these fitness values, you can use them to calculate the probabilities that you want (which depends completely on what you are trying to model).
When you have all your probabilities, you can use one of various methods to determine which one is randomly picked. I'm not going to write this out as there are already numerous examples of this exact thing on stackoverflow:
Multiple mutually exclusive events and probabilities in netlogo
In NetLogo how do use random-float with known percentage chances?
Netlogo - selecting a value from a weighted list based on a randomly drawn number

Making links based on turtles-own variable

In this example I am trying to make two directed-link-breed links based on turtles-own Value. The condition is as follows ask turtles to link to other turtles who have a smaller Value than me.
directed-link-breed [active-links active-link]
turtles-own [ Value ]
to setup
crt 100 [setxy random-xcor random-ycor set value random 500]
ask turtles [ create-active-links-to min-n-of 2 other turtles [Value < myself] ][set links to have xxxxx ]
end
when I do the following:
ask turtles [ create-active-links-to min-n-of 2 other turtles with [value < [ value ] of myself ] [set links to have xxxxx ]
I am getting the following error:
Requested 3 random agents from a set of only 2 agents.
error while turtles 8 running N-OF
called by procedure GO
called by Button 'go'
That message is telling you that (for the particular lettuce), there were only two that satisfied the criteria, but you wanted to link with 3 of them. Looking at your question, you want turtles to send links to turtles with lower values of a specific variable. What do you want the turtle with the lowest value of that variable to do? Clearly it can't link to lower valued turtles because there aren't any.
I'm also not clear why you are using min-n-of. Do you want to link to the lowest valued turtles (in which case every turtle will send links to the same few turtles) or do you want to link to randomly selected turtles with lower values (in which case turtles with higher values will have more choices)?
There are a couple of ways you can handle this once you have the logic sorted out. If you definitely want the lowest value, then use min-n-of first to find the candidates, then link to any with a lower value than the asking turtle. If you want to randomly select from a potentially larger group, use up-to-n-of instead of n-of. Or you could count the number found before trying to link to make sure there's enough.

Deleting several links in a complete directed weighted graph of turtles

I have n turtles which all have links with one another. That means I posses a complete directed weighted graph. I've managed to locate the links with maximum value per each turtle, but now I want to delete all other links without maximum value, again per each turtle.
I'm using the [die] function but in order to distinguish the set of links per each turtle and then clear links from each set
This is the line of code I'm using:
> ask turtles
[
> ask my-in-links with [trust < max [trust] of links with [other-end] = ] [die]
]
However, I thought instead of using other-end function I could also use this line of code
> ask turtles [
ask my-in-links with [trust < max [trust] of links with [out-link-to] = ] [die]
]
My initial thought was by using one of the two functions (other-end/ out-link-to), I could create a common attribute between the set of links.
My main problem is
I'm not sure whether the functions (other-end/ out-link-to) are correct
I don't know what to write after the "=" expression
I don't know which turtle the ask turtle function begins to work with
Here's a complete model that does what you are trying to achieve.
links-own [trust]
to testme
clear-all
; create complete directed network
create-turtles 10
ask turtles
[ create-links-to other turtles
[ set trust random 50
]
]
; display complete network briefly
layout-circle turtles 10
type "average trust value is: " print mean [trust] of links
wait 5
; delete all except highest trust for each turtle
ask turtles
[ let keeper my-in-links with-max [trust]
ask my-in-links with [not member? self keeper][die]
]
type "average trust value is: " print mean [trust] of links
end
I have it showing the initial network and printing average trust values so you can see what's going on.
The important concept here is that my-links is an agentset (in particular, it's a linkset because the agents in the set are links). Since what you're trying to do is just look through the links that are connected to one turtle, then it doesn't matter which turtles are at the other end and you don't need to refer to the other end at all.
You can simply look at the set of links that attach to the particular turtle (my-in-links or my-links or my-out-links) and then look at the values of trust for those links. I have used with-max to find the one with the highest value and then used set membership. But you could also use
ask turtles
[ let upper max [trust] of my-in-links
ask my-in-links with [trust < upper] [die]
]
which is closest to the code you wrote in your question.

Maintaining two states of turtles netlogo

I am doing a small university project. In which i have to maintain 2 states of turtles.
1. Disperse
2. Explore
Disperse :
In dispersion, when at the start all the turtles (20 turtles) are at 0,0 they should disperse from each other. Every turtle has a radius of 2 patches around it, no other turtle should be in that radius. All turtles should go far until they all attain this radius. then other behavior will be called i.e. Explore.
Explore:
In explore , they have to explore the world and avoid different types of obstacles. When ever two turtles come close to each other above mentioned radius then state should be changed to disperse.
I have procedures for obstacle avoidance, and move-speed, and all other individual behaviors under Disperse and Explore. But i don't know how to join all this in one simulation.
It is unclear that you really need to maintain turtle state, since you will have to repeatedly check for other turtles in any case. But since you said you wanted that, you can use turtles-own. For example:
turtles-own [state]
to setup
ca
crt 20
end
to go
ask turtles [set-state]
ask turtles [move]
end
to set-state ;;turtle proc
ifelse (any? other turtles in-radius 2) [
set state "disperse"
] [
set state "explore"
]
end
to move ;;turtle proc
if (not member? state ["disperse" "explore"]) [
error "unknown state"
]
if (state = "disperse") [
disperse
]
if (state = "explore") [
explore
]
end
to disperse ;;turtle proc
move-to one-of patch-set [neighbors] of neighbors
end
to explore
move-to one-of neighbors
end
You might want to take a look at Moore machine and Automata, NetLogo works great with those.
A Moore machine can be seen as a set of 5 elements that interact with each other, in this particular example the start state(S0) would be Dispersing. In NetLogo you can use the word run that receives a string. You'd had to make a procedure that returns a string (say "explore") by checking the actual state of a turtle.
I made something like that a few months ago. We were trying to make a hunter-prey model for polar bears and seals (or wolves and sheeps) based on Moore Machines.
You can use the example of #Alan of course, I just skimmed through and I believe it was fine.
Here is my example based on Moore Machine. It's in spanish but the idea is the same.

Is there any better way to access patch and turtle 's variables at each tick?

I need to update each turtle's wealth variable at each tick which by number of calls is on top of my profiler list.
I am sure the most computation time goes to calculating how many other agents are using same patch for their home and calculating the share for each turtle. food_carrying and my-home are turtle's properties and Storage is patch property.
to update-wealth
let h my-home
set wealth ([Storage] of my-home / (Count agents with [my-home = h])) + food_carrying
end
Can you think of any better way to do it ?
Name Calls Incl T(ms) Excl T(ms) Excl/calls
UPDATE-WEALTH 9744912 831703.608 461086.654 0.047
I think it would be much faster to do this by patch rather than by turtle, since each turtle on a patch seems to be calculating the same value of wealth. Perhaps something like:
ask patches [
let my-turtles turtles-here
if any? my-turtles [
let w Storage / count my-turtles
ask my-turtles [set wealth w + food_carrying]
]
]
]
If you kept a running set of patches with turtle homes you could just ask that subset and not have to worry about there not being any on the patch and getting a division by zero.