forked from Mechanical-Advantage/AdvantageScout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.py
109 lines (96 loc) · 5.05 KB
/
scheduler.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
def get_schedule(teams, scout_records, total_priority, prefs):
# Check for 6 scouts
if len(scout_records) < 6:
return "Error - needs 6 scouts"
# Add ids and totals to scout records array
for i in range(len(scout_records)):
scout_records[i]['id'] = i
# Update scout records array (to add teams)
for i in range(len(scout_records)):
for teamnumber in range(len(teams)):
if teams[teamnumber] not in scout_records[i].keys():
scout_records[i][teams[teamnumber]] = 0
# Update from prefs
prefs = prefs[::-1]
for i in range(len(prefs)):
index = [index for index, value in enumerate(
scout_records) if value["name"] == prefs[i]["scout"]]
if len(index) < 1:
continue
index = index[0]
scout_records[index][prefs[i]["team"]] = 100000 * (i + 1)
# Create priority lists
def priority_list(team):
sorted_scouts = []
for i in range(len(scout_records)):
sorted_scouts.append({"id": scout_records[i]['id'], "priority": float(scout_records[i][team]) - float(
scout_records[i]['total'])*(total_priority), "total": scout_records[i]['total']})
sorted_scouts = sorted(
sorted_scouts, key=lambda x: (-x['priority'], x['total']))
temp_output = []
for i in range(len(sorted_scouts)):
temp_output.append(sorted_scouts[i]['id'])
return temp_output
# Create match schedule
scheduled = {}
# Generate priority lists
priority_lists = {}
for teamnumber in range(6):
priority_lists[teams[teamnumber]] = priority_list(
team=teams[teamnumber])
# Function for removing a scout from priority lists (once assigned)
def remove_from_priority(scout):
for team, list in priority_lists.items():
while scout in priority_lists[team]:
priority_lists[team].remove(scout)
# Function for one cycle of assignments
def assign_scouts():
# Generate lists of scout requests
scout_requests = []
for i in range(len(scout_records)):
scout_requests.append([])
for team, list in priority_lists.items():
if len(list) > 0:
scout_requests[priority_lists[team][0]].append(team)
# Iterate through scout requests (resolving conflicts when neccessary)
for scout_request_number in range(len(scout_requests)):
if len(scout_requests[scout_request_number]) == 1:
# No conflict (scout requested by one team)
scheduled[scout_requests[scout_request_number][0]
] = scout_request_number # Add to schedule
# Clear priority list for team
priority_lists[scout_requests[scout_request_number][0]] = []
# Remove scout from priority lists (so cannot be selected for another team)
remove_from_priority(scout=scout_request_number)
elif len(scout_requests[scout_request_number]) > 1:
# Conflict found (scout requested by multiple teams)
# Resolved by comparing potential 'loss of experience' if each team used secondary scout
comparison_data = []
for i in range(len(scout_requests[scout_request_number])):
first_value = scout_records[priority_lists[scout_requests[scout_request_number]
[i]][0]][scout_requests[scout_request_number][i]]
first_value -= float(
scout_records[priority_lists[scout_requests[scout_request_number][i]][0]]['total'])*total_priority
second_value = scout_records[priority_lists[scout_requests[scout_request_number]
[i]][1]][scout_requests[scout_request_number][i]]
second_value -= float(
scout_records[priority_lists[scout_requests[scout_request_number][i]][1]]['total'])*total_priority
# Find difference between experience of primary and secondary scout
comparison_data.append(first_value - second_value)
maxid = 0
for i in range(len(comparison_data)):
if comparison_data[i] > comparison_data[maxid]:
maxid = i
scheduled[scout_requests[scout_request_number][maxid]
] = scout_request_number # Add to schedule
# Clear priority list for team
priority_lists[scout_requests[scout_request_number][maxid]] = []
# Remove scout from priority lists (so cannot be selected for another team)
remove_from_priority(scout=scout_request_number)
# Run cycles of assignment until schedule created
while len(scheduled) < 6:
assign_scouts()
# Update with scout names
for team, scoutnumber in scheduled.items():
scheduled[team] = scout_records[scoutnumber]["name"]
return scheduled