simultaneously coordination in Netlogo - netlogo

I'm currently trying to implement a model in Netlogo where the turtles’ behaviors depend on all of their neighbors.
My point of departure is the coordination game code provided by:
http://modelingcommons.org/browse/one_model/2549#model_tabs_browse_info
According to this model the payoff of for the turtle is determined by introducing a variable which takes the color of neighbor as its value.
ask turtles [
let his-color [color] of one-of turtles-on neighbors
if color = yellow and his-color = yellow [set payoff A-yellow-yellow set alt-payoff B-red-yellow]
However, I need to my turtles to gain their payoff by comparing their color with all of their neighbors simultaneously. The last part is problematic due to Netlogo's default synchronic update
Can anyone guide me in how to make the update simultaneously and depending on all of the neighbors, or does someone have a reference to a place where this is discussed?

Just collect all colors before changing any of them. E.g.,
turtles-own [nbr-colors]
to go
ask turtles [
set nbr-colors [color] of neighbors ;get list of current colors
]
ask turtles [
set payoff compute-payoff nbr-colors
set color anything-you-want
]
end

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

NetLogo: use value of 'stock' in SDM as input for ABM

I made two simple models; one System Dynamics Model and one Agent Based Model in NetLogo. The SDM has a stock 'tourists' and its value depends on the in- and outflow. The value is re-calculated each tick. The tourists are sprouted in the ABM each tick. Now, I would like to use the value of the touristsStock as an input for the turtles that are sprouted each tick in the Agent Based Model. What is the best way to do this? I have already checked the example codes in the Model Library (Like the Tabonuco Yagrumo model) but it doesn't make any sense to me. What is the best way to integrate these models with each other? Thanks in advance!
The relevant piece of code of the ABM is as given below:
to go
clear-turtles
;sprouts turtles only on patches with value beach AND patches that are close to the lagoon (calculated in ArcMap)
;the initial number of tourists is multiplied by the percentage of tourists that were satisfied in the previous tick.
;percentage of tourists that were not satisfied is subtracted from 100 and divided by 100 to make it a factor between 0-1.
ask n-of (initial-number-tourists * ((100 - percent-dissatisfied) / 100)) (patches with [ beach_close_to_lagoon = 1])
[
sprout 1 [
set color black
set size 10
]
]
ask turtles [
set neighbor min-one-of other turtles [distance myself] ;; choose my nearest tourist based on distance
set distance-tourist distance neighbor ; measure/calculate distance of closest neighboring tourist
]
ask turtles
[ ifelse distance-tourist > 5
[ set satisfied? True]
[ set satisfied? False ] ]
update-turtles ;before the end of each tick, the satisfaction of each turtle is updated
update-globals ;before the end of each tick, the percentage of satisfied tourists is updated in globals
;clear-turtles ;because each tick represents one day, the tourists will leave the beach after one tick so the turtles will die
tick
end

Is there a way to create an enum in netlogo?

I have some netlogo code that I would like to make more descriptive.
So instead of:
MOVE-TO ONE-OF PATCHES WITH [ PCOLOR = BLUE ]
It would say:
MOVE-TO ONE-OF PATCHES WITH [ WATER ]
In java I would create an enum to accomplish this. How can I do it in Netlogo?
Alan's answer is fine, but I would also consider the possibility of creating a patch variable instead of relying on the patch color. For example:
patches-own [ water? ]
If you set this to true for each water patch, you can then say things like:
move-to one-of patches with [ water? ]
The main reason for doing this is that you might want to change the color of the water patches at some point: make them a slightly darker or lighter blue, for example, or use color to temporarily highlight patches with some other characteristic.
Separating presentation and program semantics is generally good practice.
Another, different way to achieve this would be to create an agentset with your water patches during setup. For example, supposing you declare water-patches as a global variable, you would do:
set water-patches patches with [ pcolor = blue ]
And then you can do:
move-to one-of water-patches
The water-patches agentset is not affected if you change the color of a patch. It might also be a bit faster since you only construct it once instead of filtering over all patches over and over again.
to-report water ;patch proc
report pcolor = blue
end
Alan's answer is perfectly fine, but this question suggests to me a different conceptualisation. What you really mean is that the patch is coloured blue because it is water, but you are coding it the other way around so that colour is indicating its status as water. If other aspects of your model (eg travel speed, type of crops) depend on whether it is water or not, then you could consider a different construction.
patches-own
[ water?
]
to setup
ask patches
[ set water? FALSE
if random-float 1 < 0.2
[ set water? TRUE
set pcolor blue
]
]
end
In this construction, you have a true/false variable for each patch that indicates it is water (if true). Then you can directly have statements such as ask patches with [water?] []. You can also set up a global variable that holds the patch-set of water patches and then make statements like ask water-patches []
If you have multiple types of land style (eg water, sand, soil, rock...) then you colour is more likely to be the way to go since you don't want separate variables for all of these. Even then, though, you could have one attribute for land style and have constructions that are ask patches with [ type = "water"]

How do I make turtles avoid patches in NETLOGO?

My agents are boats moving on water, surrounding and at some places within that water are bits of land which need to be impossible to pass. I'm struggling to conceptualize how to tell an agent this information in netlogo.
I've assigned
patches-own
[DEPTH
PASSABLE?
]
with
ask patches with [DEPTH > 0] [set PASSABLE? FALSE]
How do I tell a turtle to not cross over or occupy a patch with PASSABLE? = FALSE while engaging in an otherwise random walk search for
patches in-radius VISION with [DEPTH = 10]
?
sorry for the lack of a reproducible example, but this is a more conceptual question than anything. I will rough out a simple example model if need be.
When your agent is about to take a step forward, you can have them check if they can, then make them pick a new destination if they are going onto dry land
You can do this with Patch-Ahead or In-Cone if you want. Use that to set the destination.
Somthing like:
to walk
"pick destination"
ifelse destination = water [fd 1] [walk]
end
To pick what the possible destination is, you use what the turtle's current heading is like this:
to pick-destination
let destination patch-ahead 1
end

Changing Node ID with every Setup in Netlogo

We try to show a simple infection via Netlogo. For our purpose we need to start the infection with the same turtle for several times.
But right now with every setup another turtle begins with the infection. We already tried to work with the Node ID, but unfortunately the ID of the different turtles changes with every setup, too. We are out of ideas but
maybe there is a way to sove this problem I am happy for any answers :)
This is our Code so far:
extensions [nw]
globals
[
num-informed
informed-size
]
turtles-own
[
informed?
]
to setup
clear-all
nw:load-graphml "JK_nachnamen.graphml"
ask turtles [ set size 1.5 ]
layout-radial turtles links turtle 61
ask turtles [set color red]
ask turtles [set shape "dot"]
ask links [set color grey + 1.5]
ask patches [set pcolor white]
ask turtles [set label-color black]
ask turtles [set informed? false]
ask turtle 72
[
set informed? true
set color green
]
set num-informed 1
set informed-size 2
reset-ticks
nw:save-graphml "JKnachnamennetlogo.graphml"
end
to spread
if (count turtles with [informed? = true] > .7 * count turtles) [stop]
ask turtles with [ informed? = true ]
[
ask link-neighbors with [not informed?]
[
if (random-float 1 <= 0.01)
[
set informed? true
show-turtle
set color green
]
]
]
set num-informed count turtles with [informed? = true]
tick
end
Thank you a lot.
I am a little unclear so am giving bits of different answers for different situations.
If the turtles are different each time, what do you mean by 'the same turtle'. For example, do you mean the turtle in a particular position? If so, you could select the turtle on the appropriate patch.
If it doesn't matter which particular turtle it is (just that it's the same turtle), then the simplest approach is to set the random-seed. Then every time you run any random process (including choosing one-of the turtles to select the starting infection, or ask turtles to do something), NetLogo will use the same chain of random numbers. Of course, if you are still building your model, then adding new pieces of code that change how many calls are made to the random number generator will lead to a different chain, but rerunning with the same code will give the identical run.
You may need to use with-local-randomness and random-seed new-seed if you want to have some parts actually change.
The problem is that nw does not store the WHO variable this is to avoid conflict with already existing turtles in a model.
A work-around would be assigning each turtle a separate id variable and setting that to who.
turtles-own [informed? id]
in turtles creation asign them each the id thus
set id who
you may want to write a conversion procedure like this
to convert
nw:load-graphml "JK_nachnamen.graphml"
ask turtles [set id who]
nw:save-graphml file-name "JK_nachnamen(id).graphml"
end
and use the copy. Of course you would not use
turtle 74
but
one-of turtles with [id = 74]