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_goal_obs.nlogo

WHAT IS IT?

This is our third physics-based model of a swarm, for the book entitled “Physicomimetics: Physics-Based Swarm Intelligence.” This simulation is an extension of “formation_newton.nlogo” that includes a goal and obstacles.

HOW IT WORKS

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

A goal provides an attractive force and obstacles provide a repulsive force.

WHAT IS NEW

This simulation extends “formation_newton.nlogo” in several ways.

First, particles can become disabled. This is different from killing particles because the particles remain in the system (and are sensed by neighboring particles), but they can not move.

Second, a goal has been added to the system (shown as a blue dot on the left side of the environment). This goal is attactive. The GOAL_FORCE slider controls the strength of the goal force.

Third, you can place obstacles in the environment with your mouse, to create an obstacle course. The OBSTACLE_FORCE slider controls the size of the obstacles (i.e., the extent at which the repulsive obstacle force is felt). See Chapter 3 for more details.

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 after the simulation is running will have no effect. You can change the number of particles while the simulation is running by using the ONE IS BORN and KILL ONE buttons. However, the ONE IS BORN and KILL ONE buttons do 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.

The KILL ONE button randomly kills an existing particle.

The DISABLE ONE button randomly disables an existing particle. This particle remains in the system but can not move.

There is also a TOGGLE FORMATION button that allows you to choose between square and triangular lattices. A monitor indicates whether the square or triangular lattice has been selected.

The TOGGLE GOAL button turns the goal force on and off. The strength of the force is controlled with the GOAL_FORCE slider.

Obstacles can be placed in the environment by placing the mouse where you want the obstacle to be, and then clicking the mouse. The obstacle is shown as a green disk. The OBSTACLE_FORCE slider changes the size of the obstacle. This affects all obstacles and the change is shown visually as the sizes of the disks change.

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

THINGS TO NOTICE

Particles are initialized in a random cluster at the right of the graphics pane, and self-organize into a triangular lattice. The lattice is not perfect, but that is ok because we are interested in “satisficing systems,” as opposed to “optimal systems.”

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.

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

This simulation serves to teach you about physics-based swarms, as well as the Conservation of Linear and Angular Momenta. This is covered in detail in Chapter 3 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 and you will see that it does not change over time, if the system is closed.

This model allows you to add and remove particles. Removing particles allows you to test how robust the system is to particle failure. Adding particles allows you to see how scalable the system is, and illustrates just how nicely new particles are incorporated into the lattice. Adding, disabling, or removing particles opens the system, and temporary changes in the momenta can occur during those events.

Similarly, turning the goal force on and off, as well as adding obstacles, opens the system. The center of mass will move when this happens.

Chapter 3 discusses how to balance the goal and obstacle forces properly.

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?

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

Add and remove particles. After you add a particle, turn on the goal force. Since the pen will be down on the newly added particle, you can watch the trajectory as it moves.

Try different obstacle courses. Increase the number of obstacles and increase the size of the obstacles. What happens?

EXTENDING THE MODEL

Currently all obstacles in one environment have the same size. Modify the code to allow each obstacle to have a different unique size.

Try having multiple goals. What happens?

Note, in order to change any NetLogo simulation, you must have the source code (i.e., “formation_goal_obs.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 and kill existing ones.

Killing a particle is accomplished via a call to the NetLogo procedure “die.”

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 simulation makes use of NetLogo mouse events to allow the user to place obstacles in the environment.

RELATED MODELS

This is our third physics-based swarm simulation, which adds a goal and obstacles to the environment.

CREDITS AND REFERENCES

To see our implementation on seven simple robots moving towards a goal:

Spears, W. M., Spears, D. F., Hamann, J., and Heil, R. (2004) Distributed, physics-based control of swarms of vehicles. Autonomous Robots, 17 (2-3).

For an explanation of the phase transition:

Gordon-Spears, D. F., and Spears, W. M. (2003) Analysis of a phase transition in a physics-based multiagent system. Lecture Notes in Computer Science, Volume 2699.

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.

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
; Triangular and Square Lattices and a Goal Force with Obstacles!
; You introduce the obstacles with your mouse.
; For research and educational use only

breed [particles particle]                            ; Introduce the "particle" breed
breed [goals goal]                                    ; Introduce the "goal" breed
breed [obstacles obstacle]                            ; Introduce the "obstacle" breed

globals [total_lmx total_lmy total_angular_mom G p FR D objects
         center_of_mass_x center_of_mass_y FMAX DeltaT obstacleF
         square? square_button? goalF goal? goal_button? Gt Gf heavy disabled]

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]

