I'm not sure if it is possible, but I want to direct turtles to any 1 patch belonging to a patch-set, since the food spans multiple patches. However, move-to and setxy both give me the error "move-to expected this input to be a turtle or a patch, but got a patch agent-set instead."
move-to (patch-set patch -41 -21 patch -40 -21 patch -39 -22 patch -40 -22 patch -41 -22 patch -42 -22 patch -42 -23 patch -41 -23 patch -40 -23 patch -39 -23 patch -41 -24 patch -40 -24)
If you want to move a turtle to any patch in a patch set, then randomly select a patch from that patch set (using one-of) and move it to that patch. As Seth notes, the turtle can only be in one place at a time. For readability, I would suggest creating the patch set and selecting from it in separate lines, but that's not necessary. Something like:
let food-patches (patch-set patch -41 -21 patch -40 -21 patch -39 -22 patch -40 -22 patch -41 -22 patch -42 -22 patch -42 -23 patch -41 -23 patch -40 -23 patch -39 -23 patch -41 -24 patch -40 -24)
move-to one-of food-patches
At any given time, a turtle can only occupy a single location — some definite x coordinate and y coordinate.
A turtle may appear to take up space on the screen, put that's purely a visual effect, intended for human eyes. Logically, a turtle is a dimensionless point.
Related
I'm using the following code at the command center to show the coordinates of a random patch:
ask one-of patches [show pycor pxcor]
but I get an Expected command error, how can I fix it?
show takes a single argument, and you are passing it two- pxcor and pycor. It depends how you want to display this, but depending on your goal you could do:
observer> ask one-of patches [show pycor show pxcor]
(patch -15 0): 0
(patch -15 0): -15
Or more likely
observer> ask one-of patches [show list pycor pxcor ]
(patch 16 8): [8 16]
show takes only one input, so you need the patch to perform two separate actions. ask one-of patches [show pycor show pxcor] will print both coordinates:
observer> ask one-of patches [show pycor show pxcor]
(patch -9 10): 10
(patch -9 10): -9
If for some reason you don't like the fact that this results in the output being shown on two lines, you can use word and have it reported as a single string: ask one-of patches [show (word pycor " " pxcor)]:
observer> ask one-of patches [show (word pycor " " pxcor)]
(patch -9 4): "4 -9"
Edit Using a list, as in Luke's answer, looks certainly more elegant than using a string with manually-inserted spaces... what a dumb choice lol
I have a fairly simple question: is there a way to define a patch-set based on a patch-variable? In my model, I have a large set of patches that form the food object, and food is a patch-variable. I have the turtles change color when they find themselves on the food, but I have previously manually defined the patch-set on which they change color. However, I want to adjust the location of the food, and I don't want to go through the work of checking and redefining all the food patches. Thanks!!
to setup-food
if (distancexy (0.8 * min-pxcor) (0.8 * min-pycor)) < 5
[set food 5
if food > 0
[set pcolor cyan]]
end
TO look-for-food;; procedure that controls turtle color change on food
let potential-leaders foragers-on (patch-set patch -38 -16 patch -37 -16 patch -36 -16 so on..)
[do things]
end
````````````
you can create such a patch set explicitly like:
let foods patches with [food > 0]
ask foods [ set pcolor cyan ]
This is a local variable that is thrown away (hence let) when the procedure is over, but you can also create a global variable if you are going to need this patchset in lots of places in your code, just remember to update it when the food runs out on a patch.
And you also do this implicitly whenever you use with
ask patches with [food > 0] [set pcolor cyan]
I am trying to get the first turtle to arrive at a specific patch (food source 1) to become a new breed (leader). This is what I tried so far...
ask one-of followers
if followers-at -41 -22
set breed [leaders]]
That gives me the error: Ask expected 2 inputs, an agent or agentset and a command block.
If I add a bracket before if --> [if...], the error becomes
IF expected two inputs, a true false and a command block.
Right now I have create-leaders 0 [set color red] in the to setup procedure, and a leader [leaders breed] at the beginning of the code, because I don't want any leaders to exist before the first turtle reaches food patch -41 -22. I'm still at a loss for how to determine which turtle is the first to arrive at that patch. Any suggestions for that?
I tried this to test whether I could get one follower at a specific patch to change:
to recruit
ask followers-at -41 -22
[ask one-of followers
[set color red]]
end
First, I counted 4 red followers during the course of the run, but the code specifies "one-of" which should only affect 1 follower. Then I modified it to:
to recruit
ask followers-at -41 -22
[ask one-of followers]
[set breed [ leaders ]
end
which doesn't seem like it has the right number of brackets, but is the only way that I don't get an "error no closing bracket" message. Instead, I get "Ask expected 2 inputs, an agent or agentset and a command block."
This is the way the bracketing would work (note that the spacing is not important for syntax, but I structured it so you could see where the code blocks start and end). I also added an any?:
ask one-of followers
[ if any? followers-at -41 -22
[ set breed leaders
]
]
So, the ask is asking one of the turtles of the breed named 'followers' to do everything in the outer code block. The randomly chosen turtle then checks if there are any 'followers' on the patch at coordinates -41 -22 from itself (relative, not absolute). If there are, then the randomly chosen turtle (not the ones on the specified patch) changes its breed to leaders.
What you probably want is to have one of the followers on the patch change breed. That would look like:
if any? followers-on patch -41 -22
[ ask one-of followers-on patch -41 -22
[ set breed leaders
]
]
So I changed turtles-at (relative) to turtles-on (absolute) position, as well as going to the turtles on the relevant patch.
For efficiency (so set of turtles created only once instead of twice), debugging (so you don't accidentally have different coordinates in the two places), a better way of doing this same code is:
let potential-leaders followers-on patch -41 -22
if any? potential-leaders
[ ask one-of potential-leaders
[ set breed leaders
]
]
As for finding out whether it's the first, all you need to do is check that there aren't any existing leaders as part of your conditions (eg not any? leaders)
How do I get only the x-coordinate of a randomly selected turtle from the turtles that exist in a straight line on the x-axis and make it a variable?
For example, here's a sample code, but it doesn't work.
globals [x-only]
;omitted
ask one-of turtles with [xcor < max-pxcor and pycor < 1 and pycor > -1][
set x-only list xcor ycor ;I might be wrong here, but there are no good syntax.
move-to patch x-only 1
]
;omitted
Im making a race model. the function is that the turtles move across the grid horizontally starting at xcor -13 to xcor 13 at speeds that
constantly vary and when a turtle reaches xcor = 13, all of the other turtles (besides the one that crossed first) die
to Race
wait .3
fd random 5
if xcor = 13 ( this is where i want to tell all other turtles to die )
end
how do i ask all other turtles to die?
THe first answer doesnt help me, someone else please respond
You can do that by asking the winner to ask other turtles [die]
to setup
clear-all
reset-ticks
;resize-world min-pxcor max-pxcor min-pycor max-pycor
resize-world -15 20 0 3
set-patch-size 15
;set-patch-size size
create-turtles 10
[setxy -13 1 set heading 90 set shape "car" wait 0.3]
ask patch -13 2 [Set plabel "Start" set pcolor 110] ; just for visualization
ask patch 13 2 [Set plabel "END" set pcolor 110]
end
to go
ifelse count turtles > 1
[
ask turtles
[Race]
]
[stop]
tick
end
to Race
fd random 5
if xcor >= 13 [ set size 2 ask other turtles [die] ]
end
This is a sample screenshot
I really low examples, so there is another way to improve visualization of the race by having multiple lines of cars:
resize-world -15 20 0 5
set-patch-size 15
create-turtles 20
[set xcor -13 set ycor one-of [0 1 2 3 4 ] set heading 90 set shape "car" ]
ask patch -13 5 [Set plabel "Start" set pcolor 110]
ask patch 13 5 [Set plabel "END" set pcolor 110]
What you are trying to do doesn't make much sense. When posting, please make sure that the question provides a better context for answering you question.
My interpretation of your question is that the cars don't matter at all. All you want to figure out is that when one turtle crosses the finish line, you kill all the other turtles.
Doing so I would probably give each turtle a property of a name or label. Store them all in an array.
Then if the turtle crosses the finish line, remove all the turtles from the array except for
if turtle.name == turtle[i].name.
Hope that helps. Please be a little more clear next time.
You need a way to identify the living turtle and kill the other turtles. To do this, you could either write a ton of if statements but it would look horrible.
if(larry.coordinates == 13){
kill(tom);
kill(harry);
}
Your best bet would be to read how to create an array. Store it in an array. Trust me, arrays are pretty simple.