Getting Started

Depending on your knowledge of Python and swarm behaviors, you can take two approaches to using PySwarming:

  1. Simple and low-code simulations: start using the PySwarming.Swarm class, which allows the creation of virtual swarms to be used with some behaviors functions.

  2. Custom animated simulations: here you are able to use all the available behaviors, however, you will need to code a little bit more, i.e., the creation of the swarm, the animation, and so on.

Below you have examples of each approach mentioned above. For more examples, you can see the notebooks and examples directories of PySwarming.

(Approach 1) Low-code: PySwarming.Swarm

Non-Beginners: If you’re already familiar with Python and swarm behaviors, you can see a more customized example here.

Simulating repulsion behavior among the swarm

The idea of this simulation is to use the low-code tool from PySwarming, which enables us to carry out simplified animations with some behaviors.

Firstly, we need to set some parameters to create our swarm, such as the number of robots, the linear speed of the robots, and so on. After that, we simply call simulate().

For details, see the code below.

[1]:
%matplotlib inline

# importing the swarm creator
import pyswarming.swarm as ps

# creating the swarm
my_swarm = ps.Swarm(n = 10, # number of robots
                    linear_speed = 0.5, # linear speed of each robot
                    dT = 1.0, # sampling time
                    deployment_point_limits = [[0.0, 0.0, 0.0], [5.0, 5.0, 0.0]], # lower and upper limits for the position deployment
                    deployment_orientation_limits = [[0.0, 0.0, 0.0], [0.0, 0.0, 2*3.1415]], # lower and upper limits for the orientation deployment
                    distribution_type =  'uniform', # type of distribution used to deploy the robots
                    plot_limits = [[-50.0, 50.0], [-50.0, 50.0]], # plot limits x_lim, y_lim
                    behaviors = ['repulsion']) # list of behaviors
anim = my_swarm.simulate(mode='anim')

from IPython.display import HTML
HTML(anim.to_jshtml())
[1]:
../_images/example_notebooks_01_Getting_Started_1_1.png

(Approach 2) Custom Animations

Simulating repulsion behavior among the swarm

The idea of this simulation is to use a more complicated setup with a little more knowledge of Python, Robotics, and PySwarming. In this setup, we need to define the initial pose of the robots, their linear speed, the animation functions, and so on. This enables us to carry out even more customized animations/simulations of the swarm.

For details, see the code below.

[2]:
%matplotlib inline

# importing pyswarming behaviors
import pyswarming.behaviors as ps

# importing numpy to work with arrays
import numpy as np

# importing matplotlib to plot the animation
import matplotlib.pyplot as plt
import matplotlib.animation as animation

# importing functools.partial to use in the animation
from functools import partial

# define each robot (x, y, z) position
robot_poses = np.asarray([[1., 1., 0.],
                          [-1., 1., 0.],
                          [1., -1., 0.],
                          [-1., -1., 0.]])

# set the robot linear velocity
robot_speed = 0.025

# First set up the figure, the axis, and the plot element we want to animate
fig, ax = plt.subplots()

ax.set_xlim([-10,10])
ax.set_ylim([-10,10])
ax.set_xlabel('X(m)')
ax.set_ylabel('Y(m)')
ax.grid()
ax.set_aspect('equal')
ax.set_title('Repulsion behavior')

robot1, = ax.plot([], [], marker='o', lw=0)
robot2, = ax.plot([], [], marker='o', lw=0)
robot3, = ax.plot([], [], marker='o', lw=0)
robot4, = ax.plot([], [], marker='o', lw=0)

# initialization function: plot the background of each frame
def init():
    robot1.set_data([], [])
    robot2.set_data([], [])
    robot3.set_data([], [])
    robot4.set_data([], [])
    return (robot1,robot2,robot3,robot4,)

# animation function. This is called sequentially
def animate(i, robot_poses):
    for r_ind in range(len(robot_poses)):
        r_i = robot_poses[r_ind]
        r_j = np.delete(robot_poses, np.array([r_ind]), axis=0)
        robot_poses[r_ind] += robot_speed*ps.repulsion(r_i, r_j, 5.0)
    robot1.set_data(robot_poses[0][0], robot_poses[0][1])
    robot2.set_data(robot_poses[1][0], robot_poses[1][1])
    robot3.set_data(robot_poses[2][0], robot_poses[2][1])
    robot4.set_data(robot_poses[3][0], robot_poses[3][1])
    return (robot1,robot2,robot3,robot4,)

# call the animator. blit=True means only re-draw the parts that
# have changed.
anim = animation.FuncAnimation(fig, partial(animate, robot_poses=robot_poses), init_func=init,
                               frames=480, interval=1, blit=True)

anim

from IPython.display import HTML
HTML(anim.to_jshtml())
[2]:
../_images/example_notebooks_01_Getting_Started_3_1.png

Again, for more examples, you can see the notebooks and examples directories of PySwarming.