NetLogo consumer behaviour model - netlogo

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

Related

NetLogo - move turtle to closest turtle

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.

Tell agent to not cross a road in Netlogo model

I'm trying to add a condition that doesn't allow an agent to cross over a road. The road patches are set to color red. I can't seem to figure out how to get this condition to work. I ultimately want the agent to turn around if the road is in the patch ahead. Here is my net logo code so far.
to go
ask turtles [
move
]
tick
if ticks >= 60 [stop]
end
to move
ifelse random-float 1 < q
[
ifelse random-float 1 < w
[let target-patch max-one-of neighbors [veg-suitability]
face target-patch]
[let target-patch max-one-of neighbors [pelev]
face target-patch]
]
[
ifelse [pcolor] of patch-ahead 1 = red
[lt random-float 180]
move-to one-of neighbors
ldd-normal
]
end
to ldd-normal
let ldd-distance (ldd-scale)
fd ldd-distance
end
The logic of your move procedure is a bit confused I think. First you have a random chance to either move to a patch with a higher value of a variable of interest (with the uphill primitive) or, if the random draw fails, it moves to a random neighbour. If you don't want it to move onto a red patch then you need to test if the patch that is chosen is red, but you just move it without checking.
After you have moved the turtle, you then check the colour of patch-ahead. Your problem here is that patch-ahead depends on the direction the turtle is facing, which has nothing to do with the direction it has already been moving. You either make it turn (though it may not turn enough) OR move forward. So it never actually moves away.
I can't give you an actual answer because I don't know what your logic is supposed to be. But you could look at structures like:
move-to one-of neighbors with [pcolor != red]
Or, if there are enough red patches that it is possible that there aren't any non-red neighbours (which would cause an error if you tried to move to one), you could use:
let okay-patches neighbors with [pcolor != red]
if any? okay-patches [move-to one-of okay-patches]
Another option is that you only meant to face rather than move to the patch in the first sections of code, then test whether it is facing a red patch and turn around if it is.

How to make turtles avoid "land" patches while still moving toward predefined target patch?

Im struggling to get turtles to avoid certain patches with a colour green while still pathing towards a target patch.
my netlogo model simulates a countries ocean fishing with boats (turtle) leaving from ports into the sea and returning. Each boat currently sets a target patch in the ocean based on underlying GIS fishing effort data points (patch variables).
Currently, on the way to their target, some boats have to travel out of their semi enclosed ports and if using line of site will travel across land (green patches). I cannot figure out how to get the boats to choose a path which avoids green patches while still pathing towards the same target patch?
Most answers I have found here only accommodate relatively random movement where the heading is changed if a green patch is within a cone X or in front. I am trying to figure out a pathing technique which doesnt involve head towards X until you hit green then go random left or right but this model needs to be able to determine whether heading left or right after hitting green patch (in the cone) is the right way to go. very confused!!
Patches-own
land
vessels-own
home-port ; patch of the home port
target ; target patch for fishing
ask patches ; sets the color of land and sea patches
[ ifelse (land = 1)
[set pcolor green]
[set pcolor blue]
]
to choose-target
set target one-of patches in-radius 60 with [(fishing-
effort > 100) and (fishing-effort < 1010)] ; sets the
fishermen target to one of the patches with a fishing
effort score
end
to movement
ifelse distance target < 1
[stop] ;
[ face target
forward 1
set fuel fuel - 1] ; uses one fuel per movement forwards
if distance target < 1 [ stop]
end
Thanks!

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