This page was automatically generated by NetLogo 5.0.

The applet requires Java 5 or higher. Java must be enabled in your browser settings. Mac users must have Mac OS X 10.4 or higher. Windows and Linux users may obtain the latest Java from Sun's Java site.


powered by NetLogo

view/download model file: formation_perfect.nlogo

WHAT IS IT?

This is an extension of “formation_newton.nlogo,” for the book entitled “Physicomimetics: Physics-Based Swarm Intelligence.”

HOW IT WORKS

Multiple particles use F = ma and a “split Newtonian” force law to self-organize into a perfect triangular or square lattice.

WHAT IS NEW

This is the first simulation for Chapter 4 of the book, which pushes the envelope of physicomimetics. This simulation creates perfect lattices, by making minor modifications to the existing framework.

First, we assume that each particle has an attribute assigned to it at initialization. This attribute is composed of two integers: m and n. This attribute serves to indicate where a particle should be with respect to another particle.

Second, we require that all particles share a global coordinate system (which was never assumed in the prior simulations). This can be accomplished easily - each particle is assumed to have a digital compass that tells the particle which way is north.

Finally, we deliberately break Newton’s third law to a small degree, by modifying the force law. We do this to improve performance. The reason this is important is because we want to stress that physicomimetics “mimics” physics, as opposed to merely copying physics. We are task-driven. If alterations to standard physics improves performance, that is quite acceptable.

HOW TO USE IT

Click SETUP AGENTS to initialize the particles, and click MOVE AGENTS to have them move.

The CLEAR button will clear the graphics, which becomes handy when particles have their pens down (more on this below).

The NUMBER_OF_PARTICLES slider allows you to control the number of particles created at initialization. Changing this slider while the simulation is running will have no effect. However, you can increase the number of particles while the simulation is running by using the ONE IS BORN button. However, the ONE IS BORN button does not change the number of initial particles when the simulation is restarted by clicking SETUP AGENTS.

The ONE IS BORN button creates a new particle.

There is also a TOGGLE FORMATION button that allows you to choose between square and triangular lattices.

All other sliders will affect the simulation when it is running.

THINGS TO NOTICE

Particles are initialized in a random cluster in the middle of the graphics pane, and self-organize into a perfect square or triangular lattice.

The GRAVITATIONAL_CONSTANT controls the G parameter in the split Newtonian force law. The POWER controls the value of “p” in the generalized law. The DESIRED_SEPARATION is the desired distance between neighboring particles.

The GOOD GRAVITATIONAL CONSTANT monitor uses the theory established in Chapter 3 to provide guidance on a good value of G, when you change the DESIRED_SEPARATION.

Again, FRICTION is enabled. This allows the system to stabilize.

Note that when creating triangular lattices, angular momentum is not conserved. But note that linear momentum is still conserved. The reason for this is explained in Chapter 4 of the book.

The red dot in the simulation shows the center of mass of the system. If the Conservation of Linear Momentum holds in both the x- and y-dimensions, the red dot will not move. This simulation includes a monitor for the Angular Momentum.

This model allows you to add particles. Adding particles allows you to see how scalable the system is, and illustrates just how nicely new particles are incorporated into the lattice.

THINGS TO TRY

See how the FRICTION changes behavior. Click SETUP AGENTS, lower the friction, and click MOVE AGENTS. What happens?

Similarly, change the GRAVITATIONAL_CONSTANT or the POWER.

Change the DESIRED_SEPARATION while the system is running. Try changing it slowly and then change it quickly. What happens?

Add particles and watch their trajectories.

See how the G PHASE TRANSITION value is affected when you change the GRAVITATIONAL_CONSTANT, the POWER and the FORCE_MAXIMUM.

Toggle between triangular and square formations. Note how robust the system is - the formations are torn apart and then self-repair.

EXTENDING THE MODEL

Find a more elegant way to compute the (m, n) attribute.

Consider changing the model so that the (m, n) attribute is swapped between particles, as opposed to having particles move so much.

Extend this model to create perfect honeycomb formations.

Try the Lennard-Jones force law instead.

Note, in order to change any NetLogo simulation, you must have the source code (i.e., “formation_perfect.nlogo”) downloaded to your computer, as well as NetLogo itself. You can not change the code when you are running the simulation with your browser.

