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
]
]
]
Related
I want to move turtles to one of patches not fully occupied (n-jobs-to-fill != 0). The code is
ask turtles [move-to one-of patches with [n-jobs-to-fill != 0]
After each allocation turtle-patch, n-jobs-to-fill (that works like a counter) counts one place less
ask patches [set n-jobs-to-fill n-jobs-to-fill - 1]
Can you help me to do that iteratively (for each tick) and to test it by printing each movement turlte-patch in the observer line?
Thank you
The part where you ask patches to update their variable should not be done with a general ask patches, as this will target all patches and either be called too seldom (if you do it just once per iteration of go) or too often (if you do it every time a specific patch receives a turtle).
To easily target the specific patch that just received a turtle, and exactly the amount of times that it receives a turtle, make the moving turtle ask its new patch to perform the update. Or even better: given that turtles can directly access the patches-own variables of the patch they are standing on, you can do:
patches-own [
n-jobs-to-fill
]
to setup
clear-all
reset-ticks
ask patches [
set n-jobs-to-fill random 10
]
create-turtles 10
end
to go
if (not any? patches with [n-jobs-to-fill != 0]) [stop]
ask turtles [
if (any? patches with [n-jobs-to-fill != 0]) [
move-to one-of patches with [n-jobs-to-fill != 0]
set n-jobs-to-fill n-jobs-to-fill - 1
show (word "I moved to " patch-here ", which now has " n-jobs-to-fill " jobs to fill")
]
]
tick
end
I am very new to Netlogo and I am trying to simulate culling of turtles within a certain region based on the characteristics of the patch. Within my model landscape, I have one single patch that is orange. I want 50% of turtles within 5 pixels of that patch to die. I would also like this process to continue every year after the first year of the simulation (this part I think I have). Can anyone help me?
if d = 10 [
if year >= 2 [
let ring nobody
set ring patches in-radius 5 patches with [pcolor = orange]
ask turtles-on ring [die]
]
]
in-radius reports agents that are within a certain distance from the caller, so the orange patch must be the one calling in-radius.
The code in your example is not reproducible, so I'll make an arbitrary example:
to setup
clear-all
ask one-of patches [
set pcolor orange
]
create-turtles 100 [
setxy random-xcor random-ycor
]
end
to go
ask patches with [pcolor = orange] [
ask turtles-on patches in-radius 5 [
if (random 2 < 1) [die]
]
]
end
Note that in-radius reports agents within a certain distance, where the unit of measure of distance is a patch`s side.
Identifying patches within a certain distance and then asking all turtles on those patches to die (as in the example above, which follows the approach in your question) has a different effect than asking all turtles within a certain distance to die.
Which of the two approaches fits your case is something that you have to decide. Below, the code that would address turtles directly:
to go
ask patches with [pcolor = orange] [
ask turtles in-radius 5 [
if (random 2 < 1) [die]
]
]
end
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 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.
So in this project a obstacle is created with:
set obstacle patches with [ abs(pxcor) < 15 and abs(pycor) < 15 ]
crt 1 [ set size 30 set shape "square" set color gray set heading 0 ]
Now I want to distribute my patches around the obstacle, but I can't figure out how to place them anywhere but on the obstacle. So I'm looking for something like (but this gives a syntax error):
move-to one-of patches with [ patch not obstacle and not any? turtles-here ]
try this move-to one-of patches with [ not member? self obstacle and not any? turtles-here ]. You are already telling NetLogo you want to test the patches by specifying patches with, you don't need to repeat that for the obstacle test.