256 lines
7.6 KiB
C++
256 lines
7.6 KiB
C++
// # This file performs in-lining for run_simulation.
|
|
// # Changes: Inlined find_nearest_obstacle_point()
|
|
// # Predecessor: variant_inlining_2.cpp
|
|
|
|
#include "../headers/board.hpp"
|
|
#include "../headers/constants.hpp"
|
|
#include "../headers/pedestrian.hpp"
|
|
#include "../headers/vector2d.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);
|
|
|
|
Vector2d start_pos = Vector2d(x, y);
|
|
Vector2d destination = Vector2d(BOARD_LENGTH + 30.0, y);
|
|
|
|
Vector2d init_e = destination - start_pos;
|
|
double init_e_norm = sqrt(init_e.x * init_e.x + init_e.y * init_e.y);
|
|
Vector2d init_e_unit = init_e / 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(WHITE, Vector2d(), start_pos, init_e_unit, v_max, desired_v0, destination);
|
|
}
|
|
|
|
for (int i = n_p_half; i < n_p; i++) {
|
|
double x = x_gen_right(gen);
|
|
double y = y_gen(gen);
|
|
|
|
Vector2d start_pos = Vector2d(x, y);
|
|
Vector2d destination = Vector2d(-30.0, y);
|
|
|
|
Vector2d init_e = destination - start_pos;
|
|
double init_e_norm = sqrt(init_e.x * init_e.x + init_e.y * init_e.y);
|
|
Vector2d init_e_unit = init_e / 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(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
|
|
(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];
|
|
|
|
Vector2d desired_velocity = p.e * p.desired_v0;
|
|
Vector2d actual_velocity = p.curr_vel;
|
|
Vector2d f_alpha_0_dt = (desired_velocity - actual_velocity) * dt_times_tau_alpha_inv;
|
|
Vector2d f_alpha_dt = f_alpha_0_dt;
|
|
|
|
for (int j = 0; j < n_p; j++) {
|
|
if (i == j)
|
|
continue;
|
|
|
|
Vector2d r_ab = p.curr_pos - pedestrians[j].curr_pos;
|
|
|
|
double 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;
|
|
Vector2d s_b_e = pedestrians[j].s_b_e;
|
|
Vector2d r_ab2 = r_ab - s_b_e;
|
|
double 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;
|
|
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;
|
|
two_b_sq = two_b_sq > EPS_SQ ? two_b_sq : EPS_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;
|
|
Vector2d f_alpha_beta_neg = r_ab * scalar_mult_r_ab + r_ab2 * scalar_mult_r_ab2;
|
|
double w_val;
|
|
if ((p.e.x * f_alpha_beta_neg.x + p.e.y * f_alpha_beta_neg.y) >= sqrt(f_alpha_beta_neg.x * f_alpha_beta_neg.x + f_alpha_beta_neg.y * f_alpha_beta_neg.y) * cos_phi)
|
|
w_val = dt_neg;
|
|
else
|
|
w_val = c_neg_times_dt;
|
|
Vector2d f_alpha_beta_dt = f_alpha_beta_neg * w_val;
|
|
|
|
f_alpha_dt += f_alpha_beta_dt;
|
|
}
|
|
|
|
for (int k = 0; k < board.n_obstacles; k++) {
|
|
|
|
Vector2d v1 = p.curr_pos - board.obstacles[k].start;
|
|
Vector2d v2 = board.obstacles[k].obstacle_v;
|
|
|
|
double 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;
|
|
|
|
Vector2d r_aB = p.curr_pos - (board.obstacles[k].start + board.obstacles[k].obstacle_v * projection);
|
|
|
|
double 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;
|
|
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
|
|
Vector2d f_alpha_B_neg = r_aB * scalar_mult;
|
|
double w_val;
|
|
if ((p.e.x * f_alpha_B_neg.x + p.e.y * f_alpha_B_neg.y) >= sqrt(f_alpha_B_neg.x * f_alpha_B_neg.x + f_alpha_B_neg.y * f_alpha_B_neg.y) * cos_phi)
|
|
w_val = dt_neg;
|
|
else
|
|
w_val = c_neg_times_dt;
|
|
|
|
Vector2d f_alpha_B_dt = f_alpha_B_neg * w_val;
|
|
|
|
f_alpha_dt += f_alpha_B_dt;
|
|
}
|
|
|
|
p.w += f_alpha_dt;
|
|
}
|
|
|
|
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 w_norm2 = w_norm > EPS ? w_norm : EPS;
|
|
|
|
double correction_factor = min(1.0, p.v_max / w_norm2);
|
|
|
|
p.curr_vel = p.w * correction_factor;
|
|
|
|
p.curr_pos = p.curr_pos + p.curr_vel * dt;
|
|
|
|
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;
|
|
}
|
|
|
|
p.curr_pos.y = clamp(p.curr_pos.y, WALL_EPS, BOARD_WIDTH_MINUS_WALL_EPS);
|
|
|
|
Vector2d e = p.destination - p.curr_pos;
|
|
|
|
double e_norm = sqrt(e.x * e.x + e.y * e.y);
|
|
double e_norm2 = e_norm > EPS ? e_norm : EPS;
|
|
double e_norm_inv = 1.0 / e_norm2;
|
|
|
|
p.e = e * 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 = p.e * (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;
|
|
}
|