Sequentially create agents in the world on NetLogo

88 views
Skip to first unread message

Rafaela Lorena

unread,
Apr 29, 2021, 4:31:38 AM4/29/21
to netlogo-users
Hello everyone,

I have the processing time problem to develop a model in NetLogo 6.2, and I think that someone here can help me.

I have a square world with 8 thousand meters on the side with 800 grid cells. I need to create 150 agents in each grid cell. I am using the "sprout" command to create agents uniformly across the world. However, it is taking up a lot of memory and takes a long time to run the code. So, I thought about creating the 150 agents in each cell in the world at a time, that is, in a grid cell I create 150 agents, these agents die and go to the next cell and I create another 150 agents that die and I would do that until finish the world. The question is: If I do it this way, it should take up less memory and speed up the processing time, correct? Is there a command, or code example that I can use to create this sequence of agents in every cell in the world?

Thank you
Every help is welcome
Rafaela

Aaron Andre Brandes

unread,
Apr 30, 2021, 8:59:39 AM4/30/21
to Rafaela Lorena, netlogo-users

Hi Rafaela,

You will save memory if you only use 150 turtles at a time. However unless the patches are entirely independent of each other this may not solve your problem.

I have included code below in which each patch is initialized by 150 turtles. You could use similar code to set patch variables.

This code uses ask, so the patches act in random order. You might be interested in the “Ask Ordering Example” in the Models library.

I use a small number of patches and a large patch size so that you can see the patch pattern.

 

to setup

  clear-all

  resize-world -1 1 -1 1

  set-patch-size 100

end

 

to initialize-patches

  ask patches [ initialize-patch]

end

 

to initialize-patch

  sprout 150 [set size .1 fd .4 stamp die]

end

 

Aaron

-- 

Aaron Brandes, Software Developer

Center for Connected Learning and Computer-Based Modeling

--
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/CAHs8kB_oPSUuV-m%2B3O7q_X0G0PXSC5tz0ge9kUXcaKs3cj2rfQ%40mail.gmail.com.

Wade Schuette

unread,
Apr 30, 2021, 2:44:54 PM4/30/21
to Rafaela Lorena, netlogo-users
Do you mean you want a world  with 800 x 800 patches, each patch having 150 agents?  96 million agents total?   That seems unworkable regardless.

Depending on how NetLogo's Java Virtual Machine manages memory and garbage collection I suspect you may run out of memory and patience.  On my 1.3 Gig laptop the following took maybe 30 seconds to run on an 800x800 world with 1 pixel wide patches, but I'm not sure that means anything because maybe the interpreter was  clever enough to read ahead and  not even bother making agents it didn't use.  

Whatever you do, be sure to shut off the display while you're doing it or it will take much longer to run.

to go
  no-display
  ask patches [ sprout 150
                ;; do something with the agents here
                ask turtles-here [die]
              ]
  display
  tick
end

In any case I think you may be asking the wrong question.   If you explain what you want to DO with all those agents we might be able to advise more wisely on how to go about doing it in finite space and time, or possibly with fewer patches or agents.  For example it's probably faster to create a single swarm of 150 agents once and then have them do whatever they do then move to the next patch ( ie, reset x-cor and y-cor )  instead of you doing all this sprouting and destruction.  What problem are you trying to model? What will the agents be doing between their creation and death?

I set the sizes smaller for posting this here, but with a 800x800 grid ( of 1 pixel wide patches ) and a swarm of 150 agents this code ran in 40 seconds on my 1.3 Ghz laptop.   ( a 100 x 100 grid of 15 agents took well under a second to run ).
;;===============================================
globals [ edge-size sweeps   ]
turtles-own [ summary ]

to setup
  clear-all
  set edge-size 100  ;;   could be 800
  no-display
      resize-world 0 edge-size 0 edge-size
      ask patches [ set pcolor random 140 ]
  display
 
  ;; create the working swarm of turtles at some location, say, ( 0, 0 )
  create-turtles 15 [ setxy 0 0 ]  ;; could be 150
  set sweeps 0
  reset-ticks
end

to go
  reset-timer  ;; just curious
 
  set sweeps sweeps + 1
  if sweeps > 1 [ print "all done" stop]
 
  no-display
     let x 0
     repeat edge-size
         [
            let y 0
            repeat edge-size
                [
                   ;; move the swarm to the next patch at location (x, y)
                   ask turtles [ setxy x y ]
             
                    do-something x y
             
                   ;; visually mark this patch as visited
                   ask patch x y [ set pcolor green ]
             
                  ;; ========= critical for the loop to work ================
                  set y y + 1  
                ]
            ;; =========== critical for the loop to work =============
            set x x + 1
        ]
  display
  print (word "That sweep took " timer " seconds." )
  tick
end

 
to do-something [ xx yy ]
        ;; have every turtle do something while there
        ask turtles [ set summary summary + pcolor ]
end
             




--

Michael Tamillow

unread,
Apr 30, 2021, 3:21:11 PM4/30/21
to Wade Schuette, Rafaela Lorena, netlogo-users
I know people love when I say this, but why are you doing this?

Are you just stress testing Netlogo?

Reply all
Reply to author
Forward
0 new messages