NETLOGO FEATURES

This simulation allows the user to create new particles.

To create a particle, the “hatch” command is used - this clones an existing particle and moves it away from the original. Also, the new particle has the “pen down,” which means that you will see the path that the particle takes. If the graphics pane becomes too busy, click on the CLEAR button.

This code has two procedures (“attract” and “repulse”) where values of arguments are passed to the procedures.

RELATED MODELS

This is an extension of “formation_newton.nlogo” to create perfect lattices.

Chapter 17 (by Sanza Kazadi) provides alternative mechanisms for creating perfect lattices. His chapter includes videos of these techniques.

CREDITS AND REFERENCES

To see the first papers that outlined the work presented in this simulation:

Gordon, D. F., Spears, W. M., Sokolsky, O., and Lee, I. (1999) Distributed spatial control, global monitoring and steering of mobile physical agents. In Proceedings of IEEE International Conference on Information, Intelligence, and Systems.

Spears, W. M., and Gordon, D. F. (1999) Using Artificial Physics to control agents. In Proceedings of IEEE International Conference on Information, Intelligence, and Systems.

The latter paper is the first to show the perfect formations.

HOW TO CITE

If you mention this model in an academic publication, we ask that you include these citations for the model itself and for the NetLogo software:
- Spears, W. M. and Spears, D. F. (eds.) Physicomimetics: Physics-Based Swarm Intelligence, Springer-Verlag, (2011).
- Wilensky, U. (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.

COPYRIGHT NOTICE

Copyright 2011 William M. Spears. All rights reserved.

Permission to use, modify or redistribute this model is hereby granted, provided that both of the following requirements are followed:
a) this copyright notice is included, and
b) this model will not be redistributed for profit without permission from William M. Spears. Contact William M. Spears for appropriate licenses for redistribution for profit.

http://www.swarmotics.com

CODE

; William M. Spears September 2011
; Perfect Triangular and Square Lattices Using the Split Newtonian Force Law
; For research and educational use only

breed [particles particle]                                ; Introduce the "particle" breed

globals [total_lmx total_lmy total_angular_mom G p FR D
         center_of_mass_x center_of_mass_y FMAX DeltaT
         square? square_button?]

turtles-own [hood deltax deltay r F Fx Fy v vx vy dvx dvy mass view
             lmx lmy theta lever_arm_x lever_arm_y lever_arm_r angular_mom
             squarem squaren hexm hexn pm pn km kn cx cy]

to setup
   clear-all                                              ; Clear everything
                                                          ; Create and initialize particles   
   create-particles Number_of_Particles [setup-particles]
   set square_button? true                                ; Start with a square lattice
   update-info
                                                          ; Computes center of mass and displays location
   set center_of_mass_x (sum [xcor * mass] of particles) / (sum [mass] of particles)
   set center_of_mass_y (sum [ycor * mass] of particles) / (sum [mass] of particles)
   ask patch (round center_of_mass_x) (round center_of_mass_y) 
      [ask patches in-radius 4 [set pcolor red]] 
   reset-ticks
end

to run-and-monitor
   if (count turtles < 1) [user-message "Please click HALT and then SETUP AGENTS first" stop]
   update-info
   ask particles [ap-particles]
   ask turtles [move]
                                                          ; Computes center of mass and displays location
   set center_of_mass_x (sum [xcor * mass] of particles) / (sum [mass] of particles)
   set center_of_mass_y (sum [ycor * mass] of particles) / (sum [mass] of particles)
   ask patch (round center_of_mass_x) (round center_of_mass_y)
      [ask patches in-radius 4 [set pcolor red]]  

   set total_lmx sum [lmx] of particles                   ; Total linear momentum, x-component
   set total_lmy sum [lmy] of particles                   ; Total linear momentum, y-component
   set total_angular_mom sum [angular_mom] of particles   ; Total angular momentum of objects
   tick
   do-plots
end

