How a patch to display the number of times that turtles have visited it? - netlogo

I would like to show how many times a patch has been visited by turtles after the simulation.
ask patches with [pcolor = lime] [
if count turtles-here > 0
[set counter (counter + 1)]
set plabel counter
]
Something looks like that. Each patch's value will be increased when a turtle visit it. In the end of simulation, each patch will show the different number of times that turtles have visited it. Thanks.

Your solution seems fine. You just need to give patches a counter attribute. For example,
patches-own [counter]
to setup
ask n-of 50 patches [set pcolor lime sprout 1]
ask patches [count-visits]
end
to go
ask turtles [move-to one-of patches]
ask patches [count-visits]
end
to count-visits ;; patch proc
if (pcolor = lime) [
if count turtles-here > 0 [
set counter (counter + 1)
]
set plabel counter
]
end

Related

Netlogo Creating a Link with a turtle when they both hit the same patch

Maybe i worded the question wrong but what i want to do in the code is when another turtle meets another turtle they create links with each other
I know its
to go
tick
make-circle
move
if ticks >= timer1 [stop]
end
to move
ask turtles
[
create-links-with other turtles ;here is where i want to put the code
set heading random 360 fd 1]
create-network
end
to create-network
ask links [
set thickness 0.01 * counter
if [patch-here] of end1 = [patch-here] of end2
[set counter (counter + 1)]
]
end
but im not sure how to word it correctly to link when they meet how do i do that
Establish a breed variable for your counts. Then create a link between all turtles that are on the same patch with other turtles-here. Then increment that count variable for when the others turtles have met the original calling turtle. I'll note that I increment the count variable by .5 because each turtle in the link will increment it (there's 2 turtles, so .5 * 2 = 1).
links-own [meets]
to setup
clear-all
crt 100 [setxy random-xcor random-ycor ]
end
to go
ask turtles [fd 1]
ask turtles [rt random 90]
ask turtles [ create-links-with other turtles-here]
ask turtles [ ask other turtles-here [ask link-with myself [ set meets meets + .5]]]
end

NetLogo : Keep turtles moving continuously till the end

Readers,
I'm a beginner in NetLogo. Please help me in solving few issues with my code that is below:
I'm getting an error "You can't use tick in a turtle context, because tick is observer-only.
I need to get tick value updated after each turtle go all three of "arrive-reception, arrive-triage, go-drroom".
the rest of people is not moving around the arrive reception, arrive triage is running.
to setup-people
set-default-shape turtles "person"
set destination ( patch-set patch -2 34 patch 8 34 )
create-turtles uninfected
[ set color blue
allocate-turtles
]
create-turtles infected
[ set color red
allocate-turtles
]
end
to allocate-turtles
if (pcolor = 9.9 and any? turtles-here)
[
set size 1.5
set heading 0
setxy int random-xcor int random-ycor
]
end
to go
move-people
arrive-reception
arrive-triage
go-drroom
tick
end
to move-people
ask turtles [
rt 360
forward 1
]
end
to arrive-reception
ask n-of (random count turtles) turtles
[
if windows = 1
[
move-to patch -2 34
ifelse not any? turtles-here
[ wait wait-time ]
[ wait-in-queue ]
]
]
end
to wait-in-queue
set arrival-time ticks
bk 1
if any? other turtles-here
[ wait-in-queue ]
wait wait-time
if ticks - arrival-time > wait-time
[ set arrival-time 0
fd 1 ]
end
to arrive-triage
if triage = "Open"
[
move-to patch 26 11
if any? other turtles-here
[ wait-in-queue]
wait wait-time
move-to one-of patches with [pcolor = 109 and not any? other turtles-here ]
wait wait-time
]
end
to go-drroom
move-to one-of patches with [pcolor = 128]
if ( min-one-of other turtles in-radius 5 [distance myself] != nobody)
[
move-to one-of patches with [pcolor = 129]
if ( min-one-of other turtles in-radius 5 [distance myself] != nobody)
[
move-to one-of patches with [pcolor = 5]
if any? seats with [turtles = nobody]
[
move-to one-of max-n-of 6 neighbors [seats]
]
]
]
wait wait-time
die
end
Thanks.
First, some basic programming tricks - don't write so much before trying to debug. If you make a small change and check it, then it's easy to work out where the error is. The first draft of a procedure can be as simple as:
to go-drroom
end
and then fill in the details of what happens in the procedure later.
Typically this error is because you forgot to close a bracket somewhere. That is, one of the procedures starts with ask turtles [ ... and there is no ] so NetLogo is still thinking that the code applies to turtles. However, I can't see an obvious missing ].
But you do have a conceptual problem. The term context is used in NetLogo to refer to who is asking the code to be done and to whom. So ask turtles [ forward 1] is the observer asking the turtles to move and is an observer context procedure. You are not thinking about what context you are in when writing the procedures, and this is probably what is setting off your error.
In the go procedure, you first call move-people. This does ask turtles [ ] so is (appropriately) from the observer context. Then you call arrive-reception and it is also okay.
But then you call arrive-triage and go-drroom still from the observer context and have commands like move-to. Who is being asked to move? You don't have ask turtles .... On the other hand, the procedure wait-in-queue has commands like move-to, but it is fine because it is only called from within an ask turtles ... in the arrive-reception procedure.

