Netlogo - Turtles crossing Line - netlogo

i want to count the turtles which cross the line - i tried with "turtle on patch" but if the turtle stop it counts twice. If the turtle moves more then 1 patch it counts nothing...
any ideas? Thanks Peter
Line

From the picture you added it looks like you are running a single-lane traffic model with a very narrow "finish line" that turtles can cross entirely in one tick.
To catch crossings:
One thing you could do is make a thicker "line" on the far side of the finish line. Maybe make it 5 patches wide. You could color it yellow while you're testing, and change it to hidden when you're sure it is working. If a turtle is on that patch, it has crossed the line. If you make it wide enough it should be impossible to leap over at any speed. It should be easy to test using only one turtle and running the model slowly.
To count stopped cars:
Without seeing your code it's hard to tell why you are counting stopped cars twice. Can you post your code or the relevant section of it here? I'm guessing you have some global that you are incrementing each time you find a new stopped car.
It's less efficient, but much more reliable to let the cars own a variable like "stopped?" that you initialize to false and set to true when the car crosses the line. Then at any time you can get the accurate count of stopped cars with
count cars with [ stopped? = true ]
Assuming you have a variable called my-count of stopped cars, and you want to see when that goes wrong, you could insert a line of code like
if mycount != count cars with [stopped? = true] [user-message "count is wrong!"]

If the line is definitely going to be vertical (as in your diagram) then the easiest way may be simply to count the turtles with pxcor larger than whatever coordinate the line is at plus something for the car size.

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.

How can I change a turtle's status depending on specific conditions?

