How to use Face to direct turtle movement toward a cone - netlogo

How do you direct turtle movement into a cone? I want turtles to move into a cone of highest chemical. I tried face cone, but it triggered the error that
nothing named cone has been defined.
I am aware that uphill-chemical is an existing way to direct turtle movement, but it is not working in my model, since the turtles wander in and out of the trail, instead of clearly following it towards one direction.
My proposed code is:
to uphill-total-chemical-scent-in-cone
let gradient-color red ;; gradients are a temporary debugging feature
let scent-ahead total-chemical-scent-in-cone 20 0 10
let scent-ahead-2 total-chemical-scent-in-cone 20 -45 10
let scent-ahead-3 total-chemical-scent-in-cone 20 45 10
ifelse (scent-ahead-2 > scent-ahead) or (scent-ahead-3 > scent-ahead)
[ ifelse scent-ahead-3 > scent-ahead-2
[ face cone 20 45 10 hatch-gradients 1 [set color gradient-color ]] ;;
not sure how to direct movement into the cone, but could try face or some variation/ask for help
[ face cone 21 -45 10 hatch-gradients 1 [set color gradient-color ]]]
[face cone 20 0 10 hatch-gradients 1 [ set color gradient-color fd 1]]
end

Related

Directing movement toward patchvariable-in-cone - alternative to uphill-patch-variable

I'm having major trouble trying to direct turtle movement toward an amount of patch-variable in-cone 20 45 10, and I think it might be because I am trying to modify uphill-chemical to fit the new reporter. For instance, with the code below, the ants were jumping across the world from one side to another. I just don't know how to clearly direct turtle movement toward the total-chemical-in-cone, as designated by the reporter below, instead of just uphill-chemical of the surrounding eight patches. Is the uphill primitive interfering with my goal, which is to direct movement toward the chemical within the entire cone, and if so, what could I use, since fd 1 is not directing movement properly?
to uphill-total-chemical-scent-in-cone
let scent-ahead total-chemical-scent-in-cone 20 45 10
let scent-right chemical-scent-at-angle 90
let scent-left chemical-scent-at-angle -90
if (scent-right > scent-ahead) or (scent-left > scent-ahead)
[ ifelse scent-right > scent-left
[ rt 45 ]
[ lt 45 ] ]
if scent-ahead > scent-left or scent-ahead > scent-right
[fd 1]
to-report total-chemical-scent-in-cone [cone-distance angle angle-width ] ; ant procedure - reports the mean amount of pheromone in cone
rt 45
let p patches in-cone 20 10 ;; cone distance 20 patches angle 45 angle width 10
lt 45
if p = nobody [ report 0 ]
;ask p [ show chemical ]
report (sum [chemical] of p)
end

NetLogo Battle Simulation: Placing soldiers in between specific coordinates?

I'm new to NetLogo and I'm unsure of how to place 300 spartans in a narrow row. Let's say an area of 2x5 patches, turtles overlapping one another. I have tried using sprout, this achieved the specific coordinate requirements but the turtles are only one per patch.. Here is some code I have.
ask patches with [pxcor > 0 and pycor > -2 and pycor < 2]
[ sprout 1 [ set color red ] ]
or
to setup-spartans
create-spartans 300
set-default-shape turtles "person"
ask spartans
[ setxy random-xcor -3 ;; makes only a single row and goes across entire screen
;; (I need it to be in a specific area)
set heading 180
set color red ]
end
How about something like this?
create-spartans 300 [
set xcor -2 + random-float 5
set ycor -1 + random-float 2
]

Netlogo: how to round heading into 90 180 270 360 only

