Nothing beyond bitrl for the threads of one process. For processes, bitsim built with MQTT and a broker such as mosquitto; see bitsim's install page.
The copies need the same joint actions to stay alike, so the participants run in lockstep. A participant cannot run in wall-clock mode, as a real robot would, because its copy would then move the robots with actions that came late. A real robot belongs in bitsim's distributed simulation, which sends states, not actions; see bitsim's hardware-in-the-loop example.
The second line runs every robot in a process of its own, meeting on the broker at tcp://localhost:1883 (--broker for another, --robots for another number). Every process writes the paths of all robots, as its copy saw them, to trajectories_<robot>.csv.
#include "bitsim/bitsim_config.h"
#ifdef BITSIM_MQTT
#endif
#include <algorithm>
#include <array>
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include <vector>
{
using steady = std::chrono::steady_clock;
struct Controller
{
{
const auto &o = step.observation(
robot);
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;
}
dx += 0.3 * push * (ry - rx) / r;
dy -= 0.3 * push * (ry + rx) / r;
if (rx * std::cos(o[2]) + ry * std::sin(o[2]) > 0.0)
{
slow =
std::min(slow, std::clamp((r - 2.0 *
robot_radius) / (0.5 * SENSE), 0.0, 1.0));
}
}
const real_t error = std::remainder(std::atan2(dy, dx) - o[2], 2.0 *
PI);
std::min(
max_v, distance) * std::max(0.0, std::cos(error)) * std::max(slow, 0.2);
return {v, std::clamp(3.0 * error, -
max_w,
max_w)};
}
};
{
{{"n_robots", n_robots},
{"circle_radius", 2.0},
{"start_noise", 0.05},
}
{
return {robot,
env.max_v(),
env.max_w(),
env.robot_radius()};
}
{
const MultiRobotNavigation &
env;
{
{
}
return actions;
}
};
real_t seconds(steady::duration d) {
return std::chrono::duration<real_t>(d).count(); }
typedef std::vector<std::vector<RobotPose>>
Paths;
{
MultiAgentEpisodeResult
result;
};
{
env, me, [&](
const time_step_type &) { outcome.paths.push_back(copy.poses()); });
outcome.min_separation = copy.min_separation();
return outcome;
}
{
std::cout <<
" " << (result.
all_done ?
"every robot arrived" :
"not every robot arrived")
<<
" after " << std::fixed << std::setprecision(1) << result.
n_steps *
env.dt()
<< " moves blocked; closest centres " << std::setprecision(2) << min_separation
<< " m\n";
}
{
auto start = steady::now();
const auto one =
std::cout << " in " << std::setprecision(4) << t_one << " s\n\n";
std::vector<std::unique_ptr<Transport>> transports;
for (
uint_t r = 0; r < n_robots; ++r)
{
transports.push_back(bus.connect());
}
std::vector<Outcome> outcomes(n_robots);
std::vector<std::thread> threads;
for (
uint_t r = 0; r < n_robots; ++r)
{
threads.emplace_back([&, r] { outcomes[r] =
participant(r, n_robots, *transports[r]); });
}
for (auto &t : threads)
{
t.join();
}
report(env, outcomes.front().result, outcomes.front().min_separation);
std::cout << " " << n_robots << " threads in " << std::setprecision(4) << t_distributed
<< " s, " << std::setprecision(1) << 1e6 * t_distributed / one.n_steps
<< " microseconds per step\n";
bool same = true;
for (const auto &o : outcomes)
{
same = same && o.paths.size() == paths.size();
for (std::size_t k = 0; same && k < paths.size(); ++k)
{
for (
uint_t r = 0; r < n_robots; ++r)
{
same = same && o.paths[k][r].x == paths[k][r].x &&
o.paths[k][r].y == paths[k][r].y && o.paths[k][r].theta == paths[k][r].theta;
}
}
}
std::cout << " every copy moved every robot as the one process did: " << std::boolalpha << same
<< "\n";
return same ? 0 : 1;
}
{
#ifdef BITSIM_MQTT
if (robot >= n_robots)
{
std::cerr << "--robot must be less than --robots\n";
return 1;
}
bitsim::distributed::MQTT_Transport transport(broker, "distributed_robot_navigation");
std::cout <<
" " <<
env.agent_name(robot) <<
": waiting for the others on " << broker <<
"\n";
const auto start = steady::now();
const auto outcome =
participant(robot, n_robots, transport);
report(env, outcome.result, outcome.min_separation);
std::cout << " in " << std::setprecision(2) << t << " s\n";
std::ofstream out("trajectories_" + std::to_string(robot) + ".csv");
out << std::setprecision(17) << "step,robot,x,y,theta\n";
for (std::size_t k = 0; k < outcome.paths.size(); ++k)
{
for (
uint_t r = 0; r < n_robots; ++r)
{
const auto &p = outcome.paths[k][r];
out << k << "," << r << "," << p.x << "," << p.y << "," << p.theta << "\n";
}
}
return 0;
#else
(void)robot;
(void)n_robots;
(void)broker;
std::cerr << "bitsim was built without MQTT\n";
return 1;
#endif
}
}
int main(
int argc,
char **argv)
{
std::string transport = "inprocess", broker = "tcp://localhost:1883";
for (int i = 1; i + 1 < argc; i += 2)
{
const std::string flag = argv[i], value = argv[i + 1];
if (flag == "--robots")
n_robots = std::strtoul(value.c_str(), nullptr, 10);
else if (flag == "--transport")
transport = value;
else if (flag == "--robot")
robot = std::strtoul(value.c_str(), nullptr, 10);
else if (flag == "--broker")
broker = value;
else
{
std::cerr << "unknown option " << flag << "\n";
return 1;
}
}
if (transport == "mqtt")
{
}
}
One agent of a multi-agent environment, in a process of its own.
Definition distributed_agent_env.h:151
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
Participants that are threads of one process, joined by queues.
Definition transport.h:57
How the participants of a distributed simulation reach each other: everything one publishes,...
Definition transport.h:34
int main()
Definition discounted_returns.cpp:19
Definition distributed_agent_env.h:19
Definition agent_trainer.h:14
MultiAgentEpisodeResult run_agent_episode(EnvType &env, ActorType &actor, const std::function< void(const typename EnvType::time_step_type &)> &on_step={})
Play one episode of env with the action of one agent chosen by actor.
Definition agent_trainer.h:63
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
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
CartPoleState start()
Definition cart_pole_lqr_mpc.cpp:73
Definition distributed_robot_navigation.cpp:43
std::chrono::steady_clock steady
Definition distributed_robot_navigation.cpp:51
std::array< real_t, 2 > action_type
Definition distributed_robot_navigation.cpp:54
const real_t PI
Definition distributed_robot_navigation.cpp:57
DistributedAgentEnv< MultiRobotNavigation > agent_env_type
Definition distributed_robot_navigation.cpp:55
std::vector< std::vector< RobotPose > > Paths
Definition distributed_robot_navigation.cpp:136
int run_mqtt(uint_t robot, uint_t n_robots, const std::string &broker)
Definition distributed_robot_navigation.cpp:224
real_t seconds(steady::duration d)
Definition distributed_robot_navigation.cpp:133
void report(const MultiRobotNavigation &env, const MultiAgentEpisodeResult &result, real_t min_separation)
Definition distributed_robot_navigation.cpp:159
int run_in_process(uint_t n_robots)
Definition distributed_robot_navigation.cpp:169
MultiRobotNavigation make_env(uint_t n_robots)
Definition distributed_robot_navigation.cpp:101
MultiRobotNavigation::time_step_type time_step_type
Definition distributed_robot_navigation.cpp:53
Outcome participant(uint_t robot, uint_t n_robots, Transport &transport)
Definition distributed_robot_navigation.cpp:146
Controller controller(const MultiRobotNavigation &env, uint_t robot)
Definition distributed_robot_navigation.cpp:113
real_t error(const Pose &a, const Pose &b)
Definition ekf_slam.cpp:176
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 distributed_robot_navigation.cpp:64
uint_t robot
Definition distributed_robot_navigation.cpp:65
real_t max_w
Definition distributed_robot_navigation.cpp:66
action_type act(const time_step_type &step) const
Definition distributed_robot_navigation.cpp:68
real_t max_v
Definition distributed_robot_navigation.cpp:66
real_t robot_radius
Definition distributed_robot_navigation.cpp:66
Definition distributed_robot_navigation.cpp:140
MultiAgentEpisodeResult result
Definition distributed_robot_navigation.cpp:141
Paths paths
Definition distributed_robot_navigation.cpp:142
real_t min_separation
Definition distributed_robot_navigation.cpp:143
Definition distributed_robot_navigation.cpp:120
const MultiRobotNavigation & env
Definition distributed_robot_navigation.cpp:121
MultiRobotNavigation::joint_action_type act(const time_step_type &step) const
Definition distributed_robot_navigation.cpp:122