How to run several procedures simultaneously in NetLogo? - simulation

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.

Related

Monitor the lifecycle of each turtle (NetLogo)

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.

Can a patch in net logo have two color and one is hidden sometimes?

I am doing a tree grow in net logo, I have to implement a hidden trunk. But how can I make a patch assign to trunk color when there is no leaf and sometimes hidden behind leaves and showing leaf color ?
When we standing in front a tree, we sometimes cannot see the upper trunk cause the leaves. That is what I am going to model for now.
This may not be the answer you were hoping to get, but it might be the one that helps you the most in the long run:
The tree leaves should not be represented by patches. They should be represented by turtles.
If you use turtles, you get the "hiding what's behind" property for free, but that's just one of the reasons to use turtles.
NetLogo beginners tend to resort to patches as their "go-to" type of agents because they seem easier to use, but it's a trap. Turtles are much more flexible, and it pays to use them in the long run, even if you don't expect to move them around.
A few examples:
Patches are just coloured squares, but turtles can be any shape you want, which usually looks nicer. In your case, you could use the "leaf" shape that comes with NetLogo.
Turtles can have different breeds. Even if you plan to only use one breed of turtles, this makes your code more readable and also more flexible.
You can't have links between patches, but you can have links between turtles. Even if your model is not explicitly a network model, NetLogo links are a surprisingly useful way to represent relationship between agents.
Turtles can be created and killed. This is often a much better approach than trying to modify the state of a patch to reflect the fact that something is there or not. This applies directly to your problem: instead of changing the colour of a patch to signal that there is a leaf on it, just ask your patch to sprout-leaves 1.
So do yourself a favour and start your model with:
breed [ leaves leaf ]
to setup
clear-all
set-default-shape leaves "leaf"
; ...
reset-ticks
end
You'll make your own life much easier.

Turtle Movement Netlogo

I have a network of turltes that each share two links. At the start of each turn the turtles decide to cooperate or defect, which updates a beta distribution of likelihood of cooperation in the next tick. If the turtles fail to cooperate over n turns they no longer interact. Through this I am able to create clusters in the cooperation network.
Right now, I am trying to figure out how to make the turtles move closer together proportional to the weight of their ties. Is there code to do this? I have only been able to find example code for patches.
I think this model might have something similar to what you want:
http://modelingcommons.org/browse/one_model/3632#model_tabs_browse_procedures

In a Netlogo network, how can turtles "see" properties of other turtles?

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!

Query about hidden turtles

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
]