how to set a patch-variable as the number of turtles-here - netlogo

I want to set a patch variable as equal to the number of turtles on that patch.
I have tried
ask patches [set variableA (count turtles-here)]
when i do this, i get the error: "set expected 2 inputs"
i tried to use a workaround by setting the turtle-count as the plabel
ask patches [set plabel (count turtles-here)]
that code worked, but i need to do this for multiple variables and when i tried to transfer the plabel over to the variable
ask patches [set variableA plabel]
i again get the error: "SET expected 2 inputs"
any help is appreciated.

Are you sure that variableA is truly a patch variable? If it is, your first line should work. The following program, for instance, compiles and executes without error:
patches-own [ variableA ]
to test
ask patches [ set variableA (count turtles-here) ]
end
The only case I can see that would be causing the error that you're seeing is if variableA is not a patch variable, but a reporter taking one argument. For example:
to test
ask patches [ set variableA (count turtles-here) ]
end
to-report variableA [ x ]
report 0
end
...will give you the SET expected 2 inputs error.

Related

how to call for separate procedures using the ifelse any? function in netlogo?

In brief below is the code I wrote to show whether there is a turtle on the neighbor patches
to play-the-game
ifelse any? turtles-on neighbors4
[show "turtles-found"]
[show "turtles-not-found"]
end
I need to change it to perform the procedures I have already written out;
if they are the same breed they 'gain-energy'
different breed 'fight-opponent'
I am not sure as to how to change the first part to carry out the other procedures.
If I understand correctly, you need to check the breed of both the asking turtle and the 'opponent' turtle. You're certainly on the right track by checking if there are any neighbors present, the next step is to make the breed check and then the turtles can choose what action to take. For example, look at this toy model where wolves and cows can identify whether they've landed on a patch next to the same breed or not:
breed [ cows cow ]
breed [ wolves wolf ]
to setup
ca
ask n-of 10 patches [
sprout-cows 1 [
set shape "cow"
set color white
]
]
ask n-of 10 patches with [ not any? turtles-here ] [
sprout-wolves 1 [
set shape "wolf"
set color red - 1
]
]
reset-ticks
end
to go
ask turtles [
face one-of neighbors
fd 1
play-the-game
]
tick
end
to play-the-game
if any? turtles-on neighbors4 [
let current-neighbor-turtle one-of turtles-on neighbors4
ifelse [breed] of current-neighbor-turtle = breed [
show "I see one of my own breed!"
] [
show "I see an opponent!!!"
]
]
end
If that's not quite what you had in mind, please edit your question above to provide more detail.
I think you should consider dropping ifelse. In your example, the two possible outcomes of the any? turtles-on neighbors4 condition are mutually exclusive: either there are turtles, or there are not.
However what you want to achieve in your model is a bit different: (I imagine that) on the neighboring cells there can be turtles of the same breed AND turtles of a different breed at the same time. In this case, the two scenarios are not mutually exclusive and using ifelse (where only one or the other of the two command blocks will be executed) would potentially overlook one of the two cases, depending on how you specify the condition.
I think two if statements would be more appropriate:
to play-the-game
if (any-friends-nearby?) [gain-energy]
if (any-opponents-nearby?) [fight]
end
to-report any-friends-nearby?
report (any? (turtles-on neighbors4) with [breed = [breed] of myself])
end
to-report any-opponents-nearby?
report (any? (turtles-on neighbors4) with [breed != [breed] of myself])
end
It is still relevant for you to choose which condition will be checked first (and therefore which action, between gain-energy and fight, will be executed first in case both conditions are satisfied)

error: "a patch cannot access a variable of a turtle without specifying which turtle" in NetLogo

I would like to prevent a turtle from visiting a patch it has visited before. I'm developing the code, but this error appears: error: "a patch cannot access a variable of a turtle without specifying which turtle".
I'm pretty sure it's another syntax error. But, I am not able to find this error. I tried to simplify the code to make it easier for you guys to help me. Any kind of help is welcome.
turtles-own: patchVisited
setup: set patchVisited (list patch-here)
go: set patchVisited lput patch-here patchVisited
move:
let availablePatch [neighbors with [not member? self [patchVisited] of myself and resource-value > 0.2 ]] of patch-here
let neighAvailable count availablePatch
move-to max-one-of availablePatch [resource-value]
Change
let availablePatch [ neighbors with [not member? self [patchVisited] of myself and resource-value > 0.2 ] ] of patch-here
to
let availablePatch neighbors with [not member? self [patchVisited] of myself and resource-value > 0.2 ]
You don't need neighbors of patch-here, that's what neighbors means.
In general: (1) NetLogo highlights the line with the syntax error. That is useful information for us (and you) so include it in the description of the problem. (2) Make smaller changes in your code - get the smaller change working before moving on. In this case, you could have just one turtle moving around and done something like:
ask neighbors with [not member? self [patchVisited] of myself] [set pcolor red]
and turned the patches white again afterwards.

Using the command move-to with max-one-of and the error appears: MOVE-TO expected input to be an agent but got NOBODY instead

