Distance-based mortality model in netlogo - netlogo

I am calculating cumulative turtle mortality in netlogo as a function of distance moved by turtles (100 of them) from the origin (start-patch) in the netlogo interface world. In the code below the 'Pass-Away' procedure is linked to a global interface switch called "space-death" which when switched on yields said distance-based mortality undertaken by the procedure called "Pass-Away-Space", otherwise maintains regular per-time-step (tick) mortality undertaken by the procedure called "Pass-Away-Time":
to Pass-Away-Time
ask turtles [
let chances 1 - exp( -1 * mortality * ticks )
if chances >= 1 [die
set dead-count dead-count + 1
]
]
end
to Pass-Away-Space
ask turtles [
let chances 1 - exp( -1 * mortality * [distance start-patch] of turtles)
if chances >= 1 [die
set dead-count dead-count + 1
]
]
end
to Pass-Away
ask turtles [
ifelse space-death [
Pass-Away-Space][
Pass-Away-Time
]
]
end
I get two errors doing this, both likely due to issues with the coding of the "Pass-Away-Space" procedure. The first is Only the observer can ASK the set of all turtles. Then when I move the let chances 1 - exp( -1 * mortality * [distance start-patch] of turtles) outside of the ask turtles[] brackets this error is resolved only yield another that says ** expected input to be a number but got the list* followed by a sequence of numbers. Perhaps due to the fact that the section of the mortality equation that reads * [distance start-patch] of turtles is invoked for multiple turtles (which is exactly what I want to achieve - have each random-walking turtle use its distance from the origin to calculate its cumulative per-step mortality and die above a threshold - an event that occurs at different times for different turtles at different distances). Any thoughts on how to resolve this?

The first error is because you are asking all turtles to ask all turtles. Starting with your Pass-Away procedure and assuming space-death is set to TRUE. What happens? You ask all turtles to run the Pass-Away-Space procedure. That is, each turtle will run that procedure in turn. The first step of that procedure is to ask turtles [ ]. So a turtle is asking all turtles to do something. Hence the error.
To fix it, you need to rewrite the Pass-Away-Space procedure so it runs whatever code needs to be run for a SINGLE turtle (and similarly for Pass-Away-Time). You probably want something like:
to Pass-Away-Space
if exp( -1 * mortality * [distance start-patch]) < 0
[ die
set dead-count dead-count + 1
]
end

