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

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]

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)

How to create a given number of turtles based on a distance criterion

We're creating code and we're having trouble doing the following:
We would like to create a specific number of turtles (for example 100 turtles) based on the following criteria:
distance between agents must be greater than or equal to 2
We've already tried:
to setup
ask n-of 100 patches [
if not any? turtles in-radius 2 [
sprout-turtles 1 [ setup-turtles ] ]
]
]
end
to setup-turtles
set energy 0
set size 1
set color orange
end
it didn't work, because less than 100 agents are being born even though the world holds the required amount of agents, which in this case is 100
Does anyone have any suggestions on how we can resolve this issue?
Thanks in advance
The approach you outline is running into issues because you are asking patches to sprout turtles if they meet some condition. Because the patches are operating in a random sequential order, you are selecting some patches that, by the time it is their turn to act, no longer fulfill the condition to sprout since other nearby patches have already sprouted a nearby turtle.
One option is to use a while loop to keep trying to sprout turtles until the desired number is reached:
to setup
ca
while [ count turtles < 100 ] [
ask one-of patches with [ not any? turtles in-radius 2 ] [
sprout 1
]
]
reset-ticks
end
Be careful with while loops- if you do not code such that the while condition can eventually become false, your model will run forever stuck in the loop.
Another option that will give a more explicit error if it fails would be to just create your number of turtles, then have them move to a space that fulfills the condition:
to setup-2
ca
crt 100 [
move-to one-of patches with [ not any? turtles in-radius 2 ]
]
reset-ticks
end

NetLogo: Having a turtle remember its starting location

I want to have my turtles move back and forth between a central area and their starting location. I have set the central area (patch 0 0, and its neighbouring patches). I have set these turtles to begin from random locations on setup.
Now I need them to move to the central area and be able to remember and return to their respective starting positions. Here is my attempt, but one that is not working.
ask patches
[ set target-patch patch 0 0
ask target-patch
[ set pcolor green
ask neighbors [set pcolor green]
set hold-time 5
]
]
create-turtles 10
[ set shape "car"
set size 1
set color white
setxy random-xcor random-ycor
if (patches != patches with [pcolor = green])
[ set start-position (random-xcor random-ycor)] ;; line with error
]
to go
ask turtles
[ set heading target-patch move-to target-patch
set hold-time hold-time + 5
]
ask turtles
[ if hold-time >= 10
[ set heading start-position move-to start-position]
]
end
There are several problems with your code. I strongly suggest that you code in smaller pieces. That is, add some code and make sure it works before writing the next piece. Making sure it works is not just making it through without error messages, it needs to do what you expect it to do.
On your specific question. The line if (patches != patches with [pcolor = green]) is causing an error. First, patches is the set of all patches, not just a particular patch. So you are (sort of) asking whether the set of all patches is not equal to the set of patches that are green. Is that really what you intended? If so, it is easier to simply ask whether there is any patch that is not green:
if any? patches with [pcolor != green]
or to check whether they are all green and continue if not:
if not all? patches [pcolor = green]
However, since you are asking about moving back and forth to and from the central green patches, I think you really want to have the turtle check whether the patch they happen to be located on is green. This code looks at the patch where the turtle is located (patch-here) and checks whether the color (pcolor) is green:
if [pcolor] of patch-here = green [ ]
However, one of the tricks of NetLogo is that turtles can access the variables of the patch they are on directly. Note that a patch cannot access a turtle's variables because there may be multiple turtles on the patch so the patch doesn't know which turtle you want. But a turtle can only ever be on one patch at once. So you could write:
if pcolor = green [ ]
You also need to rethink this code:
ask patches
[ set target-patch patch 0 0
ask target-patch
[ set pcolor green
ask neighbors [set pcolor green]
set hold-time 5
]
]
This suggests to me that you have misunderstood something very fundamental to NetLogo programming. You need to think from the perspective of an individual agent. Looking at this code, you first do ask turtles, so that is going to run through all the turtles in random order. Let's call them A, then B, then C and so on.
What is each turtle going to do? Everything in the [ ]. So, A sets the value of the global variable named "target-patch" to patch 0 0. Then A asks that patch to turn green, have the 8 surrounding patches to turn green, and to set the variable "hold-time" to the value 5.
So far, so good. But then turtle B does exactly the same thing - it assigns "target-patch", turns it and its neighbors green etc. Then turtle C. If you have 100 turtles, this block of code will run 100 times and do exactly the same thing each time.

Comparing patches