to setup
   clear-all                                          ; Clear everything
   set disabled 0                                     ; No one disabled at the start 
   set heavy 100000                                   ; Used for disabled particles, goals, and obstacles
                                                      ; Create and initialize particles
   create-particles Number_of_Particles [setup-particles]
                                                      ; Create blue goal on the left side
   create-goals 1 [set color sky set size 5 set shape "circle" 
                   set mass heavy setxy (- world-width / 4) 0]
      
   set square_button? false                           ; Start with a triangular lattice
   set goal_button? false                             ; Start with goal force off
   update-info
 
   ifelse goal? 
      [set objects (turtle-set turtles)]              ; If goal? need to include all entities 
      [set objects (turtle-set particles obstacles)]  ; Otherwise, just particles and obstacles
                                                      ; Computes center of mass and displays location
   set center_of_mass_x (sum [xcor * mass] of objects) / (sum [mass] of objects)
   set center_of_mass_y (sum [ycor * mass] of objects) / (sum [mass] of objects)
   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 goals     [ap-goals]
   ask obstacles [ap-obstacles]
   ask turtles   [move]

   ; Use mouse click to create obstacles. Must make sure that mouse is within black graphics pane.
   if (mouse-down? and mouse-inside?) [
      ifelse ((count obstacles) = 0) 
          [create-obstacles 1 [setxy mouse-xcor mouse-ycor set vx 0 set vy 0 set shape "circle" 
                               set size (2 * obstacleF) set mass heavy set color green]]
          [ask one-of obstacles [hatch 1 [setxy mouse-xcor mouse-ycor set vx 0 set vy 0]]]
      wait 0.2                                        ; Pause so don't get a bunch of obstacles at once
   ]
   
   ifelse goal? 
      [set objects (turtle-set turtles)]              ; If goal? need to include all entities
      [set objects (turtle-set particles obstacles)]  ; Otherwise, just particles and obstacles
                                                      ; Computes center of mass and displays location
   set center_of_mass_x (sum [xcor * mass] of objects) / (sum [mass] of objects)
   set center_of_mass_y (sum [ycor * mass] of objects) / (sum [mass] of objects)
   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 objects                 ; Total linear momentum, x-component
   set total_lmy sum [lmy] of objects                 ; Total linear momentum, y-component
   set total_angular_mom sum [angular_mom] of objects ; Total angular momentum of objects
   tick
   do-plots
end

to setup-particles                                    ; Set up the particles
   setxy ((world-width / 4) + random-normal 0 20) 
         (random-normal 0 20)                         ; Start in a cluster on the right side
   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
end

to ap-particles                                       ; Run artificial physics on the particles
   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 all other particles
   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]
      ]
    
      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) 
            [set Fx (Fx + F * (deltax / r))           ; Attractive force, x-component
             set Fy (Fy + F * (deltay / r))]          ; Attractive force, y-component
            [set Fx (Fx - F * (deltax / r))           ; Repulsive force, x-component
             set Fy (Fy - F * (deltay / r))]          ; Repulsive force, y-component
      ]
   ]
   
   ; Now include obstacles
   set hood [who] of obstacles                        ; Get the IDs of obstacles
   foreach hood [         
      set deltax (([xcor] of obstacle ?) - xcor) 
      set deltay (([ycor] of obstacle ?) - ycor) 
      set r sqrt (deltax * deltax + deltay * deltay)
      if (r <= obstacleF) [
         set F (obstacleF - r)                        ; Simple linear force law
         set Fx (Fx - (F * (deltax / r)))             ; Repulsive force, x-component
         set Fy (Fy - (F * (deltay / r)))             ; Repulsive force, y-component
      ]
   ]
   
   ; Now include goal force, if toggled on
   if (goal?) [
      set hood [who] of goals                         ; Get the IDs of goals
      foreach hood [         
         set deltax (([xcor] of goal ?) - xcor) 
         set deltay (([ycor] of goal ?) - ycor) 
         set r sqrt (deltax * deltax + deltay * deltay)
         set F goalF                                  ; Constant force magnitude
         set Fx (Fx + (F * (deltax / r)))             ; Attractive force, x-component
         set Fy (Fy + (F * (deltay / r)))             ; Attractive force, y-component
      ]
   ]
   
   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 ap-goals                                           ; Run artificial physics on the goal
   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 particles                        ; Get the IDs of all particles
   foreach hood [         
      set deltax (([xcor] of particle ?) - xcor) 
      set deltay (([ycor] of particle ?) - ycor) 
      set r sqrt (deltax * deltax + deltay * deltay)
      set F goalF                                     ; Constant force magnitude
      set Fx (Fx + F * (deltax / r))                  ; Attractive force, x-component
      set Fy (Fy + F * (deltay / r))                  ; Attractive force, y-component
   ]
      
   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 ap-obstacles                                       ; Run artificial physics on the obstacles
   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 particles                        ; Get the IDs of all particles
   foreach hood [         
      set deltax (([xcor] of particle ?) - xcor) 
      set deltay (([ycor] of particle ?) - ycor) 
      set r sqrt (deltax * deltax + deltay * deltay)
      if (r <= obstacleF) [
         set F (obstacleF - r)                        ; Simple linear force law
         set Fx (Fx - F * (deltax / r))               ; Repulsive force, x-component
         set Fy (Fy - F * (deltay / r))               ; Repulsive force, y-component
      ]
   ]
      
   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
   fd sqrt (deltax * deltax + deltay * deltay)        ; Move the turtle

   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 goalF Goal_Force
   set obstacleF Obstacle_Force
   set goal? goal_button?
   set square? square_button?
   ifelse square?                                     ; G phase transition computation, for
      [set Gt (FMAX * (D ^ p) / ((2 * sqrt(2)) + 2))] ; square lattice, and for
      [set Gt (FMAX * (D ^ p) / (2 * sqrt(3)))]       ; triangular lattice (see Chapter 3 for details)
   set Gf ((G * heavy) / (D ^ p))                     ; Need to pull away from a disabled particle,
   if (Gf > FMAX) [set Gf FMAX]                       ; but we still need to think about FMAX
   ask obstacles [set size (2 * obstacleF)]           ; Update obstacle size if necessary
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

; Kill a particle
to one-dies
   if (count (particles with [mass = 1]) > 1) [       ; Don't kill last particle
      ask one-of particles with [mass = 1] [die]      ; Ask one particle to die
      clear-drawing                                   ; A little cleanup is required
   ]
end

; Disable a particle by giving it a heavy mass so it can't move
to one-is-disabled
   if (count (particles with [mass = 1]) > 1) [       
      ask one-of particles with [mass = 1]
         [set mass heavy set color violet set disabled disabled + 1]
      ]
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       ; 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

; Toggle the goal
to toggle-goal
  if (goal_button? != 0) [set goal_button? not goal_button?]
end