Six differential drive robots start evenly spaced on a circle of radius 2 m, and each has to drive to the point opposite its start. Driven straight, all of them meet in the middle at the same time. This is bitrl::envs::multi_agent::MultiRobotNavigation: each robot is a disc of radius 0.15 m that moves with the kinematics of bitsim's DiffDriveDynamics, and its action is a linear and an angular velocity. A move that would make two robots overlap is cancelled, and the robot stands still for that step.
The first controller turns each robot towards its goal and drives, slower when the goal is near or when it is not facing it. The second adds what each robot sees of the others: it pushes the heading away from robots closer than 1 m, and a little to the right of them, and slows down when a robot is close ahead. The push to the right breaks the symmetry: two robots that meet head on both turn right, and pass each other instead of stopping face to face.
Straight to the goal, the six robots jam in the middle and none arrives before the time limit. Steering around each other, they form a roundabout and all arrive in 11 s, never closer than 0.55 m centre to centre, against 0.3 m at contact.
The last part runs both controllers with 2 to 12 robots, from 10 starts each, shifted at random by a few centimetres: 120 simulations. They share nothing, so parallel_map runs them at the same time on all the threads, and returns them in order:
Even two robots jam with the first controller: the small shifts of the starts are not enough to break the symmetry. The avoiding controller gets every robot through from every start, and the crossing takes longer the more robots share the middle.
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
{
{
const real_t distance = std::hypot(o[3], o[4]);
const real_t error =
wrap(std::atan2(o[4], o[3]) - o[2]);
const real_t v = std::min(max_v, distance) * std::max(0.0, std::cos(error));
const real_t w = std::clamp(3.0 * error, -max_w, max_w);
return {v, w};
}
{
const real_t distance = std::hypot(o[3], o[4]);
real_t dx = o[3] / std::max(distance, 1e-9);
real_t dy = o[4] / std::max(distance, 1e-9);
for (std::size_t j = 5; j + 3 < o.size(); j += 4)
{
const real_t rx = o[j], ry = o[j + 1];
const real_t r = std::hypot(rx, ry);
if (r > SENSE || r < 1e-9)
{
continue;
}
const real_t gap = std::max(r - 2.0 * robot_radius, 0.05);
const real_t push = (1.0 / gap - 1.0 / SENSE);
dx -= push * rx / r * 0.3;
dy -= push * ry / r * 0.3;
dx += push * ry / r * 0.3;
dy -= push * rx / r * 0.3;
const real_t ahead = (rx * std::cos(o[2]) + ry * std::sin(o[2])) / r;
if (ahead > 0.0)
{
slow = std::min(slow, std::clamp((r - 2.0 * robot_radius) / (SENSE * 0.5), 0.0, 1.0));
}
}
std::min(max_v, distance) * std::max(0.0, std::cos(error)) * std::max(slow, 0.2);
const real_t w = std::clamp(3.0 * error, -max_w, max_w);
return {v, w};
}
{
const MultiRobotNavigation &
env;
{
{
const auto &o =
step.observation(i);
}
return actions;
}
};
{
{{"n_robots", n_robots},
{"circle_radius", 2.0},
{"start_noise", start_noise},
{{"seed", seed}});
}
{
{
arrived +=
env.distance_to_goal(i) <= 0.1 ? 1 : 0;
}
std::cout <<
" " << name <<
": " << arrived <<
" of " <<
env.n_agents()
<< " robots arrived in " << std::fixed << std::setprecision(1)
<< " moves blocked by a collision, closest distance between two centres "
<< std::setprecision(2) <<
env.min_separation() <<
" m\n";
}
{
{
report(
"go to goal", env, result);
}
std::cout << "\n";
{
std::ofstream out("trajectories.csv");
out << "step,robot,x,y,theta,goal_x,goal_y,radius\n";
{
{
const auto &p =
env.poses()[i];
out << k << "," << i << "," << p.x << "," << p.y
<<
"," << p.theta <<
"," <<
env.goals()[i][0]
<<
"," <<
env.goals()[i][1] <<
","
<<
env.robot_radius() <<
"\n";
}
++k;
});
report(
"avoiding", env, result);
std::cout << " The paths are in trajectories.csv\n\n";
}
const std::vector<uint_t> sizes{2, 4, 6, 8, 10, 12};
struct Outcome
{
bool all_arrived;
};
sizes.size() * 2 * N_STARTS,
[&](std::size_t k)
{
const uint_t n = sizes[k / (2 * N_STARTS)];
const bool avoiding = (k / N_STARTS) % 2 == 1;
auto env = make_env(n, 0.05, bitsim::utils::concurrency::task_seed(7, k % N_STARTS));
Team team{env, avoiding};
});
std::cout << " robots controller all arrived mean time [s] blocked moves\n";
for (
uint_t s = 0; s < sizes.size(); ++s)
{
for (
uint_t c = 0; c < 2; ++c)
{
uint_t arrived = 0, collisions = 0;
for (
uint_t r = 0; r < N_STARTS; ++r)
{
const auto &o = outcomes[(s * 2 + c) * N_STARTS + r];
arrived += o.all_arrived ? 1 : 0;
time += o.all_arrived ? o.time : 0.0;
collisions += o.collisions;
}
std::ostringstream mean_time;
mean_time << std::fixed << std::setprecision(1);
if (arrived)
{
mean_time << time / arrived;
}
else
{
mean_time << "-";
}
std::cout << std::setw(8) << sizes[s] << std::setw(13) << (c ? "avoiding" : "to goal")
<< std::setw(11) << arrived << "/" << N_STARTS << std::setw(16)
<< mean_time.str() << std::setw(16) << std::setprecision(1)
<<
static_cast<real_t>(collisions) / N_STARTS <<
"\n";
}
}
}
}
int main(
int argc,
char **argv)
{
for (int i = 1; i + 1 < argc; i += 2)
{
if (std::string(argv[i]) == "--robots")
{
n_robots = std::strtoul(argv[i + 1], nullptr, 10);
}
}
return 0;
}
Differential drive robots that cross a circle to the opposite point, all at the same time,...
Definition multi_robot_navigation.h:55
base_type::time_step_type time_step_type
Definition multi_robot_navigation.h:58
base_type::joint_action_type joint_action_type
Definition multi_robot_navigation.h:59
int main()
Definition discounted_returns.cpp:19
Definition distributed_agent_env.h:19
Definition agent_trainer.h:14
MultiAgentEpisodeResult run_episode(EnvType &env, ActorType &actor, const std::function< void(const typename EnvType::time_step_type &)> &on_step={})
Play one episode: reset env, then let actor choose the joint actions until the episode is over.
Definition multi_agent_trainer.h:160
const real_t PI
The PI constant.
Definition bitsim_consts.h:63
auto parallel_map(std::size_t n, F &&f, ThreadPool &pool=default_thread_pool()) -> std::vector< std::invoke_result_t< F &, std::size_t > >
Compute f(0), f(1), ..., f(n - 1) on the threads of pool.
Definition parallel.h:119
void print_title(const std::string &title, std::ostream &out)
An empty line and then the title.
Definition print_utils.cpp:17
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
real_t error(const Pose &a, const Pose &b)
Definition ekf_slam.cpp:176
Definition multi_robot_navigation.cpp:31
action_type go_to_goal(const std::vector< real_t > &o, real_t max_v, real_t max_w)
Definition multi_robot_navigation.cpp:48
real_t wrap(real_t angle)
Definition multi_robot_navigation.cpp:44
MultiRobotNavigation::time_step_type time_step_type
Definition multi_robot_navigation.cpp:38
MultiRobotNavigation make_env(uint_t n_robots, real_t start_noise, uint_t seed)
Definition multi_robot_navigation.cpp:117
std::array< real_t, 2 > action_type
Definition multi_robot_navigation.cpp:40
const real_t PI
Definition multi_robot_navigation.cpp:42
void run(uint_t n_robots)
Definition multi_robot_navigation.cpp:144
void report(const std::string &name, const MultiRobotNavigation &env, const MultiAgentEpisodeResult &result)
Definition multi_robot_navigation.cpp:129
MultiRobotNavigation::joint_action_type joint_action_type
Definition multi_robot_navigation.cpp:39
action_type avoid(const std::vector< real_t > &o, real_t max_v, real_t max_w, real_t robot_radius)
Definition multi_robot_navigation.cpp:60
Vector step(const QuadrotorDynamicsConfig &config, const Vector &x, const Vector &u, real_t max_rotor_speed)
Definition quadrotor_lqr.cpp:114
Print labelled values, one per line, as the examples do: two spaces, the label padded to a width,...
What one episode of a multi-agent environment gave.
Definition multi_agent_trainer.h:48
uint_t n_steps
the joint steps of the episode
Definition multi_agent_trainer.h:50
uint_t n_collisions
the sum of the "collisions" the time steps reported
Definition multi_agent_trainer.h:51
bool all_done
whether every agent finished, rather than the step limit
Definition multi_agent_trainer.h:52
Definition multi_robot_navigation.cpp:100
const MultiRobotNavigation & env
Definition multi_robot_navigation.cpp:101
bool avoiding
Definition multi_robot_navigation.cpp:102
joint_action_type act(const time_step_type &step) const
Definition multi_robot_navigation.cpp:104