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

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

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: Creating Variable from Distance to Specific Patch

I have an issue in Netlogo giving agents the variable with distance (in patches) to a chosen patch that is water.
The agents are supposed to act according to their distance to water.
I want the agent to find the closest patch that has water = true, calculate the distance to that patch and save that specific distance in a variable that may change if the agent moves.
So the way I went about this is as follows:
turtles-own
[
next-river-patch
distance-to-river
]
to go
ask turtles
;find closest patch that has water = true
;calculate distance to that patch and save it in the turtles-own variable "distance-to-river"
set next-river-patch (min-one-of patches in-radius 100 with [water = true] [distance myself])
set distance-to-river distance next-river-patch
;check whether there is water in peception radius
;and whether directed walk is initiated
ifelse random-float 1 > water-directedness
and
patches with [water = true] in-radius 300 = true
[walk-directed]
[walk-undirected]
tick
end
to walk-directed
;deviation of random normal is smaller, the further from next 1st order river and is raised by a high pioneering rate
;with half the distance of the perception radius of 300 resulting in a deviation of 45 degrees and
;a pioneering rate of 0.5 leading to no influence onto the final deivation and a pioneering rate of 1 doubling the deviation.
let deviation 90 * ( 1 - distance-to-river / 300) * ( 2 * pioneering-along-rivers)
face min-one-of patches with [ water = true][distance myself]
rt random-normal 0 (1 * deviation)
fd 1
end
to walk-undirected
ask turtles [
rt random-normal 0 (2.25 * descision-randomness * 100)
fd 1
;it turns into a cumulated normal direction (with the chance of moving straight ahead being highest) and the standard
;deviation being defined by the descision-randomness-slider, with default being 45° and max being 225°
]
end
so the issue I am having is that the line
"set next-river-patch (min-one-of patches in-radius 100 with [water = true] [distance myself])"
returns a variable containing
the coordinates of the next patch that is water. Anyhow, i dont want the coordinates, but the distance to that patch so that i can use it in my cumulated direction.
set distance-to-river distance next-river-patch does not work.
Does anybody have an idea as to why that could be? I am relatively new to NetLogo so the answer may possibly seem very obvious.
Thank you very much!
P.S.: Another, possibly unrelated issue with my model is that agents turn dozens of times before each tick instead of each agent doing a single turn before going forward by 1.
So if you happen to see the solution to that by looking over the code, i would also be forever grateful.
Easiest problem first:
Why are some turtles moving more than once per tick?
walk-undirected is called by turtles, and it does ask turtles. So every turtle is making every turtle so the action. It should be like the other procedure and just do that action, no ask.
patches ...... = true
This is an error. You are comparing the agentset made by patches with ... to the boolean value true. An agentset can never be true OR false. An agentset can be empty, or have one or more agents in it. The any? reporter is used to determine if an agentset has any members.
The main question
It looks like you are already doing what you are asking. The line starting set nearest-river-patch gets the nearest water patch, and the line set distance-to-river calculates the distance to that patch. Not that if there is not any water in radius 100, next-river-patch will be nobody.
I don't see anything that would give you the coordinates (as a list?). Maybe try some parentheses to make sure the reporters are being evaluated in the right order:
set next-river-patch (min-one-of ((patches in-radius 100) with [water = true]) [distance myself])
You could also use with-min
set next-river-patch one-of ( ( ( patches in-radius 100 ) with [ water = true ] ) with-min [distance myself] )
Otherwise, correct the other errors and see what happens.

NetLogo: calculate distance to other turtles and make them dissatisfied if another turtle is too close

I sprouted my turtles over the patches and in the 'go' I want each turtle to calculate if another turtle is too close (based on a certain threshold of X meters/patches). If another turtle is too close, i want the turtle to become 'dissatisfied'.
Movements of the turtles are not required.
What is the best way to do this? The only piece of code for the go procedure I have so far is as follows:
to go
ask n-of initial-number-tourists (patches with [ lagoon_proximity = 1])
[
sprout 1 [
set color black
set size 10
]
]
tick
end
Kind regards!
Your code is creating more turtles and doesn't have anything about checking distances so it is not clear how it is related to the problem you are trying to solve. However, the following code measure the distance to the nearest turtle and prints a message if that is less than 5.
ask turtles
[ let nearest min-one-of turtles [distance myself]
if distance nearest < 5 [ show "I feel crowded" ]
]

