NetLogo - How to show the current coordinates of a turtle - netlogo

I have been trying for quite some time to show the current coordinates of a turtle in NetLogo. I'm aware that to show one of the coordinates, I could use:
show [xcor] of turtle 0
or
show [ycor] of turtle 0
But how do I show both coordinates?
Thank you.

You can show [list xcor ycor] of turtle 0.
Or, fancier: show [(word "(" xcor ", " ycor ")")] of turtle 0.

if you don't know the exact turtle (of turtle 5) who's coordinates you would like to show but you want to identify the position of specific turtle running a process you can use just
show list xcor ycor ; not need to use self or myself

From
http://ccl.northwestern.edu/netlogo/docs/programming.html#syntax
you can do
show [xcor + ycor] of turtle 5
But not sure if that helps?
sorry.. it doesnt.. 1 sec!

Related

NetLogo: calculate distance to other turtles and make them dissatisfied if another turtle is too close

I sprouted my turtles over the patches and in the 'go' I want each turtle to calculate if another turtle is too close (based on a certain threshold of X meters/patches). If another turtle is too close, i want the turtle to become 'dissatisfied'.
Movements of the turtles are not required.
What is the best way to do this? The only piece of code for the go procedure I have so far is as follows:
to go
ask n-of initial-number-tourists (patches with [ lagoon_proximity = 1])
[
sprout 1 [
set color black
set size 10
]
]
tick
end
Kind regards!
Your code is creating more turtles and doesn't have anything about checking distances so it is not clear how it is related to the problem you are trying to solve. However, the following code measure the distance to the nearest turtle and prints a message if that is less than 5.
ask turtles
[ let nearest min-one-of turtles [distance myself]
if distance nearest < 5 [ show "I feel crowded" ]
]

Coloring a route in Netlogo

I want some of my turtles to leave at trace of the steps they make. I want them to change the color of the patches they pass by on their move. Similar to what the "pen-down" command does - just with the effect, that the patches change color. It is esay for me to change the color of the patch, the turtle reach - but I want all the patches colored - like if you were walking on a lawn, and the grass under you momentaneouesly turns red :-) Nut just the steps with every tick, but the continous route.
Is there a way? I would be glad to se a small coded example. Thanks very much - this homepage does a great job.
Magellancruiser
Here is one basic solution. The key is realizing that instead of having the turtle do something like forward (random 10) + 1 you can instead have it go forward 1 a number of times equal to (random 10) + 1. Because the distances are based on path sizes (1 = 1 patch across), if you color the patches as you go forward 1 at a time, you should "draw" the color on the patch.
turtles-own [ my-color ]
to setup
clear-all
create-turtles 10 [
set my-color color ; the turtles will have random colors, store them to use later
set color white ; but they're easier to see if they're white
]
reset-ticks
end
to go
ask one-of turtles [
left (random 50) - 25 ; wiggle a bit to not just go in a straight line
let d (random 10) + 1 ; the turtle will move 1 to 10 steps
repeat d [
forward 1
set pcolor my-color ; the turtle can directly set the patch's pcolor variable to its own
]
]
tick
end
You can either use setup and go from the command center or add buttons for them.

Netlogo 'towards' behaviour

For a pursuit-evasion assignment I need to use the NetLogo command 'towards', but it doesn't seem to work, or I don't understand it.
What I think it should do: give the angle between the heading of the turtle and the line connecting the turtle with the target.
Here's the code for a simple model I made just to show the problem.
to set-up
clear-all
create-turtles 2
ask turtle 0 [
set xcor 0
set ycor 0
set shape "circle"
]
ask turtle 1 [
set xcor min-pxcor
set ycor max-pycor
set heading 135
]
end
to go
ask turtle 0 [ fd 0.1 ]
ask turtle 1 [ show towards turtle 0 ]
end
And here's a video of the behaviour. https://youtu.be/MUBiAypppc4 (I couldn't find a way to remove the audio without just replacing it using YouTube's current editing system, I'm sorry; you'll have to mute the audio yourself)
Examples of expected behaviour:
from 0:14 to 0:19, I would expect the number to gradually decrease, not increase
at about 0:38, I would expect the number to be 0, not somewhere around 300
between 0:38 and 0:42, I would expect the number to decrease or increase consistently, without those two sudden jumps
Is there a problem somewhere, or does 'towards' mean something different than I thought?
So turtle 0 is moving and turtle 1 is reporting the direction to turtle 0. I think towards is working fine but you have forgotten about the world settings. For example, in the 14-19s part, the shortest path from 0 to 1 is down and left (about 220 heading), but that shortest path is with the world wrapped. Your turtles can move off one side and come in on the other (as you can see turtle 1 doing).
NetLogo measures distances and directions taking into account the wrapping configuration. It knows that the shortest path to get from turtle 0 to turtle 1 goes off the side and comes in the other, and reports the direction that the turtle would have to move to follow that path.
Create a link and you can see this. Revised code:
to set-up
clear-all
create-turtles 2
ask turtle 0 [
set xcor 0
set ycor 0
set shape "circle"
]
ask turtle 1 [
set xcor min-pxcor
set ycor max-pycor
set heading 135
create-link-with turtle 0
]
end
to go
ask turtle 0 [ fd 0.1 ]
ask turtle 1 [ show towards turtle 0 ]
end

