-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.cpp
More file actions
247 lines (234 loc) · 11.9 KB
/
simulation.cpp
File metadata and controls
247 lines (234 loc) · 11.9 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "simulation.h"
//Loading data for simulation
void Simulation::load_data_from_files() {
bookstore.read_sections("Sections.csv");
bookstore.read_salesmen("Salesmen.csv");
bookstore.read_books("Books.csv");
bookstore.read_customer("Customers.csv");
load_simulation_data("Simulation.csv");
select_random_data();
}
//Changind simulation time
void Simulation::change_time(unsigned int minutes) {
int iteration = 0;
if (minutes < 1) {
throw out_of_range("Minutes should be bigger or equal to 1");
}
iteration = minutes;
set_time(iteration);
}
//Load starting simulation data
void Simulation::load_simulation_data(string file) {
vector<vector<string>> sim_data = read_from_file(file);
try {
customers_number = abs(stoi(sim_data[0][0]));
salesmen_number = abs(stoi(sim_data[0][1]));
time = abs(stoi(sim_data[0][2]));
}
catch (const invalid_argument &e) {
throw invalid_argument("Invalid simulation data");
}
if (salesmen_number == 0) {
throw out_of_range("There cannot be less than one salesman");
}
}
//Selecting random customers and salesmen for the bookstore
void Simulation::select_random_data() {
int customer_size = bookstore.get_customers_database().get_size();
if (customers_number > customer_size) {
ss << "Expected number of clients in simulation (" << customers_number
<< ") was bigger than number of clients in database. New number of clients in simulation set to maximal!"
<< endl;
customers_number = customer_size;
}
for (int i = 0; i < customers_number; i++) {
unsigned int random_number;
do {
random_number = rand() % (customer_size) + 1;
} while (bookstore.get_customers_shop().has_id(random_number));
customers_id.push_back(random_number);
bookstore.add_customer_to_shop(random_number);
}
int salesmen_size = bookstore.get_salesmen_database().get_size();
if (salesmen_size < salesmen_number) {
ss << "Expected number of salesmen in simulation (" << salesmen_number
<< ") was bigger than number of salesmen in database. New salesmen of clients in simulation set to maximal!"
<< endl;
salesmen_number = salesmen_size;
}
for (int i = 0; i < salesmen_number; i++) {
unsigned int random_number;
do {
random_number = rand() % (salesmen_size) + 1;
} while (bookstore.get_salesmen_shop().has_id(random_number));
salesmen_id.push_back(random_number);
bookstore.add_salesman_to_shop(random_number);
}
}
//Full implementation of simulation
void Simulation::run() {
ss << endl;
load_data_from_files();
unsigned int cust_size = bookstore.get_customers_shop().get_size();
//Text for start of the simulation
ss << "\n\nStart of simulation" << endl << endl;
ss << "Simulation time (in iterations): " << time << endl;
ss << "Number of salesmen in the bookstore: " << salesmen_number << endl;
ss << "Starting number of customers in the bookstore: " << customers_number << endl << endl;
for (unsigned int i = 0; i < time; i++) {
bool condition = false;
typename map<unsigned int, Salesman>::const_iterator it;
if (bookstore.get_salesmen_shop().get_people().empty()) { break; }
for (it = bookstore.get_salesmen_shop().get_people().cbegin();
it != bookstore.get_salesmen_shop().get_people().cend(); it++) {
if (cust_size == 0) {
condition = true;
if (i < time) {
ss << "Bookstore finished work earlier due to lack of clients." << endl;
}
break;
} else {
//Selecting random customer from shop for the current salesman
unsigned int random_cust_id = rand() % (cust_size);
unsigned int cust_id = customers_id[random_cust_id];
customers_id.erase(customers_id.begin() + random_cust_id);
--cust_size;
unsigned int sal_id = it->second.get_card_id();
bookstore.add_customer_to_salesman(cust_id, sal_id);
bookstore.remove_customer_from_shop(cust_id);
Salesman &salesman = bookstore.get_salesmen_shop().get_person(sal_id);
Customer &customer = salesman.get_customer();
ss << "Salesman with card id " << salesman.get_card_id() << " got a new client" << " Iteration: "
<< i + 1 << endl;
bool cond = true;
bool new_client = true;
//Interaction with single customer
while (cond) {
unsigned int random_book_number = rand() % bookstore.get_books_id().size();
unsigned int random_book = bookstore.get_books_id()[random_book_number];
Book &book = bookstore.get_book_col().get_book(random_book);
ss << "Client with id " << customer.get_card_id() << " asked about the book with id "
<< book.get_id() << endl;
if (!book.get_number() == 0) {
unsigned int operations;
if ((!customer.get_basket().is_empty()) || (!customer.get_ordered().is_empty()) ||
!new_client) {
operations = buy;
} else {
operations = rand() % 4 + 1;
if (operations == 1) { operations = resign; }
else { operations = buy; }
}
switch (operations) {
case buy: {
if (customer.get_money() < book.get_price()) {
ss << "Client with id " << customer.get_card_id()
<< " couldn't afford the book with id " << book.get_id() << endl;
operations = rand() % 2;
switch (operations) {
case continue_yes: {
new_client = false;
ss << "Client with id " << customer.get_card_id()
<< " decided to choose another book." << endl;
break;
}
case continue_no: {
ss << "Client with id " << customer.get_card_id()
<< " decided not to buy other book." << endl;
cond = false;
break;
}
}
} else {
if (customer.get_basket().has_book_id(book.get_id())) {
customer.add_to_basket(book);
int number = customer.get_basket().get_book(book.get_id()).get_number();
customer.get_basket().get_book(book.get_id()).set_number(number + 1);
} else {
customer.add_to_basket(book);
customer.get_basket().get_book(book.get_id()).set_number(1);
}
book.set_number(book.get_number() - 1);
customer.set_money(customer.get_money() - book.get_price());
operations = rand() % 2;
ss << "Client with id " << customer.get_card_id() << " bought the book with id "
<< book.get_id() << endl;
new_client = false;
if (operations == continue_no) {
ss << "Client with id " << customer.get_card_id()
<< " decided to leave the shop after buying book." << endl;
cond = false;
}
}
break;
}
case resign: {
ss << "Client with id " << customer.get_card_id() << " decided not to buy the book."
<< endl;
cond = false;
break;
}
}
} else {
unsigned int operations;
if ((!customer.get_basket().is_empty()) || (!customer.get_ordered().is_empty()) ||
!new_client) {
operations = order;
} else {
operations = rand() % 4 + 1;
if (operations == 1) { operations = resign; }
else { operations = order; }
}
switch (operations) {
case order: {
new_client = false;
if (customer.get_ordered().has_book_id(book.get_id())) {
customer.add_to_ordered_books(book);
int number = customer.get_ordered().get_book(book.get_id()).get_number();
customer.get_ordered().get_book(book.get_id()).set_number(number + 1);
} else {
customer.add_to_ordered_books(book);
customer.get_ordered().get_book(book.get_id()).set_number(1);
}
ss << "Client with id " << customer.get_card_id() << " ordered book with id "
<< book.get_id() << endl;
operations = rand() % 2;
if (operations == continue_no) {
ss << "Client with id " << customer.get_card_id()
<< " decided to leave the shop after ordering book." << endl;
cond = false;
} else {
ss << "Client with id " << customer.get_card_id()
<< " decided to choose another book." << endl;
}
break;
}
case resign: {
ss << "Client with id " << customer.get_card_id() << " decided not to order the book."
<< endl;
cond = false;
break;
}
}
}
}
if (!customer.get_basket().is_empty()) {
ss << customer.get_basket();
}
if (!customer.get_ordered().is_empty()) {
ss << customer.get_ordered();
}
ss << endl << "Number of clients who are in the bookstore: "
<< bookstore.get_customers_shop().get_size();
ss << endl << endl;
}
}
if (condition) { break; }
}
string end_simulation = "The simulation has ended";
ss << end_simulation;
string result = ss.str();
std::cout << result;
write_to_file("simulation_result.txt", result);
std::exit(0);
}