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).
Related
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.
I wish to draw an arrow at the top turtle representing a vector denoted by
[x1,y1] . The vector is of unit magnitude and the size of the arrow should not exceed that of the turtle.
The vector is stored in a list with two elements.
I don't wish to use the shape editor in netlogo to shape as arrow and then point the turtle in the heading denoted by the vector. The reason being I could draw one than 1 arrows for each turtle.
Edit:
Desired:
Bryan's answer gives the following:
Edit 2:
Video link : https://www.youtube.com/watch?v=9SVcLg4Oyoc&t=23 for better explanation.
Here's how I'd do it:
Make sure your turtles are all of one breed, say, particles, or whatever they represent. Create another turtles breed called vectors or something. These turtles are going to be the tip of your vectors but you'll use links to actually visualize the vectors. Now, you can create the vectors like so:
ask particles [
hatch-vectors 1 [
create-link-from myself
hide-turtle
]
]
To update the position of the vectors (given that the vector itself is stored in a turtle variable vec), you can do:
ask particles [
let abs-x xcor + first vec
let abs-y ycor + last vec
;; Since the particle is linked to the vector by a directed link, it's an out-link-neighbor
ask out-link-neighbors [ setxy abs-x abs-y ]
]
Edit in response to update:
That's tougher, since link shape editing is more limited than turtle shape editing. One possibility would be to set the shape of the vector turtles to an arrow head (you could either create a new such shape, or the default turtle shape could suffice). Rather than hiding the vectors, you'd then point them in the right direction. This can easily be done by having them face their link-partner and then turn around.
You may also want to switch from directed to undirected links to get rid of the arrow in the link itself. This should only involve minor code changes.
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.
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.
In NetLogo, I can create turtle-specific variables with turtles-own, and patch-specific variables with patches-own. But how can I create variables that are specific to a turtle AND a patch?
Specifically, I want to create a preference function - each turtle has a preference to live in a certain patch. The preferences are different for each turtle and patch, for example, turtle 1 has preference 20 to live in patch (1,1) and preference 30 to live in patch (2,2), etc. How can I define this function in NetLogo?
If I understand you correctly, you'd like to have a unique mapping between each turtle and each patch. My first thought is to use the built-in matrix extension via
extensions [matrix]
and have a turtles-own variable "preferences" that is a matrix with the dimensions of your world (e.g. if max-pxcor and max-pycor are both 16 and your origin is centered, you need a 33x33 matrix).
Each element of "preferences" then corresponds to one patch and denotes the assigned value.
See the NetLogo User Manual for documentation on how to fill the matrix with values.