I have two turtle breeds who populate each sides of the window and then only move round in there own side.
The problem I am having is that I want to constantly check to see if one singular instance of a turtle from each breed are both on the same y coordinate. And if this returns true i want both of those turtles to stop, but for all other turtles from each breed to carry on moving. I know you can identify a turtle by there unique ID but i don't know how to use this and how to use the correct syntax.
The best way to describe this in pseudo code would be
ask turtles [
if breed1 turtle ycor = breed2 turtle ycor
[ stop breed1 turtle and breed2 turtle ] ]
UPDATE
Tried getting the code to work but still nothing happening. Not sure if it is the way the procedure is wrote or the number I have chosen for the threshold.
to move-turtles
ask turtles [
if not any? turtles with [ breed != [ breed ] of myself and abs (ycor - [ycor] of myself) < 1 ]
[
ask redteam with [pcolor = green - 3] [
right random 360
forward 1
]
ask redteam with [pcolor != green - 3] [
back 1
]
ask blueteam with [pcolor = green - 2] [
right random 360
forward 1
]
ask blueteam with [pcolor != green - 2] [
back 1
]]
]
end
Note that "same coordinate" is actually somewhat ambiguous. If one turtles ycor is 5.0000001 and another's is 5.0000000, are they at the same coordinate? Because of this, you should check to see if their coordinates are within a certain amount of each other.
Also, the best way to stop moving is to simply not move. So, here is a possible go procedure that would do what you want:
to go
ask turtles [
if not any? turtles with [ breed != [ breed ] of myself and abs (ycor - [ ycor ] of myself) < threshold ] [
move ;; replace with your move procedure or code
]
]
end
Here, each turtle checks to see if there are any turtles of a different breed who's ycor is within threshold of their own ycor. If there are not, then it moves. Otherwise, it does nothing.
The myself stuff is the most confusing part here, so I recommend reading the docs.
Related
I want to make one button in Netlogo with the below code but the problem that count shows me in the observer 0 while it should be 10, any idea why?
breed [ planets planet ]
planets-own [ distance-to-earth diameter heading ]
to create_planets
ca
ask n-of 10 patches [ sprout 1 ]
show count planets
end
You told the patches to sprout but not that you wanted planets to be the breed of turtles that sprouted. This will create generic turtles. Try sprout-planets instead.
I want to move the current turtle one step closer to the others that fulfill a certain condition (e.g. have color = green).
I am doing this the hard way (because I don't know any better), by trying to calculate the the average distance of the current turtle from all others that fulfill the condition, and calculate the average from x+1, x-1, y+1, y-1. Then whichever is the smallest would indicate the direction of the move. Not very elegant, I know, and limits movements to horizontal and vertical, but I couldn't come up with anything better (the only other idea that struck me was to calculate average x and y coordinates of all turtles that fulfill the condition and move the current turtle towards that, but that seemed even more ridiculous to me)
Problem is that even with my clumsy solution, I am not getting anywhere, since I am struggling with how to calculate the average distance from the "green" turtles.
If you want to calculate the mean distance, you can have the asking turtle call mean and [distance myself].
With this setup:
to setup
ca
crt 10 [
set color green
move-to one-of patches with [ pxcor < 0 ]
]
crt 1 [
set color red
move-to one-of patches with [ pxcor > 10 ]
]
reset-ticks
end
Calling the function below will have the red turtle print out first all distances between itself and all green turtles, then the mean of those distances:
to calc-mean-distance
ask turtles with [ color = red ] [
print [ distance myself ] of turtles with [ color = green ]
print mean [ distance myself ] of turtles with [ color = green ]
]
end
Beyond that, I'm not 100% sure what you're trying to do- are you hoping to move the asking turtle towards the nearest turtle that meets some condition? If so, this might work for you:
to go
ask turtles with [ color = red ] [
let target min-one-of ( turtles with [ color = green ] ) [ distance myself ]
face target
ifelse distance target > 1 [
fd 1
] [
move-to target
]
]
tick
end
If you want the asking turtle to move instead towards the geographic center of those turtles that meet a condition, you could indeed get the mean x and y coordinates of those turtles as you describe, then have the asking turtle move towards that point:
to go
let central-x mean [ xcor ] of turtles with [ color = green ]
let central-y mean [ ycor ] of turtles with [ color = green ]
ask turtles with [ color = red ] [
facexy central-x central-y
ifelse distancexy central-x central-y > 1 [
fd 1
] [
setxy central-x central-y
]
]
tick
end
If those aren't quite what you're trying to achieve, feel free to leave a comment for clarification!
I would like a turtle to go to the closest patches with most turtles if a threshold of a given variable is met for 5 ticks.
My code is:
to move
let count-tick 5
if var >= 9.5 [
set count-tick count-tick - 1
if count-tick = 0 [
ask turtle [
let nearest-group min-one-of (patches with [sum turtles >= 3] in-radius 3 ) [ distance myself ]
move-to nearest-group ;; go to the biggest crowd near you
ask turtle [ ;; once there do the following
set shape "star"
set color red
]
]
]
]
end
The issue I have is that a) I am unsure how to say the patch with >= 3 turtles closest to you at the given range of 3 (attempted code above) and b) how to say once there, change your shape.
Revised to keep a permanent variable to track whether the variable is high enough 5 times in a row.
turtles-own
[ count-tick
]
; wherever you create the turtles, you need to `set count-tick 5`
to move
ifelse var >= 9.5
[ set count-tick count-tick - 1 ]
[ set count-tick 5 ]
if count-tick = 0
[ let nearest-group min-one-of (patches with [count turtles >= 3] in-radius 3 ) [ distance myself ]
move-to nearest-group ;; go to the biggest crowd near you
set shape "star"
set color red
]
end
First, you are already within an ask turtles code block from the procedure calling this move procedure. So you don't need the additional ask turtles. Look up ask in the NetLogo Dictionary, it iterates through the turtles, running all the code for each turtle in turn.
Second, you need count turtles rather than sum turtles as sum is to add up values.
Note that there is no error checking in this, you may have problems if there are no patches within radius of 3 that have at least 3 turtles.
How do I get a patch set that contains all patches that the turtle is facing?
I know patch-ahead report the patch with a specific distance. But what if I want to get all patches in this direction instead of the single one with specific distance?
What you can do is hatch a turtle and move it forward until it reaches the edge of the world, adding all the patches it crosses.
Here's a visible version to see the approach:
to testme
clear-all
create-turtles 1 [setxy random-xcor random-ycor]
ask one-of turtles
[ set pcolor red
hatch 1
[ while [can-move? 1]
[ forward 1
set pcolor red
]
die
]
]
end
To actually do the patchset version, you need to start with the current patch and add the patches as the hatched turtle moves over them. Try this for a procedure version and a demonstration of how it can be used:
turtles-own [ my-path ]
to testme
clear-all
create-turtles 1 [setxy random-xcor random-ycor]
ask one-of turtles
[ set my-path get-patches-forward self
print my-path
]
end
to-report get-patches-forward [ #me ] ; turtle procedure
let front-patches patch-here
hatch 1
[ while [can-move? 1]
[ forward 1
set front-patches (patch-set front-patches patch-here)
]
die
]
report front-patches
end
This will return the wrong answer if the world is wrapped because the hatched turtle can keep on going indefinitely. Instead, you would need to check its coordinates rather than relying on the can-move? primitive.
to setup
ca
reset-ticks
ask patches [
set inside? (abs pycor < 10 and abs pxcor < 10)
set exit? false
ask patch 11 0 [ set pcolor lime set exit? true]
]
repeat initial-population [ ; start condition turtles with any other turtles on neighbors
ask one-of patches with [
inside? and (not any? other turtles-here) and (not any? turtles-on neighbors)] [
sprout 1 [
set color blue
set size 1
]]]
end
to go
tick
define-neighbors-radius-2
move
end
to define-neighbors-radius-2
ask turtles [
set neighbors-ahead2 patches at-points [[2 1] [2 0] [2 -1]]
set neighbors-for-y-up2 patches at-points [[2 0] [2 -1] [1 -2] [0 -2] [-1 -2]] with [inside?]
set neighbors-for-y-down2 patches at-points [[-1 2] [0 2] [1 2] [2 1] [2 0]] with [inside?]
]
end
to move
;; my intent to move turtles to exit without their neighbors are occupied by other turtles, ;;that is the 8 patches around turtles are empty until exit?
ask turtles[
ifelse inside? [
if ycor = 0 [ ;strategy to turtles with in front exit
ifelse exit? [
set heading 90
fd .5
]
[
facexy 11 0
if (not any? turtles-on neighbors) and (not any? turtles-on neighbors-ahead2) [
fd .5
]
]
]
if ycor > 0 [ ; strategy to turtles occupied "bottom-side" of inside?
facexy 11 0
if (not any? turtles-on neighbors) and (not any? turtles-on neighbors-for-y-up2) [
fd .5
]
]
if ycor < 0 [ ; strategy to turtles occupied "down-side" of inside?
facexy 11 0
if (not any? turtles-on neighbors) and (not any? turtles-on neighbors-for-y-down2) [
fd .5
]
]
]
[
set heading 90
fd .5
]
]
end
I try to move turtles to exit but not all turtles move, why?
also, turtles must go out with ycor = 0, that is obliques direction don't allow because neighbors will occupied patches aren't inside!
Can't public this question because "looks like my post is mostly code", so talk about of my life:
seriously my intent is to create a crowd in front of exit and set some rules to delay the turtles flow exit, for this I need the neighbors empty to show interaction between agents.
(also accept some suggest to set this interaction) but at the moment turtles reach exit!
thanks
One problem with this code is that have you three ifs where it appears you only want one of them to run each time, but it's possible for more than one of them to trigger. You have:
if ycor = 0 [ ...commands #1... ]
if ycor > 0 [ ...commands #2... ]
if ycor < 0 [ ...commands #3... ]
but the turtle's ycor might be changed by commands #1 or #2. So it's possible that both #1 and #2 might run, or both #2 and #3, and perhaps other combinations as well. I assume you intended that each turtle should only move once per tick, so I recommend you rewrite this as:
ifelse ycor = 0
[ ...commands #1... ]
[ ifelse ycor > 0
[ ...commands #2... ]
[ ...commands #3... ] ]
Something else that should be fixed in this code:
reset-ticks should go at the end of setup, not near the start. It tells NetLogo setup is finished.
tick should go at the end of go, not near the start. It tells NetLogo a tick has finished.
I don't know if either of these problems I've pointed out are actually causing the unintended behavior you're seeing. Perhaps the bug isn't obvious just from reading over the code. In that case, you have two possible courses of action ahead of you:
1) Back up. Throw away this broken code and go back to the last version of your model that contained only code that have you have verified to work correctly. Then try again, but this time, don't add so much new code all at once. Attempt to make a very small improvement to the code, and get that working, before moving on to the next small improvement. And so on. If at any point you get stuck, come here, show your code, and ask a specific question about it. That question should be much easier to answer than your current question. Questions of the form “Here is a big mass of code that doesn't work, help!" are very difficult to answer.
2) Press on by investigating your questions yourself. You write, “not all turtles move, why?” Perhaps someone can answer this just by reading your code; I can't (unless my first guess above is correct). In order to figure it out, I would have to run the code myself and do experiments with it. I'd do things like try running it with just a single turtle and see if that case fails; add print statements to the code, showing each the turtle's coordinates and motions, so I could try and figure out which turtle is going wrong, and exactly under what conditions; and so forth. It's like detective work, or like doing chemistry experiments in the lab.