NetLogo: Creating Variable from Distance to Specific Patch - netlogo

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.

Related

Finding the distance to the next patch ahead of a given color in Netlogo

I'm trying to model predators hunting prey through a world filled with obstacles. In an obstacle free world, the predators go on random walks until prey fall within a certain detection radius (react-D), and then move towards the prey to capture. I guess there may be several ways to handle obstacles that block line of sight, but my initial efforts are along the following lines:
ifelse any? prey in-radius react-D
[let target min-one-of prey in-radius react-D [distance myself]
face target
...move towards target if it's closer than the nearest obstacle (blue patch) ahead
]
[continue to random walk, avoiding obstacles
]
The first condition of ifelse may need another ifelse. In any case I think I just need a distance of the nearest blue patch ahead to compare with the target distance, but it's not clear to me how I can get that from patch-ahead.
It would be nicer if I could put all this into the test of the first ifelse, but something like the following doesn't allow for patches that aren't blocking line of sight to the prey:
ifelse any? prey in-radius react-D and min-one-of (patches in-radius react-D with [pcolor = blue])[distance myself] > min-one-of (prey in-radius react-D) [distance myself]
Line of sight is actually really tricky to model in NetLogo. At the moment I do it by creating a turtle (invisible, separate breed called crawler) where the agent is that is looking and have the crawler move ahead in small steps checking the colour of the patch that it is on. That goes inside a while loop and the crawler stops when it finds the wrong colour or when it gets to the target distance. the crawler has to take small steps because crossing over the corner of a patch should still block it and it's a trade-off between resolution (how small the crawler step is) against efficiency.
You can almost certainly do the same thing by creating a patch-set as the union of patches found with patch-ahead of multiple distances like 0.1, 0.2, .... and then checking any? over that patch-set
My current solution, giving patches-own [ ob ] values of 1 to all obstacle patches, then using while to detect obstacles ahead:
to detect-ahead
let dist 1
let last-patch patch-here
while [dist <= react-D] [
let p patch-ahead dist
if p != last-patch and [ob] of p = 1 [
ask p [ sprout-markers 1 [ set color yellow ] ]
set last-patch p
]
set dist dist + 1
]
end
Then it should simply be a case of comparing min-one-of the marker agent set with the target. Obviously smaller increments would be good, as JenB says.

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

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.

Turtles move to nearest patch of a certain color - how can this process be sped up?

I am trying to build a butterfly movement model in which butterflies are attracted to patches of their host plant. This attraction is expressed as a probability which is stored in a variable called "attr-prob". If a butterfly is located within 25 m of a host-plant patch (pcolor = 9.9), it will move to the nearest host-plant patch with the probability of attr-prob.
I wrote the following code:
if (distance (min-one-of (patches with [pcolor = 9.9]) [distance myself]) ) <= 25
[if random-float 1 < attr-prob [move-to min-one-of (patches with [pcolor = 9.9]) [distance myself]]]
This code seems to be doing what I want it to do, however, when I add this part into my model, it slows it down immensely. Does anyone have any alternative suggestions for coding this that might be quicker?
I am running Netlogo in 64-bit Java.
Try something like this:
if random-float 1 < attr-prob [
let target-patch min-one-of (patches in-radius 25 with [pcolor = 9.9]) [distance myself]
if target-patch != nobody [
move-to target-patch
]
]
This should be faster for a couple reasons.
First, the fastest code is the code that never runs. So, doing the probability check at the very beginning allows you to skip computing the closest patch whenever you can.
Second, using in-radius upfront rather than checking the distance at the end reduces the number of patches that you're looking over. Basically, you'll only be checking the color and distance of the patches in the radius, rather than all of the patches in the world.
Finally, in your original code, you were finding the closest patch twice. Instead, you can store the patch in a local variable (target-patch in the code I provided) so you only have to find it once. This alone should double the speed of the code (depending on the value of attr-prob). It also increases readability.

In-Cone only works for patch centers Netlogo

I'm having some issues with the in-cone command in Netlogo. I am trying to identify the sum / mean of all the patch variables directly in front of my turtles current location (ie the sum of all the variables it crosses). However, this only appears to be working when my turtle is at the center of a patch (co-ordinates are integers not decimals), which also means I can only move my turtles at right angles. I'm yet to find any other questions pertaining to the same issue on Stackoverflow or elsewhere. So if anyone could offer some insight, I'd be greatly appreciative.
Below is the simple sample code. And I've annotated where making the changes causes this to not work.
Cheers
Paul
turtles-own [value]
patches-own [value-test]
to Set-Up
ca
reset-ticks
ask patches [if pycor > 150 [set value-test 1]]
ask patches [if pxcor > 150 [set value-test 1]]
ask patches [if value-test = 1 [set pcolor red]]
create-turtles 1
ask turtles[
;It works when the turtle is created at the origin (0 0), or at defined coordinates (but not random-xcor random-ycor)
;setxy random-xcor random-ycor
set value 0
set size 10
set color yellow]
end
to go
ask turtles[
;heading has to be 0, 90, 180, or 270.
set heading 270]
ask turtles[
let test mean [value-test] of patches in-cone 10 1
print test
print xcor
print ycor
ask patches in-cone 10 1 [set pcolor blue]
forward 10]
end
in-cone is not the right tool for the job. Unfortunately, NetLogo doesn't have a primitive that looks ahead in a straight line. It does, however, have patch-ahead, which reports a single patch at a given distance. We can use that to build something similar to what your looking for:
to-report patches-ahead [ dist step ]
report patch-set map patch-ahead n-values (dist / step) [ step + ? * step ]
end
This code may look puzzling at first, but what it does it actually quite simple:
It uses n-values to build a list of incrementing values: n-values (dist / step) [ step + ? * step ]. For example, if dist was 1 and step was 0.2, you'd get [0.2 0.4 0.6 0.8 1]. These values represent the distances at which we are going to be looking for a patch.
It uses map to call patch-ahead for each of values in the list and build a list of patches. Note that this list can contain duplicate patches, especially if step is small, since patch-ahead 0.1 and patch-ahead 0.2, for example, may very well be the same patch.
It uses patch-set to turn that list in a proper agentset of patches, without duplicates.
(You could achieve the same thing with a while loop and an incrementing counter, but the code would be longer, more error prone, and much less elegant.)
To use it, just replace instances of patches in-cone 10 1 in your code by something like patches-ahead 10 0.1.
You will notice that there is a trade-off between precision and speed: the smaller step is, the less likely it is to "skip" the corner of a patch, but the longer it will take to run. But I'm sure that you can find a value that works well for you.
Nicolas has a much better answer solving the problem of looking in a straight line but if you simply what look at the patch directly ahead use patch-ahead 1 it works at all angles and coordinates and is much faster than in-cone.
Completely an aside but probably the reason you found this bug is because your cone was set to 1 degree wide and 10 patches long. Long narrow cones tend to break up.