Where to put "stop" to end the simulation - netlogo

I want to stop my simulation if there is no certain patch (target) to occupy. I did the following code but still not working. It just stopped the turtle, not the simulation. I did this "target" variable as globals and to include it in "go" but also cannot stop the simulation.
to set-move
ask migrants
[set pot-target patches with [value < 11 and not any? turtles-here]
set target pot-target with [count neighbors with [any? turtles-here with [value < 11]] >= 1]
ifelse (count target != 0 and (status != "resident")) [move-to min-one-of target [value]
set status "resident"
set color blue]
[stop]
]
Here is the full code
breed [migrants migrant]
breed [residents resident]
patches-own [value]
turtles-own [income
status]
to setup
ca
let total problo + probmid + probhi
if (total != 100)
[print (word "prob is more than 100")]
ask patches [set value random-normal 10 3
let patch-value value
set pcolor scale-color (gray - 5) patch-value 10 3]
ask patches
[if random 100 < 3
[sprout-residents 1
[set color red
set shape "default"
set size 1
set status "resident"
]
]
]
end
to go
ask patches
[if not any? patches with [value < 11 and not any? turtles-here] [stop ]
if random 100 < 1
[sprout-migrants 1
[set color green
set shape "default"
set size 1
set status "migrant"
set-move
]]]
end
to set-move
let pot-target patches with [value < 11 and not any? turtles-here]
let target pot-target with [count neighbors with [any? turtles-here with [value < 11]] >= 1]
if any? target and (status != "resident")
[ move-to min-one-of target [value]
set status "resident"
set color blue
]
end

