Repository navigation
Expand file tree
/
Copy pathblood-match.cpp
More file actions
236 lines (217 loc) · 7.21 KB
/
Copy pathblood-match.cpp
File metadata and controls
236 lines (217 loc) · 7.21 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
#include <bits/stdc++.h>
using namespace std;
struct Donor {
int id;
string blood_type;
int units;
bool available;
int assigned_to;
};
struct Patient {
int id;
string blood_type;
int urgency;
int units_needed;
int assigned_donor_id;
string match_status;
};
struct CompatibleDonor {
int donor_id;
string blood_type;
int priority;
};
bool compatible[8][8];
map<string, int> blood_to_index;
vector<string> index_to_blood;
bool comparePatients(Patient p1, Patient p2) {
return p1.urgency > p2.urgency;
}
bool compareDonors(CompatibleDonor d1, CompatibleDonor d2) {
return d1.priority < d2.priority;
}
Donor createDonor(int id, string type, int units) {
Donor d;
d.id = id;
d.blood_type = type;
d.units = units;
d.available = true;
d.assigned_to = -1;
return d;
}
Patient createPatient(int id, string type, int urgency, int units) {
Patient p;
p.id = id;
p.blood_type = type;
p.urgency = urgency;
p.units_needed = units;
p.assigned_donor_id = -1;
p.match_status = "PENDING";
return p;
}
void setupSystem() {
blood_to_index["O-"] = 0; blood_to_index["O+"] = 1;
blood_to_index["A-"] = 2; blood_to_index["A+"] = 3;
blood_to_index["B-"] = 4; blood_to_index["B+"] = 5;
blood_to_index["AB-"] = 6; blood_to_index["AB+"] = 7;
index_to_blood.push_back("O-"); index_to_blood.push_back("O+");
index_to_blood.push_back("A-"); index_to_blood.push_back("A+");
index_to_blood.push_back("B-"); index_to_blood.push_back("B+");
index_to_blood.push_back("AB-"); index_to_blood.push_back("AB+");
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
compatible[i][j] = false;
}
}
for (int r = 0; r < 8; r++) {
compatible[0][r] = true;
if (r % 2 == 1 || r >= 6) {
compatible[1][r] = true;
}
}
compatible[0][0] = true;
compatible[0][1] = true;
compatible[0][2] = true;
compatible[0][3] = true;
compatible[0][4] = true;
compatible[0][5] = true;
compatible[0][6] = true;
compatible[0][7] = true;
compatible[1][1] = true;
compatible[1][3] = true;
compatible[1][5] = true;
compatible[1][7] = true;
compatible[2][2] = true;
compatible[2][3] = true;
compatible[2][6] = true;
compatible[2][7] = true;
compatible[3][3] = true;
compatible[3][7] = true;
compatible[4][4] = true;
compatible[4][5] = true;
compatible[4][6] = true;
compatible[4][7] = true;
compatible[5][5] = true;
compatible[5][7] = true;
compatible[6][6] = true;
compatible[6][7] = true;
compatible[7][7] = true;
}
vector<CompatibleDonor> FindCompatibleDonors(Patient patient, vector<Donor> blood_bank) {
vector<CompatibleDonor> compatible_list;
int patient_idx = blood_to_index[patient.blood_type];
for (int i = 0; i < blood_bank.size(); i++) {
Donor current_donor = blood_bank[i];
int donor_idx = blood_to_index[current_donor.blood_type];
if (compatible[donor_idx][patient_idx] && current_donor.available) {
int priority_score;
if (current_donor.blood_type == patient.blood_type) {
priority_score = 1;
} else if (current_donor.blood_type[0] == patient.blood_type[0]) {
priority_score = 2;
} else {
priority_score = 3;
}
CompatibleDonor cd;
cd.donor_id = current_donor.id;
cd.blood_type = current_donor.blood_type;
cd.priority = priority_score;
compatible_list.push_back(cd);
}
}
sort(compatible_list.begin(), compatible_list.end(), compareDonors);
return compatible_list;
}
int MatchPatientToDonor(int patient_id, vector<CompatibleDonor> compatible_list, vector<Donor>& blood_bank) {
if (compatible_list.size() == 0) {
return -1;
}
for (int i = 0; i < compatible_list.size(); i++) {
for (int j = 0; j < blood_bank.size(); j++) {
if (blood_bank[j].id == compatible_list[i].donor_id && blood_bank[j].available) {
blood_bank[j].available = false;
blood_bank[j].assigned_to = patient_id;
return blood_bank[j].id;
}
}
}
return -1;
}
void AllocateBlood(vector<Patient>& patients, vector<Donor>& blood_bank) {
sort(patients.begin(), patients.end(), comparePatients);
int successful = 0;
for (int i = 0; i < patients.size(); i++) {
vector<CompatibleDonor> compatible_list = FindCompatibleDonors(patients[i], blood_bank);
if (compatible_list.size() == 0) {
patients[i].match_status = "NO_MATCH";
continue;
}
int matched_donor_id = MatchPatientToDonor(patients[i].id, compatible_list, blood_bank);
if (matched_donor_id != -1) {
patients[i].assigned_donor_id = matched_donor_id;
patients[i].match_status = "SUCCESS";
successful++;
} else {
patients[i].match_status = "NO_MATCH";
}
}
cout << "Total patients: " << patients.size() << endl;
cout << "Successfully matched: " << successful << endl;
cout << "Unmatched: " << (patients.size() - successful) << endl;
cout << fixed << setprecision(1) << (100.0 * successful / patients.size()) << "%" << endl;
}
void PrintCompatibilityMatrix() {
for (int i = 0; i < 8; i++) {
cout << setw(4) << index_to_blood[i];
}
cout << endl;
for (int i = 0; i < 8; i++) {
cout << setw(12) << index_to_blood[i];
for (int j = 0; j < 8; j++) {
if (compatible[i][j]) {
cout << setw(4) << "v";
} else {
cout << setw(4) << "x";
}
}
cout << endl;
}
cout << endl;
}
int main() {
setupSystem();
PrintCompatibilityMatrix();
vector<Donor> blood_bank;
blood_bank.push_back(createDonor(101, "O-", 2));
blood_bank.push_back(createDonor(102, "O+", 3));
blood_bank.push_back(createDonor(103, "A-", 1));
blood_bank.push_back(createDonor(104, "A+", 2));
blood_bank.push_back(createDonor(105, "B-", 1));
blood_bank.push_back(createDonor(106, "B+", 2));
blood_bank.push_back(createDonor(107, "AB-", 1));
blood_bank.push_back(createDonor(108, "AB+", 2));
vector<Patient> patients;
patients.push_back(createPatient(1, "O-", 10, 1));
patients.push_back(createPatient(2, "AB+", 8, 1));
patients.push_back(createPatient(3, "A+", 5, 2));
patients.push_back(createPatient(4, "B-", 7, 1));
patients.push_back(createPatient(5, "AB-", 3, 1));
AllocateBlood(patients, blood_bank);
for (int i = 0; i < patients.size(); i++) {
cout << "Patient " << patients[i].id << ": ";
if (patients[i].match_status == "SUCCESS") {
cout << patients[i].assigned_donor_id << endl;
} else {
cout << "NO MATCH" << endl;
}
}
cout << endl;
for (int i = 0; i < blood_bank.size(); i++) {
cout << "Donor " << blood_bank[i].id << ": ";
if (blood_bank[i].available) {
cout << "AVAILABLE" << endl;
} else {
cout << blood_bank[i].assigned_to << endl;
}
}
return 0;
}