Plotting variables of multiple agents - netlogo

I am trying to plot a graph that shows the variables of all agents of one breed. Although the number of agents is rather small, I think there must be a more elegant way than creating a pen for every agent. Is there?

Elegance, here, depends on what exactly you want to do. It can be perfectly legitimate to create one plot pen per turtle, using create-temporary-plot-pen. Among advantages, it means that you can give each plot pen a name and show a legend on your plot.
That being said, it's also very easy to use a single pen for all your agents. Suppose you have multiple turtles, of different colors, and you want to plot their sizes. You can put something like this in a pen's "update commands":
ask turtles [ set-plot-pen-color color plotxy ticks size ]
This will change the pen to the color of each turtle and plot a single point for that turtle. Just be sure to set your pen to "point" mode in the advanced pen options.
Also notice the use of plotxy instead of just plot, allowing you to specify an x coordinate instead of relying on the "auto advance" behavior of plot.

Related

Using only patches to display a letter in Netlogo?

I am new to Netlogo and I wish to understand how to set the patches to show a static letter.
Is it possible for the patches to show a letter (e.g. letter D) without the use of turtles? I was thinking that patches could only have one color, so I am lost when it comes for the patches to display a letter? Is is possible to display a letter by changing the color of some coordinates? Even so, how should I retrieve the correct coordinates to actually be able to draw a real letter?
Thanks!
You are correct that patches must all be one colour - they are squares of environment. So you can either do a very large letter by making it from small squares. Or (more easily) you can use the plabel variable that is owned by each patch. That variable can hold any value like a letter or a word - it is intended as a label for the patch.
If you have the default settings, then the centre patch is patch 0 0. That is both the name of the patch and the co-ordinates. And patch 1 0 is the patch that is one to the right (x direction) of the centre. As well as the label, the variables automatically owned by a patch include the colour (as pcolor), x co-ordinate (pxcor) and y co-ordinate (py-cor).

Dynamically replotting every tick

I'm trying to plot two turtle variables against each other in a scatterplot. I want this plot to update continuously, and erase the points from previous steps. I.e. I want one point to move to represent the variable values for one turtle throughout its lifetime, disappearing when it dies and these values equal zero.
This is my code for the pen update commands (isspecials is my turtle agentset)
ask isspecials [plotxy r invader-stride]
I tried adding
reset-plot-pens
to the end of it, but then nothing appears on the plot. I think it's almost as if I have to create a new pen for each turtle, but I can't tell. I'm really new to this, so if anyone has any suggestions I'd really appreciate it!
I tried adding
reset-plot-pens
to the end of it
Adding plot-pen-reset is the right idea! However, you need to add it before your plotting command.
The plot-pen-reset primitive erases everything that the plot pen has drawn. If you call it right after you plot, you don't get a change to see anything! Calling it just before plotting new stuff will ensure that the stuff you plotted in the previous tick has plenty of time to be seen.
Here is what you plot pen definition should look like:

Histogram of how often actions are played

I have created a model using the Roth-Erev reinforcement learning algorithm so that every round agents select their action a from a set of actions A. The actions count how often they are played throughout the entire game.
I would now like to create a histogram that has the frequency on the y-axis and the actions on the x-axis. I would also like to label every action with its parameters, but I understand that is not possible.
Merely using histogram count [n-played] of actions would not plot every action individually. I think plotxy comes closest but isn't suitable for histograms. Is there any build in solution or do I have to visualize data outside of Netlogo?
You won't be able to label the columns, but you are not limited to the histogram command for producing bar plot. Each plot pen has a mode that can be "line", "bar" or "point". By using the "bar" mode, you can produce bar plots with the plot or plotxy commands:
Here is the code for easy cut and paste:
plot-pen-reset
foreach map [ [ n-played ] of ? ] sort actions plot
Note that this uses the "concise task syntax" for passing plot to foreach. You can write [ plot ? ] if you prefer.

Node degree in a network