to setup-particles                                        ; Set up the particles
   setxy (random-normal 0 20) (random-normal 0 20)        ; Start in a cluster
   set heading random 360                                 ; Everyone has a random heading 
   set vx 0 set vy 0 set mass 1                           ; Start with no motion and mass = 1
   set shape "circle" set size 5
   ifelse ((who mod 2) = 0) 
      [set color white] 
      [set color yellow]                                  ; Different colors for square formations
   set theta 0
   init-m-n
end

; Terribly ugly code to set up the (m,n) attributes. It would 
; be wonderful if someone could make this more elegant - hint hint.
to init-m-n
   ; For square lattices
   let ring (1 + (floor (sqrt (who / 4))))
   let index (who - ((ring - 1) * (ring - 1) * 4))

   ifelse (index < (2 * ring - 1))  [set squarem (1 + index - ring)] [
   ifelse (index <= (4 * ring - 2)) [set squarem ring] [
   ifelse (index < (6 * ring - 3))  [set squarem (5 * ring - index - 2)] [set squarem (1 - ring)]]]

   ifelse (index < ( 2 * ring))     [set squaren (ring - 1)] [
   ifelse (index < (4 * ring - 2))  [set squaren (3 * ring - 2 - index)] [
   ifelse (index < (6 * ring - 2))  [set squaren (0 - ring)] [set squaren (index - 7 * ring + 3)]]]
      
   ; For triangular lattices, good up to 126 particles
   ifelse (who = 0)  [set ring 1] [
   ifelse (who < 7)  [set ring 2] [
   ifelse (who < 19) [set ring 3] [
   ifelse (who < 37) [set ring 4] [
   ifelse (who < 61) [set ring 5] [
   ifelse (who < 91) [set ring 6] [set ring 7]]]]]]

   ifelse (who = 0) 
      [set index 0]
      [set index (who - ((3 * ring * ring) - (9 * ring) + 7))]

   ifelse (index < ring) [set hexm (2 * index - (ring - 1))] [
   ifelse (((3 * ring - 3) <= index) and (index <= (4 * ring - 4))) [set hexm ((8 * ring) - 8 - (2 * index) - (ring - 1))] [
   ifelse ((ring <= index) and (index < (2 * ring - 2))) [set hexm index] [
   ifelse (index = (2 * ring - 2)) [set hexm (2 * ring - 2)] [
   ifelse (((2 * ring - 2) < index) and (index < (3 * ring - 3))) [set hexm ((4 * ring) - index - 4)] [
   ifelse (((4 * ring - 4) < index) and (index < (5 * ring - 5))) [set hexm (3 * ring - index - 3)] [
   ifelse ((5 * ring - 5) < index) [set hexm (index - (7 * ring) + 7)] [
   if (index = (5 * ring - 5)) [set hexm (2 - 2 * ring)]]]]]]]]
      
   ifelse (index < ring) [set hexn ring - 1] [
   ifelse (((3 * ring - 3) <= index) and (index <= (4 * ring - 4))) [set hexn 1 - ring] [
   ifelse ((ring <= index) and (index < (3 * ring - 3))) [set hexn (2 * ring - index - 2)] [
   if (index > (4 * ring - 4)) [set hexn (index + 5 - 5 * ring)]]]]
end

; Attractive force, x- and y-components
to attract [wx wy]
   set Fx (Fx + wx * F * (deltax / r))
   set Fy (Fy + wy * F * (deltay / r))
end

; Repulsive force, x- and y-components
to repulse [wx wy]
   set Fx (Fx - wx * F * (deltax / r))
   set Fy (Fy - wy * F * (deltay / r))
end