How to ask turtles to place in a desired area, on Netlogo?

I want to create turtles, which they place in a desired area with random coordinate:
they should place in the white area and in middle of it in a line. in other words, in the top regtangle, their xcor should be random and their ycor is 10. in the right regtangle, their ycor should be random and their xcor is 10 and so on.
When you create turtles, you can give them instructions such as their location. For example:
create-turtles 1 [ set ycor 10 ]
Alternatively, you can sprout the turtles from the relevant patches and their location will already be set. For example:
ask n-of 5 patches with [pcolor > 1] [ sprout 1 ]
to place-on-color [#color]
let _patches (patches with [pcolor = #color])
ask turtles [
move-to one-of (_patches with [not any? turtles-here])
]
end
Add error checking if you may have too many turtles. (Or remove the unique occupancy constraint if you don't want it.)

How to make simulation run faster in netlogo instead of using Slider bar near to view update

Is there any code to make simulation run faster in netlogo, instead of using slider bar near to setting? What my code need to do is to simulate the crowd behavior, it's work fine if the number of turtles around 100,however when I increase the number up to 300-800 turtles, simulation take very long to finish. Each tick also take very long to count from 0 to 1 and next until all turtles die. one thing that I suspect cause slow simulation is when ask turtles to evacuate. without evacuate rule, everything went smoothly even set a maximum number of turtles. is there other way to write evacuate rule, so that it can run faster? thanks.
to go
ask turtles [wander fd 0.01]
if emergency? = true [move]
if all? turtles [ pcolor = red ] ;stops simuation
[stop]
tick
end
to wander
[ do..something]
end
to move
set time-to-evacuate time-to-evacuate + 1
ask turtles [avoid-obstacles fd 0.1]
ask turtles [follow-leader fd 0.1]
ask turtles [flock fd 0.1]
ask turtles with [pcolor != red] [evacuate fd 0.1]
ask turtles with [pcolor = red][die]
end
to evacuate
ask turtles with [color = black ]
[let beings-seen patches in-cone 10 135 with [pcolor = red]
if any? beings-seen
[ let target one-of beings-seen
face target]]
ask turtles with [color = white ]
[let beings-seen patches in-cone 5 135 with [pcolor = red]
if any? beings-seen
[ let target one-of beings-seen
face target]]
end
to avoid-obstacles
[do something]
end
to follow-leader
[do something]
end
to flock
[do something]
end
In your move procedure you have:
ask turtles with [pcolor != red] [ evacuate ... ]
And then in evacuate you have:
ask turtles with [color = black] [ ... ]
evacuate is already being run by all of the non-red turtles, so you've got every non-red turtle asking every black turtle to do something at every time tick.
I don't think you intended that.
I have to guess a bit at your intent, but I think if in evacuate you replace the ask with an if:
if color = black [ ... ]
that's probably closer to what you meant.

On netlogo, what command do I use to make a turtle stop if the patch it wants to move to is a certain color

I'm making a maze on netlogo and I want to do it so that once it tries to walk into the violet lines, it'll stay on its own patch instead of moving forward. What command would that be? I tried bk 1 to reverse the fd 1 but it doesn't work all the time
You can undo your step like this:
ask turtles [
fd 1
if pcolor = violet [fd -1]
]
Or you can check ahead of time as Marzy answered. Basically it's the difference of asking for forgiveness vs permission :-)
I hope this example answer your questions:
turtles-own [target]
to setup
clear-all
reset-ticks
ask n-of 100 patches [
set pcolor red
]
create-turtles 1
[ move-to one-of patches with [pcolor != red]
set heading 90
set target one-of patches with [pcolor != red]
ask target
[
set pcolor green
]
]
end
to go
ask turtles
[ifelse pcolor != green
[
ifelse [pcolor] of patch-ahead 1 != red
[
Your-Move-Function
]
[
Your-Bounce-Function
]
leave-a-trail
]
[stop
print ticks
]
]
tick
end
to Your-Move-Function
let t target
face min-one-of all-possible-moves [distance t]
fd 1
end
to Your-Bounce-Function
let t target
face min-one-of all-possible-moves [distance t]
end
to-report all-possible-moves
report patches in-radius 1 with [pcolor != red and distance myself <= 1 and distance myself > 0 and plabel = "" ]
end
to leave-a-trail
ask patch-here [set plabel ticks]
end
This is how it works:
Random patches are colored Red to show walls or obstacles, one turtle is created in a random location with a random target which is colored green:
I have used a variable to store all available patches which turtle can step on , but since I have considered a target for the turtle, turtle chooses the one patch which is closest to the target, and since I have noticed in some cases it might go in circle I have asked the turtle to leave tick number which is its move number as a plabel, you can use a variable for that for specifying if that path was already selected or not.