diff --git a/board.hpp b/board.hpp index 33da163..6ee78b4 100644 --- a/board.hpp +++ b/board.hpp @@ -1,52 +1,9 @@ #pragma once #include "vector2d.hpp" -struct Obstacle { - Vector2d start; - Vector2d end; - - Obstacle() : start(0, 0), end(0, 0) {} - Obstacle(Vector2d s, Vector2d e) : start(s), end(e) {} - - // Find the point of the obstacle that is nearest to p - // # 15 flops - Vector2d find_nearest_obstacle_point(Vector2d p) { - - // # 2 ADDs - Vector2d obstacle_v = end - start; - - // # 9 flops = 2 ADDs, 2 MULTs, 1 ADD, 1 DIV, 2 MULTs, 1 ADD - double projection = (p - start).dot(obstacle_v) / obstacle_v.dot(obstacle_v); - - if (projection < 0.0) - projection = 0.0; - else if (projection > 1.0) - projection = 1.0; - - // # 4 flops = 2 ADDs, 2 MULTs - return start + obstacle_v * projection; - - } - -}; - struct Board { double length, width; - Obstacle *obstacles; // array of obstacles (represented as lines) that are present on the board - int n_obstacles; // number of obstacles - - Board(double l, double w, int n, Obstacle *o) : length(l), width(w), n_obstacles(n + 4) { - obstacles = new Obstacle[n + 4]; - for (int i = 0; i < n; i++) { - obstacles[i] = o[i]; - } - - // borders of the board: bottom, right, top, left - obstacles[n] = Obstacle(Vector2d(0, 0), Vector2d(l, 0)); - obstacles[n + 1] = Obstacle(Vector2d(l, 0), Vector2d(l, w)); - obstacles[n + 2] = Obstacle(Vector2d(l, w), Vector2d(0, w)); - obstacles[n + 3] = Obstacle(Vector2d(0, w), Vector2d(0, 0)); - } - - ~Board() { delete[] obstacles; } + Vector2d *borders; // array containing the position of each obstacle + int n_borders; // number of obstacles + Board(double l, double w, int n, Vector2d *border) : length(l), width(w), n_borders(n), borders(border) {} }; diff --git a/forces.cpp b/forces.cpp index cd7507f..a682428 100644 --- a/forces.cpp +++ b/forces.cpp @@ -1,32 +1,35 @@ #include "forces.hpp" #include -// # 8 flops = 5 MULTs, 2 ADD, 1 SQRT +// # Done double w(Vector2d e, Vector2d f) { if (e.dot(f) >= f.norm() * cos_phi) return 1.0; - else - return c; + return c; } -// # 2 DIVs -double g(double v_max_alpha, double w_alpha_norm) { - if (w_alpha_norm <= v_max_alpha) { - return 1; - } - return v_max_alpha / w_alpha_norm; -} - -// # 8 flops +// # Done +// +// $\vec{F}_\alpha(\vec{v}_\alpha, v^0_\alpha \vec{e}_\alpha) \vec{e}_\alpha(t) = \frac{1}{\tau_\alpha}(v^0_\alpha \vec{e}_\alpha - \vec{v}_\alpha)$ +// void compute_acceleration_term(Pedestrian *p) { - // # 2 flops + + // $v_{\alpha}^{0}\vec{e}_{\alpha}$ Vector2d desired_velocity = p->e * p->desired_v0; + + // $\vec{v}_{\alpha}(t)$ Vector2d actual_velocity = p->curr_vel; - // # 6 flops = 4 ADDs, 2 DIVs - p->f_i += (desired_velocity - actual_velocity) / tau_alpha; + + double tau_alpha_inv = 1 / tau_alpha; + + p->F_i += (desired_velocity - actual_velocity) * tau_alpha_inv; } -// # 55(N - 1) = 55N - 55 flops +// # Done +// +// $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{\beta})$ +// + void compute_repulsive_effects_from_pedestrians(Pedestrian *pedestrians, Pedestrian *p, int N) { for (int i = 0; i < N; i++) { @@ -40,61 +43,78 @@ void compute_repulsive_effects_from_pedestrians(Pedestrian *pedestrians, Pedestr // continue; // } - // # 2 ADDs + // $\vec{r}_{\alpha\beta}$ Vector2d r_ab = p->curr_pos - pedestrians[i].curr_pos; - // # 4 flops = 1 SQRT, 2 MULTs, 1 ADD + // $\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert$ double r_ab_norm = r_ab.norm(); - Vector2d e_b = pedestrians[i].e; + // $\vec{e}_{\beta}$ + Vector2d e_beta = pedestrians[i].e; - // # 2 MULTs - double s_b = pedestrians[i].curr_vel.norm() * delta_t; + // $v_\beta \Delta t$ + double v_beta_delta_t = pedestrians[i].desired_v0 * delta_t; - // # 4 flops = 2 ADDs, 2 MULTs - Vector2d r_ab2 = r_ab - (e_b * s_b); + // $\vec{r}_{\alpha\beta} - \vec{v}_{\beta}\varDelta_{t}\vec{e}_{\beta}$ + Vector2d r_ab_minus_v_beta_delta_t_e_beta = r_ab - (e_beta * v_beta_delta_t); - // # 4 flops = 1 SQRT, 2 MULTs, 1 ADD - double r_ab2_norm = r_ab2.norm(); + // $\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert$ + double r_ab_minus_v_beta_delta_t_e_beta_norm = r_ab_minus_v_beta_delta_t_e_beta.norm(); - // # 7 flops = 1 SQRT, 3 ADDs, 2 MULTs, 1 DIV - double b = sqrt((r_ab_norm + r_ab2_norm) * (r_ab_norm + r_ab2_norm) - s_b * s_b) / 2; + // (4): + // + // $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}$ + // - // # 12 flops = 6 DIVs, 3 ADDs, 3 MULTs - Vector2d b_grad = (r_ab / r_ab_norm + r_ab2 / r_ab2_norm) * (r_ab_norm + r_ab2_norm) / (4 * b); + double b = sqrt((r_ab_norm + r_ab_minus_v_beta_delta_t_e_beta_norm) * (r_ab_norm + r_ab_minus_v_beta_delta_t_e_beta_norm) - (v_beta_delta_t) * (v_beta_delta_t)) / 2; - // # 6 flops = 3 MULTs, 2 DIVs, 1 EXP - Vector2d f_ped = b_grad * ((V0_AB / SIGMA) * (exp(-b / SIGMA))); + // (7): + // + // $\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})}{4b}$ + // + // + Vector2d delta_r_alpha_beta_b = (((r_ab / r_ab_norm) + (r_ab_minus_v_beta_delta_t_e_beta / r_ab_minus_v_beta_delta_t_e_beta_norm)) * (r_ab_norm + r_ab_minus_v_beta_delta_t_e_beta_norm)) / (4 * b); - // # 14 flops: 9 MULTs, 4 ADDs, 1 SQRT - p->f_i += f_ped * w(p->e, f_ped * -1); + // (5): + // + // $\vec{f}_{\alpha \beta}(\vec{r}_{\alpha \beta}) = \frac{V^0_{\alpha \beta}}{\sigma}e^{-b / \sigma} \grad_{\vec{r}_{\alpha \beta}}b$ + // + // + Vector2d f_alpha_beta_r_alpha_beta = delta_r_alpha_beta_b * ((V0_AB / SIGMA) * (exp(-b / SIGMA))); + + // (11): + // + // $\vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{\beta}) = w(\vec{e}_{\alpha}, -\vec{f}_{\alpha\beta})\vec{f}_{\alpha\beta}(\vec{r}_{\alpha} - \vec{r}_{\beta})$ + // + // Add repulsive effects from other pedestrians to current pedestrian's social force + p->F_i += f_alpha_beta_r_alpha_beta * w(p->e, f_alpha_beta_r_alpha_beta * -1); } } -// # Assume that the four borders are also 4 obstacles -// # 48 n_obstacles flops = 192 flops -void compute_repulsive_effects_from_borders(Board &board, Pedestrian *p) { +// # Done +void compute_repulsive_effects_from_borders(Board &B, Pedestrian *p) { - Obstacle *obstacles = board.obstacles; - int n_borders = board.n_obstacles; + Vector2d *borders = B.borders; + int n_borders = B.n_borders; for (int i = 0; i < n_borders; i++) { - Obstacle obstacle = obstacles[i]; + Vector2d border = borders[i]; - // # 17 flops - Vector2d r_aB = p->curr_pos - obstacle.find_nearest_obstacle_point(p->curr_pos); + // $\vec{r}_{\alpha B}$ + Vector2d r_alpha_B = p->curr_pos - border; - // # 17 flops = 2 DIVs, 1 SQRT, 2 MULTs, 1 ADD, 2 MULTs, 1 DIV, 2 MULTs, 1 EXP, 1 SQRT, 2 MULTs, 1 ADD, 1 DIV - Vector2d f_obst = (r_aB / r_aB.norm()) * (U0_AB / R) * std::exp(-r_aB.norm() / R); + // + // $\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}$ + // - // # 2 ADDs - // p->f_i += f_obst; //commented since this version dont takes the field of view / angle to the border into account + Vector2d F_alpha_B = (r_alpha_B / r_alpha_B.norm()) * (U0_AB / R) * std::exp(-r_alpha_B.norm() / R); - // I have read the paper again and on page 6 they say - // However, the formulas above for attractive and repulsive effects only hold for situations that are perceived in the desired direction e_alpha(t) of motion. - // I assumed that with formulas above its also meant for equation 8 (even though not explicitly mentioned their) - // # 14 flops: 9 MULTs, 4 ADDS, 1 SQRT - p->f_i += f_obst * w(p->e, f_obst * -1); + // (12): + // + // $\vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{ B}) = w(\vec{e}_{\alpha}, -\vec{F}_{\alpha B})\vec{F}_{\alpha B}(\vec{r}_{\alpha} - \vec{r}_{ B})$ + // + + p->F_i += F_alpha_B * w(p->e, F_alpha_B * (-1)); } } diff --git a/forces.hpp b/forces.hpp index 3d2bd4a..27cc042 100644 --- a/forces.hpp +++ b/forces.hpp @@ -3,33 +3,46 @@ #include "pedestrian.hpp" #include +// $\tau_{\alpha}$ +// https://arxiv.org/pdf/cond-mat/9805244, page 8 const double tau_alpha = 0.5; +// $V^0_{\alpha \beta}$ +// https://arxiv.org/pdf/cond-mat/9805244, page 8 const double V0_AB = 2.1; +// $\sigma$ +// https://arxiv.org/pdf/cond-mat/9805244, page 8 const double SIGMA = 0.3; +// $U^0_{\alpha B}$ +// https://arxiv.org/pdf/cond-mat/9805244, page 8 const double U0_AB = 10.0; +// $R$ +// https://arxiv.org/pdf/cond-mat/9805244, page 8 const double R = 0.2; +// $c$ +// https://arxiv.org/pdf/cond-mat/9805244, page 8 const double c = 0.5; -const double phi = 100.0 / 180.0 * M_PI; +// $\varphi$ +// https://arxiv.org/pdf/cond-mat/9805244, page 8 +const double phi = 100.0; const double cos_phi = cos(phi); +// $\varDelta_{t}$ +// step size = 0.2 seconds +// https://arxiv.org/pdf/cond-mat/9805244, page 8 const double delta_t = 2; -const double dt = 0.01; - // # Optional (forces.cpp: compute_repulsive_effects_from_pedestrians) // const double DISTANCE = 2.0; double w(Vector2d e, Vector2d f); -double g(double v_max_alpha, double w_alpha_norm); - void compute_acceleration_term(Pedestrian *p); void compute_repulsive_effects_from_pedestrians(Pedestrian *P, Pedestrian *p, int N); diff --git a/main.cpp b/main.cpp index afd1f76..36b18c8 100644 --- a/main.cpp +++ b/main.cpp @@ -2,15 +2,14 @@ #include "forces.hpp" #include "math.h" #include "pedestrian.hpp" -#include "tsc_x86.h" #include "vector2d.hpp" -#include -#include -#include -#include -#define BOARD_WIDTH 10.0 -#define BOARD_LENGTH 50.0 +double g(double v_max_alpha, double w_alpha_norm) { + if (w_alpha_norm <= v_max_alpha) { + return 1; + } + return v_max_alpha / w_alpha_norm; +} using namespace std; @@ -24,140 +23,69 @@ using namespace std; Pedestrian *init_pedestrians(int n_p) { Pedestrian *P = new Pedestrian[n_p]; - std::random_device rd; // use for truly random simulation - // std::mt19937 gen(rd()); // use rd instead of 42 as seed for real randomness - std::mt19937 gen(42); // use fixed seed for reproducibility - std::normal_distribution velocity_gen(1.34, 0.26); // 1.34 and 0.26 are the mean and stdrd deviation used in paper - std::uniform_real_distribution x_gen(2, BOARD_LENGTH - 2); - std::uniform_real_distribution y_gen(2, BOARD_WIDTH - 2); - for (int i = 0; i < n_p / 2; i++) { - double y = y_gen(gen); - Vector2d start_pos = Vector2d(x_gen(gen), y); - Vector2d destination = Vector2d(BOARD_LENGTH + 300, BOARD_WIDTH / 2); - // double desired_v0 = velocity_gen(gen); - double desired_v0; - do { - desired_v0 = velocity_gen(gen); - } while (desired_v0 <= 0.1); // to avoid small values (Way below the social force -> that would make the walk backwards) (we discovered some weird stuff in the simulation) - double v_max = 1.3 * desired_v0; - P[i] = Pedestrian(WHITE, (destination - start_pos) / (destination - start_pos).norm() * desired_v0, start_pos, (destination - start_pos) / (destination - start_pos).norm(), v_max, desired_v0, destination, Vector2d()); - } - - for (int i = n_p / 2; i < n_p; i++) { - double y = y_gen(gen); - Vector2d start_pos = Vector2d(x_gen(gen), y); - Vector2d destination = Vector2d(-300, BOARD_WIDTH / 2); - // double desired_v0 = velocity_gen(gen); - double desired_v0; - do { - desired_v0 = velocity_gen(gen); - } while (desired_v0 <= 0.1); - double v_max = 1.3 * desired_v0; - P[i] = Pedestrian(BLACK, (destination - start_pos) / (destination - start_pos).norm() * desired_v0, start_pos, (destination - start_pos) / (destination - start_pos).norm(), v_max, desired_v0, destination, Vector2d(0, 0)); + for (int i = 0; i < n_p; i++) { + P[i] = Pedestrian(); } return P; } +Board init_board(int width, int length) { + Vector2d obs[] = {Vector2d(2.0, 1.0), Vector2d(4.0, 1.0), Vector2d(6.0, 2.0), Vector2d(6.0, 4.0)}; + return Board(width, length, 4, obs); +} + int main(int argc, const char *argv[]) { - Obstacle obs[] = {}; - Board board = Board(BOARD_LENGTH, BOARD_WIDTH, 0, obs); + Board B = init_board(10, 15); int n_p = atoi(argv[1]); int n_iter = atoi(argv[2]); - // Logic to get/dump the simulation data - const int dump_interval = 5; // dump every 5 iterations -> 20 fps (since dt = 0.01) - const char *dump_path = (argc >= 4) ? argv[3] : nullptr; - - std::vector> samples; // t, id, x, y, color - if (dump_path) - samples.reserve((size_t)n_iter / dump_interval * n_p); - Pedestrian *pedestrians = init_pedestrians(n_p); - // # Start here or right after function signature - uint64_t start = start_tsc(); - - // # (8 + 55N - 55 + 192 + 4 + 8 + 4 + 10)*n_p*n_iter for (int t = 0; t < n_iter; t++) { - // Pass1: conmpute all forces from the current state + for (int i = 0; i < n_p; i++) { Pedestrian &p = pedestrians[i]; - p.f_i.reset(); + p.F_i.reset(); - // # 8 flops + // + // $\vec{F}_\alpha(\vec{v}_\alpha, v^0_\alpha \vec{e}_\alpha) \vec{e}_\alpha(t) = \frac{1}{\tau_\alpha}(v^0_\alpha \vec{e}_\alpha - \vec{v}_\alpha)$ + // compute_acceleration_term(&p); - // # 55N - 55 flops + // + // $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{\beta})$ + // compute_repulsive_effects_from_pedestrians(pedestrians, &p, n_p); - // # 192 flops - compute_repulsive_effects_from_borders(board, &p); + // + // $\sum_{B} \vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{B})$ + // + compute_repulsive_effects_from_borders(B, &p); + + // + // $\vec{w}_i = \vec{v}_i + \vec{F}_idt$ + // + p.w_alpha = p.curr_vel + p.F_i * delta_t; + + // + // $\vec{v}_i^{t+1} = \vec{w}_ig(\frac{v^{max}_i}{\Arrowvert \vec{w}_i \Arrowvert}) $ + // + p.next_vel = p.w_alpha * g(p.v_max, p.w_alpha.norm()); + + // + // $\vec{r}_i^{t+1} += \vec{r}^t_i + v^t_idt$ + // + p.curr_pos = p.curr_pos + p.curr_vel * delta_t; } - // Pass2: integrate the forces and update the current state + for (int i = 0; i < n_p; i++) { - Pedestrian &p = pedestrians[i]; - // # 4 flops = 2 ADDs, 2 MULTs - // integrate preferred velocity: dw/dt = F (Eq. 10) - p.w = p.w + p.f_i * dt; - // Vector2d w_alpha = p.curr_vel + p.f_i * dt; // commented since this was using p.curr_vel instead of p.w (different from paper) - - // # 8 flops = 2 MULTs, 2 DIVs, 1 SQRT, 2 MULTs, 1 ADD - p.curr_vel = p.w * g(p.v_max, p.w.norm()); - - // # 4 flops = 2 ADDs, 2 MULTs - p.curr_pos = p.curr_pos + p.curr_vel * dt; - - // how to handle if a pedestrian reached the end of our simulation/end of the hallway - // The option we do here is to just "teleport" the person back to the beginning - // the good thing is we will have a constant flow of pedestrians and in the simulation we should see how lines build up - if (p.color == WHITE && p.curr_pos.x > (BOARD_LENGTH - 1.5)) { - // basically just keep the y position and reset the x - p.curr_pos.x = 1.0; - p.destination.y = p.curr_pos.y; - } - 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; - } - - // # 10 flops = 5 ADDs, 2 DIVs, 1 SQRT, 2 MULTs - p.e = (p.destination - p.curr_pos) / (p.destination - p.curr_pos).norm(); - - // if we have a dump path/file we wanna save the data for visualisation - if (dump_path && (t % dump_interval == 0)) { - samples.emplace_back(t, i, p.curr_pos.x, p.curr_pos.y, p.color); - } - } - } - long long W = (long long)(8 + 55 * n_p - 55 + 192 + 4 + 8 + 4 + 10) * n_p * n_iter; - std::cout << "W(n): "; - std::cout << W; - std::cout << " flops\n"; - - double T = stop_tsc(start); - std::cout << "T(n): "; - std::cout << T; - std::cout << " cycles\n"; - - double P = W / T; - - std::cout << "P(n): "; - std::cout << P; - std::cout << " flops/cycle\n"; - - // now we wanna save the data to a csv - if (dump_path) { - std::ofstream out(dump_path); - out << "time,id,x,y,color\n"; - for (auto &[t, id, x, y, c] : samples) { - out << t << ',' << id << ',' << x << ',' << y << ',' << c << '\n'; + pedestrians[i].update(); } } - delete[] pedestrians; return 0; } diff --git a/pedestrian.hpp b/pedestrian.hpp index 9971657..126d064 100644 --- a/pedestrian.hpp +++ b/pedestrian.hpp @@ -2,23 +2,52 @@ #include "random" #include "vector2d.hpp" -// For visual simulation -enum Color { BLACK, WHITE }; - struct Pedestrian { - Color color; - Vector2d curr_vel; // v_alpha (actual, ≤ v_max) - Vector2d w; // w_alpha (preferred, integrates force) + + // TODO: Update initial values + Pedestrian() : curr_vel(), curr_pos(), e(), v_max(1.34 * 1.3), desired_v0(1.34), destination(), F_i(), next_pos(), next_vel(), w_alpha() {} + + Pedestrian(Vector2d c_v, Vector2d c_p, Vector2d e, Vector2d d, Vector2d f, Vector2d n_p, Vector2d n_v, Vector2d w_alpha) : curr_vel(c_v), curr_pos(c_p), e(e), v_max(), desired_v0(), destination(d), F_i(f), next_pos(n_p), next_vel(n_v), w_alpha(w_alpha) {} + + // $\vec{v}_{\alpha}(t)$ + Vector2d curr_vel; + + // $\vec{r}_\alpha(t)$ Vector2d curr_pos; + + // $\vec{e}_{\alpha}(t)$ Vector2d e; + + // $v_{\alpha}^{max} = 1.3 v^{0}_{\alpha}$ double v_max; + + // $v_\alpha^0$ double desired_v0; + + // # ============================================================= + + // $\vec{r}^0_\alpha$ Vector2d destination; - Vector2d f_i; - Pedestrian() : color(WHITE), curr_vel(0.0, 0.0), w(0.0, 0.0), curr_pos(0.0, 0.0), e(0.0, 0.0), v_max(0.0), desired_v0(0.0), destination(0.0, 0.0), f_i(0.0, 0.0) {} + // social force + Vector2d F_i; - Pedestrian(Color clr, Vector2d c_v, Vector2d c_p, Vector2d e, double v_max, double desired_v0, Vector2d d, Vector2d f) - : color(clr), curr_vel(c_v), w(c_v), // initialize w = v at t=0 - curr_pos(c_p), e(e), v_max(v_max), desired_v0(desired_v0), destination(d), f_i(f) {} + // # ============================================================= + + // $\vec{r}_\alpha(t+1)$ + Vector2d next_pos; + + // $\vec{v}_{\alpha}(t + 1)$ + Vector2d next_vel; + + // $\vec{w}_{\alpha}$ + Vector2d w_alpha; + + // # ============================================================= + + void update() { + curr_pos = next_pos; + curr_vel = next_vel; + e = (destination - curr_pos) / (destination - curr_pos).norm(); + } };