to ap-particles                                           ; Run artificial physics
   set Fx 0 set Fy 0                                      ; Initialize force components to zero
   set vx (1 - FR) * vx                                   ; Slow down according to friction
   set vy (1 - FR) * vy 
   
   set hood [who] of other particles                      ; Get the IDs of everyone else
   foreach hood [         
      set deltax (([xcor] of particle ?) - xcor) 
      set deltay (([ycor] of particle ?) - ycor) 
      set r sqrt (deltax * deltax + deltay * deltay)
      set view 1.5                                        ; For triangular lattice 
      if (square?) [                                      ; For square lattice
      ifelse ((who mod 2) = (? mod 2)) 
         [set view 1.3 set r (r / (sqrt 2))]              ; See Chapter 3 for details
         [set view 1.7]
      ]
    
      ifelse (square?) 
         [set pm squarem set pn squaren set cx 0.5 set cy 0.5
          set km ([squarem] of particle ?) set kn ([squaren] of particle ?)] 
         [set pm hexm set pn hexn set cx 0.25 set cy 0.433
           set km ([hexm] of particle ?) set kn ([hexn] of particle ?)] 
         
      if (r < view * D) [                                 ; The generalized split Newtonian law
         set F (G * mass * ([mass] of turtle ?) / (r ^ p)) 
         if (F > FMAX) [set F FMAX]                       ; Bounds check on force magnitude

         ifelse (r > D) [attract 1 1] [
         ifelse (((deltax < 0) and (pm < km) and (deltay < 0) and (pn < kn)) or
                 ((deltax > 0) and (pm > km) and (deltay > 0) and (pn > kn))) [attract 2 2] [
         ifelse (((deltax < 0) and (pm < km)) or ((deltax > 0) and (pm > km))) [attract 2 2] [
         ifelse (((deltay < 0) and (pn < kn)) or ((deltay > 0) and (pn > kn))) [attract 2 2] [
         ifelse (((pn = kn) and (abs(deltay) > cy * D)) or ((pm = km) and (abs(deltax) > cx * D))) [attract 2 2] [
         ifelse ((not square?) and (pn != kn) and (abs(deltay) < cy * D)) [repulse 0 3] [
         ifelse ((not square?) and (pm != km) and (abs(deltax) < cx * D)) [repulse 3 0] [repulse 1 1]]]]]]]]
   ]
   
   set dvx DeltaT * (Fx / mass)
   set dvy DeltaT * (Fy / mass)
   set vx  (vx + dvx)                                     ; The x-component of velocity
   set vy  (vy + dvy)                                     ; The y-component of velocity
   set v sqrt (vx * vx + vy * vy)

   set deltax DeltaT * vx
   set deltay DeltaT * vy 
   if ((deltax != 0) or (deltay != 0)) 
      [set heading (atan deltax deltay)] 
end

to move                                                   ; Move the turtle
   fd (sqrt (deltax * deltax + deltay * deltay))

   set lmx (mass * vx)                                    ; Linear momentum of the turtle
   set lmy (mass * vy)
   
   set lever_arm_x (xcor - center_of_mass_x)
   set lever_arm_y (ycor - center_of_mass_y)
   set lever_arm_r sqrt (lever_arm_x * lever_arm_x + lever_arm_y * lever_arm_y)
   if (((vx != 0) or (vy != 0)) and ((lever_arm_x != 0) or (lever_arm_y != 0)))
      [set theta (atan (mass * vy) (mass * vx)) - (atan lever_arm_y lever_arm_x)]
   set angular_mom (lever_arm_r * mass * v * (sin theta)) ; Angular momentum of the turtle
end

to update-info                                            ; Update information from the sliders
   set G Gravitational_Constant
   set p Power
   set FMAX Force_Maximum
   set FR Friction
   set DeltaT Time_Step
   set D Desired_Separation
   set square? square_button?
end

to do-plots
   set-current-plot "Linear and Angular Momenta"          ; Select the Momenta plot
   set-current-plot-pen "Lmx"                             ; Select the Lmx pen
   plot total_lmx                                         ; Plot the linear momentum, x-component
   set-current-plot-pen "Lmy"
   plot total_lmy                                         ; Plot the linear momentum, y-component
   set-current-plot-pen "Angular"
   plot total_angular_mom                                 ; Plot the angular momentum
end

; Create a new particle
to one-is-born
   if (count (particles with [mass = 1]) > 0) [ 
      ask one-of particles with [mass = 1]
         [hatch 1 [set deltax 0 set deltay 0 pd init-m-n  ; Clone an existing particle
                   ifelse ((who mod 2) = 0) 
                     [set color white] 
                     [set color yellow]                   ; Different colors for square formations
                   setxy xcor + (random-normal 0 (D / 2)) 
                         ycor + (random-normal 0 (D / 2))]]
   ] 
end

; Toggle between triangular and square formations
to toggle-formation
  if (square_button? != 0) [set square_button? not square_button?]
end