-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution_parallel.cpp
295 lines (241 loc) · 8.8 KB
/
solution_parallel.cpp
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <iostream>
#include <algorithm>
#include <vector>
#include <time.h>
#include <omp.h>
#include <semaphore.h>
using namespace std;
const int no_of_days = 7;
const int no_of_nurses = 10;
const int population_size = 100;
const int penalty[] = {250, 200, 50, 150, 100, 300};
const int no_of_nurses_class_A = 5;
/*
M : Morning Shift (8:00 am to 2:00 pm)
E : Evening Shift (2:00 pm to 8:00 pm)
N : Night Shift (8:00 pm to 8:00 am)
H : Holiday
Classes of Nurses:
A : Most Skilled (4 Nurses)
B : Intermediate Skill Level (3 Nurses)
C : Least Skilled (3 Nurses)
Constraints :
1. There must be exactly one holiday in a week for every nurse (Penalty: 250)
2. Certain shift-patterns are not allowed (Penalty: 200)
i. Morning shift after night shift
ii. Evening shift after night shift
3. There must be at least one and at most two night shifts in a week for every nurse (Penalty: 50)
4. There must be at least one nurse of class A in every morning and evening shift (Penalty: 150)
5. There must be exactly one nurse of class A in night shift (Penalty: 100)
6. There must be at least one nurse allocated to every shift in every day (Penalty: 300)
*/
/*
Valid Shifts
*/
const char valid_duty[4] = {'M', 'E', 'N', 'H'};
/*
randomly generates a shift
*/
char generate_duty() {
int r = rand()%4;
return valid_duty[r];
}
/*
generates a random schedule
*/
void generate_random_schedule(char (&arr) [no_of_nurses][no_of_days]) {
for (int i=0; i<no_of_nurses; i++)
for (int j=0; j<no_of_days; j++)
arr[i][j] = generate_duty();
}
/*
class to represent schedule and its associated functionalites
*/
class schedule {
public:
char arr[no_of_nurses][no_of_days];
int fitness;
schedule(char (&a)[no_of_nurses][no_of_days]);
int calculate_fitness();
schedule mate(schedule &parent2);
};
/*
Creates object of the schedule from a given 2-D array
*/
schedule :: schedule(char (&a) [no_of_nurses][no_of_days])
{
for (int i=0; i<no_of_nurses; i++)
for (int j=0; j<no_of_days; j++)
arr[i][j] = a[i][j];
fitness = calculate_fitness();
}
/*
Calculates fitness score for the corresponding schedule
*/
int schedule :: calculate_fitness()
{
int fitness = 0;
for (int i=0; i<no_of_nurses; i++) {
int H = 0; // Stores number of holidays for the ith nurse in a week
int N = 0; // Stores number of night shifts for the ith nurse in a week
int false_patterns = 0; // Stores number of forbidden pattern in a week
for (int j=0; j<no_of_days; j++) {
if (arr[i][j] == 'H')
H++;
else if (arr[i][j] == 'N') {
N++;
if (arr[i][(j+1)%no_of_days] == 'M' || arr[i][(j+1)%no_of_days] == 'E')
false_patterns++;
}
}
if (H!=1)
fitness += abs(H-1)*penalty[0];
fitness += false_patterns * penalty[1];
if (N != 1 && N != 2) {
if (N)
fitness += (N-2)*penalty[2];
else
fitness += penalty[2];
}
}
for (int j=0; j<no_of_days; j++) {
int N = 0; // Stores number of nurses having night shifts on jth day
int M = 0; // Stores number of nurses having morning shifts on jth day
int E = 0; // Stores number of nurses having evening shifts on jth day
int classA_in_M = 0; // Stores number of nurses of class A having morning shifts on jth day
int classA_in_E = 0; // Stores number of nurses of class A having evening shifts on jth day
int classA_in_N = 0; // Stores number of nurses of class A having night shifts on jth day
for (int i = 0; i<no_of_nurses; i++) {
if (arr[i][j] == 'N') {
N++;
if (i<no_of_nurses_class_A)
classA_in_N++;
} else if (arr[i][j] == 'M') {
M++;
if (i<no_of_nurses_class_A)
classA_in_M++;
} else if (arr[i][j] == 'E'){
E++;
if (i<no_of_nurses_class_A)
classA_in_E++;
}
}
if (M == 0)
fitness += penalty[5];
if (E == 0)
fitness += penalty[5];
if (N == 0)
fitness += penalty[5];
if (classA_in_M == 0)
fitness += penalty[3];
if (classA_in_E == 0)
fitness += penalty[3];
if (classA_in_N != 1)
fitness += abs(classA_in_N-1)*penalty[4];
}
return fitness;
}
/*
Generates child schedule from tow parent schedules
*/
schedule schedule :: mate(schedule &parent2)
{
char child[no_of_nurses][no_of_days];
for (int i=0; i<no_of_nurses; i++) {
for (int j=0; j<no_of_days; j++) {
int r = rand()%100;
if (r < 45)
child[i][j] = arr[i][j];
else if (r < 90)
child[i][j] = parent2.arr[i][j];
else
child[i][j] = generate_duty();
}
}
return schedule(child);
}
/*
Overloads '<' operator as it will be further used in sorting
*/
bool operator< (const schedule &S1, const schedule &S2) {
return S1.fitness < S2.fitness;
}
int main()
{
clock_t t1 = clock();
srand((unsigned)(time(0)));
//bool found = false;
vector <schedule> population;
sem_t mutex;
sem_init(&mutex, 1, 1);
#pragma omp parallel for
for (int i=0; i<population_size; i++) {
char a[no_of_nurses][no_of_days];
generate_random_schedule(a);
sem_wait(&mutex);
population.push_back(schedule(a));
sem_post(&mutex);
}
for (int k=0; k<1; k++) {
// cout << "--------Generation- Parent---------------Schedule------------------" << endl;
// for (int i=0; i<no_of_nurses; i++) {
// for (int j=0; j<no_of_days; j++) {
// cout << population[k].arr[i][j] << "\t";
// }
// cout << endl;
// }
// cout << endl << endl;
cout << "Fitness Value: " << population[k].fitness << endl;
}
// for (int k=3; k<population_size; k++) {
// cout << "Fitness Value: " << population[k].fitness << endl;
// }
int generation = 0;
while(true) {
sort(population.begin(), population.end());
if (population[0].fitness < 1)
break;
vector <schedule> new_generation;
int s = 10*population_size/100;
for (int i=0; i<s; i++)
new_generation.push_back(population[i]);
s = 90*population_size/100;
#pragma omp parallel for
for (int i=0; i<s; i++) {
int r1 = rand()%(population_size/2);
int r2 = rand()%(population_size/2);
schedule S = population[r1].mate(population[r2]);
sem_wait(&mutex);
new_generation.push_back(S);
sem_post(&mutex);
}
#pragma omp parallel for
for (int i=0; i<population_size; i++) {
for (int j=0; j<no_of_nurses; j++) {
for (int k = 0; k<no_of_days; k++)
population[i] = new_generation[i];
}
}
cout << "--------Generation-" << generation << "---------------Schedule------------------" << endl;
// for (int i=0; i<no_of_nurses; i++) {
// for (int j=0; j<no_of_days; j++) {
// cout << population[0].arr[i][j] << "\t";
// }
// cout << endl;
// }
cout << "Fitness Value: " << population[0].fitness << endl;
cout << endl << endl;
generation++;
}
cout << "------------------Optimized Schedule--------------------" << endl;
for (int i=0; i<no_of_nurses; i++) {
for (int j=0; j<no_of_days; j++) {
cout << population[0].arr[i][j] << "\t";
}
cout << endl;
}
cout << "Optimal Fitness : " << population[0].fitness << endl;
clock_t t2 = clock();
cout << "Time Taken : " << (t2-t1)/CLOCKS_PER_SEC << "s" << endl;
return 0;
}