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

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

Related

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 ask to turtles who on the cells except the origin cell?

I want to ask to turtles who on the cells except the origin cell (x, y) = (0, 0) in one-dimensional cells spaces.
The following is sample code, however, this syntax includes the origin cell (x, y) = (0, 0).
ask turtles with [xcor < max-pxcor]
Please advice here. Thank you.
Do I have it right that you want to ask turtles that are not on patch 0 0? If so, maybe this will work for you:
to setup
ca
crt 100 [ move-to one-of patches ]
reset-ticks
end
to go
ask turtles with [ patch-here != patch 0 0 ] [
rt random 61 - 30
fd 1
]
tick
end
Edit: If you want to ask turtles not on the origin patch (in this example, 0 0) and turtles not on a patch with max-pxcor:
to go-2
ask turtles with [ floor xcor > 0 and xcor < max-pxcor ] [
rt random 61 - 30
fd 1
]
tick
end

Netlogo ERROR - DISTANCE expected input to be an agent but got the number 0 instead

I am trying to create home ranges for 3 types of hosts for a model that aims to simulate the movement of ticks across a landscape based on the presence of three host-types.
Using some code that was described in a previous post I created the following code. When I run the model I eventually get the following error message:
DISTANCE expected input to be an agent but got the number 0 instead.
Any help with the issue is greatly appreciated.
to-go
if ((week-id = 13) or (week-id = 25) or (week-id = 33))
[ask patches
[sprout-mice 2
[set color gray
set shape "mouse side"
set size 0.5
setxy random-xcor random-ycor]
]
]
if week-id = 17[
let total-deer count deer
ask n-of (total-deer / 2) patches
[sprout-deer 1
[set color brown
set shape "deer"
set size 1
setxy random-xcor random-ycor]
]
]
if week-id = 17[
let total-raccoons count raccoons
ask n-of (total-raccoons) patches
[sprout-raccoons 1
[set color black
set shape "wolf 2"
set size 0.5
setxy random-xcor random-ycor]
]
]
mice-mortality
;print (count mice)
if (week-id = 40)
[deer-mortality]
;print(count deer)
if (week-id = 45)
[raccoon-mortality]
print (count raccoons)
ask deer
[deer-move]
ask raccoons
[raccoons-move]
ask mice
[mice-move]
the submodels:
to deer-move
right random 50
left random 50
forward 1
end
to raccoons-move
ifelse distance home-patch > 10
[face home-patch]
[right random 90
left random 90]
forward 1
end
to mice-move
ifelse distance home-patch > 2
[face home-patch]
[right random 90
left random 90]
forward 1
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.

Problems with defining heading pitch and roll in 3D-netlogo

i'd like to simulate sun rays on a building. For the angle of the sun rays, i have the right elevation and azimuth angles. I was calculating the corresponding values for heading and pitch. But heading, pitch and roll seem to affect each other. When heading changes, also pitch changes. I have no idea how to calculate the corresponding values to my angles.
I was checking out the "Turtle and Observer Motion" code example in the netlogo library, but that was no help.
This is my code so far:
angles shall change every 60 ticks (position in the list changes)
lists contain converted heading and pitch values respectevly
globals [h ; heading
p ; pitch
r ; roll
i ; instead of tick
data_az ; a list with the azimuth angles
data_el ; a list with the elevation angles
hour ; is the position of the angles in the list, stands for the hour
i_test
i_round
]
breed [rays ray]
to setup
ca
file-open "heading.txt"
set data_az file-read
file-close
file-open "pitch.txt"
set data_el file-read
file-close
check-date
set r 0
set i 1
set hour hour - 1
set-default-shape rays "line half"
reset-ticks
end
to go
tick
if i = 1440 ; after 24x60 i stop
[
stop
]
set i_test i / 60
set i_round round i_test
if i_test = i_round or i = 1
[
set hour hour + 1
set h item hour data_az
set p item hour data_el
]
if h != 0
[
create-sunshine
run-sunshine
]
set i i + 1
close
end
to create-sunshine
create-rays 5 [ setxyz random-pxcor max-pycor random-pzcor
set color yellow
set heading h
set pitch p
set roll r
set size 2
]
end
to run-sunshine
ask rays [fd 1]
ask rays [if pxcor = 0 [die]] ;rays stop at the edges
ask rays [if pxcor = 199 [die]]
ask rays [if pycor = 0 [die]]
ask rays [if pycor = 49 [die]]
ask rays [if pzcor = 0 [die]]
ask rays [if pzcor = 199 [die]]
end
to close
file-close-all
end
to check-date
if date = "march-equinox" [set hour 0]
if date = "summer-solstice" [set hour 24]
if date = "september-equinox" [set hour 48]
if date = "winter-solstice" [set hour 72]
end
looking forward to your recommendations!
thanks in advance
Jana
From our discussion
Idea 1
Have one of the turtles facexyz a point that gives you the correct elevation and azimuth and copying those values for the rest of the turtles. Something like
Ask one-of turtles
[
Facexyz 1000 45663 4663; or something
Ask other turtles[
Set heading [heading] of myself
Set pitch [pitch] of myself.
]
]
Idea 2
If roll is set to 0 pitch should equal elevation and heading should equal azimuth.
So it worked with idea 1. Thanks!
This is the code now:
globals [x
y
z
data_x
data_y
data_z
hour
i
i_test
i_round
leader
]
breed [rays ray]
to setup
ca
file-open "x.txt" ; three files are loaded with x y and z coordinates respectevely.
set data_x file-read
file-close
file-open "y.txt"
set data_y file-read
file-close
file-open "z.txt"
set data_z file-read
file-close
set hour hour - 1
set i 1
set-default-shape rays "line half"
reset-ticks
end
to go
tick
if i = 1440 [
stop]
set i_test i / 60
set i_round round i_test
if i_test = i_round or i = 1 [
set hour hour + 1
set x item hour data_x
set y item hour data_y
set z item hour data_z]
if i_test = i_round or i = 1 [
if y != 0 [
create-leader
create-follower
run-sunshine]]
set i i + 1
close
end
to create-leader
crt 1 [setxyz x y z
facexyz 45 1 45
set leader who
]
end
to create-follower
crt 5 [setxyz random-xcor 70 random-zcor
set heading [heading] of turtle leader
set pitch [pitch] of turtle leader
]
end
to run-sunshine
ask turtles [fd 1
pendown]
end
to close
file-close-all
end