I am currently building a model for people walking on a certain path (road patches, color = white). The agents' movement will only be limited on the white patch and when they encounter black patch they will rotate 180 deg.
The problem I found was when I tried to set the heading of the agents. I want the heading to walk to the nearest red patch (destination), but when I use:
1
set heading towards min-one-of patches with [pcolor = red ] [ distance myself ] <
the agents wont move from their patch. It turned out that the "heading" generated by the above code has the value of -360 to 360 with decimals in it. The walking procedure that I have built only allows heading with multiplies of 90 (-360, -180 ... 90, 180, 270, 360). This is the full code for my walking procedure:
2
globals [ flagl flagr ]
ask agents [
;reset flag
set flagr 0
set flagl 0
; check and note if there is a path on the left or the right
ask patch-left-and-ahead 90 1 [if (pcolor = white ) [set flagl 1]]
ask patch-right-and-ahead 90 1 [if (pcolor = white ) [set flagr 1]
]
;in T-junction, decide to turn left or right (random)
if((flagl = 1) and (flagr = 1))
[
ifelse random 100 > 50
[set heading heading - 90]
[set heading heading + 90]
]
;if it's only applicable to turn right, then turn right
if((flagl = 0) and (flagr = 1)) [set heading heading + 90]
;if it's only applicable to turn left, then turn right
if((flagl = 1) and (flagr = 0)) [set heading heading - 90]
;return if there's no mor path
if [pcolor] of patch-at-heading-and-distance heading 1 = black [rt 180]
;agent movement
**;face min-one-of patches with [pcolor = red ] [ distance myself ]**
fd 1
;stopping procedure
if [pcolor] of patch-here = red [fd 0]
]]
I tried to round the heading generated by #1 like this:
let direction heading towards min-one-of patches with [pcolor = red ] [ distance myself ]
if direction != mod 90
set direction 90 * round(arah/ 90)
However it gives error notice since "let" command cannot take that kind of input.
This is the screenshot of the map I have built:
Map
Any help to solve this problem is hugely appreciated. Thank you!
This might get you started- with minimal checking it seems to do the trick, but you will probably want to check for exceptions.
The cardinal-4 reporter takes whatever input is given and checks if the modulo is equal to 0. If not, it will check if modulo is less than/equal to 45- if so, subtracts that value from the direction (dir). If modulo is greater than 45, the subtract the modulo from the direction and add 90.
The check procedure just prints the reporter output for the values listed.
to setup
ca
reset-ticks
end
to-report cardinal-4 [ heading-input ]
let dir heading-input
let remain dir mod 90
if remain != 0 [
ifelse remain <= 45 [
set dir dir - remain
]
[
set dir dir - remain + 90
]
]
report dir
end
Edit: improved check procedure:
to check
let testVals [ -361 -359 -271 -269 -181 -179 -91 -89 -1 0 1 89 91 179 181 269 271 359 360 361 ]
print map [ i -> ( word i " becomes " cardinal-4 i "\n" ) ] testVals
end

How to measure distance covered across patches in NetLogo

I have a model setup in NetLogo where the simulation space is a GIS map with a 20 km radius that I constructed.
The max xcor and ycor are set to 20 and min xcor and ycor -20. This gives a torus of 41 x 41.
I have my agents at the centre of the map at patch 0 0 and want the maximum distance they can cover as the 20 km i.e. the extent of the GIS map.
ask turtles [
set heading 360
fd 1 ]
Am I right in saying if I iterate this code 20 times they'll be at the centre of the last patch (xcor 0 ycor 20) which isn't quite 20 km?
If that's right, how do I code it up so the agents move the appropriate distance. I feel like I'm trying to square a circle here.
I should say I can add the following line to get a patch scale that comes out at about 975.6
set patch-scale (item 1 gis:world-envelope - item 0 gis:world-envelope ) / world-width
If I multiply the patch scale by the world width I get 40000 which looks right given the radius of the circle is 20 km.
Thanks
globals [km]
to test
ca
let extent 40 ;desired world-width, in kilometers
set km (world-width / extent)
crt 1 [set heading 360]
ask turtle 0 [fd (20 * km)]
end

simulate traffic in roundabouts

I write algorithms for red cars turn right with direction from right to drive (heading = 90) towards heading = 180.
but its car turn right, go along too. how it came to a certain degree, it is first turned the corner to go towards heading 180
I tried some code without the right result?
[
ifelse heading = 90
[
if distancexy 0 0 < 30
[
rt 22
fd speed
if xcor = -2
[
set heading 180
fd speed
]
]
]
[
fd speed
]
or I write code like this?
ifelse heading = 90
[
if distancexy 0 0 < 30
[
rt 22
fd speed
if ycor = -30
[
set heading 180
fd speed
]
]
]
[
fd speed
]
You are using exact conditions like xcor = -2 and ycor = -30, but these conditions are probably never true.
NetLogo turtles don't move smoothly. They jump from point to point. Imagine the turtle disappearing from its old location and reappearing in its new location. The turtle doesn't pass through all the points in between.
For example, suppose your turtle has an xcor of -2.2 and is facing east. If the turtle does fd 1, its new xcor will be -1.2. But that doesn't mean code that says xcor = -2 will run. The turtle's xcor was never -2. At one instant, it was -2.2. At the next instant, it was -1.2. So xcor = -2 was never true.
Instead of conditions like xcor = -2, you probably want to be using conditions like pxcor = -2 (true anywhere in a whole patch) or xcor > -3 and xcor < -2 — that kind of thing.
JenB already told you this once a week ago at https://stackoverflow.com/a/34313275/86485 . I am just saying the same thing again. It's crucial you understand this.