Turtles, patches and their moving sequentially from one patch to the next - netlogo

Platfrom : NetLogo
Question
I want to move my flag specific 3 point
A(-12 8)
B(-5 12)
C(6 4)
-While travelling this point plot energy/time randomly decrease.
-When reach C flag will die.
I asked before and found this solution for moving. ( When turtle reached 2. point it will not stop ) - LINES1 -
breed [cities city]
breed [flag person]
flag-own [target]
to setup
clear-all
create-flag 1
[ set size 6
set shape "by"
setxy -5 3
set target patch -10 5
face target
]
< other commands >
end
to go
ask flag-on patch -10 5
[ set target patch <next place you want it to go>
face target
]
ask flag with [ shape = "by" ]
[ forward 1 ]
end
People suggest this code for towarding any target.
to go
ask people [
;; if at target, choose a new random target
if distance target = 0
[ set target one-of houses
face target ]
;; move towards target. once the distance is less than 1,
;; use move-to to land exactly on the target.
ifelse distance target < 1
[ move-to target ]
[ fd 1 ]
]
tick
end
In this code they will travel randomly and i dont want this. I cant implement this part at -LINES1-
I'm try to explain this with an image.
Well, this is the question : How can i move turtle along these points and connect the graph for energy/time or energy/distance.
CC : #Seth Tisue #JenB #yacc
UPDATE 1
-Guys i finished my movement part of my program aid of community. In this code your turtle will move specific point and it will die when it reach last point. While travel it is plotting the number of turtle
breed [cities city]
breed [flag person]
flag-own [target] ;;set features flag only
to setup
clear-all
reset-ticks
print "Setting up model."
set-default-shape cities "house" ;; set all cities shape by house
create-flag 1
[
set SIZE 2
set shape "turtle"
setxy -11 13
set target patch -3 12
face target
]
create-cities 1
[set color yellow set SIZE 2 setxy 8 2]
create-cities 1
[ set color yellow set SIZE 2 setxy -3 12]
create-cities 1
[ set color yellow set SIZE 2 setxy 3 3]
ask patch 3 3 [set pcolor red]
end
to go
ask flag-on patch -3 12 [
set target patch 8 2
face target
]
ask flag-on patch 8 2 [
set target patch 3 3
face target
]
ask flag-on patch 3 3 [
if distance target < 1 ;; check distance for last point
[die]]
ask flag with [ shape = "turtle" ]
[fd 1]
tick
end

Have you tried to understand the answers you have already been given? In the setup, replace -10 5 with the first place you want to go to (which is -12 8). Then update the go code accordingly.
to go
ask flag-on patch -12 8
[ set target patch -5 12
face target
]
ask flag-on patch -5 12
[ set target patch 6 4
face target
]
ask flag with [ shape = "by" ]
[ forward 1 ]
end
This is just the direction and moving. You need to try and do some code for the energy and dying etc. But do things gradually, get something working and then add the next piece.

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 - turtle to go to the closest concentration of turtles

I would like a turtle to go to the closest patches with most turtles if a threshold of a given variable is met for 5 ticks.
My code is:
to move
let count-tick 5
if var >= 9.5 [
set count-tick count-tick - 1
if count-tick = 0 [
ask turtle [
let nearest-group min-one-of (patches with [sum turtles >= 3] in-radius 3 ) [ distance myself ]
move-to nearest-group ;; go to the biggest crowd near you
ask turtle [ ;; once there do the following
set shape "star"
set color red
]
]
]
]
end
The issue I have is that a) I am unsure how to say the patch with >= 3 turtles closest to you at the given range of 3 (attempted code above) and b) how to say once there, change your shape.
Revised to keep a permanent variable to track whether the variable is high enough 5 times in a row.
turtles-own
[ count-tick
]
; wherever you create the turtles, you need to `set count-tick 5`
to move
ifelse var >= 9.5
[ set count-tick count-tick - 1 ]
[ set count-tick 5 ]
if count-tick = 0
[ let nearest-group min-one-of (patches with [count turtles >= 3] in-radius 3 ) [ distance myself ]
move-to nearest-group ;; go to the biggest crowd near you
set shape "star"
set color red
]
end
First, you are already within an ask turtles code block from the procedure calling this move procedure. So you don't need the additional ask turtles. Look up ask in the NetLogo Dictionary, it iterates through the turtles, running all the code for each turtle in turn.
Second, you need count turtles rather than sum turtles as sum is to add up values.
Note that there is no error checking in this, you may have problems if there are no patches within radius of 3 that have at least 3 turtles.

How to manually adjust wrapping topology of netlogo interface?

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:

Make the car follow the road at the right speed (NetLogo)

Currently I am working on developing an infrastructure model in NetLogo and I have difficulty making the turtle follow the road. This I do using the gis extension.
The goal of this model is to simulate traffic movement, and for that the cars within the model need to follow the roads. However, the cars do not follow the roads at all when I try to make this work.
to load
ca
clear-patches
show "Wegenbestand laden..."
set data gis:load-dataset Input_bestand_shp
show "Wegen geladen"
show "Data importen..."
let timing 1
foreach gis:feature-list-of data
[[b] -> ask patches gis:intersecting b
[
set pspeed gis:property-value b "snelheid"
set road gis:property-value b "gid"
set status gis:property-value b "status"
set a_dir_x gis:property-value b "s_d_x"
set a_dir_y gis:property-value b "s_d_y"
set b_dir_x gis:property-value b "e_d_x"
set b_dir_y gis:property-value b "e_d_y"
if status = "gesloten" [ set pspeed 0]
if pspeed = 30 [ set maxdruk Algemene_capaciteit_straat / 5 ]
if pspeed = 80 [ set maxdruk Algemene_capaciteit_autoweg / 5]
if pspeed = 130 [ set maxdruk Algemene_capaciteit_snelweg / 5]
set fid gis:property-value b "did"
show timing
set timing (timing + 1)
]
]
show "Data geimporteerd"
ask patches [ set pcolor scale-color blue pspeed 0 130 ]
set-default-shape turtles "car"
;set exp-count 1
;set border patches with [ count neighbors != 8 ]
end
to setup
clear-turtles
clear-all-plots
reset-ticks
setup-turtles
end
to setup-turtles
;een turtle is 5 autos
ask patch Start-X Start-Y
[sprout Verkeersintensiteit_geslotenweg / 5 [ set color red ]]
;1 tick = 1 minute
ask patches[
ask turtles-here[
set speed pspeed / 60
set currentroad road
set dir_x b_dir_x
set dir_y b_dir_y
]
]
end
to go
setup-turtles
;1 tick = 1 minute
ask turtles [
facexy dir_x dir_y
fd speed
]
end
With the above code I first tested to see if any direction would work. However, the cars either randomly stop, go in completely the wrong direction and I do not know what to do.
The input are line segments that are split on vertices. The a_dir_x and the others show the front and back of each segment to give a direction. The coordinate system is WGS 84 UTM 31N.
Another thing that I was considering was to use the move-to function, which should be easier for use. In that case I would require to know the cellsize and I do not know how this is determined in NetLogo.
Does anyone have any ideas or experience with this? Thank you!
here is an image of the interface tab