NetLogo - move turtle to closest turtle - netlogo

I am following the principle of a mixture of the "Bank Reserves" Model and of "Move Towards Target Example" Code Example of the Models Library. But instead of moving a person (turtle) to a random bank (turtle), I need the person (turtle) to move to the nearest bank (turtle). Following my attempt of code:
if cash >= 100[ ;; go to the nearest bank for and depose the money for saving
move-to bank with-min [distance]]] ;; move turtle to closest bank (turtle)
What can I try next?

To move the person (turtle) to the nearest bank (turtle) I used the following line of code:
to go
ask persons [
set cash cash - 3 ;; removes 3$ within each tick
set label cash ;; renews the label
if cash <= 0[
move-to min-one-of other banks [distance myself] ;; moves to the closest bank
]
]
min-one-of and myself were keywords to this solution.

The NetLogo dictionary entry for distance is a very similar problem to yours - in that case finding the turtle that is furthest away. You haven't told distance what to find the distance to, the asking turtle. Try this:
move-to min-one-of banks [distance myself]
However, I would have thought that the way you coded it should have generated an error message so I am not sure if there is another problem elsewhere.

Related

NetLogo consumer behaviour model

I'll give you a brief description of my model:
turtles can be red or green, omnivores and vegetarian respectively
two markets (blue squares): meat and substitutes (products that can be consumed instead of meat) market
When I run the model I would like that turtles start to buy meat or substitutes (only when they are in one of the two markets and based on the market they are in), if they are vegetarian and buy meat turn red while if they stop buying meat and buy only substitutes turn them green (after a certain amount of ticks, which I did not decide yet, they can be considered vegetarian).
to go
move-turtles
market
tick
end
to market
ask turtles
[ ifelse pcolor = blue
[buy]
[right random 360 forward 1]
]
end
to buy
if market-patches [set money money - Meat-Price]
if market-patches1 [set money money - Substitutes-Price]
if money = 0 [stop]
end
I do not understand why when I try to run the model it gives me this error back, and I'm sure the error is contained in the last 3 ifs statements:
IF expected input to be a TRUE/FALSE but got the patch (patch 12 11) instead.
Also if you can help me to structure the go procedure I will be eternally grateful.
This is the overview of the model:
Okay, these are your key procedures:
to go
move-turtles
market
tick
end
to market
ask turtles
[ ifelse pcolor = blue
[buy]
[right random 360 forward 1]
]
end
to buy
if market-patches [set money money - Meat-Price]
if market-patches1 [set money money - Substitutes-Price]
if money = 0 [stop]
end
The logic of your go procedure is that it calls the market procedure and that market procedure gets each turtle to look at the colour of the patch it is standing on - if the colour is blue then the turtle jumps to the buy procedure. What happens in the buy procedure? The turtle has to check whether if market-patches. What is the check that you actually want the turtle to make here? I think you mean 'if I am standing on a patch that is in the patchset named market-patches'. But NetLogo error message is telling you that you have not properly defined what you want the turtle to check.
So, to ask whether this patch that the turtle is standing on is in the market-patches patchset, you need the member? keyword. And a turtle can identify the patch it is standing on with patch-here. This is not tested, but I think it should fix your problem:
to buy
if member? patch-here market-patches [set money money - Meat-Price]
if member? patch-here market-patches1 [set money money - Substitutes-Price]
if money = 0 [stop]
end

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: 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

Netlogo: obstacle avoidance--tell turtles to take shortest path around

Model Details I'm attempting to code territory selection in NetLogo. Turtle 0 selects a territory center, and builds a territory. It select patches based on value of food, discounted by the distance away (food / # patches away from center of the territory), selecting patches in order of discounted value. It stops when it meets total food requirements. Patches selected don't have to be connected to the territory, but should become connected as the turtle walks to the selected patch. These patches crossed are added as travel corridors so that the territory remains contiguous. After Turtle 0 has finished building its territory, Turtle 1 sprouts and repeats the process to select its own territory, then Turtle 2, and so on. Patches belonging to a turtle's territory are unavailable to other turtles.
The Problem Existing territories should be seen as obstacles. Turtles need to identify and go around these obstacles to reach their destination patch using the shortest path possible.
In my current code, turtles will select patches on the other side of an existing territory because they just travel across it. They won't have added a travel corridor to get there, however, because the patches on the way there were already owned.
What are some ways I might tell turtles that if they encounter owned patches (existing territories), they must go around using the shortest distance possible so they can build a travel corridor to the selected patch? I realize this is a complex question, made even more so if actual distances are calculated in (connected question: NetLogo: measure real travel distances around obstacles before choosing destination). I've been racking my brain on this one, so any ideas are helpful. 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
if patch-here = _destination ;; add the patch once reached
[ claim-patch _destination ]
if patch-here != _destination
[ if owner = nobody [claim-travel-patch ]] ;; add a travel patch while walking to selected patch
[ if owner != nobody [avoid-obstacle]] ;; or avoid the obstacle of existing territories
]
[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 claim-travel-patch [_patch]
ask _patch [set owner myself]
set pcolor [yellow]
;; etc....
end
to avoid-obstacle
;; need to identify some means of going around owned patches
;; ideally this will use the shortest route possible....
;; but even if not shortest route, any ideas for identifying a new path
;; to avoid the obstacle and reach the destination patch?
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 patches with [owner = nobody]
report max-one-of available-destinations [benefit-to-me / cost-to-me]
end
to-report benefit-to-me
report mean [benefit] of patches in-radius 1 ;; this is basically a moving windows analysis to find a cluster of high-benefit patches.
end
to-report cost-to-me
report distance myself ;; later, this will hopefully account for actual travel costs (non-euclidean distance), including around other territories. I think this is complicated, so ignoring for now in this question.
end

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