how to call for separate procedures using the ifelse any? function in netlogo? - 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)

Related

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

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]

Netlogo: Store agentset in patch property

I am simulating a neighborhood. Patches represent households, turtles people living there.
I want to track "households" and thought it would be convenient to store an agentset of each household at the patch. That would allow me to do easy "household behavior", like ensuring regular groceries.
However, ask homePatch [ set houseHold (turtle-set partner myself) ] just stores 0 in the patch variable.
Is it possible to save agentsets in the patch variable? It is defined in patches-own.
It is possible for a patch variable to hold an agent set, as shown in the following example.
patches-own [ household ]
to test
clear-all
ask patches [set household nobody]
create-turtles 100 [
fd random 10
if any? other turtles-here [
let partner one-of other turtles-here
ask patch-here [set household (turtle-set partner myself)]
]
]
ask patches with [household != nobody] [show household]
end
To know why is seems not to be working for you, we'd need to see more of your code, since the line you provide does work. (Note that if the turtle who is "myself" is sitting on the patch homePatch, it can set the homePatch variable directly with set household (turtle-set partner self)).

netlogo: asking turtles-on patch-here whether breed is the same

Whenever two turtles with equal breeds are on the same patch, they should reproduce.
Therefore a turtle should be able to assess, whether it's alone on a patch and if not whether the other turtle is the same breed.
This is what i've tried:
ask turtles [
if any? turtles-on patch-here with [ breed != [ breed ] of myself ] [
; do something
]
]
But i just don't get it right so that turtles-on patch-here is the right thing for with.
How would i do this properly?
Thanks in advance for any help!
Edit: I'd know how to do it with multiple ifs or elseifs but the model i'm working on is relatively big so i'm trying to program as efficient as possible

NetLogo: set initial turtles number with empty neighbors around?

I ask patches sprout a number of turtles equal to slider initial-population on interface, now i wont that each turtle has neighbors around empty and that each patch contain one turtle. Below my wrong-code
to setup-turtles
set-default-shape turtles "circle"
ask n-of initial-population patches with [(pcolor = white - 1) and (not any? other turtles-here) and (not any? turtles-on neighbors)] [
sprout-normals 1 [
set color blue
set size 1
]
]
end
but no result neighbors empty foreach turtle..why?
I'm not sure that this is really what you are asking because the sentence "that each turtle has neighbors around empty and that each patch contain one turtle" is really hard to understand, but here is my guess:
You want turtles to appear only on patches where they will not have any neighbors. But it doesn't work because at the time you select your patches, every single patch fulfills the criteria (not any? other turtles-here) and (not any? turtles-on neighbors). As soon as you start sprouting turtles, that condition is not true for every patch anymore, but that fact is not taken into account by your code because you have already asked the patches to sprout turtles.
What you need to do is to check for the condition again each time you add a turtle. You can do that using repeat and then one-of instead of n-of:
repeat initial-population [
ask one-of patches with [(pcolor = white - 1) and (not any? other turtles-here) and (not any? turtles-on neighbors)] [
sprout-normals 1 [
set color blue
set size 1
]
]
]