How to manually adjust wrapping topology of netlogo interface? - netlogo

Netlogo only provides four topology options that dictates what turtles and patches do when they reach the any given edge of the netlogo world or interface, namely, box, torus, horizontal wrapping and vertical wrapping ... Is there a way to adjust these options so that just one edge is wrapped? Or three edges are wrapped for instance?

Nigus- I do remember, I never forget a Corgi! Anyway, as Jen mentioned there is no built-in for this, but you can build it into your turtle movement rules. For example, with this setup:
to setup
ca
crt 10 [ pd ]
reset-ticks
end
If you want your turtles to treat a boundary as closed, you could have them check their patch-ahead by whatever their movement speed is (example speed of 1, here) and do a simple math operation to see if they are 'allowed' to wrap at that boundary. To have the left boundary closed, try:
to left-closed ; turtle procedure
ask turtles [
let target patch-ahead 1
if ( ( [pxcor] of target ) - pxcor ) <= 1 [
fd 1
]
]
tick
end
To have the right boundary closed, you can just do the opposite:
to right-closed ; turtle procedure
ask turtles [
let target patch-ahead 1
if ( pxcor - ( [pxcor] of target ) ) <= 1 [
fd 1
]
]
tick
end
Obviously, this is a very simple example and would need some work to massage it to fit your current movement rules.
Edit:
To show this in action, check out this example code. First, a modified setup that specifies where turtles should spawn and also creates a 'wall' of red patches:
to setup
ca
ask patches with [ pxcor = 5 ] [
set pcolor red
]
ask patch -5 0 [
sprout 10 [
pd
]
]
reset-ticks
end
Now, a modified version of the right-closed procedure above that turns it into a to-report:
to-report closed-border-right? [ target-patch ]
report ( pxcor - ( [pxcor] of patch-ahead 1 ) ) <= 1
end
Now, we should expect that turtles should not be able to cross the red wall. They should also not be able to travel off the right border, but they should be able to cross the left border. So, if the turtles are free to wander, using this movement procedure:
to move-example
ask turtles [
rt random 61 - 30
let target patch-ahead 1
if closed-border-right? target and [pcolor] of target != red [
fd 1
]
]
tick
end
We should expect them to eventually get 'trapped' between the unwrapped border and the red wall- and this happens as we expect, once the turtles make their way through the left border, they cannot get back:

Related

How do I check for collisions between any two turtles?

