Help to create a given number of turtles based on a distance criterion

232 views
Skip to first unread message

Rafaela Lorena

unread,
Nov 9, 2021, 9:49:29 AM11/9/21
to netlogo-users
Hello,

We're creating code and we're having trouble doing the following:

We would like to create a specific number of turtles (for example 100 turtles)  based on the following criteria:
1) distance between agents must be greater than or equal to 2

We've already tried:
ask n-of 100 patches [
    if not any? turtles in-radius 2 [      
sprout-turtles 1 [ setup-turtles ] ]
    ]
  ]
it didn't work, because less than 100 agents are being born even though the world holds the required amount of agents, which in this case is 100

Does anyone have any suggestions on how we can resolve this issue?

Thanks in advance

Wannes...@hotmail.be

unread,
Nov 9, 2021, 1:10:36 PM11/9/21
to netlogo-users
Hello,

The following code should work:


patches-own [ crowded ]
to setup
  
  ask patches [ set crowded FALSE ]
  
  while [count turtles < 100] [
    ask turtles [
      ask patches in-radius 2 [
        set crowded TRUE
      ]
    ]
    ask one-of patches with [ not crowded ] [
      sprout 1
    ]
  ]
  
end
  

I am sure that there are more elegant solutions some other people here might give you but it is a start.

Good luck!


Op dinsdag 9 november 2021 om 15:49:29 UTC+1 schreef rafael...@gmail.com:

Wannes...@hotmail.be

unread,
Nov 9, 2021, 1:15:08 PM11/9/21
to netlogo-users
And immediately for the more elegant solution:

to setup2

  while [count turtles < 100] [
    ask one-of patches with [not any? turtles in-radius 2] [
      sprout 1
    ]
  ]
  
end

Good luck again


Op dinsdag 9 november 2021 om 19:10:36 UTC+1 schreef lei...@natur.cuni.cz:

Wannes...@hotmail.be

unread,
Nov 10, 2021, 7:13:08 AM11/10/21
to netlogo-users
One last solution if you would like to use create-turtles instead of sprout

to setup 3
  ca
  crt 100 [ let free-patch one-of patches with [ not any? other turtles in-radius 2 ]
    setxy 
    [pxcor] of free-patch
    [pycor] of free-patch
  ]
  
end

Op dinsdag 9 november 2021 om 19:15:08 UTC+1 schreef lei...@natur.cuni.cz:

Pradeesh Kumar K V

unread,
Nov 10, 2021, 11:47:09 AM11/10/21
to netlogo-users
Hello Rafaela,

The problem is that you are asking only 100 patches to create a total of 100 turtles with the condition that there are no nearby turtles. Initial set of patches will find nearby areas unoccupied and hence will sprout turtles. But as new turtles are created, patches called later on by the ask command will find that nearby patches are already occupied and hence will not sprout turtles. Since the request is made only to 100 patches, it is very likely that the number of turtles at the end is less than 100.

I suggest the following modified code:

to setup
 
  while [count turtles < 100] [
   
    ask one-of patches [

   
    if not any? turtles in-radius 2 [
     
      sprout 1 [setup-turtles]
    ]
 
  ]
 
end

Here you are repeatedly asking patches to check and sprout turtles until the total number of turtles equals the desired value i.e. 100.

Hope this helps.

Best,

Pradeesh

--
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/CAHs8kB8Gf-h3WGer-4vFRHhD1QfW0ead3Th1aDyiWYifnkTjvg%40mail.gmail.com.

James Steiner

unread,
Nov 10, 2021, 3:13:18 PM11/10/21
to netlogo-users
Check out this demonstration model. It's one from 2006 that I dusted off and put in the modeling commons.




--

Rafaela Lorena

unread,
Nov 11, 2021, 5:54:24 AM11/11/21
to James Steiner, netlogo-users
Hi,

Thank you so much to everyone who responded 😊. Your answers helped a lot.

Cheers
Rafaela

Reply all
Reply to author
Forward
0 new messages