keep turtles off wall and on grid

172 views
Skip to first unread message

rbsoc...@gmail.com

unread,
Dec 20, 2021, 1:27:17 PM12/20/21
to netlogo-users

I have two breeds of turtles, call them mobiles and immobiles.  In my model the mobiles visit one immobile and then move to the next immobile and so forth.  My problem is when both breeds are set with:

setxy random-xcor random-ycor

some of the turtles are partially or wholly off the grid.  How can I set both breeds so they are off the walls and completely visible?

Thanks, RB

James Steiner

unread,
Dec 20, 2021, 4:23:58 PM12/20/21
to netlogo-users, rbsoc...@gmail.com
Correction: below in using Setxy, I meant world-height in the formula that sets the ycor. 

Setxy 
Min-pxcor + random (world-width - 1)
Min-pycor + random (world-height - 1)

—James


On Mon, Dec 20, 2021 at 3:56 PM James Steiner <grego...@gmail.com> wrote:
It’s worth noting that NetLogo is not a grid like we tend to think of a grid… the patches centers are the integer locations. 

This means that for a world with the min-pxcor of -5 and max-pxcor of 5,

There are 11 patches  across
There is a line of patches at xcor = 0
The minimum xcor is -5.5.
The maximum xcor is 5.4999…
The world-width is 11
But (Max-pxcor - min-pxcor) is 10

So, placing turtles at random xcor and ycor means some of those turtles (drawn with size 1 shapes) may hang over the edge. 

If you want them at patch-centers, use random-p_cor

If you want them at random locations except for a border around the edge, so none hang over, use 

Setxy 
Min-pxcor + Random (world-width - 1)
Min-pycor + random (Workd-width - 1)

If your are using a turtle size other than 1, to get the same visual effect you’ll have to adjust the above to account for size. 

E.g.
Let gap (size - 1) / 2

Setxy
Min-pxcor + gap + (random (world-width - 1 - gap - gap)
… and so on

Note that doing this means your turtles are no longer evenly distributed, in that a .5 patch border is always empty. Be aware this can cause undesired biases or other artifacts in some models. 

Hope this helps

~~James
--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-users/49f7e06c-1b4b-4f5c-9f9d-9fa81bcd4364n%40googlegroups.com.

James Steiner

unread,
Dec 20, 2021, 4:24:44 PM12/20/21
to netlogo-users
It’s worth noting that NetLogo is not a grid like we tend to think of a grid… the patches centers are the integer locations. 

This means that for a world with the min-pxcor of -5 and max-pxcor of 5,

There are 11 patches  across
There is a line of patches at xcor = 0
The minimum xcor is -5.5.
The maximum xcor is 5.4999…
The world-width is 11
But (Max-pxcor - min-pxcor) is 10

So, placing turtles at random xcor and ycor means some of those turtles (drawn with size 1 shapes) may hang over the edge. 

If you want them at patch-centers, use random-p_cor

If you want them at random locations except for a border around the edge, so none hang over, use 

Setxy 
Min-pxcor + Random (world-width - 1)
Min-pycor + random (Workd-width - 1)

If your are using a turtle size other than 1, to get the same visual effect you’ll have to adjust the above to account for size. 

E.g.
Let gap (size - 1) / 2

Setxy
Min-pxcor + gap + (random (world-width - 1 - gap - gap)
… and so on

Note that doing this means your turtles are no longer evenly distributed, in that a .5 patch border is always empty. Be aware this can cause undesired biases or other artifacts in some models. 

Hope this helps

~~James



On Mon, Dec 20, 2021 at 1:27 PM rbsoc...@gmail.com <rbsoc...@gmail.com> wrote:
--

Wade Schuette

unread,
Dec 20, 2021, 5:12:02 PM12/20/21
to James Steiner, netlogo-users
@James Steiner  Wow,  there's some unexpected info that will come in handy someday!
Seriously. 

As the Chinese Poet Li Po noted centuries ago:
 
Tremble, be fearful
Night and day be careful.
Men do not trip over mountains;
they fall over earth mounds.

Happy holidays!
Wade


rbsoc...@gmail.com

unread,
Dec 21, 2021, 8:16:06 AM12/21/21
to netlogo-users
Thanks all for both concrete advice and Chinese poetry.  Logic behind grid construction interesting.
A second to a happy and healthy holiday.
Best, RB

Leo Niehorster-Cook

unread,
Dec 29, 2021, 4:12:49 PM12/29/21
to netlogo-users
Another good option might be:
ask turtles [ move-to one-of patches ]
This way, each turtle chooses a specific patch, then moves directly to its center.
Cheers!
Leo
Reply all
Reply to author
Forward
0 new messages