I have a landscape that includes a road network as in the below figure.
I would like to calculate an average number of roads (or white line in the figure) that link each color polygon (for example a black polygon) between them.
In a network, a link corresponds to a road, a node corresponds to a color polygon and I think that to calculate an average number of roads between color polygons means to calculate the average node degree of the network. For example, in the figure, the two black polygons are linked by two roads. So, the degree of a black polygon is 2. Is it possible to calculate a node degree with the extension Network of Netlogo ?
Thanks in advance for your help.
Normal NetLogo contains link agents that link turtles even without an extension. Calculating degree is usually just a matter of doing [ count my-links ] of node where node is the turtle you want to know the degree of. However, in NetLogo, turtles can't be connected by more than one link. The typical workaround for this is create a links-own variable (just like turtles-own or patches-own variable) . This variable is often called weight, but you can call it whatever you want. In that case, you would do [ sum [ weight ] of my-links ] of node to calculate the degree.
This is all assuming you have a network representation of your roadways, which it doesn't sound like you do. Furthermore, I'm not sure what you're trying to represent is a network, since (as shown in your picture) roads branch at intersections. Thus more than two polygons could be connected by a single (for some definition of "single") road. This is often called a hypernetwork or hypergraph. However, this is probably a heavier weight concept than what you want.
Now, I'm not entirely sure what you're really trying to calculate. Is it:
...the number of roads connected to a polygon? The lower polygon has 4 roads connected to it, the upper has 3 (visible).
...the number of polygons directly connected to the polygon? Both polygons are connected to 1 other (visible) polygon, though I assume in the larger picture there are more.
The number of roads connected to a polygon would be pretty easy to calculate, assuming each road is 1 pixel wide. You could just do:
count (patch-set [ neighbors4 with [ is-road? ] ] of polygon)
where polygon is a patch set containing the patches of the polygon and is-road? is a reporter that returns true for road patches and false for non-road patches (this could be something pcolor = white). Note that this will break if roads are wider than 1 patch or if the same road can touch a polygon in other places. Let me know if that's the case and I'll expand this into something that can that into account.
The number of polygons directly connected to the polygon is more difficult. The basic idea would be to follow the roads out until you hit other polygons and count the number that you hit. Code for this is somewhat tricky. I think the best way to go about it would be to have two patch-sets, frontier and explored and list of found polygons. frontier should initialize to every road patch touch the polygon. Each iteration, get the polygons touching the frontier and add them to the list of found polygons if they're not already in there. Add the frontier to explored. Get all the road patches touching the frontier that are not in explored. Set the frontier to this new set of patches. Keep going until frontier is empty. This is a version of breadth-first search. There could be a better way to do this.

How to change plot background color in NetLogo

Is there a way to change the background color of a plot window in NetLogo?
(I want turtles to be black and white, and I'd like plot pens for statistics on the two kinds of turtles to match their colors. A white pen is not visible on a white background, obviously.)
Thanks.
NetLogo has not built-in way to change the background color of a plot. Arguably, this is something it should have. If you feel strongly enough about it, I would suggest sending a feature request to feedback#ccl.northwestern.edu or even opening an issue directly on GitHub.
Now, in the meanwhile, is there a way around it? Well, I feel almost dirty for even suggesting it, but you could do something like this:
Create a new plot pen of the color you wish your background to be, and set it to "line mode". This pen has to be the first one in your plot pen list so it's drawn before the other pens. (This might require deleting your other pens and recreating them, as NetLogo doesn't have an easy way to reorder plot pens, I think.)
Now put the following in your pen's update commands:
plot-pen-reset
let y plot-y-min
while [ y <= plot-y-max ] [
plotxy plot-x-min y
plotxy plot-x-max y
set y y + 0.05
]
This will draw lines, one by one, to fill your background. Depending on the size of your plot on the screen, you might want to play with the "interval" (0.05 here) to find the biggest value that doesn't leave white lines.
Be warned: this will slow down your model. If your plot axis are never rescaled, however, maybe you can get away with putting the code in your pen's setup commands so it's only executed once.