Netlogo is honestly very weird for me, but I've been trying to make Space Invaders for a project. I need to be able to check the distance between multiple turtles of the same breed in order to run a collision check. How do I do this?
to shoot
ask turtle 0 [
hatch 1
[
set shape "dot"
set size 2
set color red
set ctrl "projectile"
set xcor [xcor] of turtle 0
set ycor [ycor] of turtle 0 + 2
repeat 40
[
ifelse ycor < 15 and distancexy [xcor] of turtle 1 [ycor] of turtle 1 > 0.5
[
set ycor ycor + 1
]
[
ifelse distancexy [xcor] of turtle 1 [ycor] of turtle 1 < 0.5
[
ask turtle 1 [die]die][die]
]
wait .01
]
]
]
end
So you want something like a 'hero' turtle to shoot all the turtles within some distance. I have modified your code a little but the basic idea is sound. You didn't really have a step for identifying the targets. I also simplified the movement by using face and used a while because there is no guarantee it will take 40 steps to get to the target.
to shoot
let shooter turtle 0 ; or however the shooter is selected
ask shooter
[ let targets other turtles with [distance myself < 0.5 ; finds the targets
if any? targets
[ let this-target one-of targets
face this-target ; so shooter is facing the target so trail better
; do the shooting
[ hatch 1
[ set shape "dot"
set size 2
set color red
set ctrl "projectile"
let step-distance 0.02
while distance this-target > step-distance
[ face this-target ; you don't need to worry about coordinates
forward step-distance
wait .01
]
die ; once close enough, projectile dies
]
ask this-target [ die ] ; and kills the target
]
]
]
end
This code is completely untested and needs another loop to get all the targets. Usually you don't use loops much in NetLogo, but an ask would change this into the perspective of the target rather than the shooter.

Divide regions accordingly to physical features

I'm working on a smaller project and got stuck on an issue, I'm not really sure if it's possible to solve it in NetLogo but I want to give StackOverflow a go!
I got a model that divides the world into different parts and randomly add physical features (such as rivers). If a feature goes through the whole region, I want it to separate the region and make into two regions. As an example, in the picture below, I want to separate the purple region into two unique regions accordingly to the physical feature (black).
The code I used to generate the picture above, can be found below.
to setup
ca
;Setting world.
resize-world 0 19 0 19
;Creating regions.
let x 5
let y 5
let col 45
while [y <= max-pycor + 1][
while [x <= max-pxcor + 1 ][
ask patches with [pxcor < x and pxcor >= x - 5 and pycor < y and pycor >= y - 5][
set pcolor col
]
set x x + 5
set col col + 10
]
set x 5
set y y + 5
]
;Generating physical features.
ask n-of 5 patches[ sprout 1[
set pcolor black]
]
let i 0
while [ i < (max-pycor * 2 )][
ask turtles [
fd 1
set pcolor black
ifelse (random 20 <= 1)
[
rt one-of [-90 0 90]
forward 1
]
[
fd 1
set pcolor black
fd 1
set pcolor black
]
set pcolor black
set i i + 1]
]
ask turtles [die]
end
My strategy for handling this is to realize that all we really need to do is "flood" a patch out by color and tag all the found adjacent patches, then repeat for any un-tagged, non-black patches until they are all done.
NetLogo does not have a "flood" command to get all patches adjacent to a patch meeting a criteria, so we make a special reporter of our own to handle it, patches-adjacent. Then it's just easy to ask those patches-adjacent to set their region to the currently chosen region.
I don't love this code, it's a little finicky and would be prone to infinite loops if tweaked incorrectly, but it should work. I bet there is a cleaner way to do this that I'm not thinking of at the moment.
; add a variable to track the different regions
; the default value will be `0` for each patch when `clear-all` is called
patches-own [ region ]
to set-regions
let current-region 1
; only act on non-black patches that haven't yet been assigned a region
let untagged patches with [ region = 0 and pcolor != black ]
while [any? untagged] [
ask one-of untagged [
ask patches-adjacent [
set region current-region
]
]
; update the region and the untagged patches we have left to process
set current-region current-region + 1
set untagged patches with [ region = 0 and pcolor != black ]
]
; this is just to get a view of the regions to quickly see if our code worked, it can be removed
ask patches [ set plabel region ]
end
to-report patches-adjacent
report patches-adjacent-ex (patch-set self) pcolor
end
to-report patches-adjacent-ex [found pc]
let newly-found neighbors4 with [ (not member? self found) and pcolor = pc and region = 0 and pcolor != black ]
set found (patch-set found newly-found)
ask newly-found [
; use recursion to find the patches adjacent to each newly-found one
; relying on updating the `found` agentset as we go to avoid duplicates
; or looping forwarder
set found (patches-adjacent-ex found pc)
]
report found
end
I solved this by using the Patch Clusters model that can be found in the NetLogo model library.

Netlogo: Can Netlogo set up an infinite number of turtles for only one specific patch?

Can Netlogo set up an infinite number of turtles for only one specific patch? And the patch is road setting. This link is the image of that specific patch. https://i.stack.imgur.com/DdBF0.jpg And the following is the sample code. However this is not compleated.
turtles-at 0 0 of patch min-pxcor 0 ; this is not compleated
Not exactly sure what you're asking, but there is no limit to the number of turtles that can be on a patch (save the limit imposed by your computer's memory).
Also, the code you're probably looking for is something like:
turtles-on patch 0 0
for the left patch and
turtles-on patch 1 0
for the right patch.
As per Bryan's answer, there is no theoretical restriction on the number of turtles in a single patch, although your computer will have a limit- the more turtles in your model (on any patch) the more memory your model will use. So the short answer is, as far as I know, there is no way to just say to Netlogo, "Spawn infinite turtles on this patch."
If, however, by infinite you really just want enough turtles that you won't run out of them for specific interactions, you could probably get by either by just spawning a large number on that patch or by just sprouting more as needed (my preference).
For the first option, you can have a bunch of turtles on the same patch:
to setup
ca
reset-ticks
ask patch 0 0 [
sprout 10000
]
ask patch 0 0 [
print count turtles-here
]
end
Alternatively, if your turtles on the patch get used up or become unavailable in some way, just have more sprout as needed to keep your numbers high enough for what you're trying to do. Here's an example where red turtles walk to a patch with "infinite" (1000) blue turtles, link to one of the blue turtles, and take them away. However, at the end of each tick, the "infinite" patch checks if there are fewer than 1000 turtles-here. If there are, it spawns enough turtles to bring that count back up to 1000. Try this code in a new file:
to setup
ca
reset-ticks
infinite-sprout
source-sprout
end
to go
ask turtles with [ color = red ] [
fd 0.5
if any? ( turtles-on patch-ahead 1 ) with [ color = blue ] [
create-link-with one-of turtles-on patch-ahead 1 [
tie
]
set color green
]
]
ask turtles with [color = green] [
move-to patch-right-and-ahead 90 1
if pycor = max-pycor [
ask link-neighbors [
die
]
die
]
]
infinite-sprout
source-sprout
tick
end
to source-sprout
ask patch max-pxcor 0 [
if not any? turtles-here and random 3 = 1 [
sprout 1 [
set shape "arrow"
set color red
set heading 270
]
]
]
end
to infinite-sprout
ask patch 0 0 [
if count turtles-here < 1000 [
sprout ( 1000 - count turtles-here) [
set shape "circle"
set color blue
]
]
]
end
Then set up your interface like this:
If you run that model for a while, you will see that at the end of every tick, the count turtles of patch 0 0 is brought back up to 1000, effectively giving you an infinite source of turtles that you can "use up." Does that accomplish what you need?

