-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroupAssigner.py
877 lines (803 loc) · 35.7 KB
/
groupAssigner.py
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
import collections
import json
from datetime import datetime
from copy import deepcopy
from collections import defaultdict
import random
import math
import numpy as np
import pandas as pd
from typing import Dict, Generic, Iterator, List, Optional, TypeVar # For the priority queue
from fpdf import FPDF # For pdfs
import pytz # for timezones
Key = TypeVar("Key")
class MaxPQ(Generic[Key]):
"""
Pretty much taken from here: https://github.itu.dk/algorithms/AlgorithmsInPython/blob/master/itu/algs4/sorting/max_pq.py
"""
def __init__(self, _max: int = 200):
self._pq: List[Optional[Key]] = [None] * (_max + 1)
self._n = 0
def insert(self, x: Key) -> None:
if self._n == len(self._pq) - 1:
self._resize(2 * len(self._pq))
self._n += 1
self._pq[self._n] = x
self._swim(self._n)
def max(self) -> Key:
if self.is_empty():
return 0
assert self._pq[1] is not None
return self._pq[1]
def del_max(self) -> Key:
_max = self._pq[1]
assert _max is not None
self._exch(1, self._n)
self._n -= 1
self._sink(1)
self._pq[self._n + 1] = None
if self._n > 0 and self._n == (len(self._pq) - 1) // 4:
self._resize(len(self._pq) // 2)
return _max
def is_empty(self) -> bool:
return self._n == 0
def size(self) -> int:
return self._n
def __len__(self) -> int:
return self.size()
def _sink(self, k) -> None:
while 2 * k <= self._n:
j = 2 * k
if j < self._n and self._less(j, j + 1):
j += 1
if not self._less(k, j):
break
self._exch(k, j)
k = j
def _swim(self, k: int) -> None:
while k > 1 and self._less(k // 2, k):
self._exch(k, k // 2)
k = k // 2
def _resize(self, capacity: int):
temp: List[Optional[Key]] = [None] * capacity
for i in range(1, self._n + 1):
temp[i] = self._pq[i]
self._pq = temp
def _less(self, i: int, j: int):
return self._pq[i][1] < self._pq[j][1]
def _exch(self, i: int, j: int):
self._pq[i], self._pq[j] = self._pq[j], self._pq[i]
def __iter__(self) -> Iterator[Key]:
"""Iterates over all the items in this priority queue in heap order."""
copy: MaxPQ[Key] = MaxPQ(self.size())
for i in range(1, self._n + 1):
key = self._pq[i]
assert key is not None
copy.insert(key)
for i in range(1, copy._n + 1):
yield copy.del_max()
def basicPr():
return 100000000 # So sorting work when someone hasn't competed
class Competitor():
def __init__(self,name):
self.name = name
self.events = set()
self.prs = defaultdict(basicPr)
self.availableDuring = set() # a set of events where they will be in the venue
self.orga = 1 # for calculation. Actual orga get 3, for the time being
self.groups = {} # Event -> groupnum
self.assignments = defaultdict(list)
self.age = 0
self.totalAssignments = 1 # so addition works
def __str__(self):
return self.name + " + info"
class Schedule():
def __init__(self):
self.name = ''
self.timezone = ''
self.events = [] # list of lists. Inner lists have three values: Event name, s time, and e time of r1.
self.eventWOTimes = []
self.eventTimes = {} # event -> touple of start and end time
self.eventCompetitors = defaultdict(list)
self.daySplit = [0] # the index where a day changes. Len = days-1
self.groups = {} # event -> groupnum -> group
self.groupJudges = {} # event -> groupnum -> group. Made later
self.groupRunners = {} # Will be event -> groupnum -> group. Made later
self.groupScramblers = {} # Will be event -> groupnum -> group. Made later
self.inVenue = defaultdict(set) # event -> set of people in venue
self.unpred = set() # I didn't use this, but was planning on using it to account for some people not being present for all individual attempts for certain events.
self.overlappingEvents = defaultdict(list) # Event -> list of events happening during the timespan of it.
self.groupTimes = {} # event -> groupnum -> time
self.organizers = None # List of organizers and delegates
def order(self): # ordering events in schedule
self.events.sort(key=lambda x:x[1])
def orderCompetitors(self,personInfo,combinedEvents): # For scrambling
for event in self.eventCompetitors:
if event == combinedEvents:
for person in personInfo:
comSplit = event.split('-')
personInfo[person].prs[combinedEvents] = personInfo[person].prs[comSplit[0]] + personInfo[person].prs[comSplit[1]]
self.eventCompetitors[event].sort(key=lambda x:personInfo[x].prs[event]*personInfo[x].orga)
def getIndividualGroupTimes(self):
for event in self.groups:
self.groupTimes[event] = {}
amountOfGroups = len(self.groups[event])
diff = self.eventTimes[event][1] - self.eventTimes[event][0]
perGroup = diff/amountOfGroups
for groupNum in self.groups[event]:
self.groupTimes[event][groupNum] = (self.eventTimes[event][0]+ (perGroup*(groupNum-1)),self.eventTimes[event][0]+ (perGroup*(groupNum)))
# self.groupTimes[event][groupNum] = ("tid 1", "tid 2")
def getDaySplit(self):
for i in range(1,len(self.events)):
if self.events[i][1].day == self.events[i-1][1].day:
pass
else:
self.daySplit.append(i)
def eventTimeChecker(self, event1,event2):
if (event1[2] > event2[1] and event1[2] < event2[2]) or (event1[1] > event2[1] and event1[1] < event2[2]) or (event1[2] > event2[2] and event1[1] < event2[2]) or (event1[1] < event2[1] and event2[2] < event1[2]):
return True
else:
return False
# if I weren't lazy this should be the same function
def groupTimeChecker(self, event1,event2): # Group1 and group2
if (event1[1] > event2[0] and event1[1] < event2[1]) or (event1[0] > event2[0] and event1[0] < event2[1]) or (event1[1] > event2[1] and event1[0] < event2[1]) or (event1[0] < event2[0] and event2[1] < event1[1]):
return True
else:
return False
def identifyOverlap(self): # Which events overlap
for idx, event in enumerate(self.events):
for event2 in self.events[idx+1:]:
if self.eventTimeChecker(event,event2):
self.overlappingEvents[event[0]].append(event2[0])
self.overlappingEvents[event2[0]].append(event[0])
def competitorBasicInfo(data):
"""
Get all the basic information for each competitor.
"""
comp_dict = {}
year = int(datetime.now().strftime("%Y"))
organizers = set()
for person in data['persons']:
try:
if person['registration']['status'] == 'accepted':
competitor = Competitor(person["name"])
for val in person["roles"]: # getOrga
if val in ('delegate','organizer'):
competitor.orga = 3 # Setting this for sorting by speed
organizers.add(person['name'])
competitor.age = year - int(person["birthdate"][:4]) #getAge
for eventData in person['personalBests']:
if eventData['eventId'] not in ('333fm','444bf','333bf','555bf'):
if eventData['type'] == 'average':
competitor.prs[eventData['eventId']] = int(eventData['worldRanking'])
else:
if eventData['type'] == 'single':
competitor.prs[eventData['eventId']] = int(eventData['worldRanking'])
for event in person['registration']['eventIds']:
competitor.events.add(event)
comp_dict[person["name"]] = competitor
except TypeError:
pass
return comp_dict,organizers
def scheduleBasicInfo(data,personInfo,organizers,stations,customGroups=[False], combinedEvents=None): # Custom groups is a dict, combined evnets is touple
"""
Get all the basic information for the schedule.
Doesn't store which stage events appear on, but will look into if events overlap (but not fully)
"""
if combinedEvents==None:
combinedEvents = ('k','k')
schedule = Schedule()
schedule.name = data['id']
already_there = set()
timezone = pytz.timezone(data["schedule"]["venues"][0]["timezone"])
tempFm = [] # not used for its purpose in the end
tempMb = [] # not used for its purpose in the end
for room in data["schedule"]["venues"][0]['rooms']:
for val in room["activities"]:
starttime = pd.Timestamp(val['startTime'][:-1]).tz_localize(pytz.utc).tz_convert(timezone)
endtime = pd.Timestamp(val['endTime'][:-1]).tz_localize(pytz.utc).tz_convert(timezone)
if val['activityCode'][0] != 'o':
if len(val['activityCode']) < 9:
if val['activityCode'][-1] not in ['3','2','4'] and val['activityCode'][:-3] not in already_there:
tempCombined = val['activityCode'][:-3]
doo = True
if tempCombined == combinedEvents[0]:
tempCombined += '-'+combinedEvents[1]
elif tempCombined == combinedEvents[1]:
doo = False
if doo:
schedule.events.append([tempCombined,starttime,endtime])
schedule.eventWOTimes.append(tempCombined)
already_there.add(val['activityCode'][:-3])
schedule.eventTimes[tempCombined] = (starttime,endtime)
else:
if val['activityCode'][:4] == '333f' and val['activityCode'][-1] not in ['3','2','4']:
tempFm.append([val['activityCode'][:-6],starttime,endtime])
schedule.eventWOTimes.append('333fm')
schedule.eventTimes[val['activityCode'][:-6]] = (starttime,endtime)
elif val['activityCode'][:4] == '333m' and val['activityCode'][-1] not in ['3','2','4']:
tempMb.append([val['activityCode'][:-6],starttime,endtime])
schedule.eventWOTimes.append('333mbf')
schedule.eventTimes[val['activityCode'][:-6]] = (starttime,endtime)
if len(tempMb) <2: # not used for its purpose in the end
schedule.events += tempMb
else:
schedule.unpred.add("333mbf")
if len(tempFm) <2: # not used for its purpose in the end
schedule.events += tempFm
else:
schedule.unpred.add("333fm")
schedule.order() # Order the events by time in schedule
schedule.getDaySplit() # See which events are each day
for person in personInfo: # Counting the combined events as one
already =False
for event in personInfo[person].events:
if event in [combinedEvents[0],combinedEvents[1]] and not already:
schedule.eventCompetitors[combinedEvents[0]+'-'+combinedEvents[1]].append(person)
already =True
elif event not in [combinedEvents[0],combinedEvents[1]]:
schedule.eventCompetitors[event].append(person)
schedule.organizers = organizers # Storing list of organizers and delegates
schedule.timezone = timezone
schedule.orderCompetitors(personInfo,combinedEvents[0]+'-'+combinedEvents[1]) # Ordering competitors by rank (used in group making and getting scramblers)
schedule.identifyOverlap() # See which events overlap. Doesn't account full overlaps, i.e. for events with same start/ending time
getGroupCount(schedule,True,stations,customGroups,just1=['333fm','333mbf','444bf','555bf']) # Getting the amount of groups needed
schedule.getIndividualGroupTimes() # Seeing the start/end time of each group
getAvailableDuring(personInfo,schedule,combinedEvents) # Identify during which events people should be present based on their registration
return schedule
def getAvailableDuring(personInfo,scheduleInfo,combinedEvents=None):
"""
Identify during which events people should be present based on their registration.
People are considered to be available for an event if they compete in it, or if they are competing on that day
and have a registration for an event before and after the event.
"""
if combinedEvents==None:
combinedEvents = ('k','k')
combinedEvents1 = ('k-k')
else:
combinedEvents1 = combinedEvents[0]+'-'+combinedEvents[1]
for person in personInfo:
for idj, days in enumerate(scheduleInfo.daySplit):
min = 18
max = 0
if idj != len(scheduleInfo.daySplit)-1:
to = scheduleInfo.daySplit[idj+1]
else:
to = len(scheduleInfo.events)
for idx,event in enumerate(scheduleInfo.events[days:to]):
if event[0] in personInfo[person].events:
if idx < min:
min = idx
if idx > max:
max = idx
elif event[0] == combinedEvents1:
for comSplit in combinedEvents:
if comSplit in personInfo[person].events:
if idx < min:
min = idx
if idx > max:
max = idx
for event in scheduleInfo.events[days+min:days+max+1]:
personInfo[person].availableDuring.add(event[0])
scheduleInfo.inVenue[event[0]].add(person)
def combineEvents(event1,event2): # Pretty stupid function. The combined events is used super inconsistently
return (event1,event2)
def getGroupCount(scheduleInfo,fixedSeating,stationCount,custom=[False],just1=[False]):
"""
The script isn't made for specifying a different amount of stations per event.
Use the 'custom' variable to specify the exact amount of groups you want if there is something extraordinary
'just1' is when you only want one group of the event.
"""
if type(custom) == dict: # dictionary
for event in custom:
scheduleInfo.groups[event] = {}
for amount in range(1,custom[event]+1):
scheduleInfo.groups[event][amount] = []
if just1[0]:
for event in just1:
if event in scheduleInfo.eventWOTimes:
scheduleInfo.groups[event] = {}
scheduleInfo.groups[event][1] = []
if fixedSeating:
for event in scheduleInfo.eventCompetitors:
if event not in just1 and event not in custom:
scheduleInfo.groups[event] = {}
for amount in range(1,math.ceil(len(scheduleInfo.eventCompetitors[event])/stationCount) +1):
scheduleInfo.groups[event][amount] = []
else:
# stationCount *=1.15
for event in scheduleInfo.eventCompetitors:
if event not in just1:
scheduleInfo.groups[event] = {}
for amount in range(1,(np.max([math.floor(len(scheduleInfo.eventCompetitors[event])/stationCount) +1,3]))):
scheduleInfo.groups[event][amount] = []
def splitNonOverlapGroups(scheduleInfo,personInfo,event):
"""
Function called for events which do not have something overlapping.
In the regular assignments, sets aside scramblerCount scramblers for each group
"""
dontSpeedScramble = ('333bf','444bf','555bf') # In these events, do not pick aside some fast people to scramble other groups.
groups = scheduleInfo.groups[event]
totalComp = scheduleInfo.eventCompetitors[event]
perGroup = len(totalComp)/len(groups)
scramblerCount = round(1/7*perGroup)
# Special stuff to make sure people of orga team is not in the same group
orgaCompetitors = [compOrga for compOrga in scheduleInfo.organizers if compOrga in totalComp]
p2 = deepcopy(totalComp)
if len(orgaCompetitors) > 1 and len(groups) > 1: # For orga
orgaCompetitors.sort(key=lambda x:personInfo[x].prs[event], reverse=True)
part1 = orgaCompetitors[:math.ceil(len(orgaCompetitors)/2)]
part2 = orgaCompetitors[math.ceil(len(orgaCompetitors)/2):]
while len(part1) > 0: # Place slowest half orga in g1
comp = part1[0]
part1 = part1[1:]
groups[1].append(comp)
personInfo[comp].groups[event] = 1
p2.remove(comp)
while len(part2) > 0: # Place fastest half in g2
comp = part2[0]
part2 = part2[1:]
groups[2].append(comp)
personInfo[comp].groups[event] = 2
p2.remove(comp)
if event in dontSpeedScramble:
for groupNum in range(1,len(groups)+1):
while len(groups[groupNum]) < perGroup and len(p2) > 0: # Assigning slowest first
comp = p2[-1]
p2 = p2[:-1]
groups[groupNum].append(comp)
personInfo[comp].groups[event] = groupNum
while len(p2) > 0: # If some people were somehow left out, add them in the last group
comp = p2[-1]
p2 = p2[:-1]
groups[groupNum].append(comp)
personInfo[comp].groups[event] = groupNum
else:
for groupNum in range(1,len(groups)+1):
offsetScramblers = 0
for _ in range(1,scramblerCount+1): # taking best people, to ensure there are scramblers later (not all fast in same group)
comp = p2[offsetScramblers]
p2.remove(comp)
groups[groupNum].append(comp)
personInfo[comp].groups[event] = groupNum
offsetScramblers +=1
while len(groups[groupNum]) < perGroup and len(p2) > 0: # Assigning slowest first
comp = p2[-1]
p2 = p2[:-1]
groups[groupNum].append(comp)
personInfo[comp].groups[event] = groupNum
while len(p2) > 0: # If some people were somehow left out, add them in the last group
comp = p2[-1]
p2 = p2[:-1]
groups[groupNum].append(comp)
personInfo[comp].groups[event] = groupNum
def splitIntoOverlapGroups(scheduleInfo,personInfo,combination):
"""
Assigns groups for all overlapping events at the same time, and does assignments.
As I could not find a proper deterministic manner of getting judges and competitors,
I have set it to perform simulations. This should find the best combination.
It will print out if there were some mistake.
Failing to assign a person adds 100 to the fail score, a missing judge is 1.
"""
compByCount = [[] for _ in range(len(combination))]
all = []
for event in combination:
for person in scheduleInfo.eventCompetitors[event]:
all.append(person)
for person in collections.Counter(all):
compByCount[collections.Counter(all)[person]-1].append(person)
bsh2 = deepcopy(scheduleInfo)
bpes2 = deepcopy(personInfo)
few_fails = 100 # Default
for ii in range(100): #100 simulations
random.shuffle(combination)
if few_fails > 0:
sh2 = deepcopy(scheduleInfo)
pes2 = deepcopy(personInfo)
for val in compByCount:
random.shuffle(val)
random.shuffle(compByCount)
j = len(compByCount) -1
fails = 0
while j >= 0:
p2 = deepcopy(compByCount[j])
while p2:
for event in combination:
assigned = False
if p2[0] in sh2.eventCompetitors[event]:
groups = sh2.groups[event]
totalComp = sh2.eventCompetitors[event]
perGroup = len(totalComp)/len(groups)
groupNumList = [j for j in range(len(groups))]
random.shuffle(groupNumList)
for idy in groupNumList:
if not assigned:
if len(groups[idy+1]) < perGroup: # Making sure there is space in the group
checkLegal = True
for event2 in pes2[p2[0]].groups:
if event2 in combination:
if not sh2.groupTimeChecker(sh2.groupTimes[event][idy+1],sh2.groupTimes[event2][pes2[p2[0]].groups[event2]]):
pass # Check that they don't have an overlapping event
else:
checkLegal = False
if checkLegal:
sh2.groups[event][idy+1].append(p2[0])
pes2[p2[0]].groups[event] = idy+1
assigned = True
if not assigned:
# print(f"failed {p2[0]} for {event}")
fails +=1
p2 = p2[1:]
j -=1
missing = judgePQOverlap(combination,sh2,pes2) # Perform assignment of staff
score = fails*100 + missing
if score < few_fails: # If there is fewer missing staff
few_fails = score
bsh2 = deepcopy(sh2)
bpes2 = deepcopy(pes2)
scheduleInfo = deepcopy(bsh2)
personInfo = deepcopy(bpes2)
if few_fails > 0:
print(f"A total fail score for the overlapping events ({combination}) of {few_fails}")
else:
print(f'sucess in overlapping events ({combination})')
return scheduleInfo,personInfo # For some reason it does not update the variables
def splitIntoGroups(scheduleInfo,personInfo):
already = set()
for event in scheduleInfo.events:
if event[0] not in already:
if event[0] not in scheduleInfo.overlappingEvents:
splitNonOverlapGroups(scheduleInfo, personInfo, event[0])
already.add(event[0])
else: # Do one set of overlapping events
combination = set()
combination.add(event[0])
for i in range(4): # Should get all potential overlaps. Kind of BFS
tempSet = set()
for event1 in combination:
for toAdd in scheduleInfo.overlappingEvents[event1]:
tempSet.add(toAdd)
combination = combination.union(tempSet)
combination = list(combination) # For the sake of simulations
scheduleInfo, personInfo = splitIntoOverlapGroups(scheduleInfo, personInfo, combination) # For some reason it does not update the variables
combination = set(combination)
already = already.union(combination) # Don't repeat the same combo of overlaps
return scheduleInfo, personInfo # For some reason it does not update the variables
def judgePQNonOverlap(event,scheduleInfo,personInfo,fixedSeating=True): ## Needs fixing. Assigns judge multiple times in same event
if fixedSeating:
scheduleInfo.groupJudges[event] = {}
groups = scheduleInfo.groups[event]
competitors = scheduleInfo.eventCompetitors[event]
maybePeople = scheduleInfo.inVenue[event]
atleast1 = set() # Make sure everyone judges at least once before giving two assignments to other people
for groupNum in groups:
pq = MaxPQ()
scheduleInfo.groupJudges[event][groupNum] = []
needed = len(scheduleInfo.groups[event][groupNum]) + round(2/7*(len(scheduleInfo.groups[event][groupNum])))
used = set() # those that were already tried
for comp in competitors: # First, get only the people who haven't judged in the event
if comp not in scheduleInfo.organizers:
if comp not in scheduleInfo.groups[event][groupNum]:
if comp not in atleast1:
pq.insert([comp,math.log((len(personInfo[comp].events)))/personInfo[comp].totalAssignments])
while not pq.is_empty() and len(scheduleInfo.groupJudges[event][groupNum]) < needed:
judge = pq.del_max()[0]
personInfo[judge].totalAssignments +=1
personInfo[judge].assignments[event].append(groupNum)
scheduleInfo.groupJudges[event][groupNum].append(judge)
atleast1.add(judge)
used.add(judge)
if len(scheduleInfo.groupJudges[event][groupNum]) < needed:
for comp in competitors: # second try competitors in the event
if comp not in used and comp not in scheduleInfo.organizers:
if comp not in scheduleInfo.groups[event][groupNum]:
pq.insert([comp,math.log((len(personInfo[comp].events)))/personInfo[comp].totalAssignments])
while not pq.is_empty() and len(scheduleInfo.groupJudges[event][groupNum]) < needed:
judge = pq.del_max()[0]
personInfo[judge].totalAssignments +=1
personInfo[judge].assignments[event].append(groupNum)
scheduleInfo.groupJudges[event][groupNum].append(judge)
atleast1.add(judge)
used.add(judge)
if len(scheduleInfo.groupJudges[event][groupNum]) < needed: # If more people are needed, try all in the venue
for comp in maybePeople:
if comp not in used and not comp in scheduleInfo.groups[event][groupNum]:
if comp in scheduleInfo.organizers:
pq.insert([comp,0])
else:
pq.insert([comp,(math.log(len(personInfo[comp].events)))/personInfo[comp].totalAssignments])
while not pq.is_empty() and len(scheduleInfo.groupJudges[event][groupNum]) < needed: # Refactor later for scramblers and judges
judge = pq.del_max()[0]
personInfo[judge].totalAssignments +=1
personInfo[judge].assignments[event].append(groupNum)
scheduleInfo.groupJudges[event][groupNum].append(judge)
if len(scheduleInfo.groupJudges[event][groupNum]) < needed:
print(f"Not possible for {event} group {groupNum}. Got {len(scheduleInfo.groupJudges[event][groupNum])} of {needed}")
def judgePQOverlap(combination,scheduleInfo,personInfo,fixedSeating=True): ## Needs fixing. Assigns judge multiple times in same event
if fixedSeating:
missing = 0
for event in combination:
scheduleInfo.groupJudges[event] = {}
groups = scheduleInfo.groups[event]
competitors = scheduleInfo.eventCompetitors[event]
maybePeople = scheduleInfo.inVenue[event]
for groupNum in groups:
pq = MaxPQ()
scheduleInfo.groupJudges[event][groupNum] = []
needed = len(scheduleInfo.groups[event][groupNum]) + round(2/7*(len(scheduleInfo.groups[event][groupNum])))
used = set() # those that were already tried
for comp in competitors:
if comp not in scheduleInfo.organizers:
used.add(comp)
if comp not in scheduleInfo.groups[event][groupNum]: # Check they aren't competing in overlapping group
checkLegal = True
for event2 in personInfo[comp].groups:
if event2 in combination:
if not scheduleInfo.groupTimeChecker(scheduleInfo.groupTimes[event][groupNum],scheduleInfo.groupTimes[event2][personInfo[comp].groups[event2]]):
pass
else:
checkLegal = False
for event2 in personInfo[comp].assignments:# Checking for overlapping assignments
if event2 in combination:
for groupAssignment in personInfo[comp].assignments[event2]:
if not scheduleInfo.groupTimeChecker(scheduleInfo.groupTimes[event][groupNum],scheduleInfo.groupTimes[event2][groupAssignment]):
pass
else:
checkLegal = False
if checkLegal:
pq.insert([comp,(math.log(len(personInfo[comp].events)))/personInfo[comp].totalAssignments])
while not pq.is_empty() and len(scheduleInfo.groupJudges[event][groupNum]) < needed: # Refactor later for scramblers and judges
judge = pq.del_max()[0]
personInfo[judge].totalAssignments +=1
personInfo[judge].assignments[event].append(groupNum)
scheduleInfo.groupJudges[event][groupNum].append(judge)
if len(scheduleInfo.groupJudges[event][groupNum]) < needed: # If we didn't get enough first time, check people in veneu
for comp in maybePeople:
if comp not in used and not comp in scheduleInfo.groups[event][groupNum]:
checkLegal = True
for event2 in personInfo[comp].groups:
if event2 in combination:
if not scheduleInfo.groupTimeChecker(scheduleInfo.groupTimes[event][groupNum],scheduleInfo.groupTimes[event2][personInfo[comp].groups[event2]]):
pass
else:
checkLegal = False
for event2 in personInfo[comp].assignments:
if event2 in combination:
for groupAssignment in personInfo[comp].assignments[event2]:
if not scheduleInfo.groupTimeChecker(scheduleInfo.groupTimes[event][groupNum],scheduleInfo.groupTimes[event2][groupAssignment]):
pass
else:
checkLegal = False
if checkLegal:
if comp in scheduleInfo.organizers:
pq.insert([comp,0])
else:
pq.insert([comp,(math.log(len(personInfo[comp].events)))/personInfo[comp].totalAssignments])
while not pq.is_empty() and len(scheduleInfo.groupJudges[event][groupNum]) < needed: # Refactor later for scramblers and judges
judge = pq.del_max()[0]
personInfo[judge].totalAssignments +=1
personInfo[judge].assignments[event].append(groupNum)
scheduleInfo.groupJudges[event][groupNum].append(judge)
if len(scheduleInfo.groupJudges[event][groupNum]) < needed:
missing += needed-len(scheduleInfo.groupJudges[event][groupNum])
# print(f"Not possible for {event} group {groupNum}. Got {len(scheduleInfo.groupJudges[event][groupNum])} of {needed}")
return missing
def assignJudges(scheduleInfo,personInfo,fixedSeating= True,dontAssign=True):
if dontAssign: # Don't assign judges when there is only one group
for event in scheduleInfo.events:
if len(scheduleInfo.groups[event[0]]) > 1:
if event[0] not in scheduleInfo.overlappingEvents:
judgePQNonOverlap(event[0],scheduleInfo,personInfo,fixedSeating)
else:
for event in scheduleInfo.events:
if event[0] not in scheduleInfo.overlappingEvents:
judgePQNonOverlap(event[0],scheduleInfo,personInfo,fixedSeating)
def reassignJudges(scheduleInfo,personInfo):
for event in scheduleInfo.groups:
scheduleInfo.groupScramblers[event] = {}
scheduleInfo.groupRunners[event] = {}
for group in scheduleInfo.groups[event]:
scheduleInfo.groupScramblers[event][group] = []
scheduleInfo.groupRunners[event][group] = []
if event in scheduleInfo.groupJudges:
if len(scheduleInfo.groupJudges[event][group]) > 0: # If judges are assigned for the event
# Always at least one scrambler
scheduleInfo.groupJudges[event][group].sort(key=lambda x:personInfo[x].prs[event]*personInfo[x].orga)
best = scheduleInfo.groupJudges[event][group][0]
scheduleInfo.groupJudges[event][group] = scheduleInfo.groupJudges[event][group][1:]
scheduleInfo.groupScramblers[event][group].append(best)
for idx,assignment in enumerate(personInfo[best].assignments[event]):
if assignment == group:
personInfo[best].assignments[event][idx] = f';S{group}' #Update assignment to scrambler
runSc = 1 # If divisible by 2 make scrambler, otherwise runner
# Alternate runner/scrambler. Only continue if there is enough judges available
while len(scheduleInfo.groups[event][group])< len(scheduleInfo.groupJudges[event][group]):
if runSc%2 == 0:
# scrmbler stuff
passed = False
for potScram in scheduleInfo.groupJudges[event][group]: # Take fastest first
if personInfo[potScram].age > 12: # Arguably can be set lower/higher for min
passed = True
break
if not passed:
potScram = scheduleInfo.groupJudges[event][group][0] # Take the fastest if no one is old enough
scheduleInfo.groupJudges[event][group].remove(potScram)
scheduleInfo.groupScramblers[event][group].append(potScram)
for idx,assignment in enumerate(personInfo[best].assignments[event]):
if assignment == group:
personInfo[potScram].assignments[event][idx] = f';S{group}'
else: # Runners
passed = False
for potRun in scheduleInfo.groupJudges[event][group][::-1]: # Take slowest first
if personInfo[potRun].age > 14 and personInfo[potRun].age < 40: # Arguably can be set to lower/higher for min/max
passed = True
break
if not passed:
potRun = scheduleInfo.groupJudges[event][group][-1]
scheduleInfo.groupJudges[event][group].remove(potRun)
scheduleInfo.groupRunners[event][group].append(potRun)
for idx,assignment in enumerate(personInfo[potRun].assignments[event]):
if assignment == group:
personInfo[potRun].assignments[event][idx] = f';R{group}'
runSc += 1
def convertCSV(scheduleInfo,personInfo,outfile,combined=None):
"""
In the accepted CSV format of https://goosly.github.io/AGE/
"""
if combined: # Fix the assignment back to regular events
combHy = combined[0]+'-'+combined[1]
for person in personInfo:
for comSplit in combined:
if comSplit in personInfo[person].events:
personInfo[person].groups[comSplit] = deepcopy(personInfo[person].groups[combHy])
if combHy in personInfo[person].assignments:
personInfo[person].assignments[comSplit] = deepcopy(personInfo[person].assignments[combHy])
if combHy in personInfo[person].groups:
personInfo[person].groups.pop(combHy)
if combHy in personInfo[person].assignments:
personInfo[person].assignments.pop(combHy)
header = 'Name'
for event in scheduleInfo.events:
if combined:
if event[0] == combHy:
for event in event[0].split('-'):
header+=f',{event}'
else:
header+=f',{event[0]}'
else:
header+=f',{event[0]}'
hCSV = header.split(',')
header+='\n'
for person in personInfo:
pString = str(person)
for event in hCSV:
if event in personInfo[person].groups:
pString+=f"{personInfo[person].groups[event]}"
if event in personInfo[person].assignments:
for assignment in personInfo[person].assignments[event]:
if type(assignment) == int:
pString += f";J{assignment}" # judges
else:
pString += assignment
pString+=','
pString = pString[:-1]
header+=pString+'\n'
writeCSVf = open(outfile,'w')
print(header,file=writeCSVf)
# None
def makePDF(scheduleInfo,personInfo,outfile):
pdf = FPDF('p','mm', 'A4')
pdf.add_page()
pdf.set_auto_page_break(auto=True,margin=15)
# See main for fonts. Needed because of utf-8 stuff and names
pdf.add_font('DejaVu','', fname='fonts/DejaVuSansCondensed.ttf', uni=True)
pdf.add_font('DejaVub','', fname='fonts/DejaVuSansCondensed-Bold.ttf', uni=True)
pdf.set_font('DejaVub','',22)
pdf.cell(65,9,f'{scheduleInfo.name} Group Overview',ln=True)
for event1 in scheduleInfo.events:
event = event1[0]
for group in scheduleInfo.groups[event]:
# print(event,group)
pdf.set_font('DejaVub','',20)
pdf.cell(65,9,f'{event} {group}',ln=True) # Event and group
pdf.set_font('DejaVu','',14)
# Time duration
pdf.cell(65,9,f'{scheduleInfo.groupTimes[event][group][0].time()}-{scheduleInfo.groupTimes[event][group][1].time()}',ln=True)
pdf.set_font('DejaVub','',12)
pdf.cell(45,9,'Competitors')
pdf.cell(45,9,'Judges')
pdf.cell(45,9,'Scramblers')
pdf.cell(45,9,'Runners',ln=True)
# print(scheduleInfo.groups[event][group])
competitors = scheduleInfo.groups[event][group]
if event in scheduleInfo.groupJudges:
judges = scheduleInfo.groupJudges[event][group]
scramblers = scheduleInfo.groupScramblers[event][group]
runners = scheduleInfo.groupRunners[event][group]
else:
judges = []
scramblers = []
runners = []
i = 0
# print(competitors)
if len(judges) > 0 and len(judges) < len(competitors): # Warning of few staff
pdf.cell(45,9,f'# {len(competitors)}')
pdf.set_text_color(194,8,8) # Highlight red
pdf.cell(45,9,f'{len(judges)}/{len(competitors)}')
pdf.cell(45,9,f'{len(scramblers)}')
pdf.cell(45,9,f'{len(runners)}',ln=True)
pdf.set_text_color(0,0,0) # Back to black
elif len(judges) == len(competitors) and len(scramblers) <=1: # Warning of few runners/scramblers
pdf.cell(45,9,f'# {len(competitors)}')
pdf.cell(45,9,f'{len(judges)}/{len(competitors)}')
pdf.set_text_color(194,8,8)
pdf.cell(45,9,f'{len(scramblers)}')
pdf.cell(45,9,f'{len(runners)}',ln=True)
pdf.set_text_color(0,0,0)
elif len(judges) == len(competitors) and len(runners) <=1: # warning of few runners
pdf.cell(45,9,f'# {len(competitors)}')
pdf.cell(45,9,f'{len(judges)}/{len(competitors)}')
pdf.cell(45,9,f'{len(scramblers)}')
pdf.set_text_color(194,8,8)
pdf.cell(45,9,f'{len(runners)}',ln=True)
pdf.set_text_color(0,0,0)
else: # All good
pdf.cell(45,9,f'# {len(competitors)}')
pdf.set_font('DejaVu','',12)
pdf.cell(45,9,f'{len(judges)}/{len(competitors)}')
pdf.cell(45,9,f'{len(scramblers)}')
pdf.cell(45,9,f'{len(runners)}',ln=True)
while i < len(competitors): # Print everyone
# print(i)
pdf.set_font('DejaVu','',8)
if len(judges) > i and len(scramblers) > i and len(runners) > i: # Enough for now
pdf.cell(45,9,f'{competitors[i]}')
pdf.cell(45,9,f'{judges[i]}')
pdf.cell(45,9,f'{scramblers[i]}')
pdf.cell(45,9,f'{runners[i]}',ln=True)
elif len(judges) > i and len(scramblers) > i: # Enough judges and scramblers for now
pdf.cell(45,9,f'{competitors[i]}')
pdf.cell(45,9,f'{judges[i]}')
pdf.cell(45,9,f'{scramblers[i]}',ln=True)
elif len(judges) > i: # Enough judges for now
pdf.cell(45,9,f'{competitors[i]}')
pdf.cell(45,9,f'{judges[i]}',ln=True)
else: # Only competitors left
pdf.cell(45,9,f'{competitors[i]}',ln=True)
i+=1
pdf.output(outfile)
def main():
# Download the file from here (Replace the comp id): https://www.worldcubeassociation.org/api/v0/competitions/VestkystCubing2021/wcif
# Fonts needed because of utf-8. Document: https://pyfpdf.github.io/fpdf2/Unicode.html. Direct link: https://github.com/reingart/pyfpdf/releases/download/binary/fpdf_unicode_font_pack.zip
# Make a folder with the ones used in the file.
# fil = open("dm21/wcif.json")
# fil = open("../vestkyst/wcif.json")
fil = open("../jontwix/wcif.json")
data = json.load(fil)
# combined = combineEvents('666','777')
people,organizers = competitorBasicInfo(data)
stations = 16
# schedule = scheduleBasicInfo(data,people,organizers,stations,{'333bf':3},combined)
schedule = scheduleBasicInfo(data,people,organizers,stations)
# schedule = scheduleBasicInfo(data,people,organizers,stations,combinedEvents=combined)
schedule, people = splitIntoGroups(schedule,people)
assignJudges(schedule,people)
reassignJudges(schedule,people)
# print(people['Martin Vædele Egdal'].groups)
# print(people['Martin Vædele Egdal'].assignments)
filenameSave = str(datetime.now().strftime("%m%d_%T")).replace(':','').replace('/','') # Ensure unique name
# convertCSV(schedule,people,'vestkystcsva.csv',combined)
# convertCSV(schedule,people,'dmcsva.csv',combined)
name = schedule.name
convertCSV(schedule,people,f'out/{name}Groups{filenameSave}.csv')
makePDF(schedule,people,f'out/{name}Overview{filenameSave}.pdf')
main()
# checking overlaps
#print(schedule.overlappingEvents)
# for event in schedule.overlappingEvents:
# for group in schedule.groups[event]:
# for event2 in schedule.overlappingEvents:
# for group2 in schedule.groups[event2]:
# g = schedule.groupTimes[event][group]
# h = schedule.groupTimes[event2][group2]
# print(event,group,event2,group2,schedule.groupTimeChecker(g,h))