How to spawn turtles a certain amount of patches away from each other - netlogo

I am trying to spawn turtles 5 patches away from each other but I'm not sure how, right now they all spawn on green patches (I don't want them to spawn on brown ones) and I'm not sure how exactly you control the distance between the spawning of turtles, thanks.
breed [ humans person ]
breed [ zombies zombie ]
to setup_world
clear-all
reset-ticks
ask patches [
set pcolor green
]
ask n-of 100 patches [
set pcolor brown
]
ask n-of 15 patches with [pcolor != brown][sprout-humans 1 [set size 5
set color blue
set shape "person"]]
ask n-of 5 patches with [pcolor != brown][sprout-zombies 1 [set size 4
set color red
set shape "person"]]
end

Have you read this question: NetLogo Create turtle at regular distance from each other?
Anyway, I thought that showing you some working functions would be helpful, here I made two alternatives, sprout-distanced1 and sprout-distanced2, you can test them both by alternating which line is commented; I also added a slider called Min-Distance to control the turtles spacing.
sprout-distanced1 uses the keyword carefully with is basically a try-else block, it's there in case that the turtle doesn't find a patch distanced enough to move to, in which case rather than sending a warning the turtle will stay where it is and print its distance to the closest turtle.
sprout-distanced2 uses a while loop, in case that the turtle doesn't find a place to move to that is at least Min-Distance from another turtle it will reduce the minimum radius by a small amount until it can distance itself from other turtles, if it had to move to a patch where it is less than Min-Distance away from other turtles it will log the distance at the Command Center.
breed [ humans person ]
breed [ zombies zombie ]
to setup_world
clear-all
reset-ticks
ask patches
[
set pcolor green
]
ask n-of 100 patches
[
set pcolor brown
]
ask n-of 15 patches with [pcolor != brown]
[
sprout-humans 1
[
set size 5
set color blue
set shape "person"
;sprout-distanced1
sprout-distanced2
]
]
ask n-of 5 patches with [pcolor != brown]
[
sprout-zombies 1
[
set size 4
set color red
set shape "person"
;sprout-distanced1
sprout-distanced2
]
]
end
to sprout-distanced1
carefully
[
; try to move at least Min-Distance away from other turtles
move-to one-of patches with [not any? other turtles in-radius Min-Distance]
]
[
; if can't move Min-Distance away from other turtles
; stay put and log the min distance to other turtle, just for reference
show distance min-one-of other turtles [distance myself]
setxy random-xcor random-ycor
sprout-distanced1
]
end
to sprout-distanced2
let min-dist Min-Distance
let moved? FALSE
while [not moved? and min-dist > 0]
[
; can distance it self somewhere?
ifelse any? patches with [not any? other turtles in-radius min-dist]
[
; if yes, go there
move-to one-of patches with [not any? other turtles in-radius min-dist]
set moved? TRUE
; if had to reduce the distancing radious log it
if moved? and min-dist < Min-Distance
[
show distance min-one-of other turtles [distance myself]
]
]
[
; no where to go, reduce the distancing radious
set min-dist min-dist - 0.1
]
]
end
Choose whichever suits better your model.

Related

NetLogo - how do I get all patches the turtle facing?

