I have a question on how to "monitor" through time some turtles. You can think of this problem like plotting the lifecycle of each turtle.
Let's say initially I have 5 turtles. At each tick, a new turtle is created.
They do some actions (e.g., each of them interacts with a dummy turtle) before their death.
I can identify the turtle created at each tick using who.
I have tried as follows:
create-temporary-plot-pen (word "Turtle " (who))
set-plot-pen-color color
plotxy ticks count XX ;(# of interactions between who and the dummy variable through time)
but nothing is displayed on the chart. Only the list of turtles that at each tick are created is correctly displayed.
The desired plot should show how many times each turtle interacts with the dummy variable before it dies.
I hope it makes sense to you. If it does not, I will be happy to answer all the question you can have.
Related
I am trying to build a model in which turtles decide to change colour depending on their environment in a network.
The approach would be to "check" the colour of the surrounding turtles and then set an if statement for the turtle in question to switch colour (there will only be 2 colours).
Specifically I would like to know how can a turtle "see" or check other turtles' colour (or other properties).
If possible I would also like to create a slider for "how many links away" can turtles see their neighbouring turtles' (or neighbours of neighbours, etc) colour.
I am new to both Netlogo and Stackoverflow, so please let me know if I should make any modifications to my model and/or question.
Thanks!
Welcome to Stack Overflow! Typically you'll want to stick to a single question per post, both for simplicity and for the benefit of future users with similar questions. As well, in cases where its applicable you should try to include some code to show what you've tried so far, as well as any setup necessary- you want to make a minimal, complete, and verifiable example. In this case, I think you're okay since your questions are clear and well explained, but if you have more complex questions in the future you will be more likely to get useful answers by following those guidelines.
For your first question, it looks like you want the of primitive- check out the dictionary entry for details. of can be used in a few ways, including allowing agents to check the value of a variable (such as color) of another agent. Check out this example code:
to setup
ca
reset-ticks
crt 10 [
setxy random 30 - 15 random 30 - 15
create-link-with one-of other turtles
]
end
to go
ask turtles [
set color [color] of one-of link-neighbors
]
end
Every time the go procedure is called, one of the turtles changes its color to the color of one of its link-neighbors. If you run it long enough, all connected turtles should end up with the same color.
For your second question, I suggest you check out the Nw extension, which is an extension built to deal more easily with Netlogo networks. Specifically, have a look at nw:turtles-in-radius, which should work with your slider approach. To get it working, include the extension using
extensions [ nw ]
at the start of your code. Then, assuming the same setup as above, you can play around with something like
to network-radius
ask one-of turtles [
set color red
ask other nw:turtles-in-radius 2 [
set color white
]
]
end
When you call the network-radius procedure above, you should see one turtle turn red, and any turtles within 2 links of that turtle turn white. To switch to a slider, just swap the "2" out for your slider variable. Hope that helps!
I have created a model in which turtles are born and die based on certain parameters. But at any given point, lets say no more than 20 turtles are alive.
With the birth and death of each new turtle, the turtle label keeps increasing incrementally i.e. initially there are 5 turtles, the 3rd turtle dies and in the next tick a new turtle is born. The new turtle born has a label of 6 and thus old labels are retired and replaced with the next label.
If i want to output a metric associated with the turtles into the monitor, is there a way to dynamically ensure that. i.e. since there can't be more than 20 turtles at any tick, can i make netlogo display the turtle metric along with the turtle label automatically. Otherwise i will have to create 100's of monitors and then code with [metric] or turtle 0.....[metric] of turtle n which is not practical.
If you really want a separate monitor for each turtle, you can do something like this:
Using [ metric ] of item 0 sort turtles instead of [ metric ] of turtle 0 (and so on) will insure that you're not depending on the who numbers of the turtles, you're only depending on their position in the sorted list of turtles.
Note that this would be very inefficient, because each monitor would keep re-sorting the turtles over and over again.
That being said, I think there would be many different, better ways to approach this. Here is one fully working example:
turtles-own [ metric ]
to setup
clear-all
create-turtles 20 [ set metric random 10 ]
reset-ticks
end
to go
ask n-of 5 turtles [ die ]
create-turtles 5 [ set metric random 10 ]
tick
end
to-report info [ the-turtle ]
; format this however you want:
report [ (word who ": " metric ", ") ] of the-turtle
end
And then, in a monitor, put:
map info sort turtles
Which will give you something like:
If map is obscure to you, you may want to check its dictionary entry. The basic idea is that we build a new list of strings by applying the info reporter to each element of our list of turtles.
I used a monitor in the example because that's what you were talking about in your question, but for displaying information about multiple turtles like this, maybe a plot or the output widget would be more appropriate. In any case, you could use a similar approach, with either map or foreach.
One word of advice in closing. Your question shows that you're probably aware of that already, but any time you're tempted to refer to turtles by their who number (i.e., turtle 0, turtle 1, turtle 27, etc.), it probably means you're on the wrong track. NetLogo is built to manipulate agentsets and lists; take advantage of that. And when you do need to refer to a particular turtle, use a reference to that turtle (e.g., the-turtle in the example above), never (or almost never) its who number.
I'm trying to model fish movement and need them to form schools when there is more than 1 of the breed in a given patch. So far I have managed to get them to form links when they encounter each other with the function below, but then they continue moving independently. I'd also like to re-scale the color of turtles in a linked group so that the more turtles in the group the darker the color is (I'm guessing this is similar to the way you make contour maps according to environmental gradients but I haven't figured it out yet).
Any assistance is always appreciated!
to form_link
if count breed_1-here > 1
[
ask breed_1
[create-links-with other breed_1-here]]
end
If linking isn't the way to get them to move together, I'm fine with another method.
What actually happens to hidden turtle? I mean after we hide the turtle it continue to live in invisible mode occupying memory as I guess.
I hide few turtles but did not ask them to be shown back and when I inspected the hidden turtles continuing simulation their attribute were changing as per my commands. So, what exactly hiding a turtle sense for.
In one of my simulations, turtles represent people making decisions about whether to protect themselves during an epidemic. There are tens of thousands of these turtles, with potentially hundreds on some patches. The turtles don't move, but they each make their own decision based on personal characteristics like attitude and environmental perception such as how close the epidemic is.
Having these turtles visible would just clutter up the screen. Instead, I hide them and colour the patch based on what proportion have adopted protective behaviour. This is much more informative.
In my most recent simulation, though, I make the turtles size 0 instead of hiding them. This still makes them disappear, but I can still right-click on the world view to access the list of turtles where I have clicked.
Another reason to hide turtles is when you are simulating an infinite plane and turtles outside the view should simply be hidden.
Note that if you are moving turtles using setxy rather than forward you should test to make sure the patch you are about to move to exists since setxy throws a runtime error if it is given coordinates outside the world. From NetLogo documentation:
ifelse patch-at (new-x - xcor) (new-y - ycor) = nobody
[ hide-turtle ]
[
setxy new-x new-y
show-turtle
]
There are different colored turtles in my model each operating under different rules. I want procedures governing the movement of one turtle(say, red turtle) to run simultaneously with other procedures governing the movement of different colored turtles.
Alan's answer is the correct one. However, just FYI - each turtle (whether red or blue) will act in turn with the above procedure, and none with act "at the same time." That just doesn't happen in NetLogo, by default.
However, you can use a form of simulated concurrency. There is a section of the user guide on "ask-concurrent" that explains this built-in function in detail.
Suppose you have two turtle procedures do-red and do-blue that you want to run on red and blue turtles. Then you can just ask turtles [do-something] and condition on the color. Assuming you are not changing the colors:
to do-something ;; turtle proc
if (color = red) [do-red]
if (color = blue) [do-blue]
end
EDIT:
This does not provide true concurrency, but seriously, how often can agent behavior be truly concurrent? For example, if do-red affects other turtles (red or blue), what is the "concurrent" outcome when turtle 0 and turtle 1 both influence turtle 2, who responds only to individual influences. To give another example, if you want every turtle to choose an unoccupied patch to move to, and two choose the same patch, who wins? The is why update-state solutions cannot address the general problem of concurrency. Of course, it is still crucial for some problems. (E.g., CA.)
I don't think Alan's answer is complete. Here he is assuming do-red doesn't effect blue turtles.
To simulate concurrency you need to store the state and update it later. All computations will performed on the stored state in the given time-step.
Example Using Alan's code:
to do-something ;; turtle proc
if (color = red) [do-red]
if (color = blue) [do-blue]
update-turtles-state
end
Note: do-blue shouldn't use the computational output of do-red in any way for a given time-step.