390 likes | 615 Views
Swarms and Crowds. Kristen Gardner. Outline. Swarm Intelligence Flocking Basic Steering Swarms Applications Animals Vehicles People. Swarm Intelligence. What is it? Artificial intelligence that is based on collective behavior of decentralized, self organized systems Examples?
E N D
Swarms and Crowds Kristen Gardner
Outline • Swarm Intelligence • Flocking • Basic • Steering • Swarms • Applications • Animals • Vehicles • People
Swarm Intelligence • What is it? • Artificial intelligence that is based on collective behavior of decentralized, self organized systems • Examples? • Flocks of birds, schools of fish (Boids) • Ant colony • Bees
What is Flocking? • An example of emergent behavior which resembles the motion of a flock of birds, school of fish, or swarm of insects • First simulated on a computer in 1986 by Craig Reynolds’ program Boids • Considered emergent because unexpected behavior may occur when flocks split up and reunite to avoid obstacles
How Does Flocking Work? • The behavior of each Boid is individual, but conforms to a few, relatively simple, rules • Separation • Alignment • Cohesion • Other, more complex rules may also be added • Obstacle avoidance • Goal seeking
Basic Rules • separation: steer to avoid crowding local flock mates • alignment: steer towards the average heading of local flock mates • cohesion: steer to move toward the average position of local flock mates
Field of View • To keep things “realistic,” respond only to the actions of neighboring Boids • Example • Fancy Example
Algorithmic Complexity • Basic un-optimized implementation is O(n2) • Each Boid must calculate its distance from each other Boid • Basic optimized implementation is O(kn) • Area the flock inhabits is divided into a grid with each Boid in a “bin” • Each Boid only needs to check its position relative to Boids in its (or neighboring) bins
Using Steering • Model mass (scalar) position (vector) velocity (vector) max_force (scalar) max_speed (scalar) orientation (N basis vectors) • Euler Integration for animation steering_force = truncate (steering_direction, max_force) acceleration = steering_force / mass velocity = truncate (velocity + acceleration, max_speed) position = position + velocity
Steering Behaviors • Seek and Flee • Pursue and Evade • Wander • Arrival • Wall Following • Path Following • Flow Field Following
Seek and Flee • Go toward or away from a static target • Seek: desired_velocity = normalize (position - target) * max_speed steering = desired_velocity – velocity • Flee: • Inverse of seek
Pursuit and Evasion • Similar to Seek and Flee, but estimates the quarry’s position in the future • Based on the distance to the quarry, estimate the number of time steps (the predictor interval T) until the Boid could reach the quarry • Base T on the distance between objects: T=Dc • Based on the quarry’s current velocity, position, and the calculated predictor interval, estimate the target • Use seek or flee algorithm to determine steering
Wander • Multiple ways • Random steering force at each frame • Easy to implement • May cause “twitching” behavior • Retain steering direction, but make small displacement • Constrain steering to a sphere (or circle in 2D) in front of the Boid • Size of sphere determines how quickly heading can change
Arrival • Similar to seek, but slows when approaching target target_offset = target - position distance = length (target_offset) ramped_speed = max_speed * (distance / slowing_distance) clipped_speed = minimum (ramped_speed, max_speed) desired_velocity = (clipped_speed / distance) * target_offset steering = desired_velocity - velocity
Path Following • Boid steers to follow a predetermined path • Velocity-based prediction of future position • Future position projected to nearest point on path • If distance from path is more than path radius, steer toward path
Variation of Path Finding • Wall Following • Approach a wall and maintain a certain distance from it • Generate a new goal position and steer toward it • Containment • Use future position to determine if Boid will remain inside area • If not steer back toward the center of area
Flow Field Following • Steer to align with flow vectors steering = flow_direction-velocity
Obstacle Avoidance • Multiple approaches can be used • Force Field model • Steer-to-avoid
Types of Collisions • Obstacles • Stationary • Incoming • Outgoing • Sideswiping • Response • Circumvention • Queuing
Force Fields • The equation: • ki is a scaling constant • di is the distance from the agent to the obstacle • ei controls the magnitude of acceleration based on distance • ki and ei should be negative for avoidance, positive values can be used for attraction • Advantages: • Easy to model • Disadvantages: • Field is exactly opposite, so no turning away from straight on • Too weak far away, so long term planning may be required
Steer-to-Avoid • Algorithm: • Calculates silhouette for each obstacle • Tests collision with current heading • If there will be a collision, the Boid will aim for a point one body length beyond the silhouette • Advantages: • Only affected by obstacles directly in front of it
Combined Behaviors • Crowd Path Following • path following and separation • Leader Following • Leader: wandering, seek random position • Followers: arrival, separation, follow path set by leader • Unaligned Collision Avoidance • Containment, predictive obstacle avoidance • Queuing • Seek, avoid, separation,
And the result is… • So, little circles running around are great, but really, what can flocking do? • The Lion King
Simple Swarms • An alternative to flocking • Due to complexity, flocking calculations may be prohibitive in real-time applications • Does not attempt to avoid collisions between swarm members • Use with large number of small creatures • Spiders, rats, etc.
Swarms • Introducing the swarm • Drop from the ceiling, “explode” from a container • Each agent has a separate timer, set to a random value, so the agents enter the scene gradually • Algorithm • Small changes in movement and direction should keep the agents separate • “Outer zone” • Increment agent’s speed if it is less than the maximum (should vary from agent to agent) • If the agent’s heading is not roughly in the right direction (within some tolerance), rotate back to the correct heading • “Inner zone” • Behavior varies, depending on the overall desired movement of the group
Modified swarm • Can add simple collision tests, simple path following, and some other behaviors while still maintaining O(n) complexity • Also can be modified for agents who fly or walk on walls
Applications • Why do we care about any of this? • Animals: flocks, schools, herds • People: crowds • Vehicles: traffic • When to flock, when to swarm? • Flocking • Small to medium number of creatures • Use when behavior of creatures is major focus of game play • Pikmin • Swarms • Large groups (tens or hundreds of agents)
Animals • Ambient • Enhance the environment • Give more life-like feel • Not relevant to game play • Secondary • Limited amount of interaction with the player • Part of game play, but not the focus • Should behave intelligently, but also limited so as not to distract from goals • Primary • Are focus of the game and have direct relation to primary objectives
Understand Behavior - Groups • Flocks • Stay together • Don’t crowd each other • Follow the leader • Switch leaders occasionally • Individuals break away sometimes, but don’t go too far • Schools • Very similar, but tend to change directions more often • High cohesion and alignment, low separation
Understand Behavior – Solitary • Does not mean antisocial, just not governed by the behavior of others • Examples – chipmunks, eagles, etc. • Stay in a limited area • Efforts mainly directed toward feeding • May or may not be comfortable with human presence • Maintain “safe” distance, but only depart if pursued • Low alignment and low to moderate cohesion
Ambient animals • Bugs - Example behavior for butterflies • Don’t fly in straight line • Don’t always flap their wings • Don’t flap wings in full beat • Attracted to flowers and bright colors • Avoid moving objects • Stay in limited area • Easily governed by simple rules
Primary and Secondary • Tend to follow a schedule throughout the day • Frequent a fixed number of places • Have more than one active sensing mechanism, some more keen than others • Senses are affected by factors in the environment • Avoid human contact, but may be dangerous when cornered • Typically predictable, but should also demonstrate unexpected behavior occasionally • Require more complex individual behavior than ambient animals
Traffic • Must obey traffic rules • Stop signs and signals, speed limits, drive on correct side of street • Can be implemented with messages when the vehicle approaches specific objects • Set precedence • A stop sign or pedestrian is more important than maintaining a certain speed • Intersections – ticket system • In order to go through, must be in possession of ticket • After passing through, vehicle relinquishes control of the ticket
People • “Milling” • Low max speed, planning for collision avoidance, small changes in heading, follow social/safety conventions (stay on sidewalk) • Pedestrians on sidewalk, small groups standing around • Organized behavior • Pay more attention to other agents • Queuing, military formations, “doing the wave,” waiting at cross-walks, etc. • Traumatic behavior • High maximum speed, large changes in heading, ignore conventions (running into traffic) • Run away from the crazy person with a big gun
Rome: Total War • Strategy game – both turn-based and real time • Game engine • Has the ability to render 30,000 units on a single battle field • Used by Decisive Battles (History Channel) to recreate historical battles • Battle of Cannae • Rome: Total War
Summary • Flocking • Emergent behavior that imitates a flock, school, or herd • Computationally expensive for large groups • Swarms • Use when flocking too expensive (hundreds of agents) • Organic motion, but flock mates may collide • Applications • Video games, movies/TV, simulations • Animals, vehicles, people • Understand what you want to model before you model it
Thank you! Questions or Comments?