How do I get a patch set that contains all patches that the turtle is facing?
I know patch-ahead report the patch with a specific distance. But what if I want to get all patches in this direction instead of the single one with specific distance?
What you can do is hatch a turtle and move it forward until it reaches the edge of the world, adding all the patches it crosses.
Here's a visible version to see the approach:
to testme
clear-all
create-turtles 1 [setxy random-xcor random-ycor]
ask one-of turtles
[ set pcolor red
hatch 1
[ while [can-move? 1]
[ forward 1
set pcolor red
]
die
]
]
end
To actually do the patchset version, you need to start with the current patch and add the patches as the hatched turtle moves over them. Try this for a procedure version and a demonstration of how it can be used:
turtles-own [ my-path ]
to testme
clear-all
create-turtles 1 [setxy random-xcor random-ycor]
ask one-of turtles
[ set my-path get-patches-forward self
print my-path
]
end
to-report get-patches-forward [ #me ] ; turtle procedure
let front-patches patch-here
hatch 1
[ while [can-move? 1]
[ forward 1
set front-patches (patch-set front-patches patch-here)
]
die
]
report front-patches
end
This will return the wrong answer if the world is wrapped because the hatched turtle can keep on going indefinitely. Instead, you would need to check its coordinates rather than relying on the can-move? primitive.

NetLogo: calculate geographic center of patch-set

I'm modeling territory selection in NetLogo, where turtles pick a territory center ("start-patch") and then build a territory out from there based on value of patches. Turtles always return to the start-patch after claiming a new patch, then choose, move to, and claim the next most valuable patch. After choosing a territory, the turtle knows which patches it owns, and patches know their owner.
Ultimately, the start-patch of a territory may not actually end up being the true geographic center. After a turtle has selected its territory, how might I ask it to evaluate the territory, identify the geographic center, and calculate proximity of the start-patch to the true center of the territory? (Note: I don't want to force turtles to keep the start-patch in the geographic center--they are free to choose any patches they want. But I may force turtles to re-select a territory if there isn't a close match--these territories are not very efficient otherwise.)
Here's an example of how territory start-patches (black stars) do not equal geographic centers. Some example code is below. Any suggestions? Thanks in advance!
patches-own [
benefit ;; ranges 0.1-1 and represents food available in the patch
owner ] ;; patches are owned once selected for a territory
turtles-own [
start-patch ;; the selected center of the territory
sum-value ;; sum of values of patches in my territory
territory-list ;; list of patches I've selected
territory ;; agentset of patches I've selected
established ] ;; true/false, true when has settled in a final territory after assessing geographic center
globals [threshold = 25]
to setup
ask patches [ set owner nobody ]
end
to go
ask turtles [
pick-center
build-territory]
tick
end
to pick-center
if start-patch = 0
[move-to best-center ;; (calculated by a reporter elsewhere as moving windows for a cluster of high-benefit patches)
set start-patch patch-here
set owner self
set territory-list (list patch-here)
set territory (patches with [owner = myself])
]
to build-territory
ifelse sum-value < threshold
[ pick-patch ] ;; keeps picking patches for territory until I've met my threshold
[ assess-geographic-center] ;; once met threshold, assess real center of patch-set
end
to pick-patch
let _destination highest-value ;; (this is calculated by reporters elsewhere based on benefit / travel costs to a patch)
face _destination forward 1
if patch-here = _destination
[ claim-patch _destination ]
end
to claim-patch [_patch]
ask _patch [set owner myself]
set sum-value sum-value + (benefit / (distance start-patch))
set territory-list lput patch-here territory-list
set territory (patch-set territory _patch)
move-to start-patch
end
to assess-geographic-center
;; Once the territory is built, the turtle should identify the actual
;; geographic center of the patch-set it has selected...how to do this?
;; Once it knows the center, the turtle should compare the distance to the original start-patch.
;; If >10 patches away, it will move to that start-patch and start over with selecting a territory....
end
Could you get away with just the mean pxcor and pycor of all patches? Something like:
to setup
ca
reset-ticks
let n 70
ask one-of patches [
set pcolor red
]
while [ ( count patches with [pcolor = red ] ) < n ] [
ask one-of patches with [ pcolor = red ] [
ask one-of neighbors4 [set pcolor red ]
]
]
let xmean mean [pxcor] of patches with [ pcolor = red ]
print xmean
let ymean mean [pycor] of patches with [ pcolor = red ]
print ymean
ask patch xmean ymean [ set pcolor blue
]
end
Following the previous answer, here is what I came up with. This goes in the last procedure in my original code:
to assess-geographic-center
let xmean mean [pxcor] of patches with [ owner = myself ]
let ymean mean [pycor] of patches with [ owner = myself ]
ask patch xmean ymean [ set pcolor black ] ;; make the geographic center visible
let geographic-center patch xmean ymean
let distance-away distance geographic-center ;; the turtle is on its start-patch when assessing this distance
ifelse distance-away <= 5
[ set established true ] ;; turtle is happy if start-patch and geographic-center are approximately equal, territory "established"
[ move-to geographic-center ;; otherwise, turtle moves to geographic-center,
reposition ] ;; and follows a procedure "reposition" to makes this the new start-patch and repick the territory
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.)

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.