@dytsou/intern-corner-scheduler 1.2.13 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -42
- package/index.html +6 -1
- package/package.json +23 -20
- package/scripts/create-nojekyll.js +2 -2
- package/src/App.css +846 -233
- package/src/App.jsx +113 -17
- package/src/components/ErrorMessage.jsx +3 -2
- package/src/components/GenerationLoadingScreen.jsx +48 -0
- package/src/components/Header.jsx +23 -5
- package/src/components/LoadingSpinner.jsx +11 -0
- package/src/components/PairInput.jsx +21 -7
- package/src/components/PairStatus.jsx +8 -2
- package/src/components/ScheduleForm.jsx +182 -113
- package/src/components/TableAssignments.jsx +7 -1
- package/src/index.css +153 -26
- package/src/services/cpSatScheduler.js +551 -0
- package/src/services/schedulerClient.js +96 -0
- package/vite.config.js +18 -0
- package/src/config.js +0 -21
- package/src/services/api.js +0 -31
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
const SOLUTION_STATUSES = new Set(['OPTIMAL', 'FEASIBLE']);
|
|
2
|
+
|
|
3
|
+
function getPairEndpoints(pair) {
|
|
4
|
+
if (Array.isArray(pair)) {
|
|
5
|
+
return pair.length === 2 ? pair : null;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (!pair || typeof pair !== 'object') {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (!Object.hasOwn(pair, 'u') || !Object.hasOwn(pair, 'v')) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return [pair.u, pair.v];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function isValidPair(endpoints, participants) {
|
|
20
|
+
const [left, right] = endpoints;
|
|
21
|
+
return (
|
|
22
|
+
Number.isSafeInteger(left) &&
|
|
23
|
+
Number.isSafeInteger(right) &&
|
|
24
|
+
left !== right &&
|
|
25
|
+
left >= 1 &&
|
|
26
|
+
right >= 1 &&
|
|
27
|
+
left <= participants &&
|
|
28
|
+
right <= participants
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function normalizePairs(pairs, participants) {
|
|
33
|
+
if (!Array.isArray(pairs)) {
|
|
34
|
+
return [];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const seen = new Set();
|
|
38
|
+
const normalized = [];
|
|
39
|
+
for (const pair of pairs) {
|
|
40
|
+
const endpoints = getPairEndpoints(pair);
|
|
41
|
+
if (!endpoints || !isValidPair(endpoints, participants)) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const [left, right] = endpoints;
|
|
46
|
+
const normalizedPair = left < right ? [left, right] : [right, left];
|
|
47
|
+
const key = `${normalizedPair[0]}:${normalizedPair[1]}`;
|
|
48
|
+
if (seen.has(key)) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
seen.add(key);
|
|
53
|
+
normalized.push(normalizedPair);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return normalized;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function readDimensions(input) {
|
|
60
|
+
const participants = Number(input.participants);
|
|
61
|
+
const tables = Number(input.tables);
|
|
62
|
+
const rounds = Number(input.rounds);
|
|
63
|
+
|
|
64
|
+
if (!Number.isSafeInteger(participants) || !Number.isSafeInteger(tables)) {
|
|
65
|
+
throw new TypeError('participants and tables must be integers');
|
|
66
|
+
}
|
|
67
|
+
if (!Number.isSafeInteger(rounds)) {
|
|
68
|
+
throw new TypeError('rounds must be an integer');
|
|
69
|
+
}
|
|
70
|
+
if (participants < tables || tables < 1 || rounds < 1) {
|
|
71
|
+
throw new RangeError(
|
|
72
|
+
'participants must be at least tables, and tables and rounds must be positive'
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return { participants, tables, rounds };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function computeTableSizes(participants, tables) {
|
|
80
|
+
const base = Math.floor(participants / tables);
|
|
81
|
+
const remainder = participants % tables;
|
|
82
|
+
return Array.from(
|
|
83
|
+
{ length: tables },
|
|
84
|
+
(_value, tableIndex) => base + (tableIndex < remainder ? 1 : 0)
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function makeVariableGrid(participants, tables, rounds) {
|
|
89
|
+
return Array.from({ length: participants + 1 }, () =>
|
|
90
|
+
Array.from({ length: tables + 1 }, () => new Array(rounds))
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function addConjunction(model, name, left, right) {
|
|
95
|
+
const together = model.newBoolVar(name);
|
|
96
|
+
model.add(together.le(left));
|
|
97
|
+
model.add(together.le(right));
|
|
98
|
+
model.add(together.ge(left.plus(right).minus(1)));
|
|
99
|
+
return together;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function addTableBalance(model, LinearExpr, x, participants, tables, rounds) {
|
|
103
|
+
const sizes = Array.from({ length: tables + 1 }, () => new Array(rounds));
|
|
104
|
+
const minSizes = new Array(rounds);
|
|
105
|
+
const maxSizes = new Array(rounds);
|
|
106
|
+
|
|
107
|
+
for (let round = 0; round < rounds; round += 1) {
|
|
108
|
+
minSizes[round] = model.newIntVar(0, participants, `min_size_r${round}`);
|
|
109
|
+
maxSizes[round] = model.newIntVar(0, participants, `max_size_r${round}`);
|
|
110
|
+
|
|
111
|
+
for (let table = 1; table <= tables; table += 1) {
|
|
112
|
+
const count = model.newIntVar(
|
|
113
|
+
0,
|
|
114
|
+
participants,
|
|
115
|
+
`size_t${table}_r${round}`
|
|
116
|
+
);
|
|
117
|
+
sizes[table][round] = count;
|
|
118
|
+
model.add(
|
|
119
|
+
LinearExpr.sum(
|
|
120
|
+
Array.from(
|
|
121
|
+
{ length: participants },
|
|
122
|
+
(_value, index) => x[index + 1][table][round]
|
|
123
|
+
)
|
|
124
|
+
).eq(count)
|
|
125
|
+
);
|
|
126
|
+
model.add(count.ge(minSizes[round]));
|
|
127
|
+
model.add(count.le(maxSizes[round]));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
model.add(maxSizes[round].minus(minSizes[round]).le(1));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return sizes;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function addSameOnce(model, LinearExpr, x, sameOncePairs, tables, rounds) {
|
|
137
|
+
const togetherByPairAndTableRound = new Map();
|
|
138
|
+
const meetByPairAndRound = [];
|
|
139
|
+
const meetByPairAndHost = new Map();
|
|
140
|
+
|
|
141
|
+
for (let pairIndex = 0; pairIndex < sameOncePairs.length; pairIndex += 1) {
|
|
142
|
+
const [left, right] = sameOncePairs[pairIndex];
|
|
143
|
+
const meetThisPairByRound = [];
|
|
144
|
+
|
|
145
|
+
for (let round = 0; round < rounds; round += 1) {
|
|
146
|
+
const tableVars = [];
|
|
147
|
+
for (let table = 1; table <= tables; table += 1) {
|
|
148
|
+
const together = addConjunction(
|
|
149
|
+
model,
|
|
150
|
+
`same_once_pair_i${pairIndex}_t${table}_r${round}`,
|
|
151
|
+
x[left][table][round],
|
|
152
|
+
x[right][table][round]
|
|
153
|
+
);
|
|
154
|
+
togetherByPairAndTableRound.set(
|
|
155
|
+
`${pairIndex}:${table}:${round}`,
|
|
156
|
+
together
|
|
157
|
+
);
|
|
158
|
+
tableVars.push(together);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const meet = model.newBoolVar(`meet_i${pairIndex}_r${round}`);
|
|
162
|
+
model.addMaxEquality(meet, tableVars);
|
|
163
|
+
meetThisPairByRound.push(meet);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
model.add(LinearExpr.sum(meetThisPairByRound).le(1));
|
|
167
|
+
meetByPairAndRound.push(meetThisPairByRound);
|
|
168
|
+
|
|
169
|
+
for (let host = 1; host <= tables; host += 1) {
|
|
170
|
+
const meetHost = model.newBoolVar(`meet_host_i${pairIndex}_h${host}`);
|
|
171
|
+
model.addMaxEquality(
|
|
172
|
+
meetHost,
|
|
173
|
+
Array.from({ length: rounds }, (_value, round) =>
|
|
174
|
+
togetherByPairAndTableRound.get(`${pairIndex}:${host}:${round}`)
|
|
175
|
+
)
|
|
176
|
+
);
|
|
177
|
+
meetByPairAndHost.set(`${pairIndex}:${host}`, meetHost);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return { meetByPairAndRound, meetByPairAndHost };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function addGlobalPairwiseUnique(
|
|
185
|
+
model,
|
|
186
|
+
LinearExpr,
|
|
187
|
+
x,
|
|
188
|
+
participants,
|
|
189
|
+
tables,
|
|
190
|
+
rounds
|
|
191
|
+
) {
|
|
192
|
+
const togetherByPair = new Map();
|
|
193
|
+
|
|
194
|
+
for (let left = tables + 1; left <= participants; left += 1) {
|
|
195
|
+
for (let right = left + 1; right <= participants; right += 1) {
|
|
196
|
+
const pairVars = [];
|
|
197
|
+
for (let table = 1; table <= tables; table += 1) {
|
|
198
|
+
for (let round = 0; round < rounds; round += 1) {
|
|
199
|
+
pairVars.push(
|
|
200
|
+
addConjunction(
|
|
201
|
+
model,
|
|
202
|
+
`pair_u${left}_v${right}_t${table}_r${round}`,
|
|
203
|
+
x[left][table][round],
|
|
204
|
+
x[right][table][round]
|
|
205
|
+
)
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
model.add(LinearExpr.sum(pairVars).le(1));
|
|
210
|
+
togetherByPair.set(`${left}:${right}`, pairVars);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return togetherByPair;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function addHostDiversity(
|
|
218
|
+
model,
|
|
219
|
+
x,
|
|
220
|
+
sameOncePairs,
|
|
221
|
+
meetByPairAndHost,
|
|
222
|
+
participants,
|
|
223
|
+
tables,
|
|
224
|
+
rounds
|
|
225
|
+
) {
|
|
226
|
+
const visitedAny = new Map();
|
|
227
|
+
for (
|
|
228
|
+
let participant = tables + 1;
|
|
229
|
+
participant <= participants;
|
|
230
|
+
participant += 1
|
|
231
|
+
) {
|
|
232
|
+
for (let host = 1; host <= tables; host += 1) {
|
|
233
|
+
const visited = model.newBoolVar(`visited_p${participant}_h${host}`);
|
|
234
|
+
model.addMaxEquality(
|
|
235
|
+
visited,
|
|
236
|
+
Array.from(
|
|
237
|
+
{ length: rounds },
|
|
238
|
+
(_value, round) => x[participant][host][round]
|
|
239
|
+
)
|
|
240
|
+
);
|
|
241
|
+
visitedAny.set(`${participant}:${host}`, visited);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const pairIndexesByParticipant = new Map();
|
|
246
|
+
for (let pairIndex = 0; pairIndex < sameOncePairs.length; pairIndex += 1) {
|
|
247
|
+
for (const participant of sameOncePairs[pairIndex]) {
|
|
248
|
+
const indexes = pairIndexesByParticipant.get(participant) ?? [];
|
|
249
|
+
indexes.push(pairIndex);
|
|
250
|
+
pairIndexesByParticipant.set(participant, indexes);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const distinctPairHost = new Map();
|
|
255
|
+
for (const [participant, pairIndexes] of pairIndexesByParticipant) {
|
|
256
|
+
for (let host = 1; host <= tables; host += 1) {
|
|
257
|
+
const pairHostVars = pairIndexes.map((pairIndex) =>
|
|
258
|
+
meetByPairAndHost.get(`${pairIndex}:${host}`)
|
|
259
|
+
);
|
|
260
|
+
if (pairHostVars.length === 0) {
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const pairHostUsed = model.newBoolVar(
|
|
265
|
+
`pair_host_used_p${participant}_h${host}`
|
|
266
|
+
);
|
|
267
|
+
model.addMaxEquality(pairHostUsed, pairHostVars);
|
|
268
|
+
distinctPairHost.set(`${participant}:${host}`, pairHostUsed);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return { visitedAny, distinctPairHost };
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function addParticipantVariables(model, x, participants, tables, rounds) {
|
|
276
|
+
for (let participant = 1; participant <= participants; participant += 1) {
|
|
277
|
+
for (let table = 1; table <= tables; table += 1) {
|
|
278
|
+
for (let round = 0; round < rounds; round += 1) {
|
|
279
|
+
x[participant][table][round] = model.newBoolVar(
|
|
280
|
+
`x_${participant}_t${table}_r${round}`
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function addOneTablePerParticipantAndRound(
|
|
288
|
+
model,
|
|
289
|
+
x,
|
|
290
|
+
participants,
|
|
291
|
+
tables,
|
|
292
|
+
rounds
|
|
293
|
+
) {
|
|
294
|
+
for (let participant = 1; participant <= participants; participant += 1) {
|
|
295
|
+
for (let round = 0; round < rounds; round += 1) {
|
|
296
|
+
model.addExactlyOne(
|
|
297
|
+
Array.from(
|
|
298
|
+
{ length: tables },
|
|
299
|
+
(_value, index) => x[participant][index + 1][round]
|
|
300
|
+
)
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function fixHostAssignments(model, x, tables, rounds) {
|
|
307
|
+
for (let host = 1; host <= tables; host += 1) {
|
|
308
|
+
for (let round = 0; round < rounds; round += 1) {
|
|
309
|
+
for (let table = 1; table <= tables; table += 1) {
|
|
310
|
+
model.add(x[host][table][round].eq(table === host ? 1 : 0));
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function applyNeverTogetherConstraints(model, x, pairs, tables, rounds) {
|
|
317
|
+
for (const [left, right] of pairs) {
|
|
318
|
+
for (let round = 0; round < rounds; round += 1) {
|
|
319
|
+
for (let table = 1; table <= tables; table += 1) {
|
|
320
|
+
model.add(x[left][table][round].plus(x[right][table][round]).le(1));
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function buildObjectiveExpression(
|
|
327
|
+
cpSatApi,
|
|
328
|
+
meetByPairAndRound,
|
|
329
|
+
visitedAny,
|
|
330
|
+
distinctPairHost
|
|
331
|
+
) {
|
|
332
|
+
const objectiveTerms = [];
|
|
333
|
+
for (const meetVars of meetByPairAndRound) {
|
|
334
|
+
for (const variable of meetVars) {
|
|
335
|
+
objectiveTerms.push(cpSatApi.LinearExpr.term(variable, 1000));
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
for (const variable of visitedAny.values()) {
|
|
339
|
+
objectiveTerms.push(cpSatApi.LinearExpr.term(variable, 1));
|
|
340
|
+
}
|
|
341
|
+
for (const variable of distinctPairHost.values()) {
|
|
342
|
+
objectiveTerms.push(cpSatApi.LinearExpr.term(variable, 5));
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
return objectiveTerms.length === 0
|
|
346
|
+
? cpSatApi.LinearExpr.constant(0)
|
|
347
|
+
: cpSatApi.LinearExpr.sum(objectiveTerms);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
export function buildCpSatModel(input, cpSatApi) {
|
|
351
|
+
if (!cpSatApi?.CpModel || !cpSatApi?.LinearExpr) {
|
|
352
|
+
throw new TypeError('buildCpSatModel requires CpModel and LinearExpr');
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const { participants, tables, rounds } = readDimensions(input);
|
|
356
|
+
const sameOncePairs = normalizePairs(input.same_once_pairs, participants);
|
|
357
|
+
const neverTogetherPairs = normalizePairs(
|
|
358
|
+
input.never_together_pairs,
|
|
359
|
+
participants
|
|
360
|
+
);
|
|
361
|
+
const model = new cpSatApi.CpModel();
|
|
362
|
+
const x = makeVariableGrid(participants, tables, rounds);
|
|
363
|
+
addParticipantVariables(model, x, participants, tables, rounds);
|
|
364
|
+
addOneTablePerParticipantAndRound(model, x, participants, tables, rounds);
|
|
365
|
+
|
|
366
|
+
const tableSizeVars = addTableBalance(
|
|
367
|
+
model,
|
|
368
|
+
cpSatApi.LinearExpr,
|
|
369
|
+
x,
|
|
370
|
+
participants,
|
|
371
|
+
tables,
|
|
372
|
+
rounds
|
|
373
|
+
);
|
|
374
|
+
fixHostAssignments(model, x, tables, rounds);
|
|
375
|
+
applyNeverTogetherConstraints(model, x, neverTogetherPairs, tables, rounds);
|
|
376
|
+
|
|
377
|
+
const { meetByPairAndRound, meetByPairAndHost } = addSameOnce(
|
|
378
|
+
model,
|
|
379
|
+
cpSatApi.LinearExpr,
|
|
380
|
+
x,
|
|
381
|
+
sameOncePairs,
|
|
382
|
+
tables,
|
|
383
|
+
rounds
|
|
384
|
+
);
|
|
385
|
+
const globalPairVars = addGlobalPairwiseUnique(
|
|
386
|
+
model,
|
|
387
|
+
cpSatApi.LinearExpr,
|
|
388
|
+
x,
|
|
389
|
+
participants,
|
|
390
|
+
tables,
|
|
391
|
+
rounds
|
|
392
|
+
);
|
|
393
|
+
const { visitedAny, distinctPairHost } = addHostDiversity(
|
|
394
|
+
model,
|
|
395
|
+
x,
|
|
396
|
+
sameOncePairs,
|
|
397
|
+
meetByPairAndHost,
|
|
398
|
+
participants,
|
|
399
|
+
tables,
|
|
400
|
+
rounds
|
|
401
|
+
);
|
|
402
|
+
|
|
403
|
+
model.maximize(
|
|
404
|
+
buildObjectiveExpression(
|
|
405
|
+
cpSatApi,
|
|
406
|
+
meetByPairAndRound,
|
|
407
|
+
visitedAny,
|
|
408
|
+
distinctPairHost
|
|
409
|
+
)
|
|
410
|
+
);
|
|
411
|
+
|
|
412
|
+
return {
|
|
413
|
+
model,
|
|
414
|
+
x,
|
|
415
|
+
tableSizeVars,
|
|
416
|
+
tableSizes: computeTableSizes(participants, tables),
|
|
417
|
+
participants,
|
|
418
|
+
tables,
|
|
419
|
+
rounds,
|
|
420
|
+
sameOncePairs,
|
|
421
|
+
neverTogetherPairs,
|
|
422
|
+
globalPairVars,
|
|
423
|
+
meetByPairAndRound,
|
|
424
|
+
meetByPairAndHost,
|
|
425
|
+
visitedAny,
|
|
426
|
+
distinctPairHost,
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function hasSolution(status) {
|
|
431
|
+
return SOLUTION_STATUSES.has(status);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
function pairSharesTable(assignments, left, right, round) {
|
|
435
|
+
return assignments[round].some(
|
|
436
|
+
(table) => table.includes(left) && table.includes(right)
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function getSolverStatus(solver, status) {
|
|
441
|
+
return typeof solver.statusName === 'function'
|
|
442
|
+
? solver.statusName(status)
|
|
443
|
+
: String(status);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function createAssignments(modelData, solver) {
|
|
447
|
+
const assignments = [];
|
|
448
|
+
|
|
449
|
+
for (let round = 0; round < modelData.rounds; round += 1) {
|
|
450
|
+
const roundTables = Array.from({ length: modelData.tables }, () => []);
|
|
451
|
+
for (let table = 1; table <= modelData.tables; table += 1) {
|
|
452
|
+
for (
|
|
453
|
+
let participant = 1;
|
|
454
|
+
participant <= modelData.participants;
|
|
455
|
+
participant += 1
|
|
456
|
+
) {
|
|
457
|
+
if (
|
|
458
|
+
Number(solver.value(modelData.x[participant][table][round])) === 1
|
|
459
|
+
) {
|
|
460
|
+
roundTables[table - 1].push(participant);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
assignments.push(roundTables);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
return assignments;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function classifySameOncePairs(sameOncePairs, assignments) {
|
|
471
|
+
const satisfiedSameOncePairs = [];
|
|
472
|
+
const unsatisfiedSameOncePairs = [];
|
|
473
|
+
|
|
474
|
+
for (const pair of sameOncePairs) {
|
|
475
|
+
const [left, right] = pair;
|
|
476
|
+
const meetingCount = assignments.reduce(
|
|
477
|
+
(count, _roundTables, round) =>
|
|
478
|
+
count + Number(pairSharesTable(assignments, left, right, round)),
|
|
479
|
+
0
|
|
480
|
+
);
|
|
481
|
+
if (meetingCount === 1) {
|
|
482
|
+
satisfiedSameOncePairs.push(pair);
|
|
483
|
+
} else {
|
|
484
|
+
unsatisfiedSameOncePairs.push(pair);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
return { satisfiedSameOncePairs, unsatisfiedSameOncePairs };
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
function findNeverTogetherViolations(pairs, assignments) {
|
|
492
|
+
const violations = [];
|
|
493
|
+
for (const pair of pairs) {
|
|
494
|
+
const [left, right] = pair;
|
|
495
|
+
const shareAnyTable = assignments.some((_roundTables, round) =>
|
|
496
|
+
pairSharesTable(assignments, left, right, round)
|
|
497
|
+
);
|
|
498
|
+
if (shareAnyTable) {
|
|
499
|
+
violations.push(pair);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
return violations;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
export function mapCpSatResponse(input, modelData, solver, status) {
|
|
507
|
+
const solverStatus = getSolverStatus(solver, status);
|
|
508
|
+
const resultHasSolution = hasSolution(solverStatus);
|
|
509
|
+
const assignments = resultHasSolution
|
|
510
|
+
? createAssignments(modelData, solver)
|
|
511
|
+
: [];
|
|
512
|
+
const tableSizesPerRound = assignments.map((roundTables) =>
|
|
513
|
+
roundTables.map((table) => table.length)
|
|
514
|
+
);
|
|
515
|
+
const { satisfiedSameOncePairs, unsatisfiedSameOncePairs } =
|
|
516
|
+
classifySameOncePairs(modelData.sameOncePairs, assignments);
|
|
517
|
+
const neverTogetherViolations = resultHasSolution
|
|
518
|
+
? findNeverTogetherViolations(modelData.neverTogetherPairs, assignments)
|
|
519
|
+
: [];
|
|
520
|
+
|
|
521
|
+
return {
|
|
522
|
+
participants: modelData.participants,
|
|
523
|
+
tables: modelData.tables,
|
|
524
|
+
rounds: modelData.rounds,
|
|
525
|
+
table_sizes: modelData.tableSizes,
|
|
526
|
+
table_sizes_per_round: tableSizesPerRound,
|
|
527
|
+
assignments,
|
|
528
|
+
satisfied_same_once_pairs: satisfiedSameOncePairs,
|
|
529
|
+
unsatisfied_same_once_pairs: unsatisfiedSameOncePairs,
|
|
530
|
+
never_together_violations: neverTogetherViolations,
|
|
531
|
+
objective_value: resultHasSolution
|
|
532
|
+
? Math.trunc(solver.objectiveValue())
|
|
533
|
+
: 0,
|
|
534
|
+
solver_status: solverStatus,
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
export async function solveSchedule(input, { cpSatApi } = {}) {
|
|
539
|
+
const api = cpSatApi ?? (await import('or-tools-wasm/cp-sat'));
|
|
540
|
+
const modelData = buildCpSatModel(input, api);
|
|
541
|
+
const solver = new api.CpSolver();
|
|
542
|
+
const timeLimitSeconds = Number(input.time_limit_seconds ?? 60);
|
|
543
|
+
if (!Number.isSafeInteger(timeLimitSeconds) || timeLimitSeconds < 1) {
|
|
544
|
+
throw new RangeError('time_limit_seconds must be a positive integer');
|
|
545
|
+
}
|
|
546
|
+
solver.parameters.maxTimeInSeconds = timeLimitSeconds;
|
|
547
|
+
solver.parameters.numSearchWorkers = 1;
|
|
548
|
+
|
|
549
|
+
const status = await solver.solve(modelData.model);
|
|
550
|
+
return mapCpSatResponse(input, modelData, solver, status);
|
|
551
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { solveSchedule } from './cpSatScheduler.js';
|
|
2
|
+
|
|
3
|
+
let activeSolve = null;
|
|
4
|
+
|
|
5
|
+
function createCancellationError() {
|
|
6
|
+
const error = new Error('Schedule generation was cancelled.');
|
|
7
|
+
error.name = 'AbortError';
|
|
8
|
+
return error;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export async function solveScheduleLocally(input, { onStatus } = {}) {
|
|
12
|
+
if (activeSolve) {
|
|
13
|
+
throw new Error('A browser schedule solve is already in progress.');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
let finishSolve;
|
|
17
|
+
const solve = {
|
|
18
|
+
cancelRequested: false,
|
|
19
|
+
cpSatApi: null,
|
|
20
|
+
solvePromise: null,
|
|
21
|
+
finished: new Promise((resolve) => {
|
|
22
|
+
finishSolve = resolve;
|
|
23
|
+
}),
|
|
24
|
+
finish: () => finishSolve(),
|
|
25
|
+
};
|
|
26
|
+
activeSolve = solve;
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
if (typeof window !== 'undefined' && !window.crossOriginIsolated) {
|
|
30
|
+
throw new Error(
|
|
31
|
+
'This page needs cross-origin isolation to load the browser solver.'
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const cpSatApi = await import('or-tools-wasm/cp-sat');
|
|
36
|
+
solve.cpSatApi = cpSatApi;
|
|
37
|
+
if (solve.cancelRequested) {
|
|
38
|
+
throw createCancellationError();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (!cpSatApi.CpSat.isWorkerBridgeEnabled()) {
|
|
42
|
+
cpSatApi.CpSat.setWorkerBridgeEnabled(true);
|
|
43
|
+
}
|
|
44
|
+
if (!cpSatApi.CpSat.isWorkerBridgeEnabled()) {
|
|
45
|
+
throw new Error('The OR-Tools browser worker bridge is unavailable.');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
solve.solvePromise = solveSchedule(input, { cpSatApi });
|
|
49
|
+
onStatus?.('solving');
|
|
50
|
+
const result = await solve.solvePromise;
|
|
51
|
+
if (solve.cancelRequested) {
|
|
52
|
+
throw createCancellationError();
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
55
|
+
} catch (error) {
|
|
56
|
+
if (solve.cancelRequested) {
|
|
57
|
+
throw createCancellationError();
|
|
58
|
+
}
|
|
59
|
+
throw error;
|
|
60
|
+
} finally {
|
|
61
|
+
try {
|
|
62
|
+
if (
|
|
63
|
+
solve.cancelRequested &&
|
|
64
|
+
solve.cpSatApi &&
|
|
65
|
+
!solve.cpSatApi.CpSat.isWorkerBridgeEnabled()
|
|
66
|
+
) {
|
|
67
|
+
solve.cpSatApi.CpSat.setWorkerBridgeEnabled(true);
|
|
68
|
+
}
|
|
69
|
+
} finally {
|
|
70
|
+
if (activeSolve === solve) {
|
|
71
|
+
activeSolve = null;
|
|
72
|
+
}
|
|
73
|
+
solve.finish();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export async function cancelScheduleSolve() {
|
|
79
|
+
const solve = activeSolve;
|
|
80
|
+
if (!solve) return false;
|
|
81
|
+
|
|
82
|
+
if (!solve.cancelRequested) {
|
|
83
|
+
solve.cancelRequested = true;
|
|
84
|
+
if (solve.solvePromise && solve.cpSatApi.CpSat.isWorkerBridgeEnabled()) {
|
|
85
|
+
try {
|
|
86
|
+
solve.cpSatApi.CpSat.setWorkerBridgeEnabled(false);
|
|
87
|
+
} catch (error) {
|
|
88
|
+
solve.cancelRequested = false;
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
await solve.finished;
|
|
95
|
+
return true;
|
|
96
|
+
}
|
package/vite.config.js
CHANGED
|
@@ -1,9 +1,21 @@
|
|
|
1
1
|
import { defineConfig } from 'vite';
|
|
2
2
|
import react from '@vitejs/plugin-react';
|
|
3
3
|
|
|
4
|
+
const isolationHeaders = {
|
|
5
|
+
'Cross-Origin-Opener-Policy': 'same-origin',
|
|
6
|
+
'Cross-Origin-Embedder-Policy': 'require-corp',
|
|
7
|
+
};
|
|
8
|
+
|
|
4
9
|
// https://vitejs.dev/config/
|
|
5
10
|
export default defineConfig({
|
|
6
11
|
plugins: [react()],
|
|
12
|
+
optimizeDeps: {
|
|
13
|
+
include: ['protobufjs'],
|
|
14
|
+
exclude: ['or-tools-wasm'],
|
|
15
|
+
},
|
|
16
|
+
worker: {
|
|
17
|
+
format: 'es',
|
|
18
|
+
},
|
|
7
19
|
build: {
|
|
8
20
|
outDir: 'docs',
|
|
9
21
|
emptyOutDir: true, // Clean docs folder on build
|
|
@@ -15,7 +27,13 @@ export default defineConfig({
|
|
|
15
27
|
},
|
|
16
28
|
base: './', // Use relative paths for GitHub Pages
|
|
17
29
|
server: {
|
|
30
|
+
host: '127.0.0.1',
|
|
18
31
|
port: 5173,
|
|
19
32
|
open: true,
|
|
33
|
+
headers: isolationHeaders,
|
|
34
|
+
},
|
|
35
|
+
preview: {
|
|
36
|
+
host: '127.0.0.1',
|
|
37
|
+
headers: isolationHeaders,
|
|
20
38
|
},
|
|
21
39
|
});
|
package/src/config.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
// API Configuration
|
|
2
|
-
// Override this in your deployment or set via environment
|
|
3
|
-
export const API_CONFIG = {
|
|
4
|
-
// Default backend URL - update this to point to your backend
|
|
5
|
-
// For local development: "http://localhost:8000"
|
|
6
|
-
// For production: "http://your-ubuntu-workstation-ip:8000" or your domain
|
|
7
|
-
BACKEND_URL:
|
|
8
|
-
window.location.hostname === 'localhost' ||
|
|
9
|
-
window.location.hostname === '127.0.0.1'
|
|
10
|
-
? 'http://localhost:8000'
|
|
11
|
-
: 'http://localhost:8000', // Update this with your backend URL
|
|
12
|
-
|
|
13
|
-
// API endpoints
|
|
14
|
-
ENDPOINTS: {
|
|
15
|
-
SCHEDULE: '/api/schedule',
|
|
16
|
-
HEALTH: '/health',
|
|
17
|
-
},
|
|
18
|
-
|
|
19
|
-
// Timeout for API calls (milliseconds)
|
|
20
|
-
TIMEOUT: 120000, // 2 minutes for solver
|
|
21
|
-
};
|