how to see the color identification signal lights?

layout intersections with traffic lights. I want the car to stop when the red lights and the lights go xanh.thi what I should use for car algorithm can recognize the color of the lights
I have used light layout algorithm as follows
to draw-stop-light
ifelse stop-light = "north"
[
ask patch -7 12 [set pcolor green ]
ask patch 10 10 [ set pcolor red ]
ask patch -10 -10 [ set pcolor red ]
]
[
ask patch -7 12 [ set pcolor red ]
ask patch 10 10 [ set pcolor green ]
ask patch -10 -10 [ set pcolor green ]
]
end
This is really a question better suited to the NetLogo users group here than Stack Overflow since you are asking how to program a poorly defined specification. For example, the users group may be able to point you to a similar model. Stack Overflow is really about fixing specific problems with your code. However, here's something to get you started until the question gets deleted.
Break down what you want to happen into very small steps and then code each step. The steps will be something like:
When the car gets near the intersection, check to see what colour the traffic light is
If the light is red, remember to stop at the intersection
Stop at the intersection if required
If at the intersection, wait until the light turns green then go
Do NOT try to write the whole thing. Instead, write one bit at a time and test is to see if it works. Here is some approximate code for the first bit:
ask cars
[ ifelse heading = 180 and ycor > 20 and ycor < 25 and not stop-flag
[ if [ pcolor of patch -7 12 ] = red [ set stop-flag true ] ]
[ ... (cars from other directions)
]
ask cars
[ forward speed
if stop-flag and ... (on intersection patches)
[ ... (move backwards to be outside the intersection)
set stop-flag false
]
]
patch-right-and-ahead (http://ccl.northwestern.edu/netlogo/docs/dictionary.html#patch-lr-and-ahead) could be useful here. Perhaps something like:
if [pcolor] of patch-right-and-ahead 90 1 = red
[ set speed 0 ]

unable to make non-stationary turtles change their direction if an obstacle a patch ahead

If any one please give some time.
I have an area(say a colony) with boundary wall as black patches and at some point within the boundary there is one building with building wall as blue patches. People(breed) are normally moving inside boundary and the building as well. Also they are entering and going out of the boundary. Due to some reason (suppose rumor) and after certain condition (if more than 15 person hears the rumor) they starts moving randomly with any of the headings 0, 90, 180 and 270. So, the problem I am unable is, to apply check on the turtles(people) randomly moving to change their heading or turn back if they sense the boundary or wall a patch ahead.
I tried following ways but not working, they simple passes form these patches
1) Asked the turtles if heard-rumor? and times-heard > 1 [
if [pcolor] of patch-ahead 1 = blue [set heading [heading] of self - 180]
if [pcolor] of patch-ahead 1 = black [set heading [heading] of self - 180] ]
2) set patches with boundary-wall [set pcolor black] and building-wall [set pcolor blue] and then set patch variables boundary-wall? And building-wall? true on these patches. Further asked the turtles
if heard-rumor? and times-heard > 1 [
if boundary-wall? or building-wall? [ set heading [heading] of self - 180 ] ]
The procedure sequence is
to go
ask people [ ;breed
fd speed
spread-rumor
people-wander ]
end
So after spread-rumor function,
to people-wander
if heard-rumor? and times-heard > 1 and inside-boundary?
[
if people-heard-rumor > 10 [ set heading one-of (list 0 90 180 270) ] ];random 360
;people-heard-rumor is a count how many have received rumor
if heard-rumor? or fear-worst? [
; if [pcolor] of patch-ahead 1 = blue [set heading [heading] of self - 180]]
; if [pcolor] of patch-ahead 1 = black [set heading [heading] of self - 180]]
boundary-wall? or temple-wall? [set i? true set heading [heading] of self - 180 show 5] ]
end
I don’t know what wrong I am doing. But surely I am not using proper way. Any help is deeply thankful.
You start out with fd speed so your people will go right through the barriers on that command without ever testing for a barrier. Note that even if you were to test 1 patch ahead before doing that, if speed can be greater than 1, you could still go right through the barriers. Furthermore, in a corner a person might have a barrier both in front of it and behind it, so reversing course can also be a problem.
Btw, [heading] of self is the same as heading, and to turn around it is more natural to say rt 180.
EDIT (in response to comment):
Here is a simple example of moving step by step, checking along the way:
to fd-with-checks [#speed]
repeat #speed [
ifelse (isbarrier? patch-ahead 1) [
stop
] [
fd 1
]
]
end
to-report isbarrier? [#patch]
report pcolor = blue or pcolor = black
end