Vicsek Model with Predator

Implementing the Avoidance Rule

In the Couzin model, particle avoidance takes absolute priority: if an individual senses another particle within a certain radius, it immediately turns directly away, ignoring all other alignment or noise rules. We can implement a similar rule in the Vicsek model to simulate predator avoidance. The predator can be a fixed point, a random walker, or even the mouse cursor. In all cases, if a boid senses the predator within a certain radius, it will turn directly away from it.

Below is a guide on how to implement this in your Vicsek animation. You can choose to implement one or more types of predators (static, random walk, mouse-following) and compare their effects on the collective behavior of the particles.


Static Predator

The simplest case is a predator fixed at a given position. To implement this:

  • Set the predator’s position (e.g., xy_pred = np.array([L/2, L/2]) for the center).
  • In your Vicsek update function, check for boids within the predator radius and set their direction directly away from the predator (as in the Couzin rule).
  • In your animation, draw the predator as a red circle.

Where to add: - Set xy_pred before the animation loop in your main script. - Pass xy_pred to your Vicsek update at each frame. - Draw the predator in the plotting section (after initializing the axes).

  • Set xy_pred before the animation loop: In run_simulation, set xy_pred = np.array([box_size/2, box_size/2]) (or wherever you want the static predator) before entering the animation.
  • Pass xy_pred to the Vicsek update: In the update_animation function, pass xy_pred as an argument to vicsek_equations.
  • Draw the predator: After initializing the axes in run_simulation, add the predator circle with circle_predator = plt.Circle(xy_pred, radius_predator, color="red", fill=False) and ax_plane.add_artist(circle_predator).
  • Update the predator’s position in the plot (if needed): For a static predator, this is only needed once. For moving predators, update circle_predator.set_center(xy_pred) in each animation frame.

Random Walk Predator

To make the predator more dynamic, let it perform a random walk:

  • At each animation frame, update xy_pred by adding a small random displacement (e.g., xy_pred += np.random.normal(0, step_size, 2)).
  • Apply periodic boundary conditions to keep the predator in the box.
  • The avoidance rule for boids remains unchanged.

Where to add: - In your animation update function, before updating the boids, update xy_pred with a random walk step. - Pass the new xy_pred to the Vicsek update. - Update the predator’s position in the plot.


Mouse-Following Predator

For an interactive experience, let the predator follow the mouse:

  • Use a mouse event callback to update xy_pred to the mouse position whenever the mouse moves over the plot.
  • The avoidance rule for boids is the same: those within the predator radius turn directly away.

Where to add:

  • Register a mouse motion callback (e.g., with fig.canvas.mpl_connect('motion_notify_event', on_mouse_move)).
  • In the callback, update xy_pred to the mouse coordinates if the mouse is over the main plot.
  • The animation update function will use the current xy_pred as usual.

Integration

In all cases, the predator logic is integrated as follows:

  • The predator’s position (xy_pred) is set or updated before each Vicsek update.
  • The Vicsek update function receives xy_pred and applies the Couzin avoidance rule.
  • The predator is drawn as a red circle in the main plot.
  • For the mouse-following version, the mouse callback is registered during figure setup.

This modular approach allows you to experiment with different predator behaviors by simply changing how xy_pred is updated, while keeping the avoidance rule and animation structure consistent.


Assignment

Check the assignment page for the next steps. You will be implementing this model in a script named vicsek_predator.py.

Assignment: Implementing the Vicsek Model with Predator