I'd like to set the value of a patch's variable to be the same as that of one of its neighbours. What'd I'd like is something along the lines of:
ask patches[
let trader max-one-of neighbors [payoff]
set strategy [strategy of trader]
]
in order to set the strategy of each patch to be the same as the strategy of its neighbour that has the highest payoff. The above doesn't work. Can anyone please tell me how to do this? Thank you.
You were very close. You just didn't have the right syntax for of. The dictionary entry shows the proper placement of square brackets.
So, this should work:
ask patches [
let trader max-one-of neighbors [payoff]
set strategy [strategy] of trader
]
Few corrections on your code:
1- you can not name in the same way a turtle-own and a patch-own, so i left
strategy as turtle-own and pstrategy as patch-own
2- neighbors returns an agentset containing the 8 surrounding patches, and not the turtles on them, so, in order to obtain the turtles on surrounding patches you should use max-one-of turtles-on neighbors [payoff] instead of max-one-of neighbors [payoff]
Here's how i would do it:
ask patches[
ask max-one-of turtles-on neighbors [payoff] [
set pstrategy strategy
]
Related
I have N groups of turtles which own the variable group-id (1 to N, N is defined at setup, each group has 25 turtles).
At the beginning each group of turtles spawns on a random patch with variable patch-group-id that matches the turtle group-id, like so:
let i 0
while [ i < n_groups ] [
let x random 100
let y random 100
ask patch x y [ set patch-group-id i set pcolor gray ]
create-turtles 25 [
set group-id i
setxy x y
]
set i i + 1
]
The turtles move around but at a later step in the process I would like them to move back to their ‘home’ patch. Other turtles may also change their group-id and therefore their home patch and I would like them to move there too.
Is there any way to do a comparison along the lines of…
Ask turtles [
Let target one-of patches with [patch-group-id = group-id]
]
And then ask the turtle to move-to or jump to the target?
NetLogo doesn’t like this (‘Min-one-of expected this input to be a number block, but got a true/false block instead’) and I think there are probably other issues too.
patch-group-id is a variable owned by patches and group-id is owned by turtles. So you need to tell NetLogo which group-id the patches are trying to match to. Try this:
ask turtles
[ let target one-of patches with [patch-group-id = [group-id] of myself]
]
Another way to do this is to drop the idea of group-id and simply have each turtle remember its home patch. Conceptually, this implements the idea that a group is entirely defined by its home patch. So your setup would look something like:
turtles-own
[ my-home
]
to setup
clear-all
ask n-of n-groups patches
[ sprout turtles 25
[ set my-home patch-here
]
set pcolor gray
]
end
Then you never need to construct the target, you simply get then to go to their variable my-home.
If you went down this path, you would also need to change the code that uses group-id. For example, you said that sometimes the turtles change their group-id, instead of set group-id [group-id] of ... you would have set my-home [my-home] of ...
I'm working on a land-use model featuring a forested World where turtles (smallholders and companies) have the ability to convert forest into crop-land. I would like to introduce a feature that turtles 'own' the patches they convert and are able to revisit them later to get these patches certified. The main issue is that when turtles move-to crop-land patches to get them certified, they do not only move to those they 'own' but also jump across the world to other turtles' crop-land patches and certify those. I've tried a few different workarounds, but I seem to run into the same two issues eventually:
#1 - error: can't use who in a patch context
I wanted to use the 'who' variable to mark crop-land patches as belonging to the turtle that converted the patch, e.g., turtle 0 goes to the forest, converts it to crop-land and that patch of cropland should be 'owned' by turtle 0, i.e., the patches owned-by variable should be equivalent to the turtle's 'who'. The issue here is that 'who' is a turtles-own variable. So, when I use it in a patch-context it produces an error. For example, ask smallholders [move-to one-of patches with [[owner = who]] --> error.
#2 - can't manage to set a global variable = 'who'
Two, I tried to work around this by using a proxy variable: a globals-variable called 'owner-ID'. I would use set owner-ID who to imprint the turtles individual number to the owner-ID. This seems to work to some extent, namely that the patches' 'owner' variable corresponds to the turtle that converted the patch. It also works when counting how many patches of certified and conventional crop-land turtles own (see set-land-ownership command below). However, when the smallholders-certify-crop-land commands are triggered, turtles don't stick to the patches they own, but 'jump' across the world. When prompting turtles through the command-center ask turtles [print owner-ID] they all return the same owner-ID value. I feel there might be a mistake in my move-to command-line but I just can't find it.
Summary & Question
I want crop-land patches to be 'owned by' the turtles that converted them, and want turtles to move only to the patches they 'own' when certifying crop-land patches, not to patches they don't own. I guess my questions revolve around whether it's possible to somehow use the 'who' variable in a patch-context. And, if not, what a good workaround for the problem could look like.
Relevant code is below (I hope)!
globals [owner-ID]
turtles-own [conventional-land-ownership certified-land-ownership]
patches-own [owned-by owner certified?]
to setup [
ask patches [
set pcolor green ;; green = forest
set certified? "no"
set owner "nobody"
]
]
to go
ask turtles [set-land-ownership]
ask smallholders [check-smallholder-status]
tick
end
to set-land-ownership
ask smallholders [
set owner-ID who
set conventional-land-ownership count patches with [owner = owner-ID and certified? = "no"]
set certified-land-ownership count patches with [owner = owner-ID and certified? = "yes"]
]
end
to check-smallholder-status
if wealth >= 0 and (conventional-land-ownership + certified-land-ownership) < SH_max-land-ownership [
smallholders-choose-activity
]
if wealth < 0 [
set color red
set shape "cow skull"
]
if (conventional-land-ownership + certified-land-ownership) >= SH_max-land-ownership [
set color orange + 2
]
end
;; smallholders-choose-activities is a reporter-based command where turtles choose the most economical option available. One of the outcomes is: smallholders-certify-crop-land
to smallholders-certify-crop-land
let available-patch max-one-of patches with [owner = owner-ID and certified? = "no"] [count neighbors with [certified? = "yes"]]
ifelse not any? turtles-on available-patch [
move-to available-patch
]
[]
set wealth wealth - smallholder-certification-cost
set pcolor brown + 1
set certified? "yes"
end
Your first approach is definitely the way to go and could be fixed with one small adjustment.
ask smallholders [move-to one-of patches with [owner = who]]
should be
ask smallholders [move-to one-of patches with [owner = [who] of myself]]
Within the block after with, variables are in the context of patches, but myself refers to the agent that asked the patches to check their owner, in this case, each smallholder. The global variable owner-ID is then unnecessary. If you carry this through the rest of the code, your second problem may solve itself.
BUT, in general it is best not to use who numbers at all, but rather refer to the agent directly. (You have actually taken that approach implicitly when you initialize owner to nobody, which is "no agent".) I don't see where you ask a patch to set its owner, but if a smallholder is on a patch, the smallholder would
ask patch-here [set owner myself]
and the line above would now read
ask smallholders [move-to one-of patches with [owner = myself]]
The NetLogo gurus suggest that we use who numbers only when there is no other approach.
For example, I would like to get the coordinates of the first patch above, below, to the left and right of the patch the turtle is currently on, as well as the value of a variable for each of those patches, such as plabel or pcolor. Essentially I would like to use this information so that then the agent can make a decision as to which patch to move to.
I think neighbors4 might be how to do this but I'm not quite sure of the code needed. For accessing variable values, I have been trying
let LabelsOfPatches neighbors4 [plabel]
or
let ColorOfPatches neighbors4 [pcolor]
But I get an error saying a command was expected in between the square brackets.
A one-liner that does the same thing would be
let NeighborList [(list pxcor pycor pcolor plabel)] of neighbors4
It can be run by either a turtle or a patch. of is great for making lists of values drawn from another agent or agentset.
I have seemingly done what I wanted by using
let NeighbourList []
ask neighbors4 [set ValuesOfInterest (list (pxcor) (pycor) (pcolor) (plabel))
set NeighbourList lput ValuesOfInterest NeighbourList
]
I've been trying to write a model where turtles create links with a certain number of other turtles, and for those turtles to create links with the link-neighbors of the turtle that linked to it. Right now, I can get turtles to create links with other turtles, but they don't share the same link-neighbors. I want to end up with little sub-groups of turtles of a certain group size. However, at the moment I can only tell turtles to create a certain number of links, but they don't end up in sub-groups because their link-neighbors don't necessarily share the same link-neighbors. I thought I could ask each turtle to ask their link-neighbors to create-link-with [link-neighbors] of myself. I think this would work, except I get an error saying a turtle can't link with itself. I've tried to change the code so it says to a turtle ask link-neighbors [create-link-with [link-neighbors] of myself with [who != self]], but this doesn't work either.
Here is some of my code:
;group size is 1 + (count link-neighbors)
;preferred-size is a slider, used to alter what group size I want turtles to be in
ask turtles
[if (preferred-size > group-size) and (any? other turtles in-radius 1 with [preferred-size > group-size])
[create-link-with one-of other turtles in-radius 1 with [preferred-size > group-size]
ask link-neighbors
[create-links-with [link-neighbors] of myself]
]
Also, is there a term like link-neighbors but referring to all the turtles on a string of connections?
Any help would be greatly appreciated!
You were very close! All you need is other:
create-links-with other [ link-neighbors ] of myself
You cannot compare who to self: who is a number and self is a turtle. And in the context of with [ who != self ]], they would always be variables of the same turtle. In any case, it's usually better to avoid dealing with who numbers anyway: there's almost always a better way to do things.
is there a term like link-neighbors but referring to all the turtles on a string of connections?
I'm not entirely sure that's what you mean, but maybe nw:turtles-on-path-to? Or perhaps you will find some other useful thing in the nw extension.
I'm simulating female animals dispersing from their mother's territory to search for their own territory. Essentially they need to find areas that are unoccupied by other female territories. Patches have a variable owner-fem that identifies which female it belongs to. Ideally, I'd like to have females:
move to a patch,
search within some radius around that patch for any other territory, and if there is another female's territory within that radius to
move to another patch to start the search process again. Below is what I have so far but I don't think I'm using the in-radius correctly.
I'm not sure what the best way is to tell the female to continue searching until the condition is met. Any help would be much appreciated.
to female-disperse
move-to one-of patches with [owner-fem = nobody]
if [owner-fem] of patches in-radius 3 != nobody
[
move-to one-of patches with [owner-fem = nobody]
]
end
If you want to it in "one shot", you could have them move directly to a suitable patch:
to female-disperse
move-to one-of patches with [
not any? patches in-radius 3 with [owner-fem != nobody]
]
end
Note that patches in-radius includes the patch that the turtle is on so there is no need for a separate move-to one-of patches with [owner-fem = nobody].
I don't know what your model requires, but if I were you, I might try to have them disperse a little more gradually. Here is another version that you could call from your go procedure (or any other procedure that runs "forever"):
to female-disperse
ask females with [owner-fem != self ] [
move-to one-of neighbors ; or however you want them to move
if not any? patches in-radius 3 with [owner-fem != nobody] [
set owner-fem self
]
]
end
In this version, all females that are not on a patch where they are the owner move to one of the neighboring patches. They then check if that new patch is suitable. If it is, they become the owner of it. If it is not, they just stop there for now: they will continue searching at the next iteration of go. You don't have to do it exactly this way; it could just be something loosely along those lines.