how to lineup the agents in the ascending or descending order of their who number in netlogo? - netlogo

I am creating an patient-surgeon-operation bed model, wherein I need to show surgeons lined up on the left side of patch awaiting to enter operation room in the center and the patients awaiting in the queue from the right side.
I want the surgeons and patients to be located on the patch as per their who number
S1 S2 S3 --> Operation room < -- P1 P2 P3
I use the below query, I am not sure where to incorporate the who number
to lineup-patients
LET gapp 10
LET directions
[45 90 230 180 45 90 230 180 45 90 45 90 230 180 45 90 230 180 45 90 45 90 ]
LET jj 0 ; counter / index
REPEAT initial-number-patients
[ create-PATIENTS 1
[ SETXY (0 + jj * gapp) 20
set shape "person"
SET size 1.2
SET label who
SET label-color black
SET heading item jj directions
]
SET jj jj + 1
ASK patients [
MOVE-TO ONE-OF PATCHES WITH [ PCOLOR = yellow ]
] ]
END

You have a move-to after you line them up. And it always moves all existing patients. To keep things cleaner, write a separate lineup proc.
to lineup [#patients #patch #gap]
let _x ([pxcor] of #patch)
let _y ([pycor] of #patch)
let _xqs n-values (count #patients) [[n] -> _x + n * #gap]
(foreach sort #patients _xqs [
[p x] -> ask p [setxy x _y]
])
end
You can test this with a new instance of NetLogo as follows:
to test
ca
crt 20
lineup turtles one-of patches 0.5
end

Related

The in-radius works different in Netlogo 6.1.0 than in previous version

I have the following code which creates a grid of agents and place links between pairs if they are in a distance of interference.
breed [ readers reader ]
undirected-link-breed [ rris rri ]
globals [
interf-rri-radius
num-readers
distance-var-x
distance-var-y
readers-per-row
readers-per-column
num-checkouts
]
to setup
ca
setup-globals
ask patches [ set pcolor blue - 3 ]
spawn-by-row-col
reset-ticks
end
to setup-globals
set interf-rri-radius 1005
set num-readers 40
set distance-var-x 12
set distance-var-y 22
set readers-per-row 8
set readers-per-column 5
set num-checkouts 0
end
to spawn-by-row-col
let half-step 0.5 * distance-var-x
let d-vals ( range ( min-pxcor + half-step ) ( min-pxcor + (readers-per-row * distance-var-x)) distance-var-x )
let dc-vals ( range ( min-pxcor + half-step ) ( min-pycor + (readers-per-column * distance-var-y)) distance-var-y )
show dc-vals
; Create an empty list to build into
let possible-coords []
; For each possible vertical value, map all horizontal values in order and
; combine these into an ordered list starting at the lowest px and py coords
foreach dc-vals [
d ->
set possible-coords ( sentence possible-coords map [ i -> (list i d) ] d-vals )
]
show (word "possible-coords = " possible-coords)
; Use the number of readers to sublist the possible coordinates, and
; create a turtle at each of the coordinate combinations left.
let max-positions length possible-coords
if max-positions > (num-readers + num-checkouts) [ set max-positions (num-readers + num-checkouts) ]
let use-coords sublist possible-coords num-checkouts max-positions
foreach use-coords [
coords ->
create-readers 1 [
setxy item 0 coords item 1 coords
set shape "square 2"
set size 2
set color 15
]
]
ask readers [ create-rris-with other readers in-radius (interf-rri-radius / 10) ]
end
The neighbors of reader 0 are
show [sort [who] of rri-neighbors] of reader 0
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39]
However, the distance between reader 0 and reader 39 is
show [distance reader 0] of reader 39
121.6552506059644
and the connection radius is 1005/10= 100.5 so they should not be connected with an rri link.
Notice, I use a origin-centered world of -50 to 50 in X and Y dimensions.
I have tested the code in previous Netlogo version 6.0.4 and the reader 39 is not a neighbour of the reader 0.
I do not know what can be the problem. I believe is the new version but I would like to be sure.
In case anyone else runs into this, there is a confirmed bug with in-radius in the NetLogo 6.1.0 release, and the details are in the bug report on GitHub.
The issue only affects in-radius when used in a non-wrapped world, and only when used on turtles, and only when used with a radius that's a significant % of the world width. If you're using a world with wrapping, or using in-radius with patches, or using a small radius relative to the world size, your data will be correct and you don't need the below workaround.
As a workaround if your model is affected, you can use a simple user-defined NetLogo procedure in your model until the fix is published. It's not going to be super-fast if your having turtles calculate in-radius many times per tick, but if you have few turtles or are just using it during setup, it should be okay:
to-report temp-in-radius [agentset r]
report agentset with [ distance myself <= r ]
end
Then, instead of create-rris-with other readers in-radius (interf-rri-radius / 10) you would do create-rris-with (temp-in-radius other readers (interf-rri-radius / 10)).
Or more generally, instead of something like count other turtles in-radius 5 would become count temp-in-radius (other turtles) 5.

Set probability range for heading in Netlogo

My problem consists of 2 things:
how to code 4 different range for heading
a. 315 - 45
b. 46 - 135
c. 136 - 225
d. 226- 314
2.how to code probability on choosing this range.
(I have attached image below to make it clear)
What I wish to do is:
a) for heading a; the probability of turtles choose any of this range should be 85%
b) for heading b,c, and b, there will be 5% of turtles choosing any side of this heading
Since I'm quite bad at explaining in words, I have attached image on what I plan to do
here is my code
to random-behave
let p random-float 100
if (p >= 85)
[set heading heading + 45
set heading heading - 45]
if (p >= 15)
[set heading other heading + 45
set heading other heading - 45]
if not can-move? 1 [ rt 180 ]
fd 1
]
end
I tried to implement rnd extension but I can't see where should I put it. And if possible, instead of if (p >=15), is there any way to code the heading according to its degree? Since other can't be use here.
Thank you in advance.
You don't need the rnd extension, your approach with random-float is fine. However, you need to use if-else rather than if because a returned random number 'p' that is >85 will also be >15. Instead, you need to break up the interval from 0 to 100 into pieces that are the appropriate length. This is not tested.
to random-behave
let p random-float 100
if-else p <= 85
[ print "got up to 85" ]
[ if-else p <= 90
[ print "got 85 to 90"]
[ if-else p <= 95
[ print "got 90 to 95" ]
[ print "got 95 to 100" ]
]
]
if not can-move? 1 [ rt 180 ]
fd 1
end
You also have errors in your heading calculation. I only answered the probability part since that's what you specifically asked. But you should ask a new question if you need help with your heading.

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

To create fixed number turtles each with separate headings on fixed ycor and equidistant xcor

I want to create 4 turtles on fixed pycor (like pycor = 10) and even spacing xcor over that pycor; and also I want to make the headings of each turtles separate from other. The display is like
............. O ............. O .............. O ........... O ............
(heading 45) (heading 90) (heading 230) (heading 180)
O is the turtle here. My code is as below.
ask n-of 4 patches with [ pcolor = 18 and pycor = 10 ] [
sprout-turtles 1 [
set shape "default"
set color blue
set size 2
set heading one-of [90 270]
]
]
With this code turtles are created but many time with same heading, sometimes on same patch, sometimes neighboring patch as shown below
..........OOO...................O or .........OO..........O.........O...
but this i don't want. Should i have to use Create turtles four times separately specifying xcor, ycor and heading? Actually i don't want to use it four times. Please any suggestion and help? Thanks a lot.
Since the only thing you are taking from the patch to the turtle, you may as well just use create-turtles instead of sprout-turtles and then put them where you want. Typically, sprout is used when the particular patch meets relevant conditions - such as having lots of resources. Also, since you want specific values, using one-of or n-of will not work because they randomly select.
Instead you want something more like this (not tested):
let gap 15 ; spacing between turtles
let directions [45 90 230 180] ; heading values
let ii 0 ; counter / index
repeat 4
[ create-turtles 1
[ setxy (0 + ii * gap) 10
set shape "default"
set color blue
set size 2
set heading item ii directions
]
set ii ii + 1
]

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