-
Notifications
You must be signed in to change notification settings - Fork 0
/
TP.py
195 lines (159 loc) · 6.73 KB
/
TP.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
import random
import json
numberOfTables = 100
numberOfPeoplePerTable = 6
companyOffset = 10000
currentStudent = 0
currentRepresentative = 0
currentNonMatchingStudent = 0
with open('./results.json', 'r') as json_file:
json_data = json.load(json_file)
allStudentsDict = json_data["similarities"]
studentsNames = json_data["matching_students"]
representativesData = json_data["company_participants"]
random.shuffle(representativesData)
random.shuffle(representativesData)
random.shuffle(representativesData)
representativeCompany = json_data["participant_to_exhibitor"]
nonMatchingStudents = json_data["non_matching_student"]
scoreBanquet = 0
allTables = []
def getNextStudent():
global currentStudent
myStudent = ['student', studentsNames[currentStudent]]
currentStudent += 1
return myStudent
def getNextRepresentative():
global currentRepresentative
myRepresentative = ['representative', representativesData[currentRepresentative],
representativeCompany[str(representativesData[currentRepresentative])] + companyOffset]
currentRepresentative += 1
return myRepresentative
def getNextNonMatchingStudent():
global currentNonMatchingStudent
nonMatchStudent = ['nonmatching_student', nonMatchingStudents[currentNonMatchingStudent]]
currentNonMatchingStudent += 1
return nonMatchStudent
def getNumberOfRepresentatives(currTable):
numberOfRepresentatives = 0
for spot in currTable:
if spot[0] == 'representative':
numberOfRepresentatives += 1
return numberOfRepresentatives
def getNumberOfStudents(currTable):
numberOfStudentss = 0
for spot in currTable:
if spot[0] == 'student':
numberOfStudentss += 1
return numberOfStudentss
def getNextRand(prevRandom, stop):
newRandom = random.randrange(stop)
while True:
if newRandom == prevRandom:
newRandom = random.randrange(stop)
else:
return newRandom
def isCompanyAlreadyAtThatTable(table1, spot1, table2, spot2):
if spot2[0] == 'representative':
for spot in table1:
if spot[0] == 'representative' and spot[2] == spot2[2]:
return True
if spot1[0] == 'representative':
for spot in table2:
if spot[0] == 'representative' and spot[2] == spot1[2]:
return True
return False
def initialPlacement():
for num in range(numberOfTables):
allTables.append([])
for spot in range(numberOfPeoplePerTable):
if spot % 100 == 1:
if currentNonMatchingStudent < len(nonMatchingStudents):
allTables[num].append(getNextNonMatchingStudent())
continue
# this is here because we want ot avoid the last few tables to only consist of students, because there are more students than representatives
if len(studentsNames) - currentStudent + 2 > len(representativesData) - currentRepresentative:
if spot % 3 != 0:
if currentStudent < len(studentsNames):
allTables[num].append(getNextStudent())
else:
if currentRepresentative < len(representativesData):
allTables[num].append(getNextRepresentative())
else:
if spot % 2 == 0:
if currentStudent < len(studentsNames):
allTables[num].append(getNextStudent())
else:
if currentRepresentative < len(representativesData):
allTables[num].append(getNextRepresentative())
else:
if currentStudent < len(studentsNames):
allTables[num].append(getNextStudent())
def calculateScores(newAllTables):
tableNum = 0
scoreBanquet = 0
scoresStudents = {}
scoresTables = []
for table in newAllTables:
scoresTables.append(0)
representativesCount = getNumberOfRepresentatives(table)
studentsCountPerTable = getNumberOfStudents(table)
for x in table:
if x[0] == 'student':
scoresStudents[x[1]] = 0
for y in table:
if y[0] == 'representative':
scoresStudents[x[1]] += allStudentsDict[str(x[1])][str(y[2] - companyOffset)]
if (representativesCount > 0):
scoresStudents[x[1]] /= representativesCount
scoresTables[tableNum] += scoresStudents[x[1]]
if (studentsCountPerTable > 0):
scoresTables[tableNum] /= studentsCountPerTable
scoreBanquet += scoresTables[tableNum]
tableNum += 1
scoreBanquet /= numberOfTables
return scoreBanquet
initialPlacement()
print("Tables initially: ", allTables)
scoreBanquet = calculateScores(allTables)
print("First score: ", scoreBanquet)
iters = 0
while True:
randTable1 = random.randrange(numberOfTables)
randTable2 = getNextRand(randTable1, numberOfTables)
randSpot1 = random.randrange(numberOfPeoplePerTable)
randSpot2 = getNextRand(randSpot1, numberOfPeoplePerTable)
if randSpot1 < len(allTables[randTable1]) and randSpot2 < len(allTables[randTable2]):
if isCompanyAlreadyAtThatTable(allTables[randTable1], allTables[randTable1][randSpot1], allTables[randTable2],
allTables[randTable2][randSpot2]):
continue
swap = allTables[randTable1][randSpot1]
allTables[randTable1][randSpot1] = allTables[randTable2][randSpot2]
allTables[randTable2][randSpot2] = swap
else:
continue
if getNumberOfStudents(allTables[randTable1]) > 1 and getNumberOfStudents(
allTables[randTable2]) > 1 and getNumberOfRepresentatives(
allTables[randTable1]) > 1 and getNumberOfRepresentatives(
allTables[randTable1]) < 4 and getNumberOfRepresentatives(
allTables[randTable2]) > 1 and getNumberOfRepresentatives(allTables[randTable2]) < 4:
newRes = calculateScores(allTables)
else:
allTables[randTable2][randSpot2] = allTables[randTable1][randSpot1]
allTables[randTable1][randSpot1] = swap
continue
if newRes > scoreBanquet:
scoreBanquet = newRes
print("Swapped! New score: ", scoreBanquet)
iters = 0
else:
allTables[randTable2][randSpot2] = allTables[randTable1][randSpot1]
allTables[randTable1][randSpot1] = swap
iters += 1
if iters == 60000:
break
print("Final table placement: ", allTables)
# Parse output, we only need the participant id's
output = [list(map(lambda l: l[1], table)) for table in allTables]
with open('placement_output.json', 'w') as file:
json.dump(output, file, indent = 4, separators=(',', ': '))