How to count turtles between specified coordinates in a row?
For example, we want to count turtles that are between (min-pxcor 0) and (max-pxcor 0).
The following is sample syntax, but does not work:
count turtles with [(min-pxcor 0) < (max-pxcor 0)]
Since you are looking for turtles with pycor = 0 and pxcor between a min and max value, say minpx and maxpx, you would write
count turtles with [pycor = 0 and pxcor >= minpx and pxcor <= maxpx]
Note that min-pxcor and max-pxcor are NetLogo reporters giving the edges of the world in the x dimension, so all turtles will have xcors between those two values. If that is what you want, you don't have to test their xcors at all.
Related
So I just started to learn Java, but my prof. just gave us this wild formula which we have to translate into code. I can't figure it out how to make this possible, can someone help me ?
σ means Standard deviation
µ means average
x means The Array x
N means N variables
]
The Σ upper-case Sigma character simply means the sum of.
So, for every data value, subtract the mean (average, in layman's terms) and square the result. Add all of those values together, divide it by the number of data values minus one, then take the square root of that.
Psuedo-code for that would be something like below. First, a function for calculating mean:
def calcMean(collection):
# Initialise for working out mean (sum / count).
sum = 0, count = 0
# Add every item to the sum and keep count.
for item in collection:
sum = sum + item
count = count + 1
# Avoid dive by zero, you choose what to do.
if count == 0:
handle empty collection somehow
# Return the mean.
return sum / count
Then using that to calculate the standard deviation:
def calcStdDev(collection):
# Get mean of the collection, initialise accumulator and count.
mean = calcMean(collection)
accum = 0, count = 0
for item in collection:
# Accumulate each '(item-mean) squared' value.
diff = item - mean
accum = accum + diff * diff
# Avoid dive by zero, you choose what to do.
if count < 2:
handle too-small collection somehow
# Divide and square root for result.
return sqrt(sum / (count - 1))
Now your job is to turn that pseudo-code into Java, something that should be a bit easier than turning the formula into Java.
So, I have a time series with a number of observations per ID. I measure a 1/0 variable at each time point. I want to know how many times a person switches from 1 to 0 or 0 to 1.
There are missing data at random in each ID.
id=c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4)
indicator=c(1,0,0,0,1,1,NA,1,1,0,1,0,NA,1,1,0)
timepoint=c(2003,2004,2005,2006)
td = data.frame(id,timepoint,indicator)
I need code to return the number of switches per person in these data.
To count the number switch from 0 to 1 or 1 to 0 all you need to do is shifting your vector and compare the two shifted versions. And they applying this function by id assuming your data is already sorted in chronological order.
library(data.table)
count.switch=function(x) sum(tail(x,-1)!= head(x,-1),na.rm=T)
td[,count.switch(indicator),keyby=.(id)]
With this method you could specify the switch you would like to count
count.switch.01=function(x) sum(tail(x,-1) == 0 & head(x,-1) ==1 ,na.rm=T)
You could also count both with a (==0 & ==1) | (==1 & ==0)
Another trick would be to count when the shifted vectors add up to 1 element wise. They only do when one element is 0 and the other 1.
count.switch=function(x) sum(tail(x,-1) + head(x,-1) == 1,na.rm=T)
I would like to take the X axis information where there is a turtle with the biggest ID with some specific conditions. And I want to count the number of turtles on the right side of the X axis information. The following is a sample program. I would like to draw a code to obtain X axis information where there is a turtle with the largest ID at the point "here?" in this sample program. Thank you for your advice.
(this link is the 3D image https://i.stack.imgur.com/DQf93.png)
count turtles with [xcor > "here?" ]
There is a lot of ambiguity in your question, but you should be able to change the following to meet your needs.
turtles-own [ID] ;if unique, you can just use `who`
to setup ;make some turtles with various xcors and IDs
ca
crt 100 [
setxy random-xcor random-ycor
set ID random 1000
]
end
to-report top ;get a turtle with biggest id
report max-one-of turtles [ID]
end
to-report winners ;get turtles to right of top
let topx [xcor] of top
report turtles with [xcor > topx]
end
to test
setup
print count winners
end
I want to count the neighbors and the neighbors neighbors of a turtle to kind of find out a way to calculate the eigenvector-centrality. Since I can´t get the NW:extensions to work and my prof neither. I thought of building a method myself. Since I don´t have that much time until the first round of presentation, I try my best to just count the neighbors and the neighbors neighbors of a turtle.
I decided to have a turtles-own, which counts all neighbors and then sum all the neighbors and neighbors neighbors. But I am stuck in my head and cant get it to work.
set Neighborscount count(link-neighbors)
Anyone has any ideas?
Also a final way to get the eigenvector-centrality to work would be very much appreciated.
First, my main recommendation is to get NW working. I'd be happy to help with that.
Barring that, the below assumes you're working with undirected networks. If that's not the cast, let me know.
The problem with the method that you describe is that it will count some neighbors-of-neighbors multiple times. Instead, you can get the agent-set of neighbors-of-neighbors, and then just count it:
to-report neighbors-of-neighbors
report turtle-set [ link-neighbors ] of link-neighbors
end
Then, [ count neighbors-of-neighbors ] of turtle 0 will give the number of neighbors of neighbors of turtle 0.
Now, a few points:
This will include the turtle itself, since a turtle is always a link neighbor of its link neighbors. If you don't want to include the turtle itself, you can just throw an other in there: [ count other neighbors-of-neighbors ] of turtle 0.
This won't include turtles that are link neighbors of turtle 0, but are not linked to any other neighbors of turtle 0. To add those in, you might consider a procedure like this:
to-report turtles-in-link-radius [ n ]
let result turtle-set self
repeat n [
set result (turtle-set result [ link-neighbors ] of result)
]
report result
end
This can then be used, for instance, like so: [ count turtle-in-link-radius 2 ] of turtle 0 to count all turtles at most 2 hops from turtle 0. This reporter has the same behavior as nw:turtles-in-radius, though it will be significantly slower.
Now, I first noted that your idea would count some turtles multiple times. Given that you're interested in something like eigenvector-centrality, maybe this is what you want. In that case you can do this: [ sum [ count link-neighbors ] of link-neighbors ] of turtle 0 to get it without having to bother with a turtles-own. That said, if you do use a turtles-own, you can actually calculate the eigenvector centrality itself:
turtles-own [
...
next-ec ;; helper variable for iteratively computing eigenvector-centrality
eigenvector-centrality
...
]
to calculate-eigenvector-centralities
ask turtles [
set eigenvector-centrality 1
]
repeat 100 [ ;; the higher this number, the more accurate the value. NW uses 100
ask turtles [
set next-ec sum [ eigenvector-centrality ] of link-neighbors
]
let max-ec max [ next-ec ] of turtles
ask turtles [
set eigenvector-centrality next-ec / max-ec ;; normalize
]
]
end
Call calculate-eigenvector-centralities to calculate all turtles eigenvector centralities. The turtles' eigenvector centralities will then be stored in eigenvector-centrality. Note that this code won't work for disconnected networks: each component has to be normalized independently, which is what NW does, but there's no easy way to do that in NetLogo without using NW.
I'm new at netlogo, and in my model, I'm trying to make it so there's one turtle per patch, so that all the patches are filled up with one turtle instead of them overlapping each other. The code for that part of the model is
to solid
set color blue
set xcor random sqrt number-of-particles - number-of-particles / 2
set ycor random sqrt number-of-particles - number-of-particles / 2
ifelse any? patches with [pcolor = black and count turtles-here = 0]
[move-to one-of patches with [pcolor = black and count turtles-here = 0]]
[die]
end
I've been trying it out with different variables, but it works for odd "Volume" (amount of patches in each row) but not the even ones.
(even numbered one) Link 1
(odd numbered one) Link 2
how would i make it so that it works for both odd and even numbers? thanks!
This is my full setup code. Sorry that I posted them in my comments, this is my first time on stackoverflow.
to Setup-Container
ca
cro 4
[set color black
rt 45
fd Volume * sqrt 2 / 2
rt 135
pd
set pen-size 6
fd Volume
die
]
ask patches
[ifelse pxcor <= Volume / 2 and pxcor >= Volume / 2 * -1
and pycor <= Volume / 2 and pycor >= Volume / 2 * -1
[set pcolor black] [set pcolor black + 3]
]
end
; Creates a number of particles, which depends on the corresponding slider.
; Executes certain commands depending on the temperature slider.
to Setup-Particles
ask turtles
[die]
cro number-of-particles
ask turtles [
set shape "water"
if Temperature <= 0 ; freezing point is 0 degrees celsius.
[ice-cube]
if Temperature > 0 and Temperature < 100
[water]
if Temperature >= 100 ; boiling point is 100 degrees celsius.
[water-vapor]
]
end
There's actually a primitive that just creates a turtle on a given patch: sprout. So, ask patches [ sprout 1 ] would create one turtle on each patch.
Bryan's answer is most probably the right one, here: if your main requirement is to have one turtle per patch, sprout is the way to go.
A few more comments:
In the screenshots you've shown us, you control your "volume" and your number of particles separately, so there is aways a chance that your container will be either too big or too small for the number of particles you want. If you really want a one-to-one relationship between the size of your container and the number of particles, you should really have a single parameter for both.
sprout allows you to give commands to the newly created turtles. If you don't want them to be "rainbow colored", you can do: ask patches with [pcolor = black] [sprout 1 [set color blue]].
In your original code, you set the xcor and the ycor of your turtles... and then immediately move them. The point will be moot if you use sprout, but I wanted to point out that these two lines were unnecessary.
If you still have trouble with sprout, I'd suggest you ask a separate question and show us what you tried. We may able to help you...