So the first issue here is the nested use of the ask turtles [ ... ] command, which we can disambiguate by removing it from the to Pass-Away procedure or alternatively, from both of the other two (preceding) functions nested within it, as seen below (demonstrating the later of the aforementioned means.
let start-patch patch 0 0
to Pass-Away-Time
ask turtles [
let chances 1 - exp( -1 * mortality * ticks )
if chances >= 1 [die
set dead-count dead-count + 1
]
]
end
to Pass-Away-Space
ask turtles [
; let chances 1 - exp( -1 * mortality * [distance start-patch] of turtles)
let chances 1 - exp( -1 * mortality * (distance start-patch))
if chances >= 1 [die
set dead-count dead-count + 1
]
]
end
to Pass-Away
ifelse space-death [
Pass-Away-Space][
Pass-Away-Time
]
end
The second error can be resolved by switching the line of code commented (;) out in the to Pass-Away-Space procedure withe the immediate succeeding line so that when each turtle attempts to execute chances, cumulative per-distance (from origin) mortality is not calculated as an array of turtle chances with the use of the [distance start-patch] of turtles command, but rather per individual turtle with (distance start-patch) whenever the procedure is called by the nesting to Pass-Away procedure.

Related

How to identify neighbors with some conditions

I'm trying to set up a toy model of connected turtles. Every turtle has a random belief [0,1] and I want to implement some conditions:
If it's a turtle with belief > .5 the turtle doesn't do anything.
If it's a turtle with belief < .5 and one of their immediate neighbour has a belief > .5, their new belief (of the turtle with belief < .5) would be belief + .1.
If the turtle with belief < .5 doesn't have an immediate neighbour with belief > .5, the turtle doesn't do anything.
The basic setup:
turtles-own [belief]
to setup
ca
reset-ticks
crt 5 [
set shape "person"
layout-circle turtles 5
set belief random-float 1
]
ask turtles [ create-links-with min-n-of 2 other turtles [distance myself] ]
end
This creates a 5 turtles cycle network. Now they have to check the beliefs of their (two) closest neighbors. I tried this:
to go
ask turtles with [one-of link-neighbors with [belief > .5]] [
set belief belief + .1
]
end
So the basic idea is that every turtle with a connected neighbour with belief > .5 has to add .1 to its own belief. I have tried several variations of the ask turtles with line, but it always shows me a runtime error of this sort: WITH expected a true/false value from (turtle 0), but got (turtle 2) instead. Any ideas? Thank you in advance.
one-of returns an agent. It is not suitable in this case for evaluating a condition with with- the condition is incomplete, since the line ask turtles with [one-of link-neighbors with [belief > .5]] translates (roughly) to "ask turtles with the condition turtle x to do something".
I think what you're after is something that uses count to count the number of believing turtles or any? to evaluate if an agentset exists:
to go-1
ask turtles with [ (count link-neighbors with [belief > 0.5]) > 0 ] [
set belief belief + 0.1
]
end
to go-2
ask turtles with [ any? link-neighbors with [ belief > 0.5 ] ] [
set belief belief + 0.1
]
end

Calculating turtle mortality based on distance from origin in netlogo

I am writing a procedure (Pass-Away-Space) to calculate mortality of turtles moving from the origin (start-patch) through out the world. Each turtle calculates its own mortality based on its distance from the origin (start-patch). The code I am attempting to implement for this procedure is as follows:
to Pass-Away-Space
ask turtles [
let chances 1 - exp( -1 * mortality * [distance start-patch] of turtles)
if chances >= 1 [die
set dead-count dead-count + 1
]
]
end
The error I am getting is expected input to be a number but got the list. I am not sure what the issue is and I was wondering if anyone could point out and rectify the problem with the code.
The problem here is your of turtles. Since an ask procedure affects one turtle at a time, each turtle in your procedure above is evaluating the [distance start-patch] of all turtles instead of just its own distance to the start patch. To clarify, check out the following setup:
globals [ start-patch ]
to setup
ca
reset-ticks
crt 10 [
setxy random 30 - 15 random 30 - 15
]
set start-patch patch 0 0
end
to incorrect-example
ask turtles [
print ([ distance start-patch ] of turtles)
]
end
to correct-example
ask turtles [
print distance start-patch
]
end
Compare the print output of the incorrect-example and the correct-example procedures, and you'll see that when you use [distance start-patch] of turtles you get the list of distances of all turtles. When you ask turtles to evaluate a turtles-own variable (including color, size, etc) each turtle will automatically access its own version of that variable- there's no need to specify which turtle. So, your pass-away-space might look something more like below (untested):
to Pass-Away-Space
ask turtles [
let chances 1 - exp( -1 * mortality * (distance start-patch) )
if chances >= 1 [
die
set dead-count dead-count + 1
]
]
end

NetLogo: How to pull coordinates from neighboring patches based on patch-variable

I have limited programming experience (mechanical engineering student, so a bit of matlab and labview experience) and am very new to NetLogo, so I apologize in advance if this question is pretty basic or my code is of poor quality.
I need to have my turtles move to 1 of 2 possible neighboring patches based on a given probability function. The two patches that I need to input to the probability function are the two neighboring patches with the lowest nest-scent value. I have been able to pull the two lowest nest-scent values, but I cannot figure out how to actually figure out which patches those are, and how to put those coordinates into an ifelse statement to move the turtle to one of them based on the aformentioned probability function. I have the following code that is obviously not working:
to move
set farthest-patch sort-by < [nest-scent] of neighbors
let a1x pxcor of item 0 farthest-patch
let a1y pycor of item 0 farthest-patch
let a2x pxcor of item 1 farthest-patch
let a2y pycor of item 1 farthest-patch
let a1 item 0 farthest-patch
let a2 item 1 farthest-patch
let x (((a1 + a2) / 100 ) - 1)
let probability-move 0.5 * (1 + ((exp(x) - exp( - x)) / (exp(x) + exp( - x))))
ifelse random-float 1 < probability-move
[set to-move 1]
[set to-move 0]
let a1-probability (a1 / (a1 + a2))
ifelse random-float 1 < a1-probability
[set destination [a1x a1y]]
[set destination [a2x a2y]]
ifelse count turtles-here >= 20
[set full 1]
[set full 0]
if [a1x a21] = full
[set destination [a2x a2y]]
if [a2x a2y] = full
[set destination [a1x a1y]]
if [a2x a2y] and [a1x a1y] = full
[set to-move 0]
ifelse to-move = 1
[move-to destination]
[stop]
end
Basically what I have (tried) to do here is sort a farthest-patches list by increasing nest-scent, and I have pulled the two lowest nest-scent values in order to input those values into my probability functions (both for whether or not to move, and if they are to move which of the two patches to select). I am not sure how to properly pull the patch coordinates of the patches that the a1 and a2 values were taken from.
Thanks for any help,
Brad
okay, you are making life way more complicated than it needs to be. You can select the two patches (or turtles) with the smallest values of a variable with min-n-of. Look it up in the dictionary to get the details.
Having found the two candidates, the best option is to use the rnd extension for choosing the destination because it has a primitive for random selection by weight. Finally, since you are using a function of your variable as the weight (rather than the variable value itself), you need a way to construct that weight. The best option is to separate it out - you could also have a second variable with the weight value, but that just proliferates variables.
Here is a complete working model. Please copy the whole thing into a new instance of NetLogo and try and understand how it works, rather than just copy the relevant bits into your code because min-n-of, using agentsets and passing variables to procedures are important aspects of NetLogo that you need to know about. I have also set up colouring etc so you can see the choices it makes.
extensions [rnd]
patches-own [ nest-scent ]
to setup
clear-all
create-turtles 1 [ set color red ]
ask patches
[ set nest-scent random 100
set plabel nest-scent
]
reset-ticks
end
to go
ask one-of turtles [ move ]
tick
end
to move
set pcolor blue
let targets min-n-of 2 neighbors [ nest-scent ]
let destination rnd:weighted-one-of targets [ calc-weight nest-scent ]
move-to destination
end
to-report calc-weight [ XX ]
let weight 0.5 * (1 + ((exp(XX) - exp( - XX)) / (exp(XX) + exp( - XX))))
report weight
end

How can I get an agent to decay as multiple agents feed on it?

In my model I have some agents which act as food items with a set energy. These are fed upon by a number of turtle breeds who each have their own food-energy which is less than the energy of the food item.
The code for the feeding agents is as follows:
to eat
ifelse [food-energy] of myfood > 1.5 [
set food-energy 1.5]
end
and the associated code for the food item to decay is:
to decay
if any? turtles-here [set food-energy
(1.5 * count feeders-here with [myfood = myself]
end
The problem occurs if the energy of the food is not an exact multiple of the amount of energy the feeders can consume. So for example it can go down to 1 and this results in the feeders taking 1.5 units which should be impossible. This is exacerbated when I have different breeds with different food energies (i.e. < or > 1.5).
So my question is how can I get this things to balance?
You need to study the Wolf-Sheep Predation model. This is the first NetLogo tutorial: http://ccl.northwestern.edu/netlogo/docs/tutorial1.html There are five versions of increasing complexity in the NetLogo Models Library, which are covered in chapter 4 of Wilensky and Rand (2015), which you should read.
See some related material here:
http://jasss.soc.surrey.ac.uk/14/2/5.html
Some hints follow, but many details need filling in.
breed [feeders feeder]
patches-own [ food-energy ]
feeders-own [ myfood ]
to setup
ca
ask patches [set food-energy random 50]
create-feeders 500 [
move-to one-of patches
set myfood one-of patches
]
end
to go
ask feeders [move]
ask feeders [feed]
ask patches [growback]
end
to move ;how shd they move?
rt random 20
left random 40
fd 1
;shd movement cost energy?
end
to feed
if (patch-here = myfood) [
let _extracted min (list food-energy 1.5)
set food-energy (food-energy - _extracted)
]
end
to growback
;do you want growback?
end
thanks for your responses. I'll try to implement them. This is one inelegant workaround that worked for me:
to eat
ifelse (food-energy / capacity) < 1 and [meat] of myfood > capacity [
set food-energy 1.5] [set food-energy [meat] of myfood
ask myfood [set shape "square"]]
if (food-energy / capacity) = 1 [
set color white]
if (food-energy > 0 and food-energy / capacity < 1)
[ set color white ]
end
This was initially causing a problem such that when the food energy went down to 0 and I asked it to die any animal looking at the [meat] of myfood lost it's target and I got an error. So I made the animals break this connection once their colour was white.
to ignore
set myfood nobody
set food-energy food-energy * 1
end

Trying to get factions to arrange in segments? NetLogo code advice

I've made a model that aranges factions (turtles in different colours) in a circle.
At the moment they arrange randomly, was wondering if someone could help me arrange them so, for example, red occupies the first 90 degrees, blue the next 90 degrees, etc (on setup).
Here's my code...
ask patch 0 0
[ ask patches in-radius ( max-pxcor * .9) with [ random-float 100 < density ]
[ sprout 1
[ set breed cons
set shape "circle"
set faction random factions
set heading random 360
set size 1
]
]
]
.. guessing I will have to do 360 / fractions, but not sure how to phrase it, if someone could help me out that'd be great. Thanks!
The NetLogo primitive that's the closest to what you want to do is in-cone, which reports the set of turtles that are in the "cone of vision" of another turtle. But your "pie slices" should just be relative to patch 0 0, not to another turtle! No problem: just make a temporary turtle at patch 0 0, use it to get turtles in-cone with the appropriate angle, and kill your temporary turtle.
The following procedure can be used "as is" with your code (just call it from your setup procedure after creating your turtles exactly as you were doing before):
to assign-factions
let angle 360 / factions
foreach n-values factions [?] [
ask patch 0 0 [
sprout 1 [
set heading ? * angle
ask turtles in-cone max-pxcor angle [ set faction ? + 1 ]
die
]
]
]
end
The code is pretty straightforward, except for maybe the more obscure n-values. You could replace it with a while loop if you prefer, but it's really just counting from 0 to factions.
Here is what you'd get with 5 factions: