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

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.

Related

How to make a color gradient for turtles in NetLogo 6.2?

I'm trying to create a yellow color gradient for the turtles. I have 31 turtle profiles and I would like each of the turtles to be a yellow color. I'm trying more what remains are all in white. And I think I'm complicating the code and the result is not coming out. Has anyone done something similar? If so, could you suggest a way?
globals [ AvailablePatch UnassignedProfileCountList ValidHabs MidpointnlColor2 TotalShades2 ]
turtles-own [ turtle-profiles-habitat ]
patches-own [ turtle-count habitatcover ]
to setup
clear-all
set ValidHabs [ 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 ]
set UnassignedProfileCountList [ 0 ]
repeat 31
[
set UnassignedProfileCountList lput 9 UnassignedProfileCountList
]
(
foreach ValidHabs [
this-profile ->
set MidpointnlColor2 yellow
set TotalShades2 2
ask one-of AvailablePatch
[
sprout 1
[
set turtle-profiles-habitat this-profile
set color make-nl-color-shade2 MidpointnlColor2 ValidHabs TotalShades2
set size 1
]
set turtle-count count turtles-here
set AvailablePatch other AvailablePatch
]
]
)
end
to-report make-nl-color-shade2 [ nl-color shade-value num-shades ]
set shade-value min list num-shades max list 0 shade-value
report scale-color nl-color shade-value num-shades 0
end
You can shade the colors using scale-color color number range1 range2.
When you want to scale if based on the ValidHabs, you want the range to go from the smallest (min ValidHabs) to the highest value (max ValidHabs) of ValidHabs.
For every turtle, you want to have their actual habitat, that is turtle-profiles-habitat.
So anywhere in a turtle procedure, you can:
set color scale-color yellow turtle-profiles-habitat (min ValidHabs) (max ValidHabs)

Netlogo: finding min variable of patches at certain radius of patch

I'm trying to estimate slopes between patches, so need to find the minimum value of a patch variable called Elevation from all patches that are at radius 4 from a specific patch. Here is the code:
ask patch 27 35 [let x min-one-of patches in-radius 4 [Elevation]
print x]
but instead of the lowest value of Elevation, it prints: (patch 27 31). What can I do to have the value instead of coordinates?
You've got the code to find the patch with the minimum value, so all you need is the value at that patch.
ask patch 27 35
[ let low-patch min-one-of patches in-radius 4 [Elevation]
let x [Elevation] of low-patch
print x
]
But it's more straightforward to just take the minimum of the values directly (not tested so syntax may need to be adjusted)
ask patch 27 35
[ let x min [Elevation] of patches in-radius 4
print x
]

how to lineup the agents in the ascending or descending order of their who number in 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

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
]