NetLogo: how to let turtles return after one tick

hope you can help me out! I have a NetLogo question about the following code:
breed [tourists tourist]
turtles-own [satisfaction]
to setup
clear-all
reset-ticks
end
to go
tick
ask n-of initial-number-tourists patches
[
sprout 1 [
set color white
set size 1
]
]
ask turtles
[ let nearest min-one-of turtles [distance myself]
if-else distance nearest < 0.0000001 [ set satisfaction 0 ]
[ set satisfaction 1 ]
]
ask turtles with [satisfaction = 0] [die]
end
The intention is to sprout "initial-number-tourists" at the start of each tick. Then, they calculate their 'satisfaction' based on the if there are turtles close and let the turtles that have a satisfaction of 0 die. Then, the turtles that remain (that are satisfied) will be sprouted again and their satisfaction will be calculated again.
The code is working, however, with unexpected outcomes. What happens is that the turtles are all sprouted, and they all die each tick. The next tick; they are sprouted again and all die again. Even if i set the distance threshold really low like i did in the code I provided.
Hopefully you can help me out!! Kind regards
The problem you are encountering is that the nearest turtle to each turtle is itself! Thus the distance to nearest is always zero and satisfaction will be set to zero for every turtle. What you want is the closest among the other turtles,
let nearest min-one-of other turtles [distance myself]
(Your distance threshold will mean that all turtles will have satisfaction of 1, but I assume that is what you wanted to accomplish in your testing. Since turtles are sprouted at the centers of the patches, a threshold of < 1.0 would accomplish the same thing.)
Minor point: the convention is to put tick at the end, not the beginning of the go procedure.

NetLogo: measure real travel distances around obstacles before choosing destination

I'm modeling territory colonization in Netlogo. Turtle 0 selects a territory center, then builds a territory by adding patches in order of value. Value is based on benefit / cost, where benefit is amount of food in the patch and cost is the distance from the territory center. Turtle 0 finishes building its territory once the summed patch values meets a threshold. Next, Turtle 1 sprouts and repeats the process to select its own territory, then Turtle 2, and so on. Importantly, new turtles should avoid traveling across other territories and cannot picked already-owned patches.
The Problem: Turtles need to travel around patches that are already owned. A patch's value (benefit/cost) should account for costs accurately based on the real travel distances required, not euclidean distance.
IMAGE: As a visual, here is one result from running this model. The turtle in lime green has a huge territory that required traveling over other territories and did not account for actual travel costs around what should be considered obstacles.
How might I code this to account for actual travel distance required (i.e., around obstacles of the existing home ranges) to have turtles pick patches in order of real value? Some code is below. Any ideas? Thanks in advance!
patches-own [
benefit ;; ranges 0.1-1 and represents food available in the patch
owner ] ;; patches are owned once selected for a territory
turtles-own
[ sum-value ] ;; sum of values of patches in my territory
globals [threshold = 25]
to setup
ask patches [ set owner nobody ]
end
to go
ask turtles [build-territory]
tick
end
to build-territory
if sum-value > threshold [stop] ;; keeps picking patches for territory until I've met my threshold
pick-patch
reproduce ;; sprouts a new hatchling when done building territory
end
to pick-patch
let _destination highest-value ;; this is calculated in reporters, below
ifelse _destination != nobody [
face _destination forward 1 ;; turtle will need to avoid obstacles here, haven't figured that out yet.
if patch-here = _destination
[ claim-patch _destination ]
]
[stop] ;; if there is no _destination
end
to claim-patch [_patch]
ask _patch [set owner myself]
set sum-value sum-value + (benefit / (distance start-patch))
set pcolor [color] of self
;; etc....
end
to reproduce
if sum-value > threshold [ ask patch 75 75 [sprout 1 ] ]
end
;;;; --reporters for calculations:--
to-report highest-value ;; calculates value, benefit / cost.
let available-destinations edge-patches
report max-one-of available-destinations [benefit-to-me / cost-to-me]
end
to-report edge-patches ;; constrains to searching along edge of territory so it remains contiguous.
report (patch-set [neighbors4] of territory) with [owner = nobody]
end
to-report benefit-to-me
report mean [benefit] of patches in-radius 2 ;; this is basically a moving windows analysis to find a cluster of high-benefit patches.
end
to-report cost-to-me
report distance myself ;; this is where turtle needs to account for actual travel costs (non-euclidean distance), including around other territories. how to accomplish?
end