I want to compare the patches in a certain radius regarding the amount of a certain class of agents on them. The agents should move to the patch where the most agents (in this case humans) are. If they are already on the patch with the most humans then they must not move. I coded it and the humans group but most of them don't stay and run around in lines (one behind the other). Would be great if anyone of you could have a quick look at my code. Thanks
if Strategy = "Gathering-Simple" [
if ((count(humans-on max-one-of patches in-radius rad [count(humans-here)] )) ) >= count(humans-here) [
if count(humans-on patches in-radius rad) - count(humans-here) > 0 [
face max-one-of patches in-radius rad [count(humans-here)]
fd 1
]]
]
This is a complete working example that uses your code. Is this displaying the behaviour you mean? It does have turtles chasing each other.
to setup
clear-all
create-turtles 100 [ setxy random-xcor random-ycor ]
reset-ticks
end
to go
let rad 5
ask turtles
[ let target-patch max-one-of patches in-radius rad [count turtles-here]
if count turtles-on target-patch >= count turtles-here ; comment 1
[ if count turtles-on patches in-radius rad > count turtles-here ; comment 2
[ face target-patch
forward 1
]
]
]
tick
end
If so, have a look at the two lines I have comments on.
Comment 1: The >= means that, even if the turtles are already on the highest count patch, this condition will be satisfied because count turtles-here will be equal to the count of the turtles on the highest count patch (this patch).
Comment 2: This line means that as long as there are any turtles on any patch within the radius but not on the particular patch where the asking turtle is located, then the turtle will move forward.
If you want to only have turtles move if not on a maximum count patch, try this instead:
to setup
clear-all
create-turtles 100 [ setxy random-xcor random-ycor ]
reset-ticks
end
to go
let rad 5
ask turtles
[ let target-patch max-one-of patches in-radius rad [count turtles-here]
if count turtles-on target-patch > count turtles-here
[ face target-patch
forward 1
]
]
tick
end
I took out the = in the comment 1 line and removed the second condition entirely so now the turtles move if their current patch has fewer (strictly, not <=) turtles than the patch they've spotted.
I agree with the previous post, but have some additional information.
If you want to move entirely to the target patch on each iteration, instead of moving just one step towards the target patch, in the above answer you could replace the code which produces one step of motion
[ face target-patch
forward 1
]
with
[
move-to target-patch
]
I confirmed by experimentation that the results of the two methods of moving will produce similar but somewhat different results.

How would I "target" patches within a certain area?

I have set up agents and nodes to represent people and stores and it is my intention that the agents will "target" the store in their "awareness" space with the highest value ("vulnerability"). I've largely coded what I have so far through trial and error however setting the turtle's target to the patch with the highest value within a 10 unit radius is a hurdle I can't get over. Currently they target the patch with the highest value regardless of its position in the world. Could somebody suggest what I might consider to achieve this please? I have pasted what I have written so far for reference.
Thanks.
breed [shoplifters a-shoplifter]
patches-own [vulnerability]
shoplifters-own [target
awareness]
to setup
clear-all
setup-patches
setup-turtles
reset-ticks
end
to setup-patches
setup-stores
end
to setup-stores
ask n-of num-stores patches [ set pcolor lime ] ;; create 'num-stores' randomly
ask patches [
if pcolor = lime
[ set vulnerability random 100
]
]
end
to setup-turtles
setup-shoplifters
setup-target
end
to setup-shoplifters
create-shoplifters num-shoplifters [ ;; create 'num-turtles' shoplifters randomly
set xcor random-xcor
set ycor random-ycor
set shape "person"
set color red
]
end
to setup-awareness
ask turtles [
set awareness
patches in-radius 10
]
end
to setup-target
ask turtles [
set target
max-one-of patches [vulnerability]
]
end
You are on the right track using max-one-of. At the moment, however, you are sending patches as the space to search through to look for the one with maximum vulnerability value, when you really want patches in-radius 10. So you could simply do this:
to setup-target
ask turtles [
set target max-one-of patches in-radius 10 [vulnerability]
]
end
However, this is going to be inefficient because NetLogo will have to first work out which are the patches within the radius. You have already asked the turtles to work this out and assign it to their variable 'awareness'. What you really want to do is therefore:
to setup-target
ask shoplifters [
set target max-one-of patches awareness [vulnerability]
]
end
Note that I also changed ask turtles to ask shoplifters. It is only shoplifters who have the attribute 'target' so you should only be asking them to calculate it. Same thing goes for 'awareness'. At the moment you don't have any other breeds so it's not causing an error, but it is good practice to use the breed, otherwise there is no point in creating it.