Baseline Flop Count Update, New Float Baseline

This commit is contained in:
[Quy Anh] «Elliot» Nguyen
2026-05-23 20:50:59 +02:00
committed by Ahmed Kallala
parent 40f4421d60
commit 43e54871df
18 changed files with 241860 additions and 68 deletions
+23 -13
View File
@@ -35,9 +35,21 @@ using namespace std;
// forces.cpp
// Flop count: 2 ADDs, 5 MULTs, 1 SQRT
double w(Vector2d e, Vector2d f) {
if (e.dot(f) >= f.norm() * cos_phi)
// Flop count: 2 ADDs, 6 MULTs
double w_ped(Vector2d e, Vector2d f) {
double f_norm_sq = f.dot(f);
double rhs = f_norm_sq * cos_phi_sq;
double e_dot_f = e.dot(f);
double e_dot_f_sq = e_dot_f * e_dot_f;
if (e_dot_f >= 0.0 || e_dot_f_sq <= rhs)
return dt_neg;
else
return c_neg_times_dt;
}
double w_obstacle(Vector2d e, Vector2d f) {
double e_dot_f = (f.y == 0) ? 0 : (f.y < 0 ? -e.y : e.y);
if (e_dot_f >= cos_phi)
return dt_neg;
else
return c_neg_times_dt;
@@ -57,7 +69,7 @@ Vector2d acceleration_term_dt(const Pedestrian &alpha) {
return f_alpha_0_dt;
}
// Flop count: 12 ADDs, 23 MULTs, 1 DIV, 4 SQRTs, 1 EXP
// Flop count: 12 ADDs, 24 MULTs, 1 DIV, 3 SQRTs, 1 EXP
Vector2d pedestrian_repulsion_dt(const Pedestrian &alpha, const Pedestrian &beta) {
// # 2 ADDs
@@ -129,16 +141,16 @@ Vector2d pedestrian_repulsion_dt(const Pedestrian &alpha, const Pedestrian &beta
//
Vector2d f_alpha_beta_neg = r_ab * scalar_mult_r_ab + r_ab2 * scalar_mult_r_ab2;
// # 2 ADDs, 7 MULTs, 1 SQRT
// # 2 ADDs, 8 MULTs
//
// $\vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha \beta}).dt = w(\vec{e}_{\alpha}, -\vec{f}_{\alpha\beta}(\vec{r}_{\alpha \beta})).\vec{f}_{\alpha\beta}(\vec{r}_{\alpha \beta}).dt$
//
Vector2d f_alpha_beta_dt = f_alpha_beta_neg * w(alpha.e, f_alpha_beta_neg);
Vector2d f_alpha_beta_dt = f_alpha_beta_neg * w_ped(alpha.e, f_alpha_beta_neg);
return f_alpha_beta_dt;
}
// Flop count: 10 ADDs, 18 MULTs, 1 DIVs, 2 SQRTs, 1 EXP
// Flop count: 8 ADDs, 13 MULTs, 1 DIV, 1 SQRT, 1 EXP
Vector2d obstacle_repulsion_dt(const Pedestrian &alpha, const Obstacle &B) {
// # 7 ADDs, 5 MULTs
@@ -162,11 +174,11 @@ Vector2d obstacle_repulsion_dt(const Pedestrian &alpha, const Obstacle &B) {
//
Vector2d f_alpha_B_neg = r_aB * scalar_mult;
// # 2 ADDs, 7 MULTs, 1 SQRT
// # 2 MULTs
//
// $\vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha B}).dt = w(\vec{e}_{\alpha}, -\vec{F}_{\alpha B}(\vec{r}_{\alpha B})).\vec{F}_{\alpha B}(\vec{r}_{\alpha B}).dt$
//
Vector2d f_alpha_B_dt = f_alpha_B_neg * w(alpha.e, f_alpha_B_neg);
Vector2d f_alpha_B_dt = f_alpha_B_neg * w_obstacle(alpha.e, f_alpha_B_neg);
return f_alpha_B_dt;
}
@@ -280,8 +292,7 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
//
Vector2d f_alpha_dt = acceleration_term_dt(p);
// # (N-1) * (12 ADDs, 23 MULTs, 1 DIV, 4 SQRTs, 1 EXP)
// + (N-1) * 2 ADDs
// # (N-1) * (14 ADDs, 24 MULTs, 1 DIV, 3 SQRTs, 1 EXP)
//
// $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha},\vec{r}_{\alpha} - \vec{r}_{\beta})$
//
@@ -292,8 +303,7 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
f_alpha_dt += pedestrian_repulsion_dt(p, pedestrians[j]);
}
// # board.n_obstacles * (10 ADDs, 18 MULTs, 1 DIV, 2 SQRTs, 1 EXP)
// + board.n_obstacles * 2 ADDs
// # board.n_obstacles * (10 ADDs, 13 MULTs, 1 DIV, 1 SQRT, 1 EXP)
//
// $\sum_{B} \vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{B})$
//
+409
View File
@@ -0,0 +1,409 @@
// This file is merged from the forces.cpp and social_forces_baseline.cpp
// The goal is that we from now on only have one file as our baseline
// This should make it more easy in the future to implement optimizations
// Our naming convention from now on is: variant_<variant_name>.cpp (Please make sure to keep that consistent, otherwise our Makefile wont work anymore)
#include "../headers/board_float.hpp"
#include "../headers/constants_float.hpp"
#include "../headers/pedestrian_float.hpp"
#include "../headers/vector2d_float.hpp"
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <math.h> // keep — forces code uses unqualified sqrt/exp
#include <random>
#include <tuple>
#include <vector>
#define SEED 42
#define BOARD_WIDTH 10.0f
#define BOARD_LENGTH 50.0f
#define WALL_EPS 1e-3f
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999f
#ifndef RANDOM_SEED
#define RANDOM_SEED 0
#endif
#ifndef BENCHMARK
#define BENCHMARK 0
#endif
using namespace std;
// forces.cpp
// Flop count: 2 ADDs, 6 MULTs
float w_ped(Vector2d e, Vector2d f) {
float f_norm_sq = f.dot(f);
float rhs = f_norm_sq * cos_phi_sq;
float e_dot_f = e.dot(f);
float e_dot_f_sq = e_dot_f * e_dot_f;
if (e_dot_f >= 0.0 || e_dot_f_sq <= rhs)
return dt_neg;
else
return c_neg_times_dt;
}
float w_obstacle(Vector2d e, Vector2d f) {
float e_dot_f = (f.y == 0) ? 0 : (f.y < 0 ? -e.y : e.y);
if (e_dot_f >= cos_phi)
return dt_neg;
else
return c_neg_times_dt;
}
// Flop count: 2 ADDs, 4 MULTs
Vector2d acceleration_term_dt(const Pedestrian &alpha) {
// # 2 MULTs
Vector2d desired_velocity = alpha.e * alpha.desired_v0;
Vector2d actual_velocity = alpha.curr_vel;
// # 2 ADDs, 2 MULTs
Vector2d f_alpha_0_dt = (desired_velocity - actual_velocity) * dt_times_tau_alpha_inv;
return f_alpha_0_dt;
}
// Flop count: 12 ADDs, 24 MULTs, 1 DIV, 3 SQRTs, 1 EXP
Vector2d pedestrian_repulsion_dt(const Pedestrian &alpha, const Pedestrian &beta) {
// # 2 ADDs
// $\vec{r}_{\alpha\beta}$
Vector2d r_ab = alpha.curr_pos - beta.curr_pos;
// # 1 ADD, 2 MULTs, 1 SQRT
// $\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert$
float r_ab_norm = r_ab.norm();
r_ab_norm = r_ab_norm > EPS ? r_ab_norm : EPS; // Avoid division by zero
// $v_\beta \Delta t \vec{e}_\beta$
Vector2d s_b_e = beta.s_b_e;
// # 2 ADDs
// $\vec{r}_{\alpha\beta} - \vec{v}_{\beta}\varDelta_{t}\vec{e}_{\beta}$
Vector2d r_ab2 = r_ab - s_b_e;
// # 1 ADD, 2 MULTs, 1 SQRT
// $\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert$
float r_ab2_norm = r_ab2.norm();
r_ab2_norm = r_ab2_norm > EPS ? r_ab2_norm : EPS; // Avoid division by zero
// # 1 ADD
// $\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert$
float r_ab_norm_sum = r_ab_norm + r_ab2_norm;
// # 1 ADD, 1 MULTs, 1 SQRT
//
// $2b = \sqrt{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert)^2 - (v_\beta \Delta t)^2}$
//
float two_b_sq = r_ab_norm_sum * r_ab_norm_sum - beta.s_b_sq;
two_b_sq = two_b_sq > EPS_SQ ? two_b_sq : EPS_SQ; // Avoid sqrt of negative and avoid division by zero
float two_b = sqrt(two_b_sq);
// # 5 MULTs, 1 DIV, 1 EXP
//
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}$
//
float scalar_mult_common = (r_ab_norm_sum / (two_b * r_ab_norm * r_ab2_norm)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b * NEG_TWO_SIGMA_INV));
// # 1 MULT
//
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}$
//
float scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
// # 1 MULT
//
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}$
//
float scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
// # 2 ADDs, 4 MULTs
//
// $\vec{-f}_{\alpha \beta}(\vec{r}_{\alpha \beta}) = \frac{-V^0_{\alpha \beta}}{2\sigma}e^{-b / \sigma} 2\grad_{\vec{r}_{\alpha \beta}}b $
//
//
// $= \frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}\,\vec{r}_{\alpha \beta} + \frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} -
// v_\beta \Delta t \vec{e}_\beta \Arrowvert}\,(\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta)$
//
//
//
// $2\grad_{\vec{r}_{\alpha \beta}}b = \frac{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert)(\frac{\vec{r}_{\alpha \beta}}{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert} + \frac{\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta}{\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert})}{2b}$
//
//
// $= \frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}\,\vec{r}_{\alpha \beta} + \frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}\,(\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta)$
//
//
Vector2d f_alpha_beta_neg = r_ab * scalar_mult_r_ab + r_ab2 * scalar_mult_r_ab2;
// # 2 ADDs, 8 MULTs
//
// $\vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha \beta}).dt = w(\vec{e}_{\alpha}, -\vec{f}_{\alpha\beta}(\vec{r}_{\alpha \beta})).\vec{f}_{\alpha\beta}(\vec{r}_{\alpha \beta}).dt$
//
Vector2d f_alpha_beta_dt = f_alpha_beta_neg * w_ped(alpha.e, f_alpha_beta_neg);
return f_alpha_beta_dt;
}
// Flop count: 8 ADDs, 13 MULTs, 1 DIV, 1 SQRT, 1 EXP
Vector2d obstacle_repulsion_dt(const Pedestrian &alpha, const Obstacle &B) {
// # 7 ADDs, 5 MULTs
// $\vec{r}_{\alpha B}$
Vector2d r_aB = alpha.curr_pos - B.find_nearest_obstacle_point(alpha.curr_pos);
// # 1 ADD, 2 MULTs, 1 SQRT
// $\Arrowvert \vec{r}_{\alpha B} \Arrowvert$
float r_aB_norm = r_aB.norm();
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS; // Avoid division by zero
// # 2 MULTs, 1 DIV, 1 EXP
//
// $\frac{-U^0_{\alpha B}}{R} e^{- \Arrowvert \vec{r}_{\alpha B} \Arrowvert / R}$
//
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
// # 2 MULTs
//
// $-\vec{F}_{\alpha B}(\vec{r}_{\alpha B}) = \frac{-U^0_{\alpha B}}{R} e^{- \Arrowvert \vec{r}_{\alpha B} \Arrowvert / R} \frac{\vec{r}_{\alpha B}}{\Arrowvert \vec{r}_{\alpha B} \Arrowvert}$
//
Vector2d f_alpha_B_neg = r_aB * scalar_mult;
// # 2 MULTs
//
// $\vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha B}).dt = w(\vec{e}_{\alpha}, -\vec{F}_{\alpha B}(\vec{r}_{\alpha B})).\vec{F}_{\alpha B}(\vec{r}_{\alpha B}).dt$
//
Vector2d f_alpha_B_dt = f_alpha_B_neg * w_obstacle(alpha.e, f_alpha_B_neg);
return f_alpha_B_dt;
}
// social_force_baseline.cpp
Pedestrian *init_pedestrians(int n_p) {
#if RANDOM_SEED
random_device rd; // Platform-provided random seed source.
mt19937 gen(rd()); // Non-reproducible initialization.
#else
mt19937 gen(SEED); // Reproducible initialization.
#endif
normal_distribution<float> velocity_gen(v0_mean, v0_std_dev);
uniform_real_distribution<float> y_gen(0.3, BOARD_WIDTH - 0.3);
uniform_real_distribution<float> x_gen_left(2.0, BOARD_LENGTH / 2.0);
uniform_real_distribution<float> x_gen_right(BOARD_LENGTH / 2.0, BOARD_LENGTH - 2.0);
Pedestrian *P = new Pedestrian[n_p];
int n_p_half = n_p / 2;
// Group of pedestrians walking from left to right.
for (int i = 0; i < n_p_half; i++) {
float x = x_gen_left(gen);
float y = y_gen(gen);
Vector2d start_pos = Vector2d(x, y);
Vector2d destination = Vector2d(BOARD_LENGTH + 30.0, y);
Vector2d init_e = destination - start_pos;
float init_e_norm = init_e.norm();
Vector2d init_e_unit = init_e / init_e_norm;
// Reject near-zero desired speeds to avoid pedestrians that barely move,
// and reject negative speeds because they would reverse the intended
// walking direction.
float desired_v0;
do {
desired_v0 = velocity_gen(gen);
} while (desired_v0 <= 0.1);
float v_max = 1.3 * desired_v0;
P[i] = Pedestrian(WHITE, Vector2d(), start_pos, init_e_unit, v_max, desired_v0, destination);
}
// Group of pedestrians walking from right to left.
for (int i = n_p_half; i < n_p; i++) {
float x = x_gen_right(gen);
float y = y_gen(gen);
Vector2d start_pos = Vector2d(x, y);
Vector2d destination = Vector2d(-30.0, y);
Vector2d init_e = destination - start_pos;
float init_e_norm = init_e.norm();
Vector2d init_e_unit = init_e / init_e_norm;
// Reject near-zero desired speeds to avoid pedestrians that barely move,
// and reject negative speeds because they would reverse the intended
// walking direction.
float desired_v0;
do {
desired_v0 = velocity_gen(gen);
} while (desired_v0 <= 0.1);
float v_max = 1.3 * desired_v0;
P[i] = Pedestrian(BLACK, Vector2d(), start_pos, init_e_unit, v_max, desired_v0, destination);
}
return P;
}
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, const char *dump_path) {
#if BENCHMARK
// Avoid compiler unused parameter warning
(void)dump_path;
#endif
#if !BENCHMARK
const int dump_interval = 1;
// Each sample stores: time step, pedestrian id, x, y, e.x, e.y, color.
vector<tuple<int, int, float, float, float, float, int>> samples;
if (dump_path) {
samples.reserve(static_cast<size_t>(n_iter / dump_interval + 1) * n_p);
// Store the initial state before the first simulation step.
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
samples.emplace_back(0, i, p.curr_pos.x, p.curr_pos.y, p.e.x, p.e.y, p.color);
}
}
#endif
for (int t = 0; t < n_iter; t++) {
// Pass 1: Compute all forces from the current state.
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
// # 2 ADDs, 4 MULTs
//
// $\vec{F}_\alpha(\vec{v}_\alpha, v^0_\alpha \vec{e}_\alpha) = \frac{1}{\tau_\alpha}(v^0_\alpha \vec{e}_\alpha - \vec{v}_\alpha)$
//
Vector2d f_alpha_dt = acceleration_term_dt(p);
// # (N-1) * (14 ADDs, 24 MULTs, 1 DIV, 3 SQRTs, 1 EXP)
//
// $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha},\vec{r}_{\alpha} - \vec{r}_{\beta})$
//
for (int j = 0; j < n_p; j++) {
if (i == j)
continue;
f_alpha_dt += pedestrian_repulsion_dt(p, pedestrians[j]);
}
// # board.n_obstacles * (10 ADDs, 13 MULTs, 1 DIV, 1 SQRT, 1 EXP)
//
// $\sum_{B} \vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{B})$
//
for (int k = 0; k < board.n_obstacles; k++) {
f_alpha_dt += obstacle_repulsion_dt(p, board.obstacles[k]);
}
// # 2 ADDs
//
//
// $\frac{d\vec{w}_\alpha}{dt} = \vec{F}_{\alpha}(t)$
//
p.w += f_alpha_dt;
}
// Pass 2: Update pedestrian states after all forces have been computed.
// # (7 ADDs, 16 MULTs, 2 DIVs, 3 SQRTs) * n_p
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
// # 1 ADD, 2 MULTs, 1 SQRT
float w_norm = p.w.norm();
float w_norm2 = w_norm > EPS ? w_norm : EPS; // Avoid division by zero.
// # 1 DIV
float correction_factor = std::min(1.0f, p.v_max / w_norm2);
// # 2 MULTs
p.curr_vel = p.w * correction_factor;
// # 2 ADDs, 2 MULTs
//
// $\vec{r}_i^{t+1} = \vec{r}_i^t + \vec{v}_i^t dt$
//
p.curr_pos = p.curr_pos + p.curr_vel * dt;
// When a pedestrian reaches the end of the corridor, teleport them back
// to the opposite entrance while keeping the same y-coordinate. This
// maintains a constant pedestrian flow and makes lane formation easier to
// observe.
if (p.color == WHITE && p.curr_pos.x > (BOARD_LENGTH - 1.5)) {
p.curr_pos.x = 1.0;
p.destination.y = p.curr_pos.y;
p.w = Vector2d(p.desired_v0, 0.0);
p.curr_vel = p.w;
}
if (p.color == BLACK && p.curr_pos.x < 1.5) {
p.curr_pos.x = BOARD_LENGTH - 1.0;
p.destination.y = p.curr_pos.y;
p.w = Vector2d(-p.desired_v0, 0.0);
p.curr_vel = p.w;
}
// Keep pedestrians inside the board boundaries.
p.curr_pos.y = clamp(p.curr_pos.y, WALL_EPS, BOARD_WIDTH_MINUS_WALL_EPS);
// # 2 ADDs
Vector2d e = p.destination - p.curr_pos;
// # 1 ADD, 2 MULTs, 1 DIV, 1 SQRT
float e_norm = e.norm();
float e_norm2 = e_norm > EPS ? e_norm : EPS; // Avoid division by zero.
float e_norm_inv = 1.0 / e_norm2;
// # 2 MULTs
p.e = e * e_norm_inv;
// # 1 ADD, 2 MULTs
float velocity_norm_sq = p.curr_vel.dot(p.curr_vel);
// # 1 SQRT
float velocity_norm = std::sqrt(velocity_norm_sq);
// # 1 MULT
p.s_b_sq = velocity_norm_sq * delta_t_sq;
// # 3 MULTs
p.s_b_e = p.e * (velocity_norm * delta_t);
#if !BENCHMARK
// Store the current pedestrian state for visualization.
if (dump_path && (t % dump_interval == 0)) {
samples.emplace_back(t + 1, i, p.curr_pos.x, p.curr_pos.y, p.e.x, p.e.y, p.color);
}
#endif
}
}
#if !BENCHMARK
// Export recorded simulation states to CSV.
if (dump_path) {
ofstream out(dump_path);
out << "time,id,x,y,p.e.x,p.e.y,color\n";
for (auto &[t, id, x, y, e_x, e_y, c] : samples) {
out << t << ',' << id << ',' << x << ',' << y << ',' << e_x << ',' << e_y << ',' << c << '\n';
}
}
#endif
return 0;
}
+296
View File
@@ -0,0 +1,296 @@
// # Changes: Replaced «double» with «float» in variable declarations
// # Predecessor: variant_float_baseline.cpp
#include "../headers/board_reduced_float.hpp"
#include "../headers/constants_float.hpp"
#include "../headers/pedestrian_reduced_float.hpp"
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <math.h>
#include <random>
#include <tuple>
#include <vector>
#define SEED 42
#define BOARD_WIDTH 10.0f
#define BOARD_LENGTH 50.0f
#define WALL_EPS 1e-3f
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999f
#ifndef RANDOM_SEED
#define RANDOM_SEED 0
#endif
#ifndef BENCHMARK
#define BENCHMARK 0
#endif
using namespace std;
Pedestrian *init_pedestrians(int n_p) {
#if RANDOM_SEED
random_device rd;
mt19937 gen(rd());
#else
mt19937 gen(SEED);
#endif
normal_distribution<float> velocity_gen(v0_mean, v0_std_dev);
uniform_real_distribution<float> y_gen(0.3, BOARD_WIDTH - 0.3);
uniform_real_distribution<float> x_gen_left(2.0, BOARD_LENGTH / 2.0);
uniform_real_distribution<float> x_gen_right(BOARD_LENGTH / 2.0, BOARD_LENGTH - 2.0);
Pedestrian *P = new Pedestrian[n_p];
int n_p_half = n_p / 2;
for (int i = 0; i < n_p_half; i++) {
float x = x_gen_left(gen);
float y = y_gen(gen);
float start_pos_x = x;
float start_pos_y = y;
float destination_x = BOARD_LENGTH + 30.0;
float destination_y = y;
float init_e_x = destination_x - start_pos_x;
float init_e_y = destination_y - start_pos_y;
float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
float init_e_unit_x = init_e_x / init_e_norm;
float init_e_unit_y = init_e_y / init_e_norm;
float desired_v0;
do {
desired_v0 = velocity_gen(gen);
} while (desired_v0 <= 0.1);
float v_max = 1.3 * desired_v0;
P[i] = Pedestrian(0, 0, 0, start_pos_x, start_pos_y, init_e_unit_x, init_e_unit_y, v_max, desired_v0, destination_x, destination_y);
}
for (int i = n_p_half; i < n_p; i++) {
float x = x_gen_right(gen);
float y = y_gen(gen);
float start_pos_x = x;
float start_pos_y = y;
float destination_x = -30;
float destination_y = y;
float init_e_x = destination_x - start_pos_x;
float init_e_y = destination_y - start_pos_y;
float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
float init_e_unit_x = init_e_x / init_e_norm;
float init_e_unit_y = init_e_y / init_e_norm;
float desired_v0;
do {
desired_v0 = velocity_gen(gen);
} while (desired_v0 <= 0.1);
float v_max = 1.3 * desired_v0;
P[i] = Pedestrian(1, 0, 0, start_pos_x, start_pos_y, init_e_unit_x, init_e_unit_y, v_max, desired_v0, destination_x, destination_y);
}
return P;
}
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, const char *dump_path) {
#if BENCHMARK
(void)dump_path;
#endif
#if !BENCHMARK
const int dump_interval = 1;
vector<tuple<int, int, float, float, float, float, int>> samples;
if (dump_path) {
samples.reserve(static_cast<size_t>(n_iter / dump_interval + 1) * n_p);
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
samples.emplace_back(0, i, p.curr_pos_x, p.curr_pos_y, p.e_x, p.e_y, p.color);
}
}
#endif
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
float desired_velocity_x = p.e_x * p.desired_v0;
float desired_velocity_y = p.e_y * p.desired_v0;
float f_alpha_dt_x = (desired_velocity_x - p.curr_vel_x) * dt_times_tau_alpha_inv;
float f_alpha_dt_y = (desired_velocity_y - p.curr_vel_y) * dt_times_tau_alpha_inv;
for (int j = 0; j < n_p; j++) {
if (i == j)
continue;
float r_ab_x = p.curr_pos_x - pedestrians[j].curr_pos_x;
float r_ab_y = p.curr_pos_y - pedestrians[j].curr_pos_y;
float r_ab_norm = sqrt(r_ab_x * r_ab_x + r_ab_y * r_ab_y);
r_ab_norm = r_ab_norm > EPS ? r_ab_norm : EPS;
float r_ab2_x = r_ab_x - pedestrians[j].s_b_e_x;
float r_ab2_y = r_ab_y - pedestrians[j].s_b_e_y;
float r_ab2_norm = sqrt(r_ab2_x * r_ab2_x + r_ab2_y * r_ab2_y);
r_ab2_norm = r_ab2_norm > EPS ? r_ab2_norm : EPS;
float r_ab_norm_sum = r_ab_norm + r_ab2_norm;
float two_b_sq = r_ab_norm_sum * r_ab_norm_sum - pedestrians[j].s_b_sq;
two_b_sq = two_b_sq > EPS_SQ ? two_b_sq : EPS_SQ;
float two_b = sqrt(two_b_sq);
float scalar_mult_common = (r_ab_norm_sum / (two_b * r_ab_norm * r_ab2_norm)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b * NEG_TWO_SIGMA_INV));
float scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
float scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
float f_alpha_beta_neg_x = r_ab_x * scalar_mult_r_ab + r_ab2_x * scalar_mult_r_ab2;
float f_alpha_beta_neg_y = r_ab_y * scalar_mult_r_ab + r_ab2_y * scalar_mult_r_ab2;
float w_val;
float f_norm_sq = f_alpha_beta_neg_x * f_alpha_beta_neg_x + f_alpha_beta_neg_y * f_alpha_beta_neg_y;
float rhs = f_norm_sq * cos_phi_sq;
float e_dot_f = p.e_x * f_alpha_beta_neg_x + p.e_y * f_alpha_beta_neg_y;
float e_dot_f_sq = e_dot_f * e_dot_f;
if (e_dot_f >= 0.0 || e_dot_f_sq <= rhs)
w_val = dt_neg;
else
w_val = c_neg_times_dt;
f_alpha_dt_x = f_alpha_dt_x + f_alpha_beta_neg_x * w_val;
f_alpha_dt_y = f_alpha_dt_y + f_alpha_beta_neg_y * w_val;
}
for (int k = 0; k < board.n_obstacles; k++) {
float v1_x = p.curr_pos_x - board.obstacles[k].start_x;
float v1_y = p.curr_pos_y - board.obstacles[k].start_y;
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv;
if (projection < 0.0)
projection = 0.0;
else if (projection > 1.0)
projection = 1.0;
float r_aB_x = p.curr_pos_x - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection);
float r_aB_y = p.curr_pos_y - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection);
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS;
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
float f_alpha_B_neg_x = r_aB_x * scalar_mult;
float f_alpha_B_neg_y = r_aB_y * scalar_mult;
float w_val;
float e_dot_f = (f_alpha_B_neg_y == 0) ? 0 : (f_alpha_B_neg_y < 0 ? -p.e_y : p.e_y);
if (e_dot_f >= cos_phi)
w_val = dt_neg;
else
w_val = c_neg_times_dt;
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
f_alpha_dt_x += f_alpha_B_dt_x;
f_alpha_dt_y += f_alpha_B_dt_y;
}
p.w_x += f_alpha_dt_x;
p.w_y += f_alpha_dt_y;
}
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
float w_norm = sqrt(p.w_x * p.w_x + p.w_y * p.w_y);
float w_norm2 = w_norm > EPS ? w_norm : EPS;
float correction_factor = min(1.0f, p.v_max / w_norm2);
p.curr_vel_x = p.w_x * correction_factor;
p.curr_vel_y = p.w_y * correction_factor;
p.curr_pos_x = p.curr_pos_x + p.curr_vel_x * dt;
p.curr_pos_y = p.curr_pos_y + p.curr_vel_y * dt;
if (p.color == 0 && p.curr_pos_x > (BOARD_LENGTH - 1.5)) {
p.curr_pos_x = 1.0;
p.destination_y = p.curr_pos_y;
p.w_x = p.desired_v0;
p.w_y = 0.0;
p.curr_vel_x = p.w_x;
p.curr_vel_y = p.w_y;
}
if (p.color == 1 && p.curr_pos_x < 1.5) {
p.curr_pos_x = BOARD_LENGTH - 1.0;
p.destination_y = p.curr_pos_y;
p.w_x = -p.desired_v0;
p.w_y = 0.0;
p.curr_vel_x = p.w_x;
p.curr_vel_y = p.w_y;
}
p.curr_pos_y = clamp(p.curr_pos_y, WALL_EPS, BOARD_WIDTH_MINUS_WALL_EPS);
float e_x = p.destination_x - p.curr_pos_x;
float e_y = p.destination_y - p.curr_pos_y;
float e_norm = sqrt(e_x * e_x + e_y * e_y);
float e_norm2 = e_norm > EPS ? e_norm : EPS;
float e_norm_inv = 1.0 / e_norm2;
p.e_x = e_x * e_norm_inv;
p.e_y = e_y * e_norm_inv;
float velocity_norm_sq = p.curr_vel_x * p.curr_vel_x + p.curr_vel_y * p.curr_vel_y;
float velocity_norm = sqrt(velocity_norm_sq);
p.s_b_sq = velocity_norm_sq * delta_t_sq;
p.s_b_e_x = p.e_x * (velocity_norm * delta_t);
p.s_b_e_y = p.e_y * (velocity_norm * delta_t);
#if !BENCHMARK
if (dump_path && (t % dump_interval == 0)) {
samples.emplace_back(t + 1, i, p.curr_pos_x, p.curr_pos_y, p.e_x, p.e_y, p.color);
}
#endif
}
}
#if !BENCHMARK
if (dump_path) {
ofstream out(dump_path);
out << "time,id,x,y,p.e.x,p.e.y,color\n";
for (auto &[t, id, x, y, e_x, e_y, c] : samples) {
out << t << ',' << id << ',' << x << ',' << y << ',' << e_x << ',' << e_y << ',' << c << '\n';
}
}
#endif
return 0;
}
+280
View File
@@ -0,0 +1,280 @@
// # This file performs in-lining for run_simulation.
// # Changes: Replaced references to Vector2d structs inside each Obstacle of Board.obstacles with references to doubles
// # Predecessor: variant_inlining_7.cpp
#include "../headers/board_reduced.hpp"
#include "../headers/constants.hpp"
#include "../headers/pedestrian_reduced.hpp"
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <math.h>
#include <random>
#include <tuple>
#include <vector>
#define SEED 42
#define BOARD_WIDTH 10.0
#define BOARD_LENGTH 50.0
#define WALL_EPS 1e-3
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999
#ifndef RANDOM_SEED
#define RANDOM_SEED 0
#endif
#ifndef BENCHMARK
#define BENCHMARK 0
#endif
using namespace std;
Pedestrian *init_pedestrians(int n_p) {
#if RANDOM_SEED
random_device rd;
mt19937 gen(rd());
#else
mt19937 gen(SEED);
#endif
normal_distribution<double> velocity_gen(v0_mean, v0_std_dev);
uniform_real_distribution<double> y_gen(0.3, BOARD_WIDTH - 0.3);
uniform_real_distribution<double> x_gen_left(2.0, BOARD_LENGTH / 2.0);
uniform_real_distribution<double> x_gen_right(BOARD_LENGTH / 2.0, BOARD_LENGTH - 2.0);
Pedestrian *P = new Pedestrian[n_p];
int n_p_half = n_p / 2;
for (int i = 0; i < n_p_half; i++) {
double x = x_gen_left(gen);
double y = y_gen(gen);
double start_pos_x = x;
double start_pos_y = y;
double destination_x = BOARD_LENGTH + 30.0;
double destination_y = y;
double init_e_x = destination_x - start_pos_x;
double init_e_y = destination_y - start_pos_y;
double init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
double init_e_unit_x = init_e_x / init_e_norm;
double init_e_unit_y = init_e_y / init_e_norm;
double desired_v0;
do {
desired_v0 = velocity_gen(gen);
} while (desired_v0 <= 0.1);
double v_max = 1.3 * desired_v0;
P[i] = Pedestrian(0, 0, 0, start_pos_x, start_pos_y, init_e_unit_x, init_e_unit_y, v_max, desired_v0, destination_x, destination_y);
}
for (int i = n_p_half; i < n_p; i++) {
double x = x_gen_right(gen);
double y = y_gen(gen);
double start_pos_x = x;
double start_pos_y = y;
double destination_x = -30;
double destination_y = y;
double init_e_x = destination_x - start_pos_x;
double init_e_y = destination_y - start_pos_y;
double init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
double init_e_unit_x = init_e_x / init_e_norm;
double init_e_unit_y = init_e_y / init_e_norm;
double desired_v0;
do {
desired_v0 = velocity_gen(gen);
} while (desired_v0 <= 0.1);
double v_max = 1.3 * desired_v0;
P[i] = Pedestrian(1, 0, 0, start_pos_x, start_pos_y, init_e_unit_x, init_e_unit_y, v_max, desired_v0, destination_x, destination_y);
}
return P;
}
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, const char *dump_path) {
#if BENCHMARK
(void)dump_path;
#endif
#if !BENCHMARK
const int dump_interval = 1;
vector<tuple<int, int, double, double, double, double, int>> samples;
if (dump_path) {
samples.reserve(static_cast<size_t>(n_iter / dump_interval + 1) * n_p);
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
samples.emplace_back(0, i, p.curr_pos_x, p.curr_pos_y, p.e_x, p.e_y, p.color);
}
}
#endif
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
double desired_velocity_x = p.e_x * p.desired_v0;
double desired_velocity_y = p.e_y * p.desired_v0;
double f_alpha_dt_x = (desired_velocity_x - p.curr_vel_x) * dt_times_tau_alpha_inv;
double f_alpha_dt_y = (desired_velocity_y - p.curr_vel_y) * dt_times_tau_alpha_inv;
for (int j = 0; j < n_p; j++) {
if (i == j)
continue;
double r_ab_x = p.curr_pos_x - pedestrians[j].curr_pos_x;
double r_ab_y = p.curr_pos_y - pedestrians[j].curr_pos_y;
double r_ab_norm = sqrt(r_ab_x * r_ab_x + r_ab_y * r_ab_y);
double r_ab2_x = r_ab_x - pedestrians[j].s_b_e_x;
double r_ab2_y = r_ab_y - pedestrians[j].s_b_e_y;
double r_ab2_norm = sqrt(r_ab2_x * r_ab2_x + r_ab2_y * r_ab2_y);
double r_ab_norm_sum = r_ab_norm + r_ab2_norm;
double two_b_sq = r_ab_norm_sum * r_ab_norm_sum - pedestrians[j].s_b_sq;
double two_b = sqrt(two_b_sq);
double scalar_mult_common = (r_ab_norm_sum / (two_b * r_ab_norm * r_ab2_norm)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b * NEG_TWO_SIGMA_INV));
double scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
double scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
double f_alpha_beta_neg_x = r_ab_x * scalar_mult_r_ab + r_ab2_x * scalar_mult_r_ab2;
double f_alpha_beta_neg_y = r_ab_y * scalar_mult_r_ab + r_ab2_y * scalar_mult_r_ab2;
double w_val = dt_neg;
double a = (p.e_x * f_alpha_beta_neg_x + p.e_y * f_alpha_beta_neg_y) * cos_phi_inv;
double b_sq = f_alpha_beta_neg_x * f_alpha_beta_neg_x + f_alpha_beta_neg_y * f_alpha_beta_neg_y;
if (a >= 0 && (a * a > b_sq)) {
w_val = c_neg_times_dt;
}
f_alpha_dt_x = f_alpha_dt_x + f_alpha_beta_neg_x * w_val;
f_alpha_dt_y = f_alpha_dt_y + f_alpha_beta_neg_y * w_val;
}
for (int k = 0; k < board.n_obstacles; k++) {
double v1_x = p.curr_pos_x - board.obstacles[k].start_x;
double v1_y = p.curr_pos_y - board.obstacles[k].start_y;
double projection = (v1_x * board.obstacles[k].obstacle_v_x + v1_y * board.obstacles[k].obstacle_v_y) * board.obstacles[k].length_sq_inv;
if (projection < 0.0)
projection = 0.0;
else if (projection > 1.0)
projection = 1.0;
double r_aB_x = p.curr_pos_x - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection);
double r_aB_y = p.curr_pos_y - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection);
double r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
double f_alpha_B_neg_x = r_aB_x * scalar_mult;
double f_alpha_B_neg_y = r_aB_y * scalar_mult;
double w_val;
double e_dot_f = (f_alpha_B_neg_y == 0) ? 0 : (f_alpha_B_neg_y < 0 ? -p.e_y : p.e_y);
if (e_dot_f >= cos_phi)
w_val = dt_neg;
else
w_val = c_neg_times_dt;
double f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
double f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
f_alpha_dt_x += f_alpha_B_dt_x;
f_alpha_dt_y += f_alpha_B_dt_y;
}
p.w_x += f_alpha_dt_x;
p.w_y += f_alpha_dt_y;
}
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
double w_norm = sqrt(p.w_x * p.w_x + p.w_y * p.w_y);
double correction_factor = min(1.0, p.v_max / w_norm);
p.curr_vel_x = p.w_x * correction_factor;
p.curr_vel_y = p.w_y * correction_factor;
p.curr_pos_x = p.curr_pos_x + p.curr_vel_x * dt;
p.curr_pos_y = p.curr_pos_y + p.curr_vel_y * dt;
if (p.color == 0 && p.curr_pos_x > (BOARD_LENGTH - 1.5)) {
p.curr_pos_x = 1.0;
p.destination_y = p.curr_pos_y;
p.w_x = p.desired_v0;
p.w_y = 0.0;
p.curr_vel_x = p.w_x;
p.curr_vel_y = p.w_y;
}
if (p.color == 1 && p.curr_pos_x < 1.5) {
p.curr_pos_x = BOARD_LENGTH - 1.0;
p.destination_y = p.curr_pos_y;
p.w_x = -p.desired_v0;
p.w_y = 0.0;
p.curr_vel_x = p.w_x;
p.curr_vel_y = p.w_y;
}
p.curr_pos_y = clamp(p.curr_pos_y, WALL_EPS, BOARD_WIDTH_MINUS_WALL_EPS);
double e_x = p.destination_x - p.curr_pos_x;
double e_y = p.destination_y - p.curr_pos_y;
double e_norm_inv = 1.0 / sqrt(e_x * e_x + e_y * e_y);
p.e_x = e_x * e_norm_inv;
p.e_y = e_y * e_norm_inv;
double velocity_norm_sq = p.curr_vel_x * p.curr_vel_x + p.curr_vel_y * p.curr_vel_y;
double velocity_norm = sqrt(velocity_norm_sq);
p.s_b_sq = velocity_norm_sq * delta_t_sq;
p.s_b_e_x = p.e_x * (velocity_norm * delta_t);
p.s_b_e_y = p.e_y * (velocity_norm * delta_t);
#if !BENCHMARK
if (dump_path && (t % dump_interval == 0)) {
samples.emplace_back(t + 1, i, p.curr_pos_x, p.curr_pos_y, p.e_x, p.e_y, p.color);
}
#endif
}
}
#if !BENCHMARK
if (dump_path) {
ofstream out(dump_path);
out << "time,id,x,y,p.e.x,p.e.y,color\n";
for (auto &[t, id, x, y, e_x, e_y, c] : samples) {
out << t << ',' << id << ',' << x << ',' << y << ',' << e_x << ',' << e_y << ',' << c << '\n';
}
}
#endif
return 0;
}