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

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
]

Related

How to use Face to direct turtle movement toward a cone

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

Turtles hatch when crossing a line

I want my turtles to hatch (= make one more turtle) when they cross a specific line. I have tried the command ifelse?, and I can get it to work on a simple model, when my turtles randomly wanders: If they move to a patch on the left side (xcor < 0) they die, if they make a move to a patch with xcor > 0 they hatch 1.
But I want the proces to be linked to witch patch they come from. If they stand on a patch with xcor < 0 and moves to another patch with xcor < 0 they shall die - but if they change xcor from negative to positive - they should multiply (= hatch 1).
My problem is: Is it possible to write a code-string that "remembers" the turtles position one tick before and use it to either let the turtle die og multiply?
{
to setup
clear-all
create-turtles 20
ask turtles [set size 2
set heading random 45 ;; turtle is now facing northeast
setxy random-xcor random-ycor
set color white
set shape "person"]
end
to go
ask turtles
[ rt random 360 ; turns in a random direction
fd 4 ;; all turtles move forward one step
rt random 180 ;; ...and turn a random amount
fd 4
lt random 180
]
ask turtles
[ifelse pxcor > 0
[hatch random 2]
[die]]
end }
You can use a turtle variable to give each turtle a memory.
turtles-own
[
old-xcor
]
Just before the turtle moves, assign ‘xcor‘ to that variable.
To move-turtle
Set old-xcor xcor
< Your movement code >
if old-xcor < 0
[ Ifelse xcor < 0
[ Die ]
[ Hatch 1 ]
]
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

How can I clump my turtles at random points in my model?

In my model I want to be able to clump my agents but have the location of this clumping to change.
At the moment I can get them to clump using
setxy random 4 random 4
but that is located around the origin of the simulation space. How can I vary this so they clump at a different point for each model run?
Thanks
I like using asking patches to do this sort of thing but you could just change your code to.
The set the clump by hand method
let clump-X random-pxcor
let clump-Y random-pycor
crt 100 ;; or however many you want to make the create-turtles in your code
[
...
setxy (random 4 + clump-X) (random 4 + clump-Y)
]
The patch and sprout method
ask one-of patches
[
sprout 100
[
set xcor xcor + random 4
set ycor ycor + random 4
]
]

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
]