-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseAlgorithm.js
More file actions
211 lines (182 loc) · 7.37 KB
/
Copy pathuseAlgorithm.js
File metadata and controls
211 lines (182 loc) · 7.37 KB
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
class Student {
constructor(name, preferences, avoids) {
this.name = name;
this.preferences = preferences;
this.avoids = avoids;
}
}
class Table {
constructor(size) {
this.size = size;
this.students = [];
}
addStudent(student) {
if (this.students.length < this.size && !this.students.includes(student)) {
this.students.push(student);
return true;
}
return false;
}
removeStudent(student) {
const index = this.students.indexOf(student);
if (index !== -1) {
this.students.splice(index, 1);
return true;
}
return false;
}
toString() {
return `Table(${this.students.map(student => student.name)})`;
}
}
function calculateGiniCoefficient(data) {
const sortedData = data.slice().sort((a, b) => a - b);
const n = data.length;
const cumulativeData = sortedData.reduce((acc, val) => {
acc.push((acc.length > 0 ? acc[acc.length - 1] : 0) + val);
return acc;
}, []);
const sumOfProducts = sortedData.reduce((acc, val, i) => acc + (i + 1) * val, 0);
const cumulativeSum = cumulativeData[cumulativeData.length - 1];
return (2 * sumOfProducts - (n + 1) * cumulativeSum) / (n * cumulativeSum);
}
function countSatisfiedPreferences(tables) {
let score = 0;
const preferenceCounts = [];
for (const table of tables) {
let tablePreferenceCount = 0;
for (const student of table.students) {
for (const preference of student.preferences) {
if (table.students.some(s => s.name === preference)) {
score++;
tablePreferenceCount++;
}
}
}
preferenceCounts.push(tablePreferenceCount);
}
for (const table of tables) {
for (const student of table.students) {
for (const avoid of student.avoids) {
if (table.students.some(s => s.name === avoid)) {
score--;
}
}
}
}
const giniCoefficient = calculateGiniCoefficient(preferenceCounts);
if (giniCoefficient >= 0 && giniCoefficient < 0.5) {
const reward = 10 - Math.round(giniCoefficient * 10 * 100) / 100;
score += reward;
} else {
const penalty = Math.round(giniCoefficient * 10 * 100) / 100;
score -= penalty;
}
return { score, giniCoefficient };
}
function assignStudentsToTables(students, numTables, tableSize) {
const tables = Array.from({ length: numTables }, () => new Table(tableSize));
students = students.slice();
students.sort(() => Math.random() - 0.5);
for (const student of students) {
let bestTable = null;
let maxPreferences = -1;
for (const table of tables) {
const currentPreferences = table.students.filter(s => student.preferences.includes(s.name)).length;
if (table.students.length < table.size && currentPreferences > maxPreferences) {
bestTable = table;
maxPreferences = currentPreferences;
}
}
if (bestTable) {
bestTable.addStudent(student);
}
}
return tables;
}
function optimizeSeating(tables, iterations = 5000) {
let bestTables = tables.map(t => new Table(t.size));
tables.forEach((table, i) => bestTables[i].students = table.students.slice());
let bestScore = countSatisfiedPreferences(tables).score;
for (let i = 0; i < iterations; i++) {
const [table1, table2] = tables.slice().sort(() => Math.random() - 0.5).slice(0, 2);
if (table1.students.length === 0 || table2.students.length === 0) continue;
const student1 = table1.students[Math.floor(Math.random() * table1.students.length)];
const student2 = table2.students[Math.floor(Math.random() * table2.students.length)];
table1.removeStudent(student1);
table2.removeStudent(student2);
table1.addStudent(student2);
table2.addStudent(student1);
const newScore = countSatisfiedPreferences(tables).score;
if (newScore > bestScore) {
bestScore = newScore;
bestTables = tables.map(t => new Table(t.size));
tables.forEach((table, i) => bestTables[i].students = table.students.slice());
} else {
table1.removeStudent(student2);
table2.removeStudent(student1);
table1.addStudent(student1);
table2.addStudent(student2);
}
}
return bestTables;
}
function findBestSeating(students, numTables, tableSize, trials = 10) {
let bestSeating = null;
let bestScore = -Infinity;
for (let i = 0; i < trials; i++) {
let tables = assignStudentsToTables(students, numTables, tableSize);
tables = optimizeSeating(tables);
const { score } = countSatisfiedPreferences(tables);
if (score > bestScore) {
bestScore = score;
bestSeating = tables;
}
}
return bestSeating;
}
const students = [
new Student('Alice', ['Bob', 'Charlie', 'David'], []),
new Student('Bob', ['Alice'], ['Yara', 'Tina']),
new Student('Charlie', ['Alice', 'Uma', 'Chloe'], []),
new Student('David', ['Alice', 'Bob', 'Charlie'], []),
new Student('Eve', ['Frank', 'Grace', 'Hannah', 'Ivy'], ['Paul', 'Charlie']),
new Student('Frank', ['Alice', 'Bob', 'Tina', 'Jack'], ['Kate']),
new Student('Grace', ['Charlie'], ['Violet']),
new Student('Hannah', ['Jack', 'Uma', 'Chloe', 'Harry'], ['Erick']),
new Student('Ivy', ['Charlie'], []),
new Student('Jack', ['Bob', 'Alice', 'David'], ['Tina', 'Ruby']),
new Student('Kate', ['Frank', 'Leo', 'Gina'], []),
new Student('Leo', ['Isla', 'Jack', 'Frank', 'Ivy'], []),
new Student('Mia', ['Ruby', 'Uma'], []),
new Student('Nina', ['Bob'], ['Ava']),
new Student('Oscar', ['Violet'], []),
new Student('Paul', ['Grace'], ['Charlie']),
new Student('Quinn', ['Tina'], []),
new Student('Ruby', ['Hannah', 'Grace', 'Kate'], []),
new Student('Sam', ['Tina', 'Gina', 'Paul', 'Kate'], []),
new Student('Tina', ['Yara', 'Harry'], []),
new Student('Uma', ['Alice'], []),
new Student('Violet', ['Uma', 'Oscar'], ['Frank', 'Ivy']),
new Student('Will', ['Gina', 'Eve', 'Quinn', 'Alice'], []),
new Student('Xander', ['Gina', 'Ruby'], []),
new Student('Yara', ['Ben', 'Hannah'], []),
new Student('Zack', ['Nina', 'Paul', 'Frank'], []),
new Student('Ava', ['Paul', 'Sam', 'Oscar', 'Xander'], []),
new Student('Ben', ['Ava', 'Harry', 'Nina', 'Xander'], ['Ava']),
new Student('Chloe', ['Sam', 'Ben'], ['Gina', 'Quinn']),
new Student('Dylan', ['Hannah', 'Bob'], []),
new Student('Ellie', ['Quinn', 'Alice', 'Tina'], []),
new Student('Finn', ['Mia', 'Jack', 'Ellie', 'Chloe'], []),
new Student('Gina', ['Nina', 'Tina', 'Jack'], ['Ruby']),
new Student('Harry', ['Sam', 'Nina'], []),
new Student('Isla', ['Ava', 'David', 'Violet'], []),
new Student('Erick', ['Ben', 'Xander'], [])
];
const numTables = 9;
const tableSize = 4;
const bestSeating = findBestSeating(students, numTables, tableSize, 10);
bestSeating.forEach(table => console.log(table.toString()));
const { score, giniCoefficient } = countSatisfiedPreferences(bestSeating);
console.log(`Best Score: ${score}`);
console.log(`Gini Coefficient: ${giniCoefficient}`);