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
Related
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)
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]
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)).
How to stop / kill two turtles if they come in some radius? Rest of the turtles should not stop moving.
Asking all turtles to check their environment
Here's an example that might be helpful as a first attempt. Since the aim is to signal to a large set of turtles when to continue walking and die, you will need to create a procedure that asks all turtles to check their environment.
Naive Implementation:
to global-step
ask turtles [ ifelse any? other turtles in-radius 3 [ die ] [ fd 1 ] ]
end
The naive implementation works well to remove turtles, but not all turtles are removed, since turtles would disappear with each turtle's check.
Improvements with multiple agents
One way to improve this is through adding a turtles-own'd variable for marking the turtles as within a certain radius of a turtle.
Improved Version:
to global-step
ask turtles [
ifelse any? other turtles in-radius 3
[ set turtle-variable true ]
[ set turtle-variable false ] ]
ask turtles [ ifelse turtle-variable [ die ] [ fd 1 ] ]
end
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.