javascript-lp-solver 1.0.0 → 1.0.3
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 +9 -9
- package/dist/index.browser.mjs +515 -774
- package/dist/index.browser.mjs.map +1 -1
- package/dist/index.cjs +515 -774
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +38 -33
- package/dist/index.mjs +515 -774
- package/dist/index.mjs.map +1 -1
- package/dist/solver.global.js +515 -774
- package/dist/solver.global.js.map +1 -1
- package/dist/types/main.d.ts +1 -1
- package/dist/types/polyopt.d.ts +1 -1
- package/dist/types/shims/external.d.ts +2 -6
- package/dist/types/tableau/index.d.ts +1 -4
- package/dist/types/tableau/min-heap.d.ts +4 -0
- package/dist/types/tableau/presolve.d.ts +0 -10
- package/dist/types/tableau/simplex.d.ts +16 -0
- package/dist/types/tableau/solution.d.ts +0 -1
- package/dist/types/tableau/tableau.d.ts +6 -0
- package/dist/types/tableau/types.d.ts +4 -21
- package/dist/types/test-utils/problem-generator.d.ts +2 -2
- package/package.json +9 -1
- package/scripts/ts-node-register.js +1 -1
package/dist/index.cjs
CHANGED
|
@@ -56,6 +56,22 @@ class BranchMinHeap {
|
|
|
56
56
|
this.heap = new Array(initialCapacity);
|
|
57
57
|
this.size = 0;
|
|
58
58
|
this.seqCounter = 0;
|
|
59
|
+
this.pool = new Array(64);
|
|
60
|
+
this.poolSize = 0;
|
|
61
|
+
}
|
|
62
|
+
allocEntry(branch, seq) {
|
|
63
|
+
if (this.poolSize > 0) {
|
|
64
|
+
const entry = this.pool[--this.poolSize];
|
|
65
|
+
entry.branch = branch;
|
|
66
|
+
entry.seq = seq;
|
|
67
|
+
return entry;
|
|
68
|
+
}
|
|
69
|
+
return { branch, seq };
|
|
70
|
+
}
|
|
71
|
+
freeEntry(entry) {
|
|
72
|
+
if (this.poolSize < 256) {
|
|
73
|
+
this.pool[this.poolSize++] = entry;
|
|
74
|
+
}
|
|
59
75
|
}
|
|
60
76
|
get length() {
|
|
61
77
|
return this.size;
|
|
@@ -83,7 +99,7 @@ class BranchMinHeap {
|
|
|
83
99
|
if (idx >= heap.length) {
|
|
84
100
|
heap.length = heap.length * 2;
|
|
85
101
|
}
|
|
86
|
-
const entry =
|
|
102
|
+
const entry = this.allocEntry(branch, this.seqCounter++);
|
|
87
103
|
// Bubble up
|
|
88
104
|
while (idx > 0) {
|
|
89
105
|
const parentIdx = (idx - 1) >> 1;
|
|
@@ -101,8 +117,11 @@ class BranchMinHeap {
|
|
|
101
117
|
return undefined;
|
|
102
118
|
}
|
|
103
119
|
const heap = this.heap;
|
|
104
|
-
const
|
|
120
|
+
const poppedEntry = heap[0];
|
|
121
|
+
const result = poppedEntry.branch;
|
|
105
122
|
this.size--;
|
|
123
|
+
// Return entry to pool
|
|
124
|
+
this.freeEntry(poppedEntry);
|
|
106
125
|
if (this.size === 0) {
|
|
107
126
|
return result;
|
|
108
127
|
}
|
|
@@ -145,15 +164,16 @@ function createBranchAndCutService() {
|
|
|
145
164
|
tableau.addCutConstraints(branchingCuts);
|
|
146
165
|
tableau.simplex();
|
|
147
166
|
if ((_a = tableau.model) === null || _a === void 0 ? void 0 : _a.useMIRCuts) {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
167
|
+
// Optimization: reuse previous "after" as next "before" to avoid redundant computation
|
|
168
|
+
let fractionalVolume = tableau.computeFractionalVolume(true);
|
|
169
|
+
while (fractionalVolume > 0) {
|
|
151
170
|
tableau.applyMIRCuts();
|
|
152
171
|
tableau.simplex();
|
|
153
172
|
const fractionalVolumeAfter = tableau.computeFractionalVolume(true);
|
|
154
|
-
if (fractionalVolumeAfter >= 0.9 *
|
|
155
|
-
|
|
173
|
+
if (fractionalVolumeAfter >= 0.9 * fractionalVolume) {
|
|
174
|
+
break;
|
|
156
175
|
}
|
|
176
|
+
fractionalVolume = fractionalVolumeAfter;
|
|
157
177
|
}
|
|
158
178
|
}
|
|
159
179
|
};
|
|
@@ -170,7 +190,10 @@ function createBranchAndCutService() {
|
|
|
170
190
|
let bestEvaluation = Infinity;
|
|
171
191
|
let bestBranch = null;
|
|
172
192
|
const bestOptionalObjectivesEvaluations = [];
|
|
173
|
-
|
|
193
|
+
// Cache optionalObjectives reference to avoid repeated property lookups in hot loop
|
|
194
|
+
const optionalObjectives = tableau.optionalObjectives;
|
|
195
|
+
const nOptionalObjectives = optionalObjectives.length;
|
|
196
|
+
for (let oInit = 0; oInit < nOptionalObjectives; oInit += 1) {
|
|
174
197
|
bestOptionalObjectivesEvaluations.push(Infinity);
|
|
175
198
|
}
|
|
176
199
|
const branch = createBranch$2(-Infinity, []);
|
|
@@ -189,7 +212,7 @@ function createBranchAndCutService() {
|
|
|
189
212
|
}
|
|
190
213
|
}
|
|
191
214
|
const activeBranch = branches.pop();
|
|
192
|
-
if (activeBranch.relaxedEvaluation
|
|
215
|
+
if (activeBranch.relaxedEvaluation >= bestEvaluation) {
|
|
193
216
|
continue;
|
|
194
217
|
}
|
|
195
218
|
const cuts = activeBranch.cuts;
|
|
@@ -204,13 +227,13 @@ function createBranchAndCutService() {
|
|
|
204
227
|
}
|
|
205
228
|
if (evaluation === bestEvaluation) {
|
|
206
229
|
let isCurrentEvaluationWorse = true;
|
|
207
|
-
for (let o = 0; o <
|
|
208
|
-
|
|
209
|
-
|
|
230
|
+
for (let o = 0; o < nOptionalObjectives; o += 1) {
|
|
231
|
+
const currentCost = optionalObjectives[o].reducedCosts[0];
|
|
232
|
+
const bestCost = bestOptionalObjectivesEvaluations[o];
|
|
233
|
+
if (currentCost > bestCost) {
|
|
210
234
|
break;
|
|
211
235
|
}
|
|
212
|
-
else if (
|
|
213
|
-
bestOptionalObjectivesEvaluations[o]) {
|
|
236
|
+
else if (currentCost < bestCost) {
|
|
214
237
|
isCurrentEvaluationWorse = false;
|
|
215
238
|
break;
|
|
216
239
|
}
|
|
@@ -227,9 +250,9 @@ function createBranchAndCutService() {
|
|
|
227
250
|
}
|
|
228
251
|
bestBranch = activeBranch;
|
|
229
252
|
bestEvaluation = evaluation;
|
|
230
|
-
for (let oCopy = 0; oCopy <
|
|
253
|
+
for (let oCopy = 0; oCopy < nOptionalObjectives; oCopy += 1) {
|
|
231
254
|
bestOptionalObjectivesEvaluations[oCopy] =
|
|
232
|
-
|
|
255
|
+
optionalObjectives[oCopy].reducedCosts[0];
|
|
233
256
|
}
|
|
234
257
|
if ((_e = tableau.model) === null || _e === void 0 ? void 0 : _e.keep_solutions) {
|
|
235
258
|
const nowSolution = tableau.model.tableau.getSolution();
|
|
@@ -247,6 +270,7 @@ function createBranchAndCutService() {
|
|
|
247
270
|
}
|
|
248
271
|
const variable = tableau.getMostFractionalVar();
|
|
249
272
|
const varIndex = variable.index;
|
|
273
|
+
const varValue = variable.value;
|
|
250
274
|
const cutsHigh = [];
|
|
251
275
|
const cutsLow = [];
|
|
252
276
|
const nCuts = cuts.length;
|
|
@@ -265,11 +289,9 @@ function createBranchAndCutService() {
|
|
|
265
289
|
cutsLow.push(cut);
|
|
266
290
|
}
|
|
267
291
|
}
|
|
268
|
-
const
|
|
269
|
-
const max = Math.floor(variable.value);
|
|
270
|
-
const cutHigh = createCut$2("min", varIndex, min);
|
|
292
|
+
const cutHigh = createCut$2("min", varIndex, Math.ceil(varValue));
|
|
271
293
|
cutsHigh.push(cutHigh);
|
|
272
|
-
const cutLow = createCut$2("max", varIndex,
|
|
294
|
+
const cutLow = createCut$2("max", varIndex, Math.floor(varValue));
|
|
273
295
|
cutsLow.push(cutLow);
|
|
274
296
|
branches.push(createBranch$2(evaluation, cutsHigh));
|
|
275
297
|
branches.push(createBranch$2(evaluation, cutsLow));
|
|
@@ -283,6 +305,50 @@ function createBranchAndCutService() {
|
|
|
283
305
|
return { applyCuts, branchAndCut };
|
|
284
306
|
}
|
|
285
307
|
|
|
308
|
+
/**
|
|
309
|
+
* Optimized cycle detector using hash-based O(1) lookup.
|
|
310
|
+
* The original checkForCycles was O(n²) - comparing every pair against all others.
|
|
311
|
+
* This version uses a Map to track where each (leaving,entering) pair occurred,
|
|
312
|
+
* making duplicate detection O(1) average case.
|
|
313
|
+
*/
|
|
314
|
+
class CycleDetector {
|
|
315
|
+
constructor() {
|
|
316
|
+
this.pairs = [];
|
|
317
|
+
this.positions = new Map();
|
|
318
|
+
}
|
|
319
|
+
add(leaving, entering) {
|
|
320
|
+
const key = `${leaving}_${entering}`;
|
|
321
|
+
const pos = this.pairs.length;
|
|
322
|
+
this.pairs.push([leaving, entering]);
|
|
323
|
+
const prevPositions = this.positions.get(key);
|
|
324
|
+
if (prevPositions === undefined) {
|
|
325
|
+
this.positions.set(key, [pos]);
|
|
326
|
+
return [];
|
|
327
|
+
}
|
|
328
|
+
// Check if any previous occurrence starts a repeating cycle
|
|
329
|
+
for (const startPos of prevPositions) {
|
|
330
|
+
const cycleLength = pos - startPos;
|
|
331
|
+
// Need at least cycleLength more elements to verify
|
|
332
|
+
if (cycleLength > this.pairs.length - pos) {
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
let cycleFound = true;
|
|
336
|
+
for (let i = 1; i < cycleLength && startPos + cycleLength + i < this.pairs.length; i++) {
|
|
337
|
+
const p1 = this.pairs[startPos + i];
|
|
338
|
+
const p2 = this.pairs[startPos + cycleLength + i];
|
|
339
|
+
if (p1[0] !== p2[0] || p1[1] !== p2[1]) {
|
|
340
|
+
cycleFound = false;
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
if (cycleFound) {
|
|
345
|
+
return [startPos, cycleLength];
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
prevPositions.push(pos);
|
|
349
|
+
return [];
|
|
350
|
+
}
|
|
351
|
+
}
|
|
286
352
|
function simplex() {
|
|
287
353
|
this.bounded = true;
|
|
288
354
|
this.phase1();
|
|
@@ -291,21 +357,125 @@ function simplex() {
|
|
|
291
357
|
}
|
|
292
358
|
return this;
|
|
293
359
|
}
|
|
360
|
+
/**
|
|
361
|
+
* Dual simplex algorithm for warm-starting after adding constraints.
|
|
362
|
+
*
|
|
363
|
+
* Use when: The current solution is dual feasible (reduced costs valid) but
|
|
364
|
+
* may be primal infeasible (some RHS values negative). This is common after
|
|
365
|
+
* adding bound constraints in branch-and-cut.
|
|
366
|
+
*
|
|
367
|
+
* Algorithm:
|
|
368
|
+
* 1. Find a basic variable with negative value (leaving variable)
|
|
369
|
+
* 2. Find entering variable using dual ratio test
|
|
370
|
+
* 3. Pivot to restore primal feasibility
|
|
371
|
+
* 4. Repeat until all basic variables are non-negative
|
|
372
|
+
*
|
|
373
|
+
* @returns Number of iterations, or -1 if dual infeasible
|
|
374
|
+
*/
|
|
375
|
+
function dualSimplex() {
|
|
376
|
+
const matrix = this.matrix;
|
|
377
|
+
const width = this.width;
|
|
378
|
+
const rhsColumn = this.rhsColumn;
|
|
379
|
+
const lastColumn = width - 1;
|
|
380
|
+
const lastRow = this.height - 1;
|
|
381
|
+
const precision = this.precision;
|
|
382
|
+
const negPrecision = -precision;
|
|
383
|
+
let iterations = 0;
|
|
384
|
+
const maxIterations = 10000; // Safety limit
|
|
385
|
+
while (iterations < maxIterations) {
|
|
386
|
+
// Step 1: Find leaving variable (row with most negative RHS)
|
|
387
|
+
let leavingRow = 0;
|
|
388
|
+
let minRHS = negPrecision;
|
|
389
|
+
for (let r = 1; r <= lastRow; r++) {
|
|
390
|
+
const rhsValue = matrix[r * width + rhsColumn];
|
|
391
|
+
if (rhsValue < minRHS) {
|
|
392
|
+
minRHS = rhsValue;
|
|
393
|
+
leavingRow = r;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
// If no negative RHS, we're primal feasible - done!
|
|
397
|
+
if (leavingRow === 0) {
|
|
398
|
+
this.feasible = true;
|
|
399
|
+
this.setEvaluation();
|
|
400
|
+
return iterations;
|
|
401
|
+
}
|
|
402
|
+
// Step 2: Find entering variable using dual ratio test
|
|
403
|
+
// For each non-basic variable j with a_ij < 0 (negative coefficient in leaving row),
|
|
404
|
+
// compute ratio = reduced_cost[j] / |a_ij|
|
|
405
|
+
// Choose the one with minimum ratio (to maintain dual feasibility)
|
|
406
|
+
let enteringColumn = 0;
|
|
407
|
+
let minRatio = Infinity;
|
|
408
|
+
const leavingRowOffset = leavingRow * width;
|
|
409
|
+
for (let c = 1; c <= lastColumn; c++) {
|
|
410
|
+
const coefficient = matrix[leavingRowOffset + c];
|
|
411
|
+
// Only consider columns with negative coefficient in leaving row
|
|
412
|
+
if (coefficient < negPrecision) {
|
|
413
|
+
// Reduced cost is in row 0 (cost row)
|
|
414
|
+
const reducedCost = matrix[c];
|
|
415
|
+
// For minimization, reduced costs should be >= 0 for optimality
|
|
416
|
+
// Ratio test: reducedCost / |coefficient|
|
|
417
|
+
if (reducedCost >= negPrecision) {
|
|
418
|
+
const ratio = reducedCost / -coefficient;
|
|
419
|
+
if (ratio < minRatio) {
|
|
420
|
+
minRatio = ratio;
|
|
421
|
+
enteringColumn = c;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
// If no entering column found, the problem is dual infeasible (primal unbounded)
|
|
427
|
+
if (enteringColumn === 0) {
|
|
428
|
+
this.feasible = false;
|
|
429
|
+
return -1;
|
|
430
|
+
}
|
|
431
|
+
// Step 3: Pivot
|
|
432
|
+
this.pivot(leavingRow, enteringColumn);
|
|
433
|
+
iterations++;
|
|
434
|
+
}
|
|
435
|
+
// Hit iteration limit - something went wrong
|
|
436
|
+
this.feasible = false;
|
|
437
|
+
return iterations;
|
|
438
|
+
}
|
|
294
439
|
function phase1() {
|
|
295
440
|
const debugCheckForCycles = this.model.checkForCycles;
|
|
296
|
-
const
|
|
441
|
+
const cycleDetector = debugCheckForCycles ? new CycleDetector() : null;
|
|
297
442
|
const matrix = this.matrix;
|
|
298
443
|
const width = this.width;
|
|
299
444
|
const rhsColumn = this.rhsColumn;
|
|
300
445
|
const lastColumn = this.width - 1;
|
|
301
446
|
const lastRow = this.height - 1;
|
|
302
|
-
|
|
447
|
+
const precision = this.precision;
|
|
448
|
+
const negPrecision = -precision;
|
|
449
|
+
// Cache arrays for faster access in hot loops
|
|
450
|
+
const unrestrictedVars = this.unrestrictedVars;
|
|
451
|
+
const varIndexByRow = this.varIndexByRow;
|
|
452
|
+
const varIndexByCol = this.varIndexByCol;
|
|
453
|
+
// Anti-cycling: on degenerate problems, the max-quotient pivot rule
|
|
454
|
+
// can oscillate, corrupting the matrix. We detect stalling early
|
|
455
|
+
// (after SAVE_THRESHOLD iterations without RHS improvement) and save
|
|
456
|
+
// the matrix state. If still stuck after maxQuotientLimit iterations,
|
|
457
|
+
// we restore the clean matrix and switch to Bland's rule. The lazy
|
|
458
|
+
// save avoids the copy cost on the vast majority of phase1 calls
|
|
459
|
+
// that converge quickly.
|
|
460
|
+
const SAVE_THRESHOLD = 10;
|
|
461
|
+
const maxQuotientLimit = Math.max(lastRow, lastColumn);
|
|
303
462
|
let iterations = 0;
|
|
463
|
+
let useBland = false;
|
|
464
|
+
let initialRHS = -Infinity;
|
|
465
|
+
let savedMatrix = null;
|
|
466
|
+
let savedVarIndexByRow = null;
|
|
467
|
+
let savedVarIndexByCol = null;
|
|
468
|
+
let savedRowByVarIndex = null;
|
|
469
|
+
let savedColByVarIndex = null;
|
|
304
470
|
while (true) {
|
|
471
|
+
// Find leaving row (most negative RHS among restricted basic vars).
|
|
472
|
+
// Unrestricted variables can validly have negative values, so rows
|
|
473
|
+
// where the basic variable is unrestricted are not infeasible.
|
|
305
474
|
let leavingRowIndex = 0;
|
|
306
|
-
let rhsValue =
|
|
475
|
+
let rhsValue = negPrecision;
|
|
307
476
|
for (let r = 1; r <= lastRow; r++) {
|
|
308
|
-
|
|
477
|
+
if (unrestrictedVars[varIndexByRow[r]] === true)
|
|
478
|
+
continue;
|
|
309
479
|
const value = matrix[r * width + rhsColumn];
|
|
310
480
|
if (value < rhsValue) {
|
|
311
481
|
rhsValue = value;
|
|
@@ -316,18 +486,95 @@ function phase1() {
|
|
|
316
486
|
this.feasible = true;
|
|
317
487
|
return iterations;
|
|
318
488
|
}
|
|
489
|
+
// Detect non-convergence and apply anti-cycling.
|
|
490
|
+
if (!useBland && iterations > 0 && rhsValue <= initialRHS) {
|
|
491
|
+
if (iterations >= SAVE_THRESHOLD && savedMatrix === null) {
|
|
492
|
+
// Stalling detected: save matrix for potential rollback
|
|
493
|
+
savedMatrix = matrix.slice();
|
|
494
|
+
savedVarIndexByRow = varIndexByRow.slice();
|
|
495
|
+
savedVarIndexByCol = varIndexByCol.slice();
|
|
496
|
+
savedRowByVarIndex = this.rowByVarIndex.slice();
|
|
497
|
+
savedColByVarIndex = this.colByVarIndex.slice();
|
|
498
|
+
}
|
|
499
|
+
if (iterations >= maxQuotientLimit) {
|
|
500
|
+
// Still stuck after generous budget: restore and switch
|
|
501
|
+
useBland = true;
|
|
502
|
+
if (savedMatrix) {
|
|
503
|
+
matrix.set(savedMatrix);
|
|
504
|
+
for (let i = 0; i < savedVarIndexByRow.length; i++) {
|
|
505
|
+
varIndexByRow[i] = savedVarIndexByRow[i];
|
|
506
|
+
}
|
|
507
|
+
for (let i = 0; i < savedVarIndexByCol.length; i++) {
|
|
508
|
+
varIndexByCol[i] = savedVarIndexByCol[i];
|
|
509
|
+
}
|
|
510
|
+
for (let i = 0; i < savedRowByVarIndex.length; i++) {
|
|
511
|
+
this.rowByVarIndex[i] = savedRowByVarIndex[i];
|
|
512
|
+
}
|
|
513
|
+
for (let i = 0; i < savedColByVarIndex.length; i++) {
|
|
514
|
+
this.colByVarIndex[i] = savedColByVarIndex[i];
|
|
515
|
+
}
|
|
516
|
+
iterations = 0;
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
if (iterations === 0) {
|
|
522
|
+
initialRHS = rhsValue;
|
|
523
|
+
}
|
|
524
|
+
// Find entering column.
|
|
525
|
+
// Prefer columns with negative coefficient in leaving row (these directly
|
|
526
|
+
// fix the infeasibility by making RHS positive after pivot).
|
|
527
|
+
// Only fall back to unrestricted variables with positive coefficient when
|
|
528
|
+
// no negative coefficient column exists - this "swaps" the infeasibility
|
|
529
|
+
// to an unrestricted variable (which is allowed to be negative).
|
|
319
530
|
let enteringColumn = 0;
|
|
320
|
-
let maxQuotient = -Infinity;
|
|
321
|
-
const costRowOffset = 0; // row 0
|
|
322
531
|
const leavingRowOffset = leavingRowIndex * width;
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
const
|
|
328
|
-
if (
|
|
329
|
-
maxQuotient = quotient;
|
|
532
|
+
if (useBland) {
|
|
533
|
+
// Bland's rule: pick the first eligible column (smallest index).
|
|
534
|
+
// First pass: negative coefficients only (directly fix infeasibility)
|
|
535
|
+
for (let c = 1; c <= lastColumn; c++) {
|
|
536
|
+
const coefficient = matrix[leavingRowOffset + c];
|
|
537
|
+
if (coefficient < negPrecision) {
|
|
330
538
|
enteringColumn = c;
|
|
539
|
+
break;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
// Fallback: unrestricted with non-zero coefficient (swap infeasibility)
|
|
543
|
+
if (enteringColumn === 0) {
|
|
544
|
+
for (let c = 1; c <= lastColumn; c++) {
|
|
545
|
+
const coefficient = matrix[leavingRowOffset + c];
|
|
546
|
+
if (unrestrictedVars[varIndexByCol[c]] === true &&
|
|
547
|
+
(coefficient < negPrecision || coefficient > precision)) {
|
|
548
|
+
enteringColumn = c;
|
|
549
|
+
break;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
else {
|
|
555
|
+
// Max-quotient rule: faster in practice but can oscillate on
|
|
556
|
+
// degenerate problems.
|
|
557
|
+
// First pass: negative coefficients only
|
|
558
|
+
let maxQuotient = -Infinity;
|
|
559
|
+
for (let c = 1; c <= lastColumn; c++) {
|
|
560
|
+
const coefficient = matrix[leavingRowOffset + c];
|
|
561
|
+
if (coefficient < negPrecision) {
|
|
562
|
+
const quotient = -matrix[c] / coefficient;
|
|
563
|
+
if (maxQuotient < quotient) {
|
|
564
|
+
maxQuotient = quotient;
|
|
565
|
+
enteringColumn = c;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
// Fallback: unrestricted with non-zero coefficient
|
|
570
|
+
if (enteringColumn === 0) {
|
|
571
|
+
for (let c = 1; c <= lastColumn; c++) {
|
|
572
|
+
const coefficient = matrix[leavingRowOffset + c];
|
|
573
|
+
if (unrestrictedVars[varIndexByCol[c]] === true &&
|
|
574
|
+
(coefficient < negPrecision || coefficient > precision)) {
|
|
575
|
+
enteringColumn = c;
|
|
576
|
+
break;
|
|
577
|
+
}
|
|
331
578
|
}
|
|
332
579
|
}
|
|
333
580
|
}
|
|
@@ -335,12 +582,8 @@ function phase1() {
|
|
|
335
582
|
this.feasible = false;
|
|
336
583
|
return iterations;
|
|
337
584
|
}
|
|
338
|
-
if (
|
|
339
|
-
|
|
340
|
-
this.varIndexByRow[leavingRowIndex],
|
|
341
|
-
this.varIndexByCol[enteringColumn],
|
|
342
|
-
]);
|
|
343
|
-
const cycleData = this.checkForCycles(varIndexesCycle);
|
|
585
|
+
if (cycleDetector) {
|
|
586
|
+
const cycleData = cycleDetector.add(varIndexByRow[leavingRowIndex], varIndexByCol[enteringColumn]);
|
|
344
587
|
if (cycleData.length > 0) {
|
|
345
588
|
this.model.messages.push("Cycle in phase 1");
|
|
346
589
|
this.model.messages.push("Start :" + cycleData[0]);
|
|
@@ -355,18 +598,38 @@ function phase1() {
|
|
|
355
598
|
}
|
|
356
599
|
function phase2() {
|
|
357
600
|
const debugCheckForCycles = this.model.checkForCycles;
|
|
358
|
-
const
|
|
601
|
+
const cycleDetector = debugCheckForCycles ? new CycleDetector() : null;
|
|
359
602
|
const matrix = this.matrix;
|
|
360
603
|
const width = this.width;
|
|
361
604
|
const rhsColumn = this.rhsColumn;
|
|
362
605
|
const lastColumn = this.width - 1;
|
|
363
606
|
const lastRow = this.height - 1;
|
|
364
607
|
const precision = this.precision;
|
|
608
|
+
const negPrecision = -precision;
|
|
365
609
|
const nOptionalObjectives = this.optionalObjectives.length;
|
|
366
610
|
let optionalCostsColumns = null;
|
|
611
|
+
// Cache arrays for faster access in hot loops
|
|
612
|
+
const unrestrictedVars = this.unrestrictedVars;
|
|
613
|
+
const varIndexByCol = this.varIndexByCol;
|
|
614
|
+
const varIndexByRow = this.varIndexByRow;
|
|
615
|
+
// Note: costRowIndex is always 0, so we access matrix[c] directly
|
|
367
616
|
let iterations = 0;
|
|
368
617
|
let reducedCost;
|
|
369
618
|
let unrestricted;
|
|
619
|
+
// Anti-cycling for phase 2: if the objective stalls (no meaningful
|
|
620
|
+
// improvement over several hundred iterations), the simplex is
|
|
621
|
+
// cycling through degenerate vertices. Switch to Bland's rule which
|
|
622
|
+
// guarantees termination in exact arithmetic. In floating-point,
|
|
623
|
+
// even Bland's can cycle indefinitely, so we also impose a limit:
|
|
624
|
+
// if Bland's runs for lastRow iterations without improving the
|
|
625
|
+
// objective, we accept the current solution as optimal.
|
|
626
|
+
const PHASE2_WINDOW = 100;
|
|
627
|
+
const PHASE2_STALE_LIMIT = 5; // consecutive stale windows before switching
|
|
628
|
+
let useBland = false;
|
|
629
|
+
let windowStartObj = matrix[rhsColumn];
|
|
630
|
+
let staleWindows = 0;
|
|
631
|
+
let blandStartIter = 0;
|
|
632
|
+
let blandStartObj = 0;
|
|
370
633
|
// Partial pricing setup
|
|
371
634
|
// Batch size: use configured value or auto-compute (sqrt of columns, min 50, max 500)
|
|
372
635
|
const nColumns = lastColumn;
|
|
@@ -376,14 +639,68 @@ function phase2() {
|
|
|
376
639
|
// For small problems, just scan everything (no benefit from partial pricing)
|
|
377
640
|
const usePartialPricing = nColumns > batchSize * 2;
|
|
378
641
|
while (true) {
|
|
379
|
-
const costRowOffset = this.costRowIndex * width;
|
|
380
642
|
if (nOptionalObjectives > 0) {
|
|
381
643
|
optionalCostsColumns = [];
|
|
382
644
|
}
|
|
645
|
+
// Detect degenerate cycling: check every WINDOW iterations
|
|
646
|
+
// whether the objective has made meaningful progress.
|
|
647
|
+
if (!useBland && iterations > 0 && iterations % PHASE2_WINDOW === 0) {
|
|
648
|
+
const currentObj = matrix[rhsColumn];
|
|
649
|
+
const delta = Math.abs(currentObj - windowStartObj);
|
|
650
|
+
const scale = Math.max(1, Math.abs(windowStartObj));
|
|
651
|
+
if (delta / scale < 1e-10) {
|
|
652
|
+
staleWindows++;
|
|
653
|
+
if (staleWindows >= PHASE2_STALE_LIMIT) {
|
|
654
|
+
useBland = true;
|
|
655
|
+
blandStartIter = iterations;
|
|
656
|
+
blandStartObj = currentObj;
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
else {
|
|
660
|
+
staleWindows = 0;
|
|
661
|
+
}
|
|
662
|
+
windowStartObj = currentObj;
|
|
663
|
+
}
|
|
664
|
+
// Bland's termination: in floating-point arithmetic, Bland's rule
|
|
665
|
+
// can cycle through degenerate bases indefinitely. If it hasn't
|
|
666
|
+
// improved the objective after lastRow pivots, accept the current
|
|
667
|
+
// solution (reduced costs are within floating-point noise of zero).
|
|
668
|
+
if (useBland && iterations - blandStartIter > lastRow) {
|
|
669
|
+
const currentObj = matrix[rhsColumn];
|
|
670
|
+
const delta = Math.abs(currentObj - blandStartObj);
|
|
671
|
+
const scale = Math.max(1, Math.abs(blandStartObj));
|
|
672
|
+
if (delta / scale < 1e-10) {
|
|
673
|
+
this.setEvaluation();
|
|
674
|
+
this.simplexIters += 1;
|
|
675
|
+
return iterations;
|
|
676
|
+
}
|
|
677
|
+
// Objective did improve; reset the counter
|
|
678
|
+
blandStartIter = iterations;
|
|
679
|
+
blandStartObj = currentObj;
|
|
680
|
+
}
|
|
383
681
|
let enteringColumn = 0;
|
|
384
682
|
let enteringValue = precision;
|
|
385
683
|
let isReducedCostNegative = false;
|
|
386
|
-
if (
|
|
684
|
+
if (useBland) {
|
|
685
|
+
// Bland's rule: pick first eligible column (smallest index)
|
|
686
|
+
for (let c = 1; c <= lastColumn; c++) {
|
|
687
|
+
reducedCost = matrix[c];
|
|
688
|
+
unrestricted = unrestrictedVars[varIndexByCol[c]] === true;
|
|
689
|
+
if (unrestricted && reducedCost < 0) {
|
|
690
|
+
enteringColumn = c;
|
|
691
|
+
enteringValue = -reducedCost;
|
|
692
|
+
isReducedCostNegative = true;
|
|
693
|
+
break;
|
|
694
|
+
}
|
|
695
|
+
if (reducedCost > precision) {
|
|
696
|
+
enteringColumn = c;
|
|
697
|
+
enteringValue = reducedCost;
|
|
698
|
+
isReducedCostNegative = false;
|
|
699
|
+
break;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
else if (usePartialPricing) {
|
|
387
704
|
// Partial pricing: scan columns in batches
|
|
388
705
|
const startBatch = this.pricingBatchStart;
|
|
389
706
|
let batchesScanned = 0;
|
|
@@ -393,10 +710,10 @@ function phase2() {
|
|
|
393
710
|
const batchStart = this.pricingBatchStart;
|
|
394
711
|
const batchEnd = Math.min(batchStart + batchSize - 1, lastColumn);
|
|
395
712
|
for (let c = batchStart; c <= batchEnd; c++) {
|
|
396
|
-
reducedCost = matrix[costRowOffset
|
|
397
|
-
unrestricted =
|
|
713
|
+
reducedCost = matrix[c]; // costRowOffset is 0
|
|
714
|
+
unrestricted = unrestrictedVars[varIndexByCol[c]] === true;
|
|
398
715
|
if (nOptionalObjectives > 0 &&
|
|
399
|
-
|
|
716
|
+
negPrecision < reducedCost &&
|
|
400
717
|
reducedCost < precision) {
|
|
401
718
|
optionalCostsColumns === null || optionalCostsColumns === void 0 ? void 0 : optionalCostsColumns.push(c);
|
|
402
719
|
continue;
|
|
@@ -427,10 +744,10 @@ function phase2() {
|
|
|
427
744
|
else {
|
|
428
745
|
// Full pricing for small problems
|
|
429
746
|
for (let c = 1; c <= lastColumn; c++) {
|
|
430
|
-
reducedCost = matrix[costRowOffset
|
|
431
|
-
unrestricted =
|
|
747
|
+
reducedCost = matrix[c]; // costRowOffset is 0
|
|
748
|
+
unrestricted = unrestrictedVars[varIndexByCol[c]] === true;
|
|
432
749
|
if (nOptionalObjectives > 0 &&
|
|
433
|
-
|
|
750
|
+
negPrecision < reducedCost &&
|
|
434
751
|
reducedCost < precision) {
|
|
435
752
|
optionalCostsColumns === null || optionalCostsColumns === void 0 ? void 0 : optionalCostsColumns.push(c);
|
|
436
753
|
continue;
|
|
@@ -462,8 +779,8 @@ function phase2() {
|
|
|
462
779
|
for (let i = 0; i < optionalCostsColumns.length; i++) {
|
|
463
780
|
const c = optionalCostsColumns[i];
|
|
464
781
|
reducedCost = reducedCosts[c];
|
|
465
|
-
unrestricted =
|
|
466
|
-
if (
|
|
782
|
+
unrestricted = unrestrictedVars[varIndexByCol[c]] === true;
|
|
783
|
+
if (negPrecision < reducedCost && reducedCost < precision) {
|
|
467
784
|
optionalCostsColumns2.push(c);
|
|
468
785
|
continue;
|
|
469
786
|
}
|
|
@@ -492,15 +809,14 @@ function phase2() {
|
|
|
492
809
|
}
|
|
493
810
|
let leavingRow = 0;
|
|
494
811
|
let minQuotient = Infinity;
|
|
495
|
-
this.varIndexByRow;
|
|
496
812
|
for (let r = 1; r <= lastRow; r++) {
|
|
497
813
|
const rowOffset = r * width;
|
|
498
814
|
const rhsValue = matrix[rowOffset + rhsColumn];
|
|
499
815
|
const colValue = matrix[rowOffset + enteringColumn];
|
|
500
|
-
if (
|
|
816
|
+
if (negPrecision < colValue && colValue < precision) {
|
|
501
817
|
continue;
|
|
502
818
|
}
|
|
503
|
-
if (colValue > 0 && precision > rhsValue && rhsValue >
|
|
819
|
+
if (colValue > 0 && precision > rhsValue && rhsValue > negPrecision) {
|
|
504
820
|
minQuotient = 0;
|
|
505
821
|
leavingRow = r;
|
|
506
822
|
break;
|
|
@@ -514,15 +830,11 @@ function phase2() {
|
|
|
514
830
|
if (minQuotient === Infinity) {
|
|
515
831
|
this.evaluation = -Infinity;
|
|
516
832
|
this.bounded = false;
|
|
517
|
-
this.unboundedVarIndex =
|
|
833
|
+
this.unboundedVarIndex = varIndexByCol[enteringColumn];
|
|
518
834
|
return iterations;
|
|
519
835
|
}
|
|
520
|
-
if (
|
|
521
|
-
|
|
522
|
-
this.varIndexByRow[leavingRow],
|
|
523
|
-
this.varIndexByCol[enteringColumn],
|
|
524
|
-
]);
|
|
525
|
-
const cycleData = this.checkForCycles(varIndexesCycle);
|
|
836
|
+
if (cycleDetector) {
|
|
837
|
+
const cycleData = cycleDetector.add(varIndexByRow[leavingRow], varIndexByCol[enteringColumn]);
|
|
526
838
|
if (cycleData.length > 0) {
|
|
527
839
|
this.model.messages.push("Cycle in phase 2");
|
|
528
840
|
this.model.messages.push("Start :" + cycleData[0]);
|
|
@@ -535,13 +847,20 @@ function phase2() {
|
|
|
535
847
|
iterations += 1;
|
|
536
848
|
}
|
|
537
849
|
}
|
|
538
|
-
// Pre-allocated
|
|
539
|
-
|
|
850
|
+
// Pre-allocated typed arrays for pivot optimization (better cache performance)
|
|
851
|
+
let nonZeroColumns = new Int32Array(1024);
|
|
852
|
+
let pivotRowCache = new Float64Array(1024);
|
|
540
853
|
function pivot(pivotRowIndex, pivotColumnIndex) {
|
|
541
854
|
const matrix = this.matrix;
|
|
542
855
|
const width = this.width;
|
|
856
|
+
// Ensure work arrays are large enough
|
|
857
|
+
if (width > nonZeroColumns.length) {
|
|
858
|
+
nonZeroColumns = new Int32Array(width * 2);
|
|
859
|
+
pivotRowCache = new Float64Array(width * 2);
|
|
860
|
+
}
|
|
543
861
|
const pivotRowOffset = pivotRowIndex * width;
|
|
544
862
|
const quotient = matrix[pivotRowOffset + pivotColumnIndex];
|
|
863
|
+
const invQuotient = 1 / quotient;
|
|
545
864
|
const height = this.height;
|
|
546
865
|
const leavingBasicIndex = this.varIndexByRow[pivotRowIndex];
|
|
547
866
|
const enteringBasicIndex = this.varIndexByCol[pivotColumnIndex];
|
|
@@ -551,22 +870,24 @@ function pivot(pivotRowIndex, pivotColumnIndex) {
|
|
|
551
870
|
this.rowByVarIndex[leavingBasicIndex] = -1;
|
|
552
871
|
this.colByVarIndex[enteringBasicIndex] = -1;
|
|
553
872
|
this.colByVarIndex[leavingBasicIndex] = pivotColumnIndex;
|
|
554
|
-
// Normalize pivot row
|
|
873
|
+
// Normalize pivot row, track non-zero columns, and cache values for locality
|
|
555
874
|
let nNonZeroColumns = 0;
|
|
556
875
|
for (let c = 0; c < width; c++) {
|
|
557
876
|
const idx = pivotRowOffset + c;
|
|
558
877
|
const val = matrix[idx];
|
|
559
878
|
if (!(val >= -1e-16 && val <= 1e-16)) {
|
|
560
|
-
|
|
879
|
+
const normalized = val / quotient;
|
|
880
|
+
matrix[idx] = normalized;
|
|
561
881
|
nonZeroColumns[nNonZeroColumns] = c;
|
|
882
|
+
pivotRowCache[nNonZeroColumns] = normalized;
|
|
562
883
|
nNonZeroColumns++;
|
|
563
884
|
}
|
|
564
885
|
else {
|
|
565
886
|
matrix[idx] = 0;
|
|
566
887
|
}
|
|
567
888
|
}
|
|
568
|
-
matrix[pivotRowOffset + pivotColumnIndex] =
|
|
569
|
-
// Update all other rows
|
|
889
|
+
matrix[pivotRowOffset + pivotColumnIndex] = invQuotient;
|
|
890
|
+
// Update all other rows using cached pivot row values
|
|
570
891
|
for (let r = 0; r < height; r++) {
|
|
571
892
|
if (r !== pivotRowIndex) {
|
|
572
893
|
const rowOffset = r * width;
|
|
@@ -574,13 +895,16 @@ function pivot(pivotRowIndex, pivotColumnIndex) {
|
|
|
574
895
|
if (!(pivotColVal >= -1e-16 && pivotColVal <= 1e-16)) {
|
|
575
896
|
const coefficient = pivotColVal;
|
|
576
897
|
if (!(coefficient >= -1e-16 && coefficient <= 1e-16)) {
|
|
898
|
+
// Use cached pivot row values for better cache locality
|
|
577
899
|
for (let i = 0; i < nNonZeroColumns; i++) {
|
|
578
900
|
const c = nonZeroColumns[i];
|
|
579
|
-
const v0 =
|
|
901
|
+
const v0 = pivotRowCache[i];
|
|
902
|
+
// Inner zero check is critical for numerical stability
|
|
580
903
|
if (!(v0 >= -1e-16 && v0 <= 1e-16)) {
|
|
581
|
-
matrix[rowOffset + c]
|
|
904
|
+
matrix[rowOffset + c] -= coefficient * v0;
|
|
582
905
|
}
|
|
583
906
|
else if (v0 !== 0) {
|
|
907
|
+
// Clean up near-zero values in pivot row
|
|
584
908
|
matrix[pivotRowOffset + c] = 0;
|
|
585
909
|
}
|
|
586
910
|
}
|
|
@@ -592,7 +916,7 @@ function pivot(pivotRowIndex, pivotColumnIndex) {
|
|
|
592
916
|
}
|
|
593
917
|
}
|
|
594
918
|
}
|
|
595
|
-
// Update optional objectives
|
|
919
|
+
// Update optional objectives using cached pivot row values
|
|
596
920
|
const optionalObjectives = this.optionalObjectives;
|
|
597
921
|
const nOptionalObjectives = optionalObjectives.length;
|
|
598
922
|
if (nOptionalObjectives > 0) {
|
|
@@ -602,12 +926,9 @@ function pivot(pivotRowIndex, pivotColumnIndex) {
|
|
|
602
926
|
if (coefficient !== 0) {
|
|
603
927
|
for (let i = 0; i < nNonZeroColumns; i++) {
|
|
604
928
|
const c = nonZeroColumns[i];
|
|
605
|
-
|
|
606
|
-
if (v0 !== 0) {
|
|
607
|
-
reducedCosts[c] = reducedCosts[c] - coefficient * v0;
|
|
608
|
-
}
|
|
929
|
+
reducedCosts[c] -= coefficient * pivotRowCache[i];
|
|
609
930
|
}
|
|
610
|
-
reducedCosts[pivotColumnIndex] = -coefficient
|
|
931
|
+
reducedCosts[pivotColumnIndex] = -coefficient * invQuotient;
|
|
611
932
|
}
|
|
612
933
|
}
|
|
613
934
|
}
|
|
@@ -639,642 +960,6 @@ function checkForCycles(varIndexes) {
|
|
|
639
960
|
return [];
|
|
640
961
|
}
|
|
641
962
|
|
|
642
|
-
/**
|
|
643
|
-
* @file src/tableau/sparse-matrix.ts
|
|
644
|
-
* @description Compressed Sparse Column (CSC) matrix implementation
|
|
645
|
-
*
|
|
646
|
-
* Optimized for column operations which are frequent in the simplex algorithm.
|
|
647
|
-
* Provides O(nnz) pivot operations instead of O(n*m) for dense matrices.
|
|
648
|
-
*
|
|
649
|
-
* CSC Storage Format:
|
|
650
|
-
* - values: non-zero values stored column by column
|
|
651
|
-
* - rowIndices: row index for each value
|
|
652
|
-
* - colPointers: starting index in values/rowIndices for each column
|
|
653
|
-
*
|
|
654
|
-
* Column c's entries are at indices colPointers[c] to colPointers[c+1]-1
|
|
655
|
-
*/
|
|
656
|
-
class SparseMatrix {
|
|
657
|
-
constructor(nRows, nCols, precision = 1e-12) {
|
|
658
|
-
this.nRows = nRows;
|
|
659
|
-
this.nCols = nCols;
|
|
660
|
-
this.precision = precision;
|
|
661
|
-
// Initialize empty sparse matrix
|
|
662
|
-
this.values = [];
|
|
663
|
-
this.rowIndices = [];
|
|
664
|
-
this.colPointers = new Array(nCols + 1).fill(0);
|
|
665
|
-
// Workspace for row/column operations
|
|
666
|
-
this.workVector = new Float64Array(Math.max(nRows, nCols));
|
|
667
|
-
this.workIndices = [];
|
|
668
|
-
}
|
|
669
|
-
/**
|
|
670
|
-
* Create sparse matrix from dense Float64Array
|
|
671
|
-
*/
|
|
672
|
-
static fromDense(dense, width, height, precision = 1e-12) {
|
|
673
|
-
const sparse = new SparseMatrix(height, width, precision);
|
|
674
|
-
const values = [];
|
|
675
|
-
const rowIndices = [];
|
|
676
|
-
const colPointers = [0];
|
|
677
|
-
// Build CSC by iterating column by column
|
|
678
|
-
for (let c = 0; c < width; c++) {
|
|
679
|
-
for (let r = 0; r < height; r++) {
|
|
680
|
-
const val = dense[r * width + c];
|
|
681
|
-
if (val < -precision || val > precision) {
|
|
682
|
-
values.push(val);
|
|
683
|
-
rowIndices.push(r);
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
colPointers.push(values.length);
|
|
687
|
-
}
|
|
688
|
-
sparse.values = values;
|
|
689
|
-
sparse.rowIndices = rowIndices;
|
|
690
|
-
sparse.colPointers = colPointers;
|
|
691
|
-
return sparse;
|
|
692
|
-
}
|
|
693
|
-
/**
|
|
694
|
-
* Convert to dense Float64Array
|
|
695
|
-
*/
|
|
696
|
-
toDense() {
|
|
697
|
-
const dense = new Float64Array(this.nRows * this.nCols);
|
|
698
|
-
for (let c = 0; c < this.nCols; c++) {
|
|
699
|
-
const start = this.colPointers[c];
|
|
700
|
-
const end = this.colPointers[c + 1];
|
|
701
|
-
for (let i = start; i < end; i++) {
|
|
702
|
-
const r = this.rowIndices[i];
|
|
703
|
-
dense[r * this.nCols + c] = this.values[i];
|
|
704
|
-
}
|
|
705
|
-
}
|
|
706
|
-
return dense;
|
|
707
|
-
}
|
|
708
|
-
/**
|
|
709
|
-
* Get value at (row, col)
|
|
710
|
-
*/
|
|
711
|
-
get(row, col) {
|
|
712
|
-
const start = this.colPointers[col];
|
|
713
|
-
const end = this.colPointers[col + 1];
|
|
714
|
-
// Binary search for row in this column
|
|
715
|
-
let lo = start;
|
|
716
|
-
let hi = end;
|
|
717
|
-
while (lo < hi) {
|
|
718
|
-
const mid = (lo + hi) >>> 1;
|
|
719
|
-
if (this.rowIndices[mid] < row) {
|
|
720
|
-
lo = mid + 1;
|
|
721
|
-
}
|
|
722
|
-
else {
|
|
723
|
-
hi = mid;
|
|
724
|
-
}
|
|
725
|
-
}
|
|
726
|
-
if (lo < end && this.rowIndices[lo] === row) {
|
|
727
|
-
return this.values[lo];
|
|
728
|
-
}
|
|
729
|
-
return 0;
|
|
730
|
-
}
|
|
731
|
-
/**
|
|
732
|
-
* Set value at (row, col)
|
|
733
|
-
* Handles insertion and removal of entries
|
|
734
|
-
*/
|
|
735
|
-
set(row, col, value) {
|
|
736
|
-
const start = this.colPointers[col];
|
|
737
|
-
const end = this.colPointers[col + 1];
|
|
738
|
-
const isZero = value >= -this.precision && value <= this.precision;
|
|
739
|
-
// Binary search for insertion point
|
|
740
|
-
let lo = start;
|
|
741
|
-
let hi = end;
|
|
742
|
-
while (lo < hi) {
|
|
743
|
-
const mid = (lo + hi) >>> 1;
|
|
744
|
-
if (this.rowIndices[mid] < row) {
|
|
745
|
-
lo = mid + 1;
|
|
746
|
-
}
|
|
747
|
-
else {
|
|
748
|
-
hi = mid;
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
const exists = lo < end && this.rowIndices[lo] === row;
|
|
752
|
-
if (exists) {
|
|
753
|
-
if (isZero) {
|
|
754
|
-
// Remove existing entry
|
|
755
|
-
this.values.splice(lo, 1);
|
|
756
|
-
this.rowIndices.splice(lo, 1);
|
|
757
|
-
// Update column pointers
|
|
758
|
-
for (let c = col + 1; c <= this.nCols; c++) {
|
|
759
|
-
this.colPointers[c]--;
|
|
760
|
-
}
|
|
761
|
-
}
|
|
762
|
-
else {
|
|
763
|
-
// Update existing entry
|
|
764
|
-
this.values[lo] = value;
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
else if (!isZero) {
|
|
768
|
-
// Insert new entry
|
|
769
|
-
this.values.splice(lo, 0, value);
|
|
770
|
-
this.rowIndices.splice(lo, 0, row);
|
|
771
|
-
// Update column pointers
|
|
772
|
-
for (let c = col + 1; c <= this.nCols; c++) {
|
|
773
|
-
this.colPointers[c]++;
|
|
774
|
-
}
|
|
775
|
-
}
|
|
776
|
-
}
|
|
777
|
-
/**
|
|
778
|
-
* Get column as sparse vector
|
|
779
|
-
*/
|
|
780
|
-
getColumn(col) {
|
|
781
|
-
const start = this.colPointers[col];
|
|
782
|
-
const end = this.colPointers[col + 1];
|
|
783
|
-
const len = end - start;
|
|
784
|
-
return {
|
|
785
|
-
indices: this.rowIndices.slice(start, end),
|
|
786
|
-
values: this.values.slice(start, end),
|
|
787
|
-
length: len,
|
|
788
|
-
};
|
|
789
|
-
}
|
|
790
|
-
/**
|
|
791
|
-
* Get row as sparse vector (less efficient - requires full scan)
|
|
792
|
-
*/
|
|
793
|
-
getRow(row) {
|
|
794
|
-
const indices = [];
|
|
795
|
-
const values = [];
|
|
796
|
-
for (let c = 0; c < this.nCols; c++) {
|
|
797
|
-
const val = this.get(row, c);
|
|
798
|
-
if (val !== 0) {
|
|
799
|
-
indices.push(c);
|
|
800
|
-
values.push(val);
|
|
801
|
-
}
|
|
802
|
-
}
|
|
803
|
-
return { indices, values, length: indices.length };
|
|
804
|
-
}
|
|
805
|
-
/**
|
|
806
|
-
* Get row into pre-allocated dense array (more efficient for repeated access)
|
|
807
|
-
* Returns array of non-zero column indices
|
|
808
|
-
*/
|
|
809
|
-
getRowDense(row, output) {
|
|
810
|
-
const nonZeroCols = [];
|
|
811
|
-
// Clear relevant portion
|
|
812
|
-
output.fill(0, 0, this.nCols);
|
|
813
|
-
for (let c = 0; c < this.nCols; c++) {
|
|
814
|
-
const start = this.colPointers[c];
|
|
815
|
-
const end = this.colPointers[c + 1];
|
|
816
|
-
// Binary search for row
|
|
817
|
-
let lo = start;
|
|
818
|
-
let hi = end;
|
|
819
|
-
while (lo < hi) {
|
|
820
|
-
const mid = (lo + hi) >>> 1;
|
|
821
|
-
if (this.rowIndices[mid] < row) {
|
|
822
|
-
lo = mid + 1;
|
|
823
|
-
}
|
|
824
|
-
else {
|
|
825
|
-
hi = mid;
|
|
826
|
-
}
|
|
827
|
-
}
|
|
828
|
-
if (lo < end && this.rowIndices[lo] === row) {
|
|
829
|
-
output[c] = this.values[lo];
|
|
830
|
-
nonZeroCols.push(c);
|
|
831
|
-
}
|
|
832
|
-
}
|
|
833
|
-
return nonZeroCols;
|
|
834
|
-
}
|
|
835
|
-
/**
|
|
836
|
-
* Add a new row (expands matrix vertically)
|
|
837
|
-
*/
|
|
838
|
-
addRow() {
|
|
839
|
-
this.nRows++;
|
|
840
|
-
if (this.workVector.length < this.nRows) {
|
|
841
|
-
this.workVector = new Float64Array(this.nRows * 2);
|
|
842
|
-
}
|
|
843
|
-
}
|
|
844
|
-
/**
|
|
845
|
-
* Add a new column (expands matrix horizontally)
|
|
846
|
-
*/
|
|
847
|
-
addColumn() {
|
|
848
|
-
this.colPointers.push(this.colPointers[this.nCols]);
|
|
849
|
-
this.nCols++;
|
|
850
|
-
if (this.workVector.length < this.nCols) {
|
|
851
|
-
this.workVector = new Float64Array(this.nCols * 2);
|
|
852
|
-
}
|
|
853
|
-
}
|
|
854
|
-
/**
|
|
855
|
-
* Set entire column from sparse vector
|
|
856
|
-
*/
|
|
857
|
-
setColumn(col, sparseCol) {
|
|
858
|
-
const oldStart = this.colPointers[col];
|
|
859
|
-
const oldEnd = this.colPointers[col + 1];
|
|
860
|
-
const oldLen = oldEnd - oldStart;
|
|
861
|
-
const newLen = sparseCol.length;
|
|
862
|
-
const diff = newLen - oldLen;
|
|
863
|
-
if (diff !== 0) {
|
|
864
|
-
// Need to resize
|
|
865
|
-
if (diff > 0) {
|
|
866
|
-
// Make room
|
|
867
|
-
for (let i = 0; i < diff; i++) {
|
|
868
|
-
this.values.splice(oldEnd, 0, 0);
|
|
869
|
-
this.rowIndices.splice(oldEnd, 0, 0);
|
|
870
|
-
}
|
|
871
|
-
}
|
|
872
|
-
else {
|
|
873
|
-
// Remove excess
|
|
874
|
-
this.values.splice(oldStart, -diff);
|
|
875
|
-
this.rowIndices.splice(oldStart, -diff);
|
|
876
|
-
}
|
|
877
|
-
// Update column pointers
|
|
878
|
-
for (let c = col + 1; c <= this.nCols; c++) {
|
|
879
|
-
this.colPointers[c] += diff;
|
|
880
|
-
}
|
|
881
|
-
}
|
|
882
|
-
// Copy new values
|
|
883
|
-
const start = this.colPointers[col];
|
|
884
|
-
for (let i = 0; i < newLen; i++) {
|
|
885
|
-
this.values[start + i] = sparseCol.values[i];
|
|
886
|
-
this.rowIndices[start + i] = sparseCol.indices[i];
|
|
887
|
-
}
|
|
888
|
-
}
|
|
889
|
-
/**
|
|
890
|
-
* Number of non-zero entries
|
|
891
|
-
*/
|
|
892
|
-
get nnz() {
|
|
893
|
-
return this.values.length;
|
|
894
|
-
}
|
|
895
|
-
/**
|
|
896
|
-
* Density (fraction of non-zeros)
|
|
897
|
-
*/
|
|
898
|
-
get density() {
|
|
899
|
-
return this.nnz / (this.nRows * this.nCols);
|
|
900
|
-
}
|
|
901
|
-
/**
|
|
902
|
-
* Scale a row by a factor
|
|
903
|
-
*/
|
|
904
|
-
scaleRow(row, factor) {
|
|
905
|
-
for (let c = 0; c < this.nCols; c++) {
|
|
906
|
-
const start = this.colPointers[c];
|
|
907
|
-
const end = this.colPointers[c + 1];
|
|
908
|
-
for (let i = start; i < end; i++) {
|
|
909
|
-
if (this.rowIndices[i] === row) {
|
|
910
|
-
this.values[i] *= factor;
|
|
911
|
-
break;
|
|
912
|
-
}
|
|
913
|
-
}
|
|
914
|
-
}
|
|
915
|
-
}
|
|
916
|
-
/**
|
|
917
|
-
* Add scaled row to another row: targetRow += factor * sourceRow
|
|
918
|
-
* This is the core operation in Gaussian elimination / pivot
|
|
919
|
-
*/
|
|
920
|
-
addScaledRow(targetRow, sourceRow, factor) {
|
|
921
|
-
if (factor >= -this.precision && factor <= this.precision) {
|
|
922
|
-
return;
|
|
923
|
-
}
|
|
924
|
-
// Get source row as dense for efficiency
|
|
925
|
-
const sourceNonZeros = this.getRowDense(sourceRow, this.workVector);
|
|
926
|
-
// Add scaled source to target
|
|
927
|
-
for (const c of sourceNonZeros) {
|
|
928
|
-
const sourceVal = this.workVector[c];
|
|
929
|
-
const newVal = this.get(targetRow, c) + factor * sourceVal;
|
|
930
|
-
this.set(targetRow, c, newVal);
|
|
931
|
-
}
|
|
932
|
-
}
|
|
933
|
-
/**
|
|
934
|
-
* Perform pivot operation at (pivotRow, pivotCol)
|
|
935
|
-
* This is optimized for sparse operations
|
|
936
|
-
*/
|
|
937
|
-
pivot(pivotRow, pivotCol) {
|
|
938
|
-
const pivotVal = this.get(pivotRow, pivotCol);
|
|
939
|
-
if (pivotVal === 0) {
|
|
940
|
-
throw new Error("Cannot pivot on zero element");
|
|
941
|
-
}
|
|
942
|
-
const invPivot = 1 / pivotVal;
|
|
943
|
-
// Get pivot row as dense
|
|
944
|
-
const pivotRowNonZeros = this.getRowDense(pivotRow, this.workVector);
|
|
945
|
-
// Scale pivot row
|
|
946
|
-
for (const c of pivotRowNonZeros) {
|
|
947
|
-
this.workVector[c] *= invPivot;
|
|
948
|
-
}
|
|
949
|
-
this.workVector[pivotCol] = invPivot;
|
|
950
|
-
// Update pivot row in matrix
|
|
951
|
-
for (const c of pivotRowNonZeros) {
|
|
952
|
-
this.set(pivotRow, c, this.workVector[c]);
|
|
953
|
-
}
|
|
954
|
-
this.set(pivotRow, pivotCol, invPivot);
|
|
955
|
-
// Get pivot column to find rows that need updating
|
|
956
|
-
const pivotColData = this.getColumn(pivotCol);
|
|
957
|
-
// Update all other rows
|
|
958
|
-
for (let i = 0; i < pivotColData.length; i++) {
|
|
959
|
-
const r = pivotColData.indices[i];
|
|
960
|
-
if (r === pivotRow)
|
|
961
|
-
continue;
|
|
962
|
-
const coefficient = pivotColData.values[i];
|
|
963
|
-
if (coefficient >= -this.precision && coefficient <= this.precision)
|
|
964
|
-
continue;
|
|
965
|
-
// row[r] -= coefficient * pivotRow
|
|
966
|
-
for (const c of pivotRowNonZeros) {
|
|
967
|
-
if (c === pivotCol)
|
|
968
|
-
continue;
|
|
969
|
-
const pivotRowVal = this.workVector[c];
|
|
970
|
-
if (pivotRowVal >= -this.precision && pivotRowVal <= this.precision)
|
|
971
|
-
continue;
|
|
972
|
-
const newVal = this.get(r, c) - coefficient * pivotRowVal;
|
|
973
|
-
this.set(r, c, newVal);
|
|
974
|
-
}
|
|
975
|
-
// Set pivot column value for this row
|
|
976
|
-
this.set(r, pivotCol, -coefficient * invPivot);
|
|
977
|
-
}
|
|
978
|
-
return pivotRowNonZeros;
|
|
979
|
-
}
|
|
980
|
-
/**
|
|
981
|
-
* Clone the matrix
|
|
982
|
-
*/
|
|
983
|
-
clone() {
|
|
984
|
-
const copy = new SparseMatrix(this.nRows, this.nCols, this.precision);
|
|
985
|
-
copy.values = this.values.slice();
|
|
986
|
-
copy.rowIndices = this.rowIndices.slice();
|
|
987
|
-
copy.colPointers = this.colPointers.slice();
|
|
988
|
-
return copy;
|
|
989
|
-
}
|
|
990
|
-
}
|
|
991
|
-
|
|
992
|
-
/**
|
|
993
|
-
* @file src/tableau/sparse-simplex.ts
|
|
994
|
-
* @description Sparse simplex algorithm for large LP problems
|
|
995
|
-
*
|
|
996
|
-
* Optimized simplex implementation that operates directly on SparseMatrix
|
|
997
|
-
* storage. Converts the dense tableau to CSC format, solves, and converts
|
|
998
|
-
* back for solution extraction.
|
|
999
|
-
*
|
|
1000
|
-
* Provides significant speedup for large, sparse problems where most
|
|
1001
|
-
* coefficients are zero.
|
|
1002
|
-
*/
|
|
1003
|
-
/**
|
|
1004
|
-
* Convert dense tableau to sparse and solve
|
|
1005
|
-
*/
|
|
1006
|
-
function sparseSimplex() {
|
|
1007
|
-
// Convert to sparse
|
|
1008
|
-
const sparse = SparseMatrix.fromDense(this.matrix, this.width, this.height, this.precision);
|
|
1009
|
-
this.bounded = true;
|
|
1010
|
-
// Run sparse phase 1
|
|
1011
|
-
sparsePhase1.call(this, sparse);
|
|
1012
|
-
if (this.feasible === true) {
|
|
1013
|
-
// Run sparse phase 2
|
|
1014
|
-
sparsePhase2.call(this, sparse);
|
|
1015
|
-
}
|
|
1016
|
-
// Copy results back to dense matrix (needed for solution extraction)
|
|
1017
|
-
const dense = sparse.toDense();
|
|
1018
|
-
this.matrix.set(dense);
|
|
1019
|
-
return this;
|
|
1020
|
-
}
|
|
1021
|
-
/**
|
|
1022
|
-
* Sparse Phase 1: Find a basic feasible solution
|
|
1023
|
-
*/
|
|
1024
|
-
function sparsePhase1(sparse) {
|
|
1025
|
-
const debugCheckForCycles = this.model.checkForCycles;
|
|
1026
|
-
const varIndexesCycle = [];
|
|
1027
|
-
const rhsColumn = this.rhsColumn;
|
|
1028
|
-
const lastColumn = this.width - 1;
|
|
1029
|
-
const lastRow = this.height - 1;
|
|
1030
|
-
const precision = this.precision;
|
|
1031
|
-
let unrestricted;
|
|
1032
|
-
let iterations = 0;
|
|
1033
|
-
while (true) {
|
|
1034
|
-
// Find leaving row (most negative RHS)
|
|
1035
|
-
let leavingRowIndex = 0;
|
|
1036
|
-
let rhsValue = -precision;
|
|
1037
|
-
for (let r = 1; r <= lastRow; r++) {
|
|
1038
|
-
const value = sparse.get(r, rhsColumn);
|
|
1039
|
-
if (value < rhsValue) {
|
|
1040
|
-
rhsValue = value;
|
|
1041
|
-
leavingRowIndex = r;
|
|
1042
|
-
}
|
|
1043
|
-
}
|
|
1044
|
-
if (leavingRowIndex === 0) {
|
|
1045
|
-
this.feasible = true;
|
|
1046
|
-
return iterations;
|
|
1047
|
-
}
|
|
1048
|
-
// Find entering column
|
|
1049
|
-
let enteringColumn = 0;
|
|
1050
|
-
let maxQuotient = -Infinity;
|
|
1051
|
-
// Get leaving row as sparse
|
|
1052
|
-
const leavingRowData = sparse.getRow(leavingRowIndex);
|
|
1053
|
-
for (let i = 0; i < leavingRowData.length; i++) {
|
|
1054
|
-
const c = leavingRowData.indices[i];
|
|
1055
|
-
if (c === 0 || c === rhsColumn)
|
|
1056
|
-
continue;
|
|
1057
|
-
const coefficient = leavingRowData.values[i];
|
|
1058
|
-
unrestricted = this.unrestrictedVars[this.varIndexByCol[c]] === true;
|
|
1059
|
-
if (unrestricted || coefficient < -precision) {
|
|
1060
|
-
const costValue = sparse.get(0, c);
|
|
1061
|
-
const quotient = -costValue / coefficient;
|
|
1062
|
-
if (maxQuotient < quotient) {
|
|
1063
|
-
maxQuotient = quotient;
|
|
1064
|
-
enteringColumn = c;
|
|
1065
|
-
}
|
|
1066
|
-
}
|
|
1067
|
-
}
|
|
1068
|
-
// Also check columns with zero in leaving row but non-zero cost
|
|
1069
|
-
for (let c = 1; c <= lastColumn; c++) {
|
|
1070
|
-
if (c === rhsColumn)
|
|
1071
|
-
continue;
|
|
1072
|
-
const coefficient = sparse.get(leavingRowIndex, c);
|
|
1073
|
-
unrestricted = this.unrestrictedVars[this.varIndexByCol[c]] === true;
|
|
1074
|
-
if (unrestricted || coefficient < -precision) {
|
|
1075
|
-
const costValue = sparse.get(0, c);
|
|
1076
|
-
const quotient = -costValue / coefficient;
|
|
1077
|
-
if (maxQuotient < quotient) {
|
|
1078
|
-
maxQuotient = quotient;
|
|
1079
|
-
enteringColumn = c;
|
|
1080
|
-
}
|
|
1081
|
-
}
|
|
1082
|
-
}
|
|
1083
|
-
if (enteringColumn === 0) {
|
|
1084
|
-
this.feasible = false;
|
|
1085
|
-
return iterations;
|
|
1086
|
-
}
|
|
1087
|
-
if (debugCheckForCycles) {
|
|
1088
|
-
varIndexesCycle.push([
|
|
1089
|
-
this.varIndexByRow[leavingRowIndex],
|
|
1090
|
-
this.varIndexByCol[enteringColumn],
|
|
1091
|
-
]);
|
|
1092
|
-
const cycleData = this.checkForCycles(varIndexesCycle);
|
|
1093
|
-
if (cycleData.length > 0) {
|
|
1094
|
-
this.model.messages.push("Cycle in phase 1");
|
|
1095
|
-
this.feasible = false;
|
|
1096
|
-
return iterations;
|
|
1097
|
-
}
|
|
1098
|
-
}
|
|
1099
|
-
// Perform pivot
|
|
1100
|
-
sparsePivot.call(this, sparse, leavingRowIndex, enteringColumn);
|
|
1101
|
-
iterations += 1;
|
|
1102
|
-
}
|
|
1103
|
-
}
|
|
1104
|
-
/**
|
|
1105
|
-
* Sparse Phase 2: Optimize the objective
|
|
1106
|
-
*/
|
|
1107
|
-
function sparsePhase2(sparse) {
|
|
1108
|
-
const debugCheckForCycles = this.model.checkForCycles;
|
|
1109
|
-
const varIndexesCycle = [];
|
|
1110
|
-
const rhsColumn = this.rhsColumn;
|
|
1111
|
-
const lastColumn = this.width - 1;
|
|
1112
|
-
this.height - 1;
|
|
1113
|
-
const precision = this.precision;
|
|
1114
|
-
this.optionalObjectives.length;
|
|
1115
|
-
let iterations = 0;
|
|
1116
|
-
let reducedCost;
|
|
1117
|
-
let unrestricted;
|
|
1118
|
-
// Partial pricing for sparse
|
|
1119
|
-
const batchSize = Math.min(500, Math.max(50, Math.floor(Math.sqrt(lastColumn))));
|
|
1120
|
-
let batchStart = 1;
|
|
1121
|
-
while (true) {
|
|
1122
|
-
const costRow = 0;
|
|
1123
|
-
let enteringColumn = 0;
|
|
1124
|
-
let enteringValue = precision;
|
|
1125
|
-
let isReducedCostNegative = false;
|
|
1126
|
-
// Partial pricing: scan columns in batches
|
|
1127
|
-
let batchesScanned = 0;
|
|
1128
|
-
const totalBatches = Math.ceil(lastColumn / batchSize);
|
|
1129
|
-
while (enteringColumn === 0 && batchesScanned < totalBatches) {
|
|
1130
|
-
const batchEnd = Math.min(batchStart + batchSize - 1, lastColumn);
|
|
1131
|
-
for (let c = batchStart; c <= batchEnd; c++) {
|
|
1132
|
-
if (c === rhsColumn)
|
|
1133
|
-
continue;
|
|
1134
|
-
reducedCost = sparse.get(costRow, c);
|
|
1135
|
-
unrestricted = this.unrestrictedVars[this.varIndexByCol[c]] === true;
|
|
1136
|
-
if (unrestricted && reducedCost < 0) {
|
|
1137
|
-
if (-reducedCost > enteringValue) {
|
|
1138
|
-
enteringValue = -reducedCost;
|
|
1139
|
-
enteringColumn = c;
|
|
1140
|
-
isReducedCostNegative = true;
|
|
1141
|
-
}
|
|
1142
|
-
continue;
|
|
1143
|
-
}
|
|
1144
|
-
if (reducedCost > enteringValue) {
|
|
1145
|
-
enteringValue = reducedCost;
|
|
1146
|
-
enteringColumn = c;
|
|
1147
|
-
isReducedCostNegative = false;
|
|
1148
|
-
}
|
|
1149
|
-
}
|
|
1150
|
-
batchStart = batchEnd >= lastColumn ? 1 : batchEnd + 1;
|
|
1151
|
-
batchesScanned++;
|
|
1152
|
-
}
|
|
1153
|
-
if (enteringColumn === 0) {
|
|
1154
|
-
this.setEvaluation();
|
|
1155
|
-
this.simplexIters += 1;
|
|
1156
|
-
return iterations;
|
|
1157
|
-
}
|
|
1158
|
-
// Find leaving row using ratio test
|
|
1159
|
-
let leavingRow = 0;
|
|
1160
|
-
let minQuotient = Infinity;
|
|
1161
|
-
// Get entering column as sparse
|
|
1162
|
-
const enteringColData = sparse.getColumn(enteringColumn);
|
|
1163
|
-
for (let i = 0; i < enteringColData.length; i++) {
|
|
1164
|
-
const r = enteringColData.indices[i];
|
|
1165
|
-
if (r === 0)
|
|
1166
|
-
continue; // Skip cost row
|
|
1167
|
-
const colValue = enteringColData.values[i];
|
|
1168
|
-
if (colValue >= -precision && colValue <= precision)
|
|
1169
|
-
continue;
|
|
1170
|
-
const rhsValue = sparse.get(r, rhsColumn);
|
|
1171
|
-
if (colValue > 0 && precision > rhsValue && rhsValue > -precision) {
|
|
1172
|
-
minQuotient = 0;
|
|
1173
|
-
leavingRow = r;
|
|
1174
|
-
break;
|
|
1175
|
-
}
|
|
1176
|
-
const quotient = isReducedCostNegative ? -rhsValue / colValue : rhsValue / colValue;
|
|
1177
|
-
if (quotient > precision && minQuotient > quotient) {
|
|
1178
|
-
minQuotient = quotient;
|
|
1179
|
-
leavingRow = r;
|
|
1180
|
-
}
|
|
1181
|
-
}
|
|
1182
|
-
if (minQuotient === Infinity) {
|
|
1183
|
-
this.evaluation = -Infinity;
|
|
1184
|
-
this.bounded = false;
|
|
1185
|
-
this.unboundedVarIndex = this.varIndexByCol[enteringColumn];
|
|
1186
|
-
return iterations;
|
|
1187
|
-
}
|
|
1188
|
-
if (debugCheckForCycles) {
|
|
1189
|
-
varIndexesCycle.push([
|
|
1190
|
-
this.varIndexByRow[leavingRow],
|
|
1191
|
-
this.varIndexByCol[enteringColumn],
|
|
1192
|
-
]);
|
|
1193
|
-
const cycleData = this.checkForCycles(varIndexesCycle);
|
|
1194
|
-
if (cycleData.length > 0) {
|
|
1195
|
-
this.model.messages.push("Cycle in phase 2");
|
|
1196
|
-
this.feasible = false;
|
|
1197
|
-
return iterations;
|
|
1198
|
-
}
|
|
1199
|
-
}
|
|
1200
|
-
// Perform pivot
|
|
1201
|
-
sparsePivot.call(this, sparse, leavingRow, enteringColumn);
|
|
1202
|
-
iterations += 1;
|
|
1203
|
-
}
|
|
1204
|
-
}
|
|
1205
|
-
/**
|
|
1206
|
-
* Sparse pivot operation
|
|
1207
|
-
*/
|
|
1208
|
-
function sparsePivot(sparse, pivotRowIndex, pivotColumnIndex) {
|
|
1209
|
-
// Update basis tracking
|
|
1210
|
-
const leavingBasicIndex = this.varIndexByRow[pivotRowIndex];
|
|
1211
|
-
const enteringBasicIndex = this.varIndexByCol[pivotColumnIndex];
|
|
1212
|
-
this.varIndexByRow[pivotRowIndex] = enteringBasicIndex;
|
|
1213
|
-
this.varIndexByCol[pivotColumnIndex] = leavingBasicIndex;
|
|
1214
|
-
this.rowByVarIndex[enteringBasicIndex] = pivotRowIndex;
|
|
1215
|
-
this.rowByVarIndex[leavingBasicIndex] = -1;
|
|
1216
|
-
this.colByVarIndex[enteringBasicIndex] = -1;
|
|
1217
|
-
this.colByVarIndex[leavingBasicIndex] = pivotColumnIndex;
|
|
1218
|
-
// Perform sparse pivot
|
|
1219
|
-
sparse.pivot(pivotRowIndex, pivotColumnIndex);
|
|
1220
|
-
// Handle optional objectives (these are stored separately, not in sparse matrix)
|
|
1221
|
-
const optionalObjectives = this.optionalObjectives;
|
|
1222
|
-
const nOptionalObjectives = optionalObjectives.length;
|
|
1223
|
-
if (nOptionalObjectives > 0) {
|
|
1224
|
-
const pivotVal = sparse.get(pivotRowIndex, pivotColumnIndex);
|
|
1225
|
-
const pivotRowData = sparse.getRow(pivotRowIndex);
|
|
1226
|
-
for (let o = 0; o < nOptionalObjectives; o++) {
|
|
1227
|
-
const reducedCosts = optionalObjectives[o].reducedCosts;
|
|
1228
|
-
const coefficient = reducedCosts[pivotColumnIndex];
|
|
1229
|
-
if (coefficient !== 0) {
|
|
1230
|
-
for (let i = 0; i < pivotRowData.length; i++) {
|
|
1231
|
-
const c = pivotRowData.indices[i];
|
|
1232
|
-
const v0 = pivotRowData.values[i];
|
|
1233
|
-
if (v0 !== 0) {
|
|
1234
|
-
reducedCosts[c] = reducedCosts[c] - coefficient * v0;
|
|
1235
|
-
}
|
|
1236
|
-
}
|
|
1237
|
-
reducedCosts[pivotColumnIndex] = -coefficient * pivotVal;
|
|
1238
|
-
}
|
|
1239
|
-
}
|
|
1240
|
-
}
|
|
1241
|
-
}
|
|
1242
|
-
/**
|
|
1243
|
-
* Threshold for using sparse (total cells)
|
|
1244
|
-
* Currently disabled - CSC format overhead exceeds benefits.
|
|
1245
|
-
* A dual-indexed (CSC+CSR) approach would be needed for efficient sparse simplex.
|
|
1246
|
-
*/
|
|
1247
|
-
const SPARSE_THRESHOLD = Infinity; // Disabled
|
|
1248
|
-
/**
|
|
1249
|
-
* Density threshold - only use sparse if below this density
|
|
1250
|
-
*/
|
|
1251
|
-
const DENSITY_THRESHOLD = 0.1; // 10%
|
|
1252
|
-
/**
|
|
1253
|
-
* Check if sparse mode should be used for this tableau
|
|
1254
|
-
*/
|
|
1255
|
-
function shouldUseSparse(tableau) {
|
|
1256
|
-
const totalCells = tableau.width * tableau.height;
|
|
1257
|
-
if (totalCells < SPARSE_THRESHOLD) {
|
|
1258
|
-
return false;
|
|
1259
|
-
}
|
|
1260
|
-
// Estimate density from constraint rows (skip row 0 which is cost row and often dense)
|
|
1261
|
-
const matrix = tableau.matrix;
|
|
1262
|
-
const width = tableau.width;
|
|
1263
|
-
const startRow = 1;
|
|
1264
|
-
const sampleRows = Math.min(20, tableau.height - 1);
|
|
1265
|
-
let nonZeros = 0;
|
|
1266
|
-
for (let r = startRow; r < startRow + sampleRows; r++) {
|
|
1267
|
-
const rowOffset = r * width;
|
|
1268
|
-
for (let c = 0; c < width; c++) {
|
|
1269
|
-
if (matrix[rowOffset + c] !== 0) {
|
|
1270
|
-
nonZeros++;
|
|
1271
|
-
}
|
|
1272
|
-
}
|
|
1273
|
-
}
|
|
1274
|
-
const estimatedDensity = nonZeros / (sampleRows * width);
|
|
1275
|
-
return estimatedDensity < DENSITY_THRESHOLD;
|
|
1276
|
-
}
|
|
1277
|
-
|
|
1278
963
|
class Variable {
|
|
1279
964
|
constructor(id, cost, index, priority) {
|
|
1280
965
|
this.id = id;
|
|
@@ -1359,6 +1044,7 @@ class Constraint {
|
|
|
1359
1044
|
setVariableCoefficient(newCoefficient, variable) {
|
|
1360
1045
|
const varIndex = variable.index;
|
|
1361
1046
|
if (varIndex === -1) {
|
|
1047
|
+
// eslint-disable-next-line no-console
|
|
1362
1048
|
console.warn("[Constraint.setVariableCoefficient] Trying to change coefficient of inexistant variable.");
|
|
1363
1049
|
return;
|
|
1364
1050
|
}
|
|
@@ -1441,44 +1127,52 @@ function addCutConstraints(cutConstraints) {
|
|
|
1441
1127
|
const heightWithCuts = height + nCutConstraints;
|
|
1442
1128
|
const width = this.width;
|
|
1443
1129
|
const lastColumn = width - 1;
|
|
1444
|
-
// Grow the matrix to accommodate new rows
|
|
1130
|
+
// Grow the matrix to accommodate new rows (with over-allocation to reduce reallocation frequency)
|
|
1445
1131
|
const oldMatrix = this.matrix;
|
|
1446
1132
|
const newSize = heightWithCuts * width;
|
|
1447
1133
|
if (oldMatrix.length < newSize) {
|
|
1448
|
-
|
|
1134
|
+
// Over-allocate by 50% to reduce future reallocations
|
|
1135
|
+
const allocSize = Math.ceil(newSize * 1.5);
|
|
1136
|
+
const newMatrix = new Float64Array(allocSize);
|
|
1449
1137
|
newMatrix.set(oldMatrix);
|
|
1450
1138
|
this.matrix = newMatrix;
|
|
1451
1139
|
}
|
|
1452
1140
|
const matrix = this.matrix;
|
|
1453
1141
|
this.height = heightWithCuts;
|
|
1454
1142
|
this.nVars = this.width + this.height - 2;
|
|
1143
|
+
// Cache array references for faster access in loop
|
|
1144
|
+
const rhsColumn = this.rhsColumn;
|
|
1145
|
+
const rowByVarIndex = this.rowByVarIndex;
|
|
1146
|
+
const colByVarIndex = this.colByVarIndex;
|
|
1147
|
+
const varIndexByRow = this.varIndexByRow;
|
|
1148
|
+
const variablesPerIndex = this.variablesPerIndex;
|
|
1455
1149
|
for (let h = 0; h < nCutConstraints; h += 1) {
|
|
1456
1150
|
const cut = cutConstraints[h];
|
|
1457
1151
|
const cutRow = height + h;
|
|
1458
1152
|
const cutRowOffset = cutRow * width;
|
|
1459
1153
|
const sign = cut.type === "min" ? -1 : 1;
|
|
1460
1154
|
const varIndex = cut.varIndex;
|
|
1461
|
-
let varRowIndex =
|
|
1155
|
+
let varRowIndex = rowByVarIndex[varIndex];
|
|
1462
1156
|
if (varRowIndex === -1) {
|
|
1463
|
-
matrix[cutRowOffset +
|
|
1157
|
+
matrix[cutRowOffset + rhsColumn] = sign * cut.value;
|
|
1464
1158
|
for (let c = 1; c <= lastColumn; c += 1) {
|
|
1465
1159
|
matrix[cutRowOffset + c] = 0;
|
|
1466
1160
|
}
|
|
1467
|
-
matrix[cutRowOffset +
|
|
1161
|
+
matrix[cutRowOffset + colByVarIndex[varIndex]] = sign;
|
|
1468
1162
|
}
|
|
1469
1163
|
else {
|
|
1470
1164
|
const varRowOffset = varRowIndex * width;
|
|
1471
|
-
const varValue = matrix[varRowOffset +
|
|
1472
|
-
matrix[cutRowOffset +
|
|
1165
|
+
const varValue = matrix[varRowOffset + rhsColumn];
|
|
1166
|
+
matrix[cutRowOffset + rhsColumn] = sign * (cut.value - varValue);
|
|
1473
1167
|
for (let c = 1; c <= lastColumn; c += 1) {
|
|
1474
1168
|
matrix[cutRowOffset + c] = -sign * matrix[varRowOffset + c];
|
|
1475
1169
|
}
|
|
1476
1170
|
}
|
|
1477
1171
|
varRowIndex = this.getNewElementIndex();
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1172
|
+
varIndexByRow[cutRow] = varRowIndex;
|
|
1173
|
+
rowByVarIndex[varRowIndex] = cutRow;
|
|
1174
|
+
colByVarIndex[varRowIndex] = -1;
|
|
1175
|
+
variablesPerIndex[varRowIndex] = new SlackVariable("s" + varRowIndex, varRowIndex);
|
|
1482
1176
|
this.nVars += 1;
|
|
1483
1177
|
}
|
|
1484
1178
|
}
|
|
@@ -1500,10 +1194,12 @@ function addLowerBoundMIRCut(rowIndex) {
|
|
|
1500
1194
|
}
|
|
1501
1195
|
const height = this.height;
|
|
1502
1196
|
const newRowOffset = height * width;
|
|
1503
|
-
// Grow matrix to add new row
|
|
1197
|
+
// Grow matrix to add new row (with over-allocation to reduce reallocation frequency)
|
|
1504
1198
|
const newSize = (height + 1) * width;
|
|
1505
1199
|
if (matrix.length < newSize) {
|
|
1506
|
-
|
|
1200
|
+
// Over-allocate by 50% to reduce future reallocations
|
|
1201
|
+
const allocSize = Math.ceil(newSize * 1.5);
|
|
1202
|
+
const newMatrix = new Float64Array(allocSize);
|
|
1507
1203
|
newMatrix.set(matrix);
|
|
1508
1204
|
this.matrix = newMatrix;
|
|
1509
1205
|
}
|
|
@@ -1515,18 +1211,23 @@ function addLowerBoundMIRCut(rowIndex) {
|
|
|
1515
1211
|
this.rowByVarIndex[slackVarIndex] = height;
|
|
1516
1212
|
this.colByVarIndex[slackVarIndex] = -1;
|
|
1517
1213
|
this.variablesPerIndex[slackVarIndex] = new SlackVariable("s" + slackVarIndex, slackVarIndex);
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1214
|
+
const rhsColumn = this.rhsColumn;
|
|
1215
|
+
mat[newRowOffset + rhsColumn] = Math.floor(rhsValue);
|
|
1216
|
+
// Cache array references for faster access in hot loop
|
|
1217
|
+
const variablesPerIndex = this.variablesPerIndex;
|
|
1218
|
+
const varIndexByCol = this.varIndexByCol;
|
|
1219
|
+
const varIndexByColLen = varIndexByCol.length;
|
|
1220
|
+
const oneMinusFrac = 1 - fractionalPart;
|
|
1221
|
+
for (let colIndex = 1; colIndex < varIndexByColLen; colIndex += 1) {
|
|
1222
|
+
const variable = variablesPerIndex[varIndexByCol[colIndex]];
|
|
1521
1223
|
const coefficient = mat[cutRowOffset + colIndex];
|
|
1522
1224
|
if (variable !== undefined && variable.isInteger) {
|
|
1523
|
-
const
|
|
1524
|
-
|
|
1525
|
-
(1 - fractionalPart);
|
|
1225
|
+
const floorCoeff = Math.floor(coefficient);
|
|
1226
|
+
const termCoeff = floorCoeff + Math.max(0, coefficient - floorCoeff - fractionalPart) / oneMinusFrac;
|
|
1526
1227
|
mat[newRowOffset + colIndex] = termCoeff;
|
|
1527
1228
|
}
|
|
1528
1229
|
else {
|
|
1529
|
-
mat[newRowOffset + colIndex] = Math.min(0, coefficient /
|
|
1230
|
+
mat[newRowOffset + colIndex] = Math.min(0, coefficient / oneMinusFrac);
|
|
1530
1231
|
}
|
|
1531
1232
|
}
|
|
1532
1233
|
for (let c = 0; c < width; c += 1) {
|
|
@@ -1552,10 +1253,12 @@ function addUpperBoundMIRCut(rowIndex) {
|
|
|
1552
1253
|
}
|
|
1553
1254
|
const height = this.height;
|
|
1554
1255
|
const newRowOffset = height * width;
|
|
1555
|
-
// Grow matrix to add new row
|
|
1256
|
+
// Grow matrix to add new row (with over-allocation to reduce reallocation frequency)
|
|
1556
1257
|
const newSize = (height + 1) * width;
|
|
1557
1258
|
if (matrix.length < newSize) {
|
|
1558
|
-
|
|
1259
|
+
// Over-allocate by 50% to reduce future reallocations
|
|
1260
|
+
const allocSize = Math.ceil(newSize * 1.5);
|
|
1261
|
+
const newMatrix = new Float64Array(allocSize);
|
|
1559
1262
|
newMatrix.set(matrix);
|
|
1560
1263
|
this.matrix = newMatrix;
|
|
1561
1264
|
}
|
|
@@ -1567,9 +1270,15 @@ function addUpperBoundMIRCut(rowIndex) {
|
|
|
1567
1270
|
this.rowByVarIndex[slackVarIndex] = height;
|
|
1568
1271
|
this.colByVarIndex[slackVarIndex] = -1;
|
|
1569
1272
|
this.variablesPerIndex[slackVarIndex] = new SlackVariable("s" + slackVarIndex, slackVarIndex);
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1273
|
+
const rhsColumn = this.rhsColumn;
|
|
1274
|
+
mat[newRowOffset + rhsColumn] = -fractionalPart;
|
|
1275
|
+
// Cache array references for faster access in hot loop
|
|
1276
|
+
const variablesPerIndex = this.variablesPerIndex;
|
|
1277
|
+
const varIndexByCol = this.varIndexByCol;
|
|
1278
|
+
const varIndexByColLen = varIndexByCol.length;
|
|
1279
|
+
const oneMinusFrac = 1 - fractionalPart;
|
|
1280
|
+
for (let colIndex = 1; colIndex < varIndexByColLen; colIndex += 1) {
|
|
1281
|
+
const variable = variablesPerIndex[varIndexByCol[colIndex]];
|
|
1573
1282
|
const coefficient = mat[cutRowOffset + colIndex];
|
|
1574
1283
|
const termCoeff = coefficient - Math.floor(coefficient);
|
|
1575
1284
|
if (variable !== undefined && variable.isInteger) {
|
|
@@ -1580,9 +1289,7 @@ function addUpperBoundMIRCut(rowIndex) {
|
|
|
1580
1289
|
}
|
|
1581
1290
|
else {
|
|
1582
1291
|
mat[newRowOffset + colIndex] =
|
|
1583
|
-
coefficient >= 0
|
|
1584
|
-
? -coefficient
|
|
1585
|
-
: (coefficient * fractionalPart) / (1 - fractionalPart);
|
|
1292
|
+
coefficient >= 0 ? -coefficient : (coefficient * fractionalPart) / oneMinusFrac;
|
|
1586
1293
|
}
|
|
1587
1294
|
}
|
|
1588
1295
|
return true;
|
|
@@ -1957,12 +1664,15 @@ function isIntegral() {
|
|
|
1957
1664
|
const rhsColumn = this.rhsColumn;
|
|
1958
1665
|
const integerVariables = this.model.integerVariables;
|
|
1959
1666
|
const nIntegerVars = integerVariables.length;
|
|
1667
|
+
// Cache array reference for faster access in hot loop
|
|
1668
|
+
const rowByVarIndex = this.rowByVarIndex;
|
|
1669
|
+
const precision = this.precision;
|
|
1960
1670
|
for (let v = 0; v < nIntegerVars; v++) {
|
|
1961
1671
|
const varIndex = integerVariables[v].index;
|
|
1962
|
-
const row =
|
|
1672
|
+
const row = rowByVarIndex[varIndex];
|
|
1963
1673
|
if (row !== -1) {
|
|
1964
1674
|
const value = matrix[row * width + rhsColumn];
|
|
1965
|
-
if (Math.abs(value - Math.round(value)) >
|
|
1675
|
+
if (Math.abs(value - Math.round(value)) > precision) {
|
|
1966
1676
|
return false;
|
|
1967
1677
|
}
|
|
1968
1678
|
}
|
|
@@ -1978,12 +1688,17 @@ function computeFractionalVolume(ignoreIntegerValues) {
|
|
|
1978
1688
|
const width = this.width;
|
|
1979
1689
|
const matrix = this.matrix;
|
|
1980
1690
|
const rhsColumn = this.rhsColumn;
|
|
1981
|
-
|
|
1982
|
-
|
|
1691
|
+
const height = this.height;
|
|
1692
|
+
// Cache array references for faster access in hot loop
|
|
1693
|
+
const variablesPerIndex = this.variablesPerIndex;
|
|
1694
|
+
const varIndexByRow = this.varIndexByRow;
|
|
1695
|
+
const precision = this.precision;
|
|
1696
|
+
for (let r = 1; r < height; r += 1) {
|
|
1697
|
+
const variable = variablesPerIndex[varIndexByRow[r]];
|
|
1983
1698
|
if (variable !== undefined && variable.isInteger) {
|
|
1984
1699
|
const value = matrix[r * width + rhsColumn];
|
|
1985
1700
|
const distance = Math.abs(value);
|
|
1986
|
-
if (Math.min(distance - Math.floor(distance), Math.floor(distance + 1)) <
|
|
1701
|
+
if (Math.min(distance - Math.floor(distance), Math.floor(distance + 1)) < precision) {
|
|
1987
1702
|
if (ignoreIntegerValues !== true) {
|
|
1988
1703
|
return 0;
|
|
1989
1704
|
}
|
|
@@ -2012,9 +1727,11 @@ function getMostFractionalVar() {
|
|
|
2012
1727
|
const rhsColumn = this.rhsColumn;
|
|
2013
1728
|
const integerVars = this.model.integerVariables;
|
|
2014
1729
|
const nIntegerVars = integerVars.length;
|
|
1730
|
+
// Cache array reference for faster access in hot loop
|
|
1731
|
+
const rowByVarIndex = this.rowByVarIndex;
|
|
2015
1732
|
for (let v = 0; v < nIntegerVars; v += 1) {
|
|
2016
1733
|
const varIndex = integerVars[v].index;
|
|
2017
|
-
const row =
|
|
1734
|
+
const row = rowByVarIndex[varIndex];
|
|
2018
1735
|
if (row !== -1) {
|
|
2019
1736
|
const varValue = matrix[row * width + rhsColumn];
|
|
2020
1737
|
const fraction = Math.abs(varValue - Math.round(varValue));
|
|
@@ -2231,13 +1948,7 @@ class Tableau {
|
|
|
2231
1948
|
}
|
|
2232
1949
|
// ========== Core Simplex Operations ==========
|
|
2233
1950
|
simplex() {
|
|
2234
|
-
|
|
2235
|
-
if (shouldUseSparse(this)) {
|
|
2236
|
-
sparseSimplex.call(this);
|
|
2237
|
-
}
|
|
2238
|
-
else {
|
|
2239
|
-
simplex.call(this);
|
|
2240
|
-
}
|
|
1951
|
+
simplex.call(this);
|
|
2241
1952
|
return this;
|
|
2242
1953
|
}
|
|
2243
1954
|
phase1() {
|
|
@@ -2246,6 +1957,14 @@ class Tableau {
|
|
|
2246
1957
|
phase2() {
|
|
2247
1958
|
return phase2.call(this);
|
|
2248
1959
|
}
|
|
1960
|
+
/**
|
|
1961
|
+
* Dual simplex for warm-starting after adding bound constraints.
|
|
1962
|
+
* Use when solution is dual feasible but may be primal infeasible.
|
|
1963
|
+
* @returns Number of iterations, or -1 if dual infeasible
|
|
1964
|
+
*/
|
|
1965
|
+
dualSimplex() {
|
|
1966
|
+
return dualSimplex.call(this);
|
|
1967
|
+
}
|
|
2249
1968
|
pivot(pivotRowIndex, pivotColumnIndex) {
|
|
2250
1969
|
pivot.call(this, pivotRowIndex, pivotColumnIndex);
|
|
2251
1970
|
}
|
|
@@ -2907,6 +2626,7 @@ class Model {
|
|
|
2907
2626
|
_removeConstraint(constraint) {
|
|
2908
2627
|
const idx = this.constraints.indexOf(constraint);
|
|
2909
2628
|
if (idx === -1) {
|
|
2629
|
+
// eslint-disable-next-line no-console
|
|
2910
2630
|
console.warn("[Model.removeConstraint] Constraint not present in model");
|
|
2911
2631
|
return;
|
|
2912
2632
|
}
|
|
@@ -2936,6 +2656,7 @@ class Model {
|
|
|
2936
2656
|
removeVariable(variable) {
|
|
2937
2657
|
const idx = this.variables.indexOf(variable);
|
|
2938
2658
|
if (idx === -1) {
|
|
2659
|
+
// eslint-disable-next-line no-console
|
|
2939
2660
|
console.warn("[Model.removeVariable] Variable not present in model");
|
|
2940
2661
|
return;
|
|
2941
2662
|
}
|
|
@@ -3055,11 +2776,20 @@ class Model {
|
|
|
3055
2776
|
const unrestrictedVarIds = jsonModel.unrestricted || {};
|
|
3056
2777
|
// Instantiating variables and constraint terms
|
|
3057
2778
|
const objectiveName = jsonModel.optimize;
|
|
2779
|
+
// Check if objectiveName exists as a coefficient key in any variable.
|
|
2780
|
+
// If not, and it matches a variable name, the user wants to optimize
|
|
2781
|
+
// that variable directly (implicit cost of 1).
|
|
2782
|
+
const objectiveIsAttribute = variableIds.some((id) => objectiveName in variables[id]);
|
|
2783
|
+
const objectiveIsVariable = !objectiveIsAttribute && variableIds.includes(objectiveName);
|
|
3058
2784
|
for (let v = 0; v < nVariables; v += 1) {
|
|
3059
2785
|
// Creation of the variables
|
|
3060
2786
|
const variableId = variableIds[v];
|
|
3061
2787
|
const variableConstraints = variables[variableId];
|
|
3062
|
-
const cost =
|
|
2788
|
+
const cost = objectiveIsVariable
|
|
2789
|
+
? variableId === objectiveName
|
|
2790
|
+
? 1
|
|
2791
|
+
: 0
|
|
2792
|
+
: variableConstraints[objectiveName] || 0;
|
|
3063
2793
|
const isBinary = !!binaryVarIds[variableId];
|
|
3064
2794
|
const isInteger = !!integerVarIds[variableId] || isBinary;
|
|
3065
2795
|
const isUnrestricted = !!unrestrictedVarIds[variableId];
|
|
@@ -3904,7 +3634,7 @@ function Polyopt(solver, model) {
|
|
|
3904
3634
|
}
|
|
3905
3635
|
}
|
|
3906
3636
|
// Strip metadata so each Pareto vertex only contains value-bearing fields.
|
|
3907
|
-
const { feasible, result, bounded, ...paretoPayload } = solution;
|
|
3637
|
+
const { feasible: _feasible, result: _result, bounded: _bounded, ...paretoPayload } = solution;
|
|
3908
3638
|
paretoVertices.push(paretoPayload);
|
|
3909
3639
|
}
|
|
3910
3640
|
// Derive equality constraints that represent the averaged objective values.
|
|
@@ -3930,8 +3660,16 @@ function Polyopt(solver, model) {
|
|
|
3930
3660
|
function createCut$1(type, varIndex, value) {
|
|
3931
3661
|
return { type, varIndex, value };
|
|
3932
3662
|
}
|
|
3933
|
-
function createBranch$1(relaxedEvaluation, cuts, depth) {
|
|
3934
|
-
return {
|
|
3663
|
+
function createBranch$1(relaxedEvaluation, cuts, depth, branchVarIndex, branchDirection, branchFractionality, parentEvaluation) {
|
|
3664
|
+
return {
|
|
3665
|
+
relaxedEvaluation,
|
|
3666
|
+
cuts,
|
|
3667
|
+
depth,
|
|
3668
|
+
branchVarIndex,
|
|
3669
|
+
branchDirection,
|
|
3670
|
+
branchFractionality,
|
|
3671
|
+
parentEvaluation,
|
|
3672
|
+
};
|
|
3935
3673
|
}
|
|
3936
3674
|
/**
|
|
3937
3675
|
* Enhanced branch-and-cut with:
|
|
@@ -3940,7 +3678,8 @@ function createBranch$1(relaxedEvaluation, cuts, depth) {
|
|
|
3940
3678
|
* - Diving heuristic for quick feasible solutions
|
|
3941
3679
|
*/
|
|
3942
3680
|
function createEnhancedBranchAndCutService(options = {}) {
|
|
3943
|
-
const { nodeSelection = "hybrid", branching = "pseudocost", useDiving = true,
|
|
3681
|
+
const { nodeSelection = "hybrid", branching = "pseudocost", useDiving: _useDiving = true, // Reserved for future diving heuristic
|
|
3682
|
+
strongBranchingCandidates = 5, } = options;
|
|
3944
3683
|
// Pseudocost data per variable index
|
|
3945
3684
|
const pseudoCosts = new Map();
|
|
3946
3685
|
const getPseudoCost = (varIndex) => {
|
|
@@ -3973,7 +3712,7 @@ function createEnhancedBranchAndCutService(options = {}) {
|
|
|
3973
3712
|
// Product score (like SCIP's default)
|
|
3974
3713
|
return Math.max(upEstimate, 1e-6) * Math.max(downEstimate, 1e-6);
|
|
3975
3714
|
};
|
|
3976
|
-
const selectBranchingVariable = (tableau,
|
|
3715
|
+
const selectBranchingVariable = (tableau, _currentEval) => {
|
|
3977
3716
|
const width = tableau.width;
|
|
3978
3717
|
const matrix = tableau.matrix;
|
|
3979
3718
|
const rhsColumn = tableau.rhsColumn;
|
|
@@ -4117,11 +3856,11 @@ function createEnhancedBranchAndCutService(options = {}) {
|
|
|
4117
3856
|
else {
|
|
4118
3857
|
break;
|
|
4119
3858
|
}
|
|
4120
|
-
if (activeBranch.relaxedEvaluation
|
|
3859
|
+
if (activeBranch.relaxedEvaluation >= bestEvaluation) {
|
|
4121
3860
|
continue;
|
|
4122
3861
|
}
|
|
4123
3862
|
const cuts = activeBranch.cuts;
|
|
4124
|
-
|
|
3863
|
+
tableau.evaluation;
|
|
4125
3864
|
applyCuts(tableau, cuts);
|
|
4126
3865
|
iterations++;
|
|
4127
3866
|
if (!tableau.feasible) {
|
|
@@ -4132,12 +3871,12 @@ function createEnhancedBranchAndCutService(options = {}) {
|
|
|
4132
3871
|
continue;
|
|
4133
3872
|
}
|
|
4134
3873
|
// Update pseudocosts based on observed improvement
|
|
4135
|
-
if (
|
|
4136
|
-
|
|
4137
|
-
|
|
4138
|
-
|
|
4139
|
-
const
|
|
4140
|
-
updatePseudoCost(
|
|
3874
|
+
if (activeBranch.branchVarIndex !== undefined &&
|
|
3875
|
+
activeBranch.branchDirection !== undefined &&
|
|
3876
|
+
activeBranch.branchFractionality !== undefined &&
|
|
3877
|
+
activeBranch.parentEvaluation !== undefined) {
|
|
3878
|
+
const improvement = Math.abs(evaluation - activeBranch.parentEvaluation);
|
|
3879
|
+
updatePseudoCost(activeBranch.branchVarIndex, activeBranch.branchDirection, improvement, activeBranch.branchFractionality);
|
|
4141
3880
|
}
|
|
4142
3881
|
if (evaluation === bestEvaluation) {
|
|
4143
3882
|
let isCurrentEvaluationWorse = true;
|
|
@@ -4216,22 +3955,24 @@ function createEnhancedBranchAndCutService(options = {}) {
|
|
|
4216
3955
|
cutsLow.push(cut);
|
|
4217
3956
|
}
|
|
4218
3957
|
}
|
|
4219
|
-
const
|
|
4220
|
-
const
|
|
4221
|
-
const
|
|
3958
|
+
const ceilVal = Math.ceil(varValue);
|
|
3959
|
+
const floorVal = Math.floor(varValue);
|
|
3960
|
+
const fracUp = ceilVal - varValue; // Distance to ceil
|
|
3961
|
+
const fracDown = varValue - floorVal; // Distance to floor
|
|
3962
|
+
const cutHigh = createCut$1("min", varIndex, ceilVal);
|
|
4222
3963
|
cutsHigh.push(cutHigh);
|
|
4223
|
-
const cutLow = createCut$1("max", varIndex,
|
|
3964
|
+
const cutLow = createCut$1("max", varIndex, floorVal);
|
|
4224
3965
|
cutsLow.push(cutLow);
|
|
4225
3966
|
const newDepth = activeBranch.depth + 1;
|
|
4226
3967
|
if (useDepthFirst) {
|
|
4227
3968
|
// Push in reverse order so 'up' branch is explored first
|
|
4228
3969
|
// (often better for minimization with binary vars)
|
|
4229
|
-
depthFirstStack.push(createBranch$1(evaluation, cutsLow, newDepth));
|
|
4230
|
-
depthFirstStack.push(createBranch$1(evaluation, cutsHigh, newDepth));
|
|
3970
|
+
depthFirstStack.push(createBranch$1(evaluation, cutsLow, newDepth, varIndex, "down", fracDown, evaluation));
|
|
3971
|
+
depthFirstStack.push(createBranch$1(evaluation, cutsHigh, newDepth, varIndex, "up", fracUp, evaluation));
|
|
4231
3972
|
}
|
|
4232
3973
|
else {
|
|
4233
|
-
branches.push(createBranch$1(evaluation, cutsHigh, newDepth));
|
|
4234
|
-
branches.push(createBranch$1(evaluation, cutsLow, newDepth));
|
|
3974
|
+
branches.push(createBranch$1(evaluation, cutsHigh, newDepth, varIndex, "up", fracUp, evaluation));
|
|
3975
|
+
branches.push(createBranch$1(evaluation, cutsLow, newDepth, varIndex, "down", fracDown, evaluation));
|
|
4235
3976
|
}
|
|
4236
3977
|
}
|
|
4237
3978
|
}
|
|
@@ -4469,7 +4210,7 @@ function createIncrementalBranchAndCutService(options = {}) {
|
|
|
4469
4210
|
else {
|
|
4470
4211
|
break;
|
|
4471
4212
|
}
|
|
4472
|
-
if (activeBranch.relaxedEvaluation
|
|
4213
|
+
if (activeBranch.relaxedEvaluation >= bestEvaluation) {
|
|
4473
4214
|
continue;
|
|
4474
4215
|
}
|
|
4475
4216
|
const parentEval = tableau.evaluation;
|