In-Cone only works for patch centers Netlogo

I'm having some issues with the in-cone command in Netlogo. I am trying to identify the sum / mean of all the patch variables directly in front of my turtles current location (ie the sum of all the variables it crosses). However, this only appears to be working when my turtle is at the center of a patch (co-ordinates are integers not decimals), which also means I can only move my turtles at right angles. I'm yet to find any other questions pertaining to the same issue on Stackoverflow or elsewhere. So if anyone could offer some insight, I'd be greatly appreciative.
Below is the simple sample code. And I've annotated where making the changes causes this to not work.
Cheers
Paul
turtles-own [value]
patches-own [value-test]
to Set-Up
ca
reset-ticks
ask patches [if pycor > 150 [set value-test 1]]
ask patches [if pxcor > 150 [set value-test 1]]
ask patches [if value-test = 1 [set pcolor red]]
create-turtles 1
ask turtles[
;It works when the turtle is created at the origin (0 0), or at defined coordinates (but not random-xcor random-ycor)
;setxy random-xcor random-ycor
set value 0
set size 10
set color yellow]
end
to go
ask turtles[
;heading has to be 0, 90, 180, or 270.
set heading 270]
ask turtles[
let test mean [value-test] of patches in-cone 10 1
print test
print xcor
print ycor
ask patches in-cone 10 1 [set pcolor blue]
forward 10]
end
in-cone is not the right tool for the job. Unfortunately, NetLogo doesn't have a primitive that looks ahead in a straight line. It does, however, have patch-ahead, which reports a single patch at a given distance. We can use that to build something similar to what your looking for:
to-report patches-ahead [ dist step ]
report patch-set map patch-ahead n-values (dist / step) [ step + ? * step ]
end
This code may look puzzling at first, but what it does it actually quite simple:
It uses n-values to build a list of incrementing values: n-values (dist / step) [ step + ? * step ]. For example, if dist was 1 and step was 0.2, you'd get [0.2 0.4 0.6 0.8 1]. These values represent the distances at which we are going to be looking for a patch.
It uses map to call patch-ahead for each of values in the list and build a list of patches. Note that this list can contain duplicate patches, especially if step is small, since patch-ahead 0.1 and patch-ahead 0.2, for example, may very well be the same patch.
It uses patch-set to turn that list in a proper agentset of patches, without duplicates.
(You could achieve the same thing with a while loop and an incrementing counter, but the code would be longer, more error prone, and much less elegant.)
To use it, just replace instances of patches in-cone 10 1 in your code by something like patches-ahead 10 0.1.
You will notice that there is a trade-off between precision and speed: the smaller step is, the less likely it is to "skip" the corner of a patch, but the longer it will take to run. But I'm sure that you can find a value that works well for you.
Nicolas has a much better answer solving the problem of looking in a straight line but if you simply what look at the patch directly ahead use patch-ahead 1 it works at all angles and coordinates and is much faster than in-cone.
Completely an aside but probably the reason you found this bug is because your cone was set to 1 degree wide and 10 patches long. Long narrow cones tend to break up.

How do I count turtle above, below, and diagonal using netlogo?

I am working on creating a connect four game using netlogo for an assignment. I am having trouble keeping track of how many reds or blues are in a row. (determining the winner)
This is pretty much what I want it to do:
if this circle is red
then add one to successive circles
else
set successive reds back down to 0
if successive reds is 4 (or greater)
show you win buddy/gal
else set successive-reds 0
This is what I have tried and does not work.
ifelse any? patches with [ (pcolor = red) and (pxcor 1)]
[set successive-reds = successive-reds + 1]
[set successive-reds 0]
if successive-reds = 4
[show "you won"]
I have searched stackoverflow for help and found ways to do this but not using the netlogo program.
I have also tried using neighbors and turtles-on and can not find a way to ask turtles to ask their neighboring turtle if their color is the same as the turtle asking.
any help would be greatly appreciated.
Thanks
Argh... I know that, since this is for an assignment, I should just gently guide you into figuring out for yourself how to do that, but this is such a cool little problem, for which NetLogo is so well suited, that I can't resist posting a solution:
to-report wins? ; patch procedure
report member? true map wins-in-direction? n-values 8 [ ? * 45 ]
end
to-report wins-in-direction? [ h ] ; patch procedure
report not member? false map [
([ pcolor ] of patch-at-heading-and-distance h ?) = red
] (n-values 4 [ ? ])
end
Given the above code, a statement like:
any? patches with [wins?]
...will tell you if any patch is the extremity of a winning sequence.
Now you should not use that code unless you understand it. The use of map and n-values probably makes it a little challenging if you are not used to that style, but feel free to ask questions in the comments, and I'll update my answer to address them.
Bonus points: This only checks for a red win. Generalize this so it can check for another color, without naively duplicating the code.