I really need your help to fix a problem with my code.
Unfortunately, I am having difficulty by updating the status of turtles once a condition is satisfied.
It is a virus spread problem: each node, called human, has its own 'bag' which contains different items (virus and medicines). The bag is made up of items from the neighbours that are linked to the chosen turtle.
The virus can come from a zombie at the beginning (t=0) and move from turtle to turtle, depending on what they decide to do. If a turtle picks up a virus (spread action), it can become infected.
The part of code that is giving me some problems is the following:
to spreading
ask one-of humans
[ let choice one-of out-link-neighbors
ifelse empty? bag
[ ifelse random-float 1 < p
[action1 set bag lput act1 bag print "act1"]
[action3 print "act3"]
]
[spread set bag lput spreadvirus bag print "spreadvirus"
if [infected?] of choice
[become-infected] ]
]
]
end
more specifically the part with spread.
What that part of code should do is to build for each randomly chosen turtle a bag made up by all the'items' from neighbours' bags, and transform that turtle according to what it chooses.
For example, as you can see in the figure below, at the beginning nodes 2,5 and 7 can be potentially infected because they are linked to the zombies. Each turtle can 'see' all its items plus all the items contained in its neighbours bags (for example, 7 can see - and potentially choose - all the items from Z, from 8 - its bag is empty in this case - and from its own bag, made by historical stored items, i.e. past choices). Let's say that 7 picks the virus from the zombie (action 2, i.e. spread) and becomes infected. Node 5, that can see all the items contained in the lists of nodes 4 and 7, can become infected as well, depending on what it chooses. If 5 prefers an item from 4, that is a node not infected because in its own list there are only uninfected items, it becomes susceptible. If it chooses, among all, the infected item from 7, it becomes infected as well. And so on...
What I suspect is that the bags are not storing information from the neighbours as I want. This explains why no humans are becoming infected from the ifelse statements, but only susceptible and they change color in white and blue depending on their choices.
I am not getting any error from the code, so it is difficult for me to understand what I am really doing wrong. I also put some print to see what is not working, and it seems that the part of code with spread as action is the problem.
What I should expect is a list that contains all the items from turtle's neighbours. At each tick the turtle can choose what to do with those items (action1, spread, action3), by adding a new item, selecting an item from neighbours, or do nothing. This choice should be added to the turtle bag. This means that the list, at each tick, should be updated, taking into account all the elements from the neighbours, plus the selected action/element from the turtle. But the turtle becomes infected only if it chooses (spread) not a medicine from the neighbours, but a virus.
UPDATED after JenB answer and to better explain how the human breed should become infected:
The possible types of items that each bags can contain are new medicines, medicines from the same turtle or from neighbours and/or virus, for humans; only virus for zombies - I do not need to add elements to zombies' bags.
To better clarify: Once there is a link between one turtle and another one, these turtles can have access to all the items in their bags, respectively. When it is time to select an action, the turtle can have access to its own bag that contains all the items stored from the neighbours’ bags, as bags are made up of items (medicines and virus) from all the turtles linked to a chosen turtle.
However, the turtle becomes infected only if it decides to select the action spread, and to select an item infected from the neighbors’ bags.
After the choice, the element chosen is added to its own bag.
I edited part of the code. What I did is to create a new bag for zombies, initialising with only "virus"; furthermore, I edited the part of code regarding 'to spread' as following:
to spread
set action1? false
set spread? true
ifelse any? out-link-neighbors with [infected?]
[ print "Virus!"
set spreadvirus spreadvirus + 1
become-infected]
[ print "Medicine from neighbour"
set spreadvirus spreadvirus + 1
become-susceptible]
end
But also in this way, I am not having humans that become infected. And I think that the bag that I am trying to build is not storing all the items from the turtle's neighbours. And this explains why turtles cannot become infected.
I hope you can help me to understand what I have done wrong.
Thank you for your time, effort and help.
That's a much clearer explanation of what should happen, but you still haven't told us what is happening. For example, you have print statements in there, is the appropriate output being printed (so the code is getting to that part) or is that bit of the code not being selected? Are you getting an error message? What does one of these lists look like?
I think you mean that the logic is not working. I suggest you put some more diagnostic prints at the top once you have selected the human:
ask one-of humans
[ type "Neighbour count: " print count out-link-neigbors
type "Bag at start: " print bag
ifelse...
Then you can see why the selected path is being selected. It will also let you know if the variable contents aren't what you expect.
You have a comment that the line set bag lput spreadvirus bag is not working. So replace:
[ show one-of bag
set bag lput spreadvirus bag ;; NOT WORKING
print "spreadvirus" ]
with
[ show bag
set bag lput spreadvirus bag
show bag
print "spreadvirus" ]
to see what it is doing.
The other thing you can do to debug is replace ask one-of human with 'ask human 10` (or some other specific human). Find humans with different situations - one with only one neighbour for example, and try them out.
UPDATED in response to comment
The [6 2] is the contents of the bag. When you put act1 or spreadvirus or whatever into the bag, you are putting a number into the bag. This is because act1 and spreadvirus are numbers. They start as 0 and you add 1 to them at various points in your code.
It seems to me that you have put the wrong things in the bag. If you want a human to become infected if they pull out an infected item from the bag, then the bag needs to contain infected and uninfected items, not numbers.

Set value of speed slider programmatically

In setup, I draw a bunch of turtles--as small circles--to display two curves defined by functions. A very simple way to do this is
ask patches with [pycor = (myfunction pxcor)] [sprout 1 [...]]
and that's what my code does at present. It's kind of wasteful, since every patch has to be consulted--in random order--for each curve, but it's simple and easy to read, and it only happens during setup.
However, there's a little bit of a pause as the curves are constructed. If I move the speed slider all the way to the right, the pause is not noticeable. If I wrap the curve display routines in no-display and display, the user doesn't see the curves being constructed, but the speed is unchanged, AFAICS. If I move the slider to the left, it takes a long time to construct the curves even with no-display; the user doesn't see the points being placed one by one, but nevertheless has to wait while twiddling her/his thumbs.
Is there a way to set the model speed programmatically (for normal, "headfull" use)? I don't want to tell users "Move the speed slider to the right, then press setup, then move it back to the center before pressing go.
If not, maybe I'll code the curves properly using loops, but I thought I'd ask. Seems like there would be a way to do this, but I haven't found anything in the dictionary or programming docs so far.
(edit: no-display, if it did help, isn't available in NetLogo Web, which I am targetting along with regular NetLogo.)
I don't believe there is. However, you are asking all patches, when you could simply ask the pxcor values. This should speed it up a lot - square root of the number of iterations if a square world. Something like:
to testme
clear-all
let counter min-pxcor
while [counter <= max-pxcor]
[ let fn-out (function counter)
if fn-out >= min-pycor and fn-out <= max-pycor
[ ask patch counter fn-out [ set pcolor red]
]
set counter counter + 1
]
end
to-report function [#invalue]
report #invalue ^ 2
end

How do I make linked turtles move together in NetLogo?

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.

netlogo turtles moving on coordinates

I created turtles called "birds" that shall move on a specific route. The route is made out of links between turtles I called "rasts". As I couldn't find a way to make the turtles move from the first rast to the second, third, fourth and fifth, I changed the rasts an created them as patches.
So, now I have patches in red (rests).
How do I get the birds moving to exactly these patches and, when they are at the patch, how do I make them go to the next one?
I have no code at the moment, because I always hope to find the fault in my first model (see my other questions).
Is there anybody who knows how to solve my problem?
the move-to command moves your turtle to any other turtle or patch you specify. You can also use the face and forward commands to gradually move along a route (see the 'Move Towards Target' code example in the Models Library that comes with NetLogo)