-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarSharingSimulation.hpp
More file actions
115 lines (93 loc) · 3.88 KB
/
CarSharingSimulation.hpp
File metadata and controls
115 lines (93 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#ifndef CAR_SHARING_SIMULATION_HPP_INCLUDED
#define CAR_SHARING_SIMULATION_HPP_INCLUDED
#include <cstdint>
#include <random>
using std::uint32_t;
using std::discrete_distribution;
class CarSharingSimulation {
public:
CarSharingSimulation(
uint32_t num_stations, // number of stations in the system
uint32_t cars_per_station, // initial cars per station
uint32_t spaces_per_station, // number of parking spaces per station
double prob_class_1, // the probability that a user makes a single (as opposed to double) reservation
double lam, // combined rate of reservations
double nu, // pickup rate
double mu // dropoff rate
);
// run the simulation repeatedly, averaging results
std::vector<double> repeated_runs(uint32_t num_runs, double end_time, double dt, uint32_t column);
private:
// transitions of the underlying markov process
enum class Transition {
CLASS1_RESERVATION = 0,
CLASS2_RESERVATION = 1,
CLASS1_PICKUP = 2,
CLASS2_PICKUP = 3,
CLASS1_DROPOFF = 4,
CLASS2_DROPOFF = 5
};
const uint32_t num_stations, cars_per_station, spaces_per_station;
const double prob_class_1, lam, nu, mu;
std::vector<uint32_t>
class1_reservations,
class1_driving,
class1_waiting,
available_cars,
class2_driving,
unavailable_spaces,
_class2_reservations;
uint32_t &class2_reservations(uint32_t start_station, uint32_t dest_station) {
return _class2_reservations.at(start_station * num_stations + dest_station);
}
std::vector<double> cumulative_stats, stats, cumulative_times, transition_times;
uint32_t num_reservations_1, num_reservations_2, // number of Class 1 and Class 2 reservations, resp.
num_driving_1, num_driving_2, // number of Class 1 and Class 2 users driving, resp.
num_waiting, // class 1 users waiting
num_stats; // number of records logged on the current run
std::mt19937 rng;
discrete_distribution<uint32_t> class2_reservation_distribution() const {
discrete_distribution<uint32_t> result(_class2_reservations.cbegin(),
_class2_reservations.cend());
return result;
}
discrete_distribution<uint32_t> class1_reservation_distribution() const {
discrete_distribution<uint32_t> result(class1_reservations.cbegin(),
class1_reservations.cend());
return result;
}
discrete_distribution<uint32_t> class2_driving_distribution() const {
discrete_distribution<uint32_t> result(class2_driving.cbegin(),
class2_driving.cend());
return result;
}
discrete_distribution<uint32_t> class1_driving_distribution() const {
discrete_distribution<uint32_t> result(class1_driving.cbegin() + num_stations,
class1_driving.cend());
return result;
}
uint32_t uniform_random_station();
bool reservation_update_1();
bool reservation_update_2();
void pickup_update_1();
void pickup_update_2();
void dropoff_update_1();
void dropoff_update_2();
Transition next_transition();
double next_holding_time();
void push_time_and_column(double t, uint32_t column);
void run(double end_time, uint32_t column);
void average(double end_time, double dt);
void reset_state();
};
std::vector<double> repeated_runs(
uint32_t num_stations,
uint32_t cars_per_station,
uint32_t spaces_per_station,
double prob_class_1,
double lam,
double nu,
double mu,
double end_time,
double dt);
#endif