Plotting variables from a list - netlogo

I am trying to plot the velocity distribution of the turtles in my model. The velocity vector vi of each turtle is contained in a list (coordinates vix, viy) I am not sure if using
plot max item 0 [vi] of turtles
is showing what I really want which is the maximum velocity of all turtles. I wonder if max-one-of primitive would be better? but when I use
plot max-one-of turtles item 0 [vi]
of course, I received the error expected a literal value. As it is a list I do not know how to rewrite item 0 [vi] to be recognized by the plot.
What I am doing now is creating a monitor using max-one-of as I think this will do what I want
max-one-of adults [item 0 vi]
but the monitor is showing me (adult 24) and it's okay, I have checked that from all turtles in the simulation for that particular tick, adult 24 has the higher velocity but I want the monitor to show me the velocity value (number) not the who number. How can I do this?
I guess after making the monitor do what I want I can copy that piece of code into the plot to create the graph.
I thank you in advance for your thought and recommendations =)!

If I understand the question correctly, then this should do the trick:
plot max [item 0 vi] of turtles
If you were to write this with max-one-of, it would come out as:
let winner max-one-of turtles [item 0 vi]
plot [item 0 vi] of winner
but as you can see, max-one-of isn't really helping.

Related

why are my results from behaviorspace (netlogo) so inconsistent?

I am somewhat new to Netlogo, and have been scratching my head over some of the results I get from behaviorspace. I have been playing with the wolf-sheep predation model, and changing their movement based on what color patch they are in. There is a single wolf and a single sheep, and what I want to measure is the number of time steps it takes for the wolf to eat the sheep. The patches are colored randomly, based on some proportion from 0-100, as below:
to color-patches
let total 100
let p-red slider1 / total
let p-green total - p-red
ask patches [
let x random-float 1.0
if x <= p-red + p-green [set pcolor green]
if x <= p-red [set pcolor red]
]
end
The issue I am having is when I set the movement of the wolf and the sheep to move independently of the patch color:
ask sheep [
set heading random 360
fd 1
]
ask wolves[
set heading random 360
fd 1
eat-sheep
]
My expectation is that the mean and standard error for the number of time steps until the wolf eats the sheep should be pretty similar regardless of how many red patches and how many green patches there are, since their movement is not affected by it. I ran it in behaviorspace, with 1000 iterations per 10% increase in proportion of red patches (from 0 - 100%). However, I keep getting results that look kind of like this:
enter image description here
Basically the means+se are all over the place. Every time I run it, they are distributed differently (but same grand mean). This is particularly odd since when I introduce any sort of patchcolor-specific behavior for either the wolf or the sheep, I get very clear patterns with less variation.
Any ideas what might be going on here? The only thing I could think of is that relative starting position of each is pretty important (but each is placed at random xy coords). I assumed that in behaviorspace for each iteration for a given set of parameters, it would run through all the code (thus generating a new random landscape and new random starting points for the wolf and the sheep for each of the 1000 runs per parameter combination). Does behaviorspace maybe take the first landscape and starting coordinates for each turtle and use them for each of the 1000 iterations per parameter combination?
Thanks!
Now I think you may not have a mistake but instead are just misinterpreting your graph. The Y axis on the graph you posted ranges only from 2000 to 2200; if you set the Y axis scale to 0 to 2500, the results from each experiment would look very similar to each other.
The difference in mean between your results (~2100) and my results (~3100) is probably just due to different world sizes. I presented standard deviation while you graphed standard error.
If you histogram the results, they seem to follow an exponential distribution.

Compute weighted mean of headings

