-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteambot.py
162 lines (109 loc) · 4.26 KB
/
teambot.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
from operator import truediv
from os import dup
import random
from teamvisualizer import Visualizer
import os
scoresraw = open(f"C:/Users/s2000925/Documents/Unity/Brackeys 2022/teambuilder/scores.txt", "r")
alternativeteam_scoretreshhold = 15
def print_team(team):
teamtotalscore = get_team_score(team)
return f'{team[0][0]}, {team[1][0]}, {team[2][0]}, (Score: {teamtotalscore})'
def print_not_participating(parts):
np = f''
for p in range(len(parts)):
if p > 11:
np += f'{parts[p][0]}, '
return np
def get_team_score(team):
return round((team[0][1] + team[1][1] + team[2][1]) / 3)
def swap(parts, p1, p2):
parts[p1], parts[p2] = parts[p2], parts[p1]
def generate_teams(parts):
team1 = [parts[0], parts[1], parts[2]]
team2 = [parts[3], parts[4], parts[5]]
team3 = [parts[6], parts[7], parts[8]]
team4 = [parts[9], parts[10], parts[11]]
return [team1,team2,team3,team4]
def get_score_diff(teams):
scores = [get_team_score(teams[0]),get_team_score(teams[1]),get_team_score(teams[2]),get_team_score(teams[3])]
return max(scores) - min(scores)
def check_for_duplicates(team, allparts, bestparts):
allparts.append(bestparts)
for a in allparts:
diffinpart = 0
for p in range(len(a)):
if a[p][0] != team[p][0]:
diffinpart += 1
if diffinpart == 0:
return True
return False
s = scoresraw.readlines()
participants = []
for x in s:
x = x.replace('\n','')
x = x.split(',')
name = x[0]
season1score = int(x[1])
season2score = int(x[2])
averagescore = season2score - ((season2score - season1score) / 4)
#averagescore = (season1score + season2score) / 2
if (season1score == 0 or season2score == 0):
averagescore = max(season1score, season2score)
participants.append([name,averagescore])
participants_sorted = sorted(participants, key=lambda p : p[1],reverse=True)
participants = participants_sorted
teams = generate_teams(participants)
scorediff = get_score_diff(teams)
alternatives = 0
alternativeparts = []
while True:
tempparts = participants.copy()
thisbestscore = scorediff
thisbestparts = tempparts
improvements = 0
for p in range(len(tempparts)):
for s in range(len(tempparts)):
swap(tempparts,p,s)
tempteams = generate_teams(tempparts)
thisscorediff = get_score_diff(tempteams)
if (thisscorediff < thisbestscore):
thisbestscore = thisscorediff
thisbestparts = tempparts
alternatives = 0
alternativeparts.clear()
improvements += 1
elif (thisscorediff <= thisbestscore + alternativeteam_scoretreshhold and not check_for_duplicates(tempparts, alternativeparts, thisbestparts)):
alternatives += 1
alternativeparts.append(tempparts)
tempparts = participants.copy()
sd = get_score_diff(generate_teams(thisbestparts))
if (sd < scorediff):
scorediff = sd
participants = thisbestparts.copy()
else:
break
print(f'Teams generated. {improvements} optimization(s) found. Diff: ({scorediff})')
bestteams = generate_teams(participants)
for f in bestteams:
f.sort()
print(f'\nBEST TEAMS:\nTeam 1: {print_team(bestteams[0])} \nTeam 2: {print_team(bestteams[1])} \nTeam 3: {print_team(bestteams[2])} \nTeam 4: {print_team(bestteams[3])}\nScore diff ({scorediff})')
print(f'')
#v = Visualizer()
#v.generate_image(participants)
al = 0
if (alternatives > 0):
for a in alternativeparts:
altbestteams = generate_teams(a)
altscorediff = get_score_diff(altbestteams)
for t in altbestteams:
t.sort()
duplicates = 0
for t2 in range(len(altbestteams)):
for p in range(len(altbestteams[t2])):
if (altbestteams[t2][p] == bestteams[t2][p]):
duplicates += 1
if (duplicates == 12):
continue
al += 1
print(f'ALTERNATIVE {al}')
print(f'Team 1: {print_team(altbestteams[0])} \nTeam 2: {print_team(altbestteams[1])} \nTeam 3: {print_team(altbestteams[2])} \nTeam 4: {print_team(altbestteams[3])}\nScore diff ({altscorediff})\n')