The primitive stop will terminate the code block within which the stop appears. In your code, once the condition is met, the set-move procedure will end, but that doesn't end the simulation. What you need to do is test for the condition at the top level (the go procedure) and that will terminate the run. I'm a little confused by your code, but I think the answer is to separate the moving part from the checking whether to stop.
So, in set-move change:
ifelse (count target != 0 and (status != "resident"))
[ move-to min-one-of target [value]
set status "resident"
set color blue
]
[ stop ]
simply to an if block instead of ifelse (removing the stop as well). Then, at the top level, add a line something like:
if not any? patches with [value < 11 and not any? turtles-here] [stop]
I'm also concerned by your code structure of set-move starting with ask migrants. I suspect that this is an error. What you are saying is, any time set-move gets called, ALL the migrants try and move. I think you intended that only the newly created migrant tries to move since you called this from within the sprout code block. If so, then your set-move is a turtle procedure and should look like:
to set-move
set pot-target patches with [value < 11 and not any? turtles-here]
set target pot-target with [count neighbors with [any? turtles-here with [value < 11]] >= 1]
if any? target and (status != "resident")
[ move-to min-one-of target [value]
set status "resident"
set color blue
]
end
Note that I also changed your count != 0 to not any? as a readability suggestion. Personally, I would also name the agentset targets rather than target to remind yourself that it may have multiple members.
UPDATE: You need to have your stop at the top level of the go procedure so that it stops that procedure. It should look something like this:
to go
if not any? patches with [value < 11 and not any? turtles-here] [stop ]
ask patches
[ if random 100 < 1
[ sprout-migrants 1
[ set color green

rather than using [stop] to end a simulation, I would propose you use while loop instead.
while [not stop-condition][
run
]

Related

Netlogo - Turtle-Patch interaction with string-variable

I need help in Netlogo: when a turtle interacts witch a patch where it is currently placed on and the patch-variable (which is a string) decides if the turtles variable (which is an integer) grows. For example:
turtles-own [weight]
patches-own [food]
to setup
clear-all
create-turtles 1 [
setxy random-xcor random-ycor
]
ask patches[
if pxcor >= 0 [set pcolor green
set food "Fries" ]
if pxcor < 0 [set pcolor blue
set food "Burger" ]
]
reset-ticks
end
to go
increase-weight
walk-around
tick
end
to walk-around
ask turtles [
right random 120 - 60
fd 0.1
]
end
to increase-weight
ask turtles [
if food != "fries" [
set weight (weight + 1)]
if food != "burger" and [
set weight (weight + 10)]
]
end
The problem is the turles weight goes up by 11 not with the value of 1 OR 10. I guess its something with patch-here !? But i cant get it running.
Thanks a lot
You were close. The Major issue was the differences in strings. "Fries" and "fries" are not exactly equal. Change one to match the other.
While the line if food = "Fries" will work I would recommend using the patch-here in an if statement with the name of the patch variable in brackets like this:
to increase-weight
ask turtles
[
if [food] of patch-here = "Fries"
[set weight (weight + 1)]
if [food] of patch-here = "Burger"
[set weight (weight + 10)]
set label weight
]
end
That will make it super clear where the food variable is coming from.
I was confused by your use of the != and what type of food you thought the turtle was on. The above code increments weight by 1 for fries and by 10 for a burger. Your original code with your != though inverted that, giving +10 for fries and +1 for burger. The use of the != did account for your increase by 11 (10 and 1) because it was always not on either of the patches.
References:
How to check the value of a variable that belongs to a patch
NetLogo User Manual

How to assign the shortest-distance to all agents in NetLogo?

How to assign the shortest-distance to all agents
Current status: My codes only finds the shortest-path for 1 person but cannot when there are more than 1 person.
Question: How can I assign the shortest-path for each person?
Background
I manipulated the traffic grid model inside the NetLogo model library to create a shortest-path algorithm for pedestrians (somewhat close to A*). The interface below shows that an agent is standing on its origin which is coloured in pale yellow, and has the same colour as a destination. It also works fine for 3 people.
1 person
3 people
Setup
Here is a code chunck that I wrote as a setup. It doesn't seem to have any problem in this section.
globals
[
grid-x-inc ;; the amount of patches in between two roads in the x direction
grid-y-inc ;; the amount of patches in between two roads in the y direction
intersections ;; agentset containing the patches that are intersections
roads ;; agentset containing the patches that are roads
population
]
breed[people person]
patches-own
[
father
navigated?
active?
Heuristic
step
total-cost
is-intersection?
start
finish
]
people-own
[
origin
destination
h-distance ;; short for heuristic distance. Euclidean distance to goal
path
]
to setup
clear-all
setup-globals
setup-patches
setup-people
setup-destination
reset-ticks
end
to setup-globals
set grid-x-inc world-width / grid-size-x
set grid-y-inc world-height / grid-size-y
ask patches
[
set father nobody
set navigated? false
set active? false
set total-cost 0
]
end
to setup-patches
;; initialize the patch-owned variables and color the patches to a base-color
ask patches [ set pcolor brown + 3 ]
;; initialize the global variables that hold patch agentsets
set roads patches with
[(floor((pxcor + max-pxcor - floor(grid-x-inc - 1)) mod grid-x-inc) = 0) or
(floor((pycor + max-pycor) mod grid-y-inc) = 0)]
set intersections roads with
[(floor((pxcor + max-pxcor - floor(grid-x-inc - 1)) mod grid-x-inc) = 0) and
(floor((pycor + max-pycor) mod grid-y-inc) = 0)]
ask roads [ set pcolor white ]
ask intersections [ set is-intersection? true
ask neighbors [ set is-intersection? true ]]
end
to setup-people
create-people no-of-people
[
set shape "person"
set size 1
set color black
move-to one-of patches with [pcolor != brown + 3]
set origin patch-here
set destination one-of patches with [pcolor != brown + 3 and is-intersection? != true]
set h-distance distance destination
]
set population [] ;; create list of individuals
let sort-turtles sort people set population sort-turtles
end
to setup-destination
foreach population [ individual ->
ask individual [
let roads_ roads with [pcolor = white]
ask roads_ [
set navigated? false
set active? false
set Heuristic 0
set step 0
set total-cost 0
set start false
set finish false
]]
let current [origin] of individual
let target [destination] of individual
ask individual [
if target != nobody [
ask current [
set pcolor (10 + random 130)
set step 0
set start true
set finish false
set navigated? false
]
ask target [
set pcolor [pcolor] of current
set navigated? true
set start false
set finish true
]]
]]
end
Problem 1: Set all paths
set-path traces all the steps from the origin patch to everywhere inside the virtual world. The code will colour the road to yellow and add a label on the path. This procedure will repeat 50 times to find all the possible steps.
The problem occurs from this section where this code only works for one agent not for the entire agents.
to set-path
repeat 50 [
ask patches with [pcolor = white and pcolor != yellow] [
if any? neighbors4 with [ start = true ] or
any? neighbors4 with [ pcolor = yellow ]
[ set pcolor yellow
let laststep [step] of one-of neighbors4 with [pcolor = yellow or
start = true]
ifelse [plabel] of patches != nobody [
set step laststep + 1
set plabel step
set father step
set total-cost father + heuristic
set plabel-color red][set plabel ""]
]]]
let allroads [self] of patches with [pcolor != brown]
let final_location one-of patches with [finish = true]
foreach allroads [ road_patch ->
ask road_patch [set Heuristic distance final_location]
]
end
Problem 2: Set shortest path
This procedure finds the shortest path by finding the patches and adding them to a list named path. Because the previous procedure only worked for one agent this procedure will also assign the patches to one agent, which is not what I want.
to shortest-path
ask patches with [start = true] [
let maxsteps 0
ask patches with [navigated? = true]
[ let check-steps min-one-of neighbors4 with
[pcolor != brown + 3 and pcolor != white][step]
set maxsteps [step] of check-steps
]
let num (range maxsteps 0 -1)
let paths []
foreach num [ navigation ->
ask patches with [navigated? = true] [
let nav-patch min-one-of neighbors4 with
[step = navigation and pcolor != brown + 3][step]
if nav-patch != nobody [ask nav-patch [set navigated? true set paths fput self paths]]
]]
ask people [set path paths]
]
ask patches with [pcolor = yellow][set pcolor white set plabel ""]
end
Summary: What I want the model to look like
How can I assign the shortest-path for each person if there are more than 2 people inside NetLogo?
What I want the model to look like
File: https://drive.google.com/file/d/1gxhkcnlBniB5M4aSYmt65PE6LBekgVp6/view?usp=sharing

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 a patch to display the number of times that turtles have visited it?

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

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.