I'm new to NetLogo and I have a question that I'm sure is pretty basic. But, I'm not getting over the difficulty.
If anyone can help me overcome this difficulty, I would be very grateful.
I would like from the patch where the turtle is found to consider the 8 neighboring cells in search of the highest pveg value. If it has equally high values, choose 1 of these randomly. Upon finding the highest pveg value of the neighbors, the turtle went there.
I am using the command: max-one-of. I think it serves my purpose. But, I'm making some syntax error that shows the following error: MOVE-TO expected input to be an agent but got NOBODY instead.
Thanks in advance
extensions [ gis ]
globals [ veg ]
patches-own [pveg]
to setup
clear-all
reset-ticks
setup-patches
crt 1 [
ask neighbors [ set pcolor blue ]
set color black
]
end
to setup-patches
end
to go
ask turtles [neighboring]
end
to neighboring
let my-neighWith-pveg [ neighbors with [pveg > 0.2] ]of patch-here
ifelse neighWith-pveg = 0
[ ]
[ move-to max-one-of patches [my-neighWith-pveg] set pcolor red ;;ERROR HERE
]
end
The NetLogo dictionary says, max-one-of needs an agentset and a reporter as input:
max-one-of agentset [reporter]
In your code, you use two agentsets: turtles and my-neighWith-pveg
Since you want to chose from the neighbors (and not all turtles) with the hightes pveg, you can write:
max-one-of my-neighWith-pveg [pveg]

Adaptation of segregation model: how to avoid other breed densities at patch-here

The objective of my submodel is to simulate wolves avoiding patches that have a human density greater than their tolerance threshold. The human agents are created only on the urban-patches (grey), but there are grass-patches (brown) and forest-patches (green) in the NetLogo world as well (See interface tab link below for visual). The human agents are stationary whereas the wolf agents have the option to flee and [find-new-spot] if they are [not happy?] with their current location.
Interface Tab
globals [
grass-patches
forest-patches
urban-patches
percent-unhappy
]
breed [wolves wolf]
breed [humans human]
wolves-own [
happy?
anthro_tolerance
]
humans-own [
human-population]
patches-own [
human-density]
;; creating the urban patches
set urban-patches patches with [pxcor < 10 and pycor < -30 ]
ask urban-patches [set pcolor grey]
ask urban-patches [set human-density human-density = pop-density]
to-report pop-density
report count humans / (count urban-patches)
end
The code for the wolves to determine their happy? level and report the %-unhappy is:
to update-wolves
ask wolves [
set anthro_tolerance 0.049
ifelse (patch-here human-density >= anthro_tolerance) ;;Error message
[set happy? FALSE]
[set happy? TRUE]
]
end
to update-globals
set percent-unhappy (count wolves with [not happy?]) / (count wolves) * 100
end
How do I code the ifelse happy? to represent the individual wolf asking itself "what is the human-density of the patch I'm on, and is it above my anthro_tolerance?"
Also, when I inspect a patch, the human-density variable is zero for all urban patches (even if there is a human on the patch). How can I correct this?
Okay, I can see a couple of problems here. The first is:
ask urban-patches [set human-density human-density = pop-density]
I'm not sure why that's not throwing an error. But anyway, you don't use '=' to set variable values in NetLogo. Assuming your intent is that you want to assign the calculated value of pop-density to the patch variable named human-density, that line should be:
ask urban-patches [set human-density pop-density]
On to your actual error. You have:
ifelse (patch-here human-density >= anthro_tolerance)
The correct syntax for retrieving the value of a variable belonging to some model entity uses the primitive of, so you could write (not tested):
ifelse ([human-density] of patch-here >= anthro_tolerance)
But you can also take advantage of that fact that turtles have access to the patch variables of the patch where they are located. Note that this doesn't work in reverse - turtle to patch is unique because a turtle can only be in one place at a time. However, there can be many turtles on the one patch, so a patch wouldn't know which turtle to access the variable of.
Using that trick gets you to:
ifelse human-density >= anthro_tolerance
There's another trick that you can use because your ifelse is setting a variable to true of false. This is a bit subtle so some people don't use it as it's a little harder to read, particularly if you are newish to NetLogo. But you can replace:
ifelse human-density >= anthro_tolerance
[set happy? FALSE]
[set happy? TRUE]
with:
set happy? human-density < anthro_tolerance
Read this from right to left. First it applies the comparison operator '<' and reports true if human-density < anthro_tolerance or false if not human-density < anthro_tolerance. That value (true or false) is then given to the variable named "happy?".
I answered the last part of my question on my own, but I wanted to post how I resolved the issue if anyone is curious or new to NetLogo like myself..
When I inspected an individual urban-patch, the patch's variable "human-density" would be 0 even if there was a human agent (very clearly) on the patch. I worked this kink out by adding an [update-patches] at the end of my [setup] and [go] procedure.
to update-patches
ask urban-patches [set human-density pop-density]
end
By having this update-patches command called at the end of the [setup] and [go] procedures, the human-density was accurately shown when I inspected a patch.

Expected reporter error in Netlogo

I have tried for a long time, but I cant solve the following error.
In a turtle I want to change the pcolor, into the pcolor of a patch somewhere else in the field. Example:
ask turtles [
set pcolor (ask patch 0 0 [show pcolor])
]
However I just recieve the error "Expected error".
Thank you very much in advance!
If you use
ask turtles [
set pcolor [pcolor] of patch 0 0
]
It does what I assume you want.