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_lj_goal_obs.nlogo
This is a modification of our third physics-based model of a swarm, for the book entitled “Physicomimetics: Physics-Based Swarm Intelligence.” This simulation uses a force law based on the Lennard-Jones potential, rather than the split Newtonian force law.
Multiple particles use F = ma and a generalized Lennard-Jones 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.
This simulation modifies “formation_goal_obs.nlogo” by using a generalized Lennard-Jones force law rather than the split Newtonian force law.
This force law is very robust - the formation acts more like a viscous fluid than a solid formation. Hence we have found that it generally performs the obstacle avoidance task extremely well (see reference below).
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. 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.
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 LENNARD_JONES_CONSTANT controls a multiplicative parameter in the Lennard-Jones force. The POWER controls the value of “p” in the generalized law. The DESIRED_SEPARATION is the desired distance between neighboring particles. For more details on this force law, see Chapter 14 of the book.
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.
See how the FRICTION changes behavior. Click SETUP AGENTS, lower the friction, and click MOVE AGENTS. What happens?
Similarly, change the LENNARD_JONES_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 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?
In “formation_hooke.nlogo” we noted that unusual behavior can occur with certain parameter settings. Try similar settings with this simulation (which uses the Lennard-Jones force law). Do you see the same behavior?
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?
Particles can still cluster when using the Lennard-Jones force law. Derive a mathematical expression that predicts when the phase transition will occur.
Note, in order to change any NetLogo simulation, you must have the source code (i.e., “formation_lj_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.
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.
This is a modification of our third physics-based swarm simulation.
For details on our first implementation of the Lennard-Jones force law:
Hettiarachchi, S. and W. M. Spears (2005) Moving swarm formations through obstacle fields. In International Conference on Artificial Intelligence, Volume 1, pp. 97-103, CSREA Press.
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 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.
; William M. Spears September 2011
; Triangular and Square Lattices and a Goal Force with Obstacles,
; Using the Lennard-Jones Force Law!
; 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? 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 lattices
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) [ ; Modified and generalized Lennard-Jones force law
set F (G * (((D ^ (2 * p)) / (r ^ ((2 * p) + 1))) -
((D ^ p) / (r ^ (p + 1)))))
if (F > FMAX) [set F FMAX] ; Bounds check on force magnitude
set Fx (Fx - F * (deltax / r)) ; x-component of force
set Fy (Fy - F * (deltay / r)) ; y-component of force
]
]
; 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 Lennard_Jones_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_Size
set goal? goal_button?
set square? square_button?
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