-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathzone.cpp
531 lines (448 loc) · 18.5 KB
/
zone.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#include <fstream>
#include <map>
#include <iomanip>
#include <set>
#include "student.h"
#include "constants_and_globals.h"
//==========================================================================
bool is_lefty_seat(const std::string& s) {
return s.back() == 'L';
}
class ZoneInfo {
public:
ZoneInfo() { max = 0; count = 0; }
std::string building;
std::string room;
std::string zone;
std::string image_url;
int max;
int count;
bool take_seat(std::string r, std::string s) {
assert (count < max);
std::vector<std::pair<std::string,std::string> >* available_seats;
if (is_lefty_seat(s)) {
available_seats = &available_lefty_seats;
} else {
available_seats = &available_nonlefty_seats;
}
for (std::vector<std::pair<std::string,std::string> >::iterator itr = available_seats->begin();
itr != available_seats->end(); itr++) {
if (itr->first == r && itr->second == s) {
available_seats->erase(itr);
count++;
return true;
}
}
return false;
}
void assign_seat(std::string &r, std::string &s, bool is_lefty) {
if (num_available_seats() == 0) {
r = "N/A";
s = "N/A";
count++;
return;
}
std::vector<std::pair<std::string,std::string> >* available_seats;
if (is_lefty) {
available_seats = &available_lefty_seats;
} else {
available_seats = &available_nonlefty_seats;
}
assert (available_seats->size() > 0);
assert (count < max);
int random_seat = std::rand()%available_seats->size();
std::vector<std::pair<std::string,std::string> >::iterator itr = available_seats->begin() + random_seat;
assert (itr != available_seats->end());
r = itr->first;
s = itr->second;
available_seats->erase(itr);
count++;
}
void clear_seats() {
available_lefty_seats.clear();
available_nonlefty_seats.clear();
}
int num_available_seats() {
return available_lefty_seats.size() + available_nonlefty_seats.size();
}
int num_lefty_seats() {
return available_lefty_seats.size();
}
void add_seat(const std::string &r, const std::string &s) {
if (is_lefty_seat(s)) {
available_lefty_seats.push_back(std::make_pair(r,s));
} else {
available_nonlefty_seats.push_back(std::make_pair(r,s));
}
}
private:
std::vector<std::pair<std::string,std::string> > available_lefty_seats;
std::vector<std::pair<std::string,std::string> > available_nonlefty_seats;
};
// random generator function:
int myrandomzone (int i) { return std::rand()%i;}
//==========================================================================
// More intuitive sort function to organize zones first by length (#
// of characters), then alphabetical.
class ZoneSorter {
public:
bool operator()(const std::string& a, const std::string& b) const {
return (a.size() < b.size() ||
(a.size() == b.size() && a < b));
}
};
int CountLefties(std::vector<Student*> &students,const std::string& lefty_file, float /*min_for_assignment*/) {
if (lefty_file == "") return 0;
std::ifstream istr(lefty_file.c_str());
assert (istr.good());
int count = 0;
std::string user,handedness;
while (istr >> user >> handedness) {
Student *s = GetStudent(students,user);
assert (s != NULL);
if (handedness == "left") {
//std::cout << "LEFTY " << user << std::endl;
s->setLefty();
if (s->getExamRoom() != "") {
//std::cout << "ALREADY ASSIGNED ROOM " << user << std::endl;
} else if (s->getRegistrationStatus() != "graded") {
// skipping
} else if (s->overall() < GLOBAL_MIN_OVERALL_FOR_ZONE_ASSIGNMENT) {
//std::cout << "NOT ASSIGNING LEFTY " << user << std::endl;
} else {
//std::cout << "NEED SEAT FOR LEFTY " << user << std::endl;
count++;
}
}
}
return count;
}
void LoadExamSeatingFile(const std::string &zone_counts_filename,
const std::string &zone_assignments_filename,
const std::string &/*seating_spacing*/,
const std::string &left_right_handedness,
std::vector<Student*> &students) {
std::cout << "zone counts filename '" << zone_counts_filename << "'" << std::endl;
std::cout << "zone assignments filename '" << zone_assignments_filename << "'" << std::endl;
std::cout << "left_right_handedness '" << left_right_handedness << "'" << std::endl;
std::cout << "min overall " << GLOBAL_MIN_OVERALL_FOR_ZONE_ASSIGNMENT << std::endl;
assert (zone_counts_filename != "");
assert (zone_assignments_filename != "");
int num_lefties = CountLefties(students,left_right_handedness, GLOBAL_MIN_OVERALL_FOR_ZONE_ASSIGNMENT);
std::cout << "NUM LEFTIES " << num_lefties << std::endl;
// ============================================================
// read in the desired zone counts
std::map<std::string,ZoneInfo,ZoneSorter> zones;
std::ifstream istr_zone_counts(zone_counts_filename.c_str());
assert (istr_zone_counts.good());
int total_seats = 0;
ZoneInfo zi;
std::string line;
int lefty_desk_count = 0;
std::string token;
while (1) {
std::getline(istr_zone_counts,line);
if (istr_zone_counts.eof()) break;
//std::cout << "LINE " << line << std::endl;
if (line[0] == '#') continue;
if (line == "SKIP") break;
if (line.size() == 0) continue;
std::stringstream ss(line);
if (line.substr(0,4) == "zone") {
// process prev zone
if (zi.zone != "") {
if (zi.max == -1) {
zi.max = zi.num_available_seats();
total_seats += zi.max;
}
if (zi.num_available_seats() != 0 &&
zi.max != zi.num_available_seats()) {
std::cout << "AVAILABLE SEATS FOR ZONE " << zi.zone << " are incorrect " <<
zi.max << " max vs " << zi.num_available_seats() << " available" << std::endl;
exit(1);
}
zones.insert(std::make_pair(zi.zone,zi));
}
if (!(ss >> token >> zi.zone >> zi.building >> zi.room >> zi.max)) {
std::cout << "MISFORMATTED ZONE COUNTS" << std::endl; exit(1);
}
assert (token == "zone");
ss >> zi.image_url;
//std::cout << "ZONE " << zi.zone << std::endl;
zi.count=0;
zi.clear_seats();
if (zones.find(zi.zone) != zones.end()) {
std::cerr << "\nERROR: duplicate zone " << zi.zone << " in " << zone_counts_filename << std::endl;
exit(0);
}
if (zi.max != -1) {
assert (zi.max >= 0);
total_seats += zi.max;
}
} else {
assert (line.substr(0,3) == "row");
ss >> token;
assert (token == "row");
std::string row;
ss >> row >> token;
assert (token == ":");
//std::cout << "ROW " << row << std::endl;
std::string seat;
std::vector<std::string> all_seats;
while (ss >> seat) {
//std::cout << "SEAT READ " << seat << std::endl;
all_seats.push_back(seat);
}
//std::cout << "seat count " << all_seats.size() << std::endl;
bool skip = false;
for (std::vector<std::string>::size_type i = 0; i < all_seats.size(); i++) {
seat = all_seats[i];
//std::cout << "i is " << i << " " << seat << std::endl;
//std::cout << "SEAT " << seat << std::endl;
if (seat.back() == 'X') {
//std::cout << "BROKEN DESK " << zi.zone << " "<< row << " " << seat << std::endl;
skip = false;
continue;
}
if (skip) {
skip = false;
//std::cout << "SKIPPING " << zi.zone << " "<< row << " " << seat << std::endl;
continue;
}
if (seat.back() == 'L') {
//std::cout << "LEFTY DESK " << zi.zone << " " << row << " " << seat << std::endl;
lefty_desk_count++;
}
skip = true;
//std::cout << "USE SEAT " << seat << std::endl;
zi.add_seat(row,all_seats[i]);
}
}
}
// process last zone
if (zi.zone != "") {
if (zi.max == -1) {
zi.max = zi.num_available_seats();
total_seats += zi.max;
}
if (zi.num_available_seats() != 0 &&
zi.max != zi.num_available_seats()) {
std::cout << "AVAILABLE SEATS FOR ZONE " << zi.zone << " are incorrect " <<
zi.max << " max vs " << zi.num_available_seats() << " available" << std::endl;
exit(1);
}
zones.insert(std::make_pair(zi.zone,zi));
}
std::cout << "TOTAL SEATS FOR EXAM " << total_seats << std::endl;
std::cout << "LEFTY DESK COUNT " << lefty_desk_count << std::endl;
// if we don't have specific seating assignments in this zone,
// fill in with generic seats
for (std::map<std::string,ZoneInfo,ZoneSorter>::iterator itr = zones.begin(); itr != zones.end(); itr++) {
int max = itr->second.max;
int na = itr->second.num_available_seats();
assert (max >= na);
for (int i = 0; i < max-na; i++) {
itr->second.add_seat("N/A","N/A");
}
}
// ============================================================
// read in any existing assignments...
std::cout << "READING " << zone_assignments_filename << std::endl;
int existing_assignments = 0;
{
std::ifstream istr_zone_assignments(zone_assignments_filename.c_str());
if (istr_zone_assignments.good()) {
std::string line;
while (getline(istr_zone_assignments,line)) {
std::stringstream ss(line.c_str());
std::string token,last,first,rcs,section,building,room,zone,row,seat,time;
ss >> last >> first >> rcs >> section >> building >> room >> zone >> row >> seat >> time;
while (ss >> token) { time += " " + token; }
//std::cout << "FOUND " << rcs << std::endl;
if (last == "") break;
Student *s = GetStudent(students,rcs);
if (s == NULL) {
std::cout << "seating assignment... couldn't find this userid " << rcs << std::endl;
}
assert (s != NULL);
if (zone != "") {
std::map<std::string,ZoneInfo>::iterator itr = zones.find(zone);
if (itr == zones.end()) {
std::cerr << "ERROR! this zone '" << zone << "' assigned to '" << s->getUserName() << "'does not exist!" << std::endl;
exit(1);
}
if (itr->second.max <= itr->second.count) {
std::cerr << "ERROR! this zone '" << zone << "' is full (max:" << itr->second.max << ")" << std::endl;
exit(1);
}
std::cout << itr->second.building << " " << building << std::endl;
assert (itr->second.building == building);
assert (itr->second.room == room);
bool success = itr->second.take_seat(row,seat);
if (!success) {
std::cout << "ERROR COULD NOT TAKE SEAT " << zone << " row:" << row
<< " seat:" << seat << std::endl;
exit(1);
}
existing_assignments++;
s->setExamRoom(room);
s->setExamBuilding(building);
s->setExamZone(zone,row,seat);
s->setExamZoneImage(itr->second.image_url);
if (time != "") {
s->setExamTime(time);
} else {
s->setExamTime(GLOBAL_EXAM_TIME);
}
}
}
}
}
std::cout << "EXISTING ASSIGNMENTS " << existing_assignments << std::endl;
// ============================================================
// make a vector of available seats
// FIXME: this belongs once, at start of program
std::srand ( unsigned ( std::time(0) ) );
std::vector<std::string> randomized_lefty_available;
std::vector<std::string> randomized_nonlefty_available;
for (std::map<std::string,ZoneInfo>::iterator itr = zones.begin(); itr != zones.end(); itr++) {
assert (itr->second.count <= itr->second.max);
int num_l = itr->second.num_lefty_seats();
int num_nl = itr->second.num_available_seats() - num_l;
for (int i = 0; i < num_l; i++) {
randomized_lefty_available.push_back(itr->first);
}
for (int i = 0; i < num_nl; i++) {
randomized_nonlefty_available.push_back(itr->first);
}
}
std::cout << "AVAILABLE LEFTY SEATS " << randomized_lefty_available.size() << std::endl;
std::cout << "AVAILABLE NONLEFTY SEATS " << randomized_nonlefty_available.size() << std::endl;
std::random_shuffle ( randomized_lefty_available.begin(), randomized_lefty_available.end(), myrandomzone );
std::random_shuffle ( randomized_nonlefty_available.begin(), randomized_nonlefty_available.end(), myrandomzone );
// ============================================================
// do the assignments!
int not_reg = 0;
int low_overall_grade = 0;
int new_zone_assign = 0;
int already_zoned = 0;
int next_lefty_za = 0;
int next_nonlefty_za = 0;
for (unsigned int i = 0; i < students.size(); i++) {
Student* &s = students[i];
if (s->getExamRoom() != "") {
already_zoned++;
} else if (!validSection(s->getSection())) {
not_reg++;
} else if (s->getRegistrationStatus() != "graded") {
not_reg++;
} else if (s->overall() < GLOBAL_MIN_OVERALL_FOR_ZONE_ASSIGNMENT) {
low_overall_grade++;
} else if (OmitSectionFromStats(s->getSection())) {
std::cout << "SKIP SECTION " << s->getSection() << " USER " << s->getUserName() << std::endl;
low_overall_grade++;
} else {
if(s->getExamTime().empty()){
s->setExamTime(GLOBAL_EXAM_TIME);
}
if (s->getLefty()) {
if (next_lefty_za >= (int)randomized_lefty_available.size()) {
std::cout << "OOPS! we ran out of lefty exam seating" << std::endl;
}
assert (next_lefty_za < (int)randomized_lefty_available.size());
ZoneInfo &next_zi = zones.find(randomized_lefty_available[next_lefty_za])->second;
s->setExamRoom(next_zi.room);
s->setExamBuilding(next_zi.building);
std::string row,seat;
//std::cout << "ASSIGN student " << s->getUserName() << std::endl;
next_zi.assign_seat(row,seat,s->getLefty());
s->setExamZone(next_zi.zone,row,seat);
s->setExamZoneImage(next_zi.image_url);
next_lefty_za++;
new_zone_assign++;
} else {
if (next_nonlefty_za >= (int)randomized_nonlefty_available.size()) {
std::cout << "OOPS! we ran out of nonlefty exam seating" << std::endl;
}
assert (next_nonlefty_za < (int)randomized_nonlefty_available.size());
ZoneInfo &next_zi = zones.find(randomized_nonlefty_available[next_nonlefty_za])->second;
s->setExamRoom(next_zi.room);
s->setExamBuilding(next_zi.building);
std::string row,seat;
//std::cout << "ASSIGN student " << s->getUserName() << std::endl;
next_zi.assign_seat(row,seat,s->getLefty());
s->setExamZone(next_zi.zone,row,seat);
s->setExamZoneImage(next_zi.image_url);
next_nonlefty_za++;
new_zone_assign++;
}
}
}
std::cout << "new zone assignments " << new_zone_assign << std::endl;
std::cout << "low overall grade (not assigning a zone) " << low_overall_grade << std::endl;
std::cout << "not registered in valid section " << not_reg << std::endl;
assert (new_zone_assign <=
int(randomized_lefty_available.size()) +
int(randomized_nonlefty_available.size()));
// ============================================================
// write out the assignments!
if (1) { //new_zone_assign > 0) {
std::ofstream ostr_zone_assignments(zone_assignments_filename.c_str());
assert (ostr_zone_assignments.good());
for (unsigned int i = 0; i < students.size(); i++) {
Student* &s = students[i];
if (s->getLastName() == "") continue;
std::string f = s->getPreferredFirstName();
std::string l = s->getPreferredLastName();
std::replace( f.begin(), f.end(), ' ', '_');
std::replace( l.begin(), l.end(), ' ', '_');
ostr_zone_assignments << std::setw(20) << std::left << l << " ";
ostr_zone_assignments << std::setw(15) << std::left << f << " ";
ostr_zone_assignments << std::setw(12) << std::left << s->getUserName() << " ";
/*if (s->getSection() != "null")
ostr_zone_assignments << std::setw(12) << std::left << s->getSection() << " ";
else
ostr_zone_assignments << std::setw(12) << std::left << "" << " ";*/
ostr_zone_assignments << std::setw(12) << std::left << s->getSection() << " ";
ostr_zone_assignments << std::setw(10) << std::left << s->getExamBuilding() << " ";
ostr_zone_assignments << std::setw(10) << std::left << s->getExamRoom() << " ";
ostr_zone_assignments << std::setw(10) << std::left << s->getExamZone() << " ";
ostr_zone_assignments << std::setw(10) << std::left << s->getExamRow() << " ";
ostr_zone_assignments << std::setw(10) << std::left << s->getExamSeat() << " ";
ostr_zone_assignments << std::setw(10) << std::left << s->getExamTime();
ostr_zone_assignments << std::endl;
}
}
// ============================================================
// data for preparing exams
std::map<std::string,std::map<std::string,std::set<std::string> > > new_order;
for (std::map<std::string,ZoneInfo>::iterator itr = zones.begin();
itr != zones.end(); itr++) {
std::cout << "ADD ZONE " << itr->second.building << " " << itr->second.room << " " << itr->first << std::endl;
new_order[itr->second.building][itr->second.room].insert(itr->first);
}
int total_assignments = 0;
for (std::map<std::string,std::map<std::string,std::set<std::string> > >::iterator no_itr = new_order.begin();
no_itr != new_order.end();
no_itr++) {
for (std::map<std::string,std::set<std::string> >::iterator no_itr2 = no_itr->second.begin();
no_itr2 != no_itr->second.end();
no_itr2++) {
int room_total = 0;
for (std::set<std::string>::iterator no_itr3 = no_itr2->second.begin();
no_itr3 != no_itr2->second.end();
no_itr3++) {
std::map<std::string,ZoneInfo>::iterator itr = zones.find(*no_itr3);
assert (itr != zones.end());
std::cout << "ZONE " << std::left << std::setw(8) << itr->first
<< " " << std::left << std::setw(10) << itr->second.building << " "
<< " " << std::left << std::setw(4) << itr->second.room << " "
<< " " << std::right << std::setw(4) << itr->second.count << " (" << itr->second.max-itr->second.count << " seats remain)" << std::endl;
room_total += itr->second.count;
total_assignments += itr->second.count;
}
std::cout << "ROOM TOTAL: " << room_total << "\n" << std::endl;
}
}
std::cout << "OVERALL TOTAL: " << total_assignments << std::endl;
}