I have each patch holding two lists: directions and magnitudes. Both lists contain values that are computed based on nearby turtles:
directions contains the headings from the patch to the various turtles (so basically it is just the turtle's heading rotated by 180°);
magnitudes contains a value that is directly proportional to the turtles' mass and inversely proportional to the distance between the patch and the turtles;
The items of the two lists are coupled in their order, i.e. the first item of directions and the first item of magnitudes are computed based on the same turtle, the two second items are computed based on another turtle and so on.
What I want to achieve is to come up with two single patch-own values, my-direction and my-magnitude, representing in some way the weighted average of directions, where the weights are magnitudes.
To put it another way, I am thinking of this in terms of vectors: turtles are exerting on the patch a force that can be represented as a vector, always pointing in the direction of the turtle and with a certain intensity (the magnitude). The resulting vector (represented by my-direction and my-magnitude) should be the resulting average of these forces.
I have seen this question. It does not address the issue of a weighted average; however it mentions the concept of circular mean. I've delved into it a bit, but I'm not sure how to apply it to my case and even if to apply it: does it still apply even with the formulation of the problem in terms of vectors?
I've seen this question/answer on SE Mathematics where it is said that the average vector can be found by averaging x- and y-coordinates of the initial vectors. In my case, ideally all the pairs of values in the two lists form a different vector with origin in the patch at issue, with heading found in directions and length found in magnitude. I suspect I can find the coordinates of each vector by multiplying its sine and cosine by its magnitude, however at this point I'd use some guidance as I might be overcomplicating things (either from the maths perspective or the NetLogo perspective).
This one below is a reduced version of the code that brings to the point where target-patches (not focusing on all patches, in order to make it quicker) have the two lists populated.
globals [
target-patches
]
turtles-own [
mass
reach
]
patches-own [
my-direction
my-magnitude
directions-list
magnitudes-list
]
to setup
clear-all
set target-patches n-of 10 patches
ask target-patches [
set directions-list (list)
set magnitudes-list (list)
set pcolor yellow + 3
]
create-turtles 10 [
move-to one-of patches with [(not any? turtles-here) AND (not member? self target-patches)]
set mass (random 11) + 5
set reach (mass * 0.8)
set size (mass / 8)
set shape "circle"
]
populate-lists
end
to populate-lists
ask turtles [
let relevant-targets (target-patches in-radius reach)
ask relevant-targets [
set directions-list lput (towards myself) (directions-list)
set magnitudes-list lput (magnitude-based-on-distance) (magnitudes-list)
]
]
end
to-report magnitude-based-on-distance
report [mass] of myself / (distance myself * 1.2)
end
Your initial instinct is right. You can do something like
LET my-dx mean (map direction magnitude [ theta scalar -> scalar * sin theta ])
(I may have that map and anonymous syntax wrong please edit)
And do the same for my-dy using cos (edit: or maybe negative cos?)
patch-at my-dx my-dy is one way of getting the patch.
You can also do (atan my-dx my-dy) to gets the new direction
And distancexy my-dx my-dy to get the magnitude
Don’t remember if patch can use distancexy or patch-at, but hopefully so. Otherwise you have to use a helper turtle, and do the math yourself.
I think I did a orbital mechanics toy model once that did something like this. It’s hiding on turtlezero.com.

How to plot a distribution in netlogo?

I have a NetLogo model. each turtle has two attributes, "closeness" and "deviation_from_oracle". Now let's say there are 1000 agents in the model. The question is, how can I plot the "closeness" against "deviation_from_oracle" ?
It would also be helpful if I can get a csv file from NetLogo that has the value of closeness and deviaiton_from_oracle of all turtles after for example 1500 steps.
I definitely agree with Hugh_Kelley regarding using Behaviorspace to output your values (or custom export functions that might make for easier data cleanup if you're looking to report values for a large dynamic number of turtles- depends on your comfort with your statistical software of choice).
If you do need to plot something on the interface to show your users or something, you may find the plotxy function does what you need. For example, you'll need a plot on the interface called "plot 1" and a single blank pen in that plot called "pen-0".
You can control that plot either by manually setting up its x and y extent or by using the set-plot-... commands as in this setup:
to setup
ca
crt 10
set-current-plot "plot 1"
set-current-plot-pen "pen-0"
set-plot-pen-mode 2
set-plot-x-range 0 17
set-plot-y-range 0 25
reset-ticks
end
If you need to have a value plotted for each of your turtles, you can get the turtles to call plotxy for whatever values you're looking to plot- here I just use their absolute x coordinate and distance to the center as an example:
to go
ask turtles [
rt random 61 - 30
fd 1
set-plot-pen-color color
plotxy ( abs xcor ) distance patch 0 0
]
tick
end
This gives output like:
Where each point was plotted by an individual turtle.
If you want instead some reported mean value, have the observer call plotxy instead- another example that plots the average distance to other turtles and the average distance to center:
to go
plotxy mean-closeness-to-others mean-distance-center
ask turtles [
rt random 61 - 30
fd 1
]
tick
end
to-report mean-closeness-to-others
report mean [ mean map distance sort other turtles ] of turtles
end
to-report mean-distance-center
report mean [ distancexy 0 0 ] of turtles
end
For an output like:

Netlogo: How to plot degree of nodes vs. average "reward" of turtles with that degree?

I am building a model that tries to simulate a network-based market. In this model the turtles/nodes get a reward called points, which is a turtles-own variable.
I am now trying to plot a graph of the degree of the nodes against the average number of points that nodes with a given degree have. I have attempted to do this by creating a plot from the interface tab but I cannot manage to make this work.
Here are images of the windows of the plot settings.
Anybody know how can I make this work?
Also, I keep getting these "Runtime error: Can't find the maximum of an empty list" in all the plots/histograms I create. It is not a big deal at the moment as they seem to work fine, however if you know why these appear please let me know!
Thanks beforehand,
Carlos
For simplicity and to avoid overloading your plot setup, I like to use to-report procedures for things like this. As a quick example try this setup:
turtles-own [ points degree ]
to setup
ca
crt 50 [
set degree 5 + random 5
set points random 10
setxy random-xcor random-ycor
]
reset-ticks
end
Make a to-report each for a list of existing degrees, the average points of turtles that have each degree, and the maximum of those average point values:
to-report degrees-list
report sort remove-duplicates [degree] of turtles
end
to-report avg-points-list
let avg-list map [ i ->
mean [points] of turtles with [ degree = i ]
] degrees-list
report avg-list
end
to-report max-avg
report precision ( max avg-points-list + 1 ) 2
end
In this example, degrees-list reports [ 1 2 3 4 5 ], avg-points-list reports something like [6.5 3.9285714285714284 6 3.75 4.2], and max-avg reports something like 7.5- note that of course the exact values will vary since the setup is random.
Now, you can set up your plot window:
The actual plotting is handled by the foreach primitive in the plot pen, which uses plotxy to plot the point value in avg-points-list against the corresponding value in degrees-list. Should give a plot that looks something like:
Hope that's sort of what you're after!

NetLogo - How to show the current coordinates of a turtle

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!