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.browser.mjs
CHANGED
|
@@ -51,6 +51,22 @@ class BranchMinHeap {
|
|
|
51
51
|
this.heap = new Array(initialCapacity);
|
|
52
52
|
this.size = 0;
|
|
53
53
|
this.seqCounter = 0;
|
|
54
|
+
this.pool = new Array(64);
|
|
55
|
+
this.poolSize = 0;
|
|
56
|
+
}
|
|
57
|
+
allocEntry(branch, seq) {
|
|
58
|
+
if (this.poolSize > 0) {
|
|
59
|
+
const entry = this.pool[--this.poolSize];
|
|
60
|
+
entry.branch = branch;
|
|
61
|
+
entry.seq = seq;
|
|
62
|
+
return entry;
|
|
63
|
+
}
|
|
64
|
+
return { branch, seq };
|
|
65
|
+
}
|
|
66
|
+
freeEntry(entry) {
|
|
67
|
+
if (this.poolSize < 256) {
|
|
68
|
+
this.pool[this.poolSize++] = entry;
|
|
69
|
+
}
|
|
54
70
|
}
|
|
55
71
|
get length() {
|
|
56
72
|
return this.size;
|
|
@@ -78,7 +94,7 @@ class BranchMinHeap {
|
|
|
78
94
|
if (idx >= heap.length) {
|
|
79
95
|
heap.length = heap.length * 2;
|
|
80
96
|
}
|
|
81
|
-
const entry =
|
|
97
|
+
const entry = this.allocEntry(branch, this.seqCounter++);
|
|
82
98
|
// Bubble up
|
|
83
99
|
while (idx > 0) {
|
|
84
100
|
const parentIdx = (idx - 1) >> 1;
|
|
@@ -96,8 +112,11 @@ class BranchMinHeap {
|
|
|
96
112
|
return undefined;
|
|
97
113
|
}
|
|
98
114
|
const heap = this.heap;
|
|
99
|
-
const
|
|
115
|
+
const poppedEntry = heap[0];
|
|
116
|
+
const result = poppedEntry.branch;
|
|
100
117
|
this.size--;
|
|
118
|
+
// Return entry to pool
|
|
119
|
+
this.freeEntry(poppedEntry);
|
|
101
120
|
if (this.size === 0) {
|
|
102
121
|
return result;
|
|
103
122
|
}
|
|
@@ -140,15 +159,16 @@ function createBranchAndCutService() {
|
|
|
140
159
|
tableau.addCutConstraints(branchingCuts);
|
|
141
160
|
tableau.simplex();
|
|
142
161
|
if ((_a = tableau.model) === null || _a === void 0 ? void 0 : _a.useMIRCuts) {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
162
|
+
// Optimization: reuse previous "after" as next "before" to avoid redundant computation
|
|
163
|
+
let fractionalVolume = tableau.computeFractionalVolume(true);
|
|
164
|
+
while (fractionalVolume > 0) {
|
|
146
165
|
tableau.applyMIRCuts();
|
|
147
166
|
tableau.simplex();
|
|
148
167
|
const fractionalVolumeAfter = tableau.computeFractionalVolume(true);
|
|
149
|
-
if (fractionalVolumeAfter >= 0.9 *
|
|
150
|
-
|
|
168
|
+
if (fractionalVolumeAfter >= 0.9 * fractionalVolume) {
|
|
169
|
+
break;
|
|
151
170
|
}
|
|
171
|
+
fractionalVolume = fractionalVolumeAfter;
|
|
152
172
|
}
|
|
153
173
|
}
|
|
154
174
|
};
|
|
@@ -165,7 +185,10 @@ function createBranchAndCutService() {
|
|
|
165
185
|
let bestEvaluation = Infinity;
|
|
166
186
|
let bestBranch = null;
|
|
167
187
|
const bestOptionalObjectivesEvaluations = [];
|
|
168
|
-
|
|
188
|
+
// Cache optionalObjectives reference to avoid repeated property lookups in hot loop
|
|
189
|
+
const optionalObjectives = tableau.optionalObjectives;
|
|
190
|
+
const nOptionalObjectives = optionalObjectives.length;
|
|
191
|
+
for (let oInit = 0; oInit < nOptionalObjectives; oInit += 1) {
|
|
169
192
|
bestOptionalObjectivesEvaluations.push(Infinity);
|
|
170
193
|
}
|
|
171
194
|
const branch = createBranch$2(-Infinity, []);
|
|
@@ -184,7 +207,7 @@ function createBranchAndCutService() {
|
|
|
184
207
|
}
|
|
185
208
|
}
|
|
186
209
|
const activeBranch = branches.pop();
|
|
187
|
-
if (activeBranch.relaxedEvaluation
|
|
210
|
+
if (activeBranch.relaxedEvaluation >= bestEvaluation) {
|
|
188
211
|
continue;
|
|
189
212
|
}
|
|
190
213
|
const cuts = activeBranch.cuts;
|
|
@@ -199,13 +222,13 @@ function createBranchAndCutService() {
|
|
|
199
222
|
}
|
|
200
223
|
if (evaluation === bestEvaluation) {
|
|
201
224
|
let isCurrentEvaluationWorse = true;
|
|
202
|
-
for (let o = 0; o <
|
|
203
|
-
|
|
204
|
-
|
|
225
|
+
for (let o = 0; o < nOptionalObjectives; o += 1) {
|
|
226
|
+
const currentCost = optionalObjectives[o].reducedCosts[0];
|
|
227
|
+
const bestCost = bestOptionalObjectivesEvaluations[o];
|
|
228
|
+
if (currentCost > bestCost) {
|
|
205
229
|
break;
|
|
206
230
|
}
|
|
207
|
-
else if (
|
|
208
|
-
bestOptionalObjectivesEvaluations[o]) {
|
|
231
|
+
else if (currentCost < bestCost) {
|
|
209
232
|
isCurrentEvaluationWorse = false;
|
|
210
233
|
break;
|
|
211
234
|
}
|
|
@@ -222,9 +245,9 @@ function createBranchAndCutService() {
|
|
|
222
245
|
}
|
|
223
246
|
bestBranch = activeBranch;
|
|
224
247
|
bestEvaluation = evaluation;
|
|
225
|
-
for (let oCopy = 0; oCopy <
|
|
248
|
+
for (let oCopy = 0; oCopy < nOptionalObjectives; oCopy += 1) {
|
|
226
249
|
bestOptionalObjectivesEvaluations[oCopy] =
|
|
227
|
-
|
|
250
|
+
optionalObjectives[oCopy].reducedCosts[0];
|
|
228
251
|
}
|
|
229
252
|
if ((_e = tableau.model) === null || _e === void 0 ? void 0 : _e.keep_solutions) {
|
|
230
253
|
const nowSolution = tableau.model.tableau.getSolution();
|
|
@@ -242,6 +265,7 @@ function createBranchAndCutService() {
|
|
|
242
265
|
}
|
|
243
266
|
const variable = tableau.getMostFractionalVar();
|
|
244
267
|
const varIndex = variable.index;
|
|
268
|
+
const varValue = variable.value;
|
|
245
269
|
const cutsHigh = [];
|
|
246
270
|
const cutsLow = [];
|
|
247
271
|
const nCuts = cuts.length;
|
|
@@ -260,11 +284,9 @@ function createBranchAndCutService() {
|
|
|
260
284
|
cutsLow.push(cut);
|
|
261
285
|
}
|
|
262
286
|
}
|
|
263
|
-
const
|
|
264
|
-
const max = Math.floor(variable.value);
|
|
265
|
-
const cutHigh = createCut$2("min", varIndex, min);
|
|
287
|
+
const cutHigh = createCut$2("min", varIndex, Math.ceil(varValue));
|
|
266
288
|
cutsHigh.push(cutHigh);
|
|
267
|
-
const cutLow = createCut$2("max", varIndex,
|
|
289
|
+
const cutLow = createCut$2("max", varIndex, Math.floor(varValue));
|
|
268
290
|
cutsLow.push(cutLow);
|
|
269
291
|
branches.push(createBranch$2(evaluation, cutsHigh));
|
|
270
292
|
branches.push(createBranch$2(evaluation, cutsLow));
|
|
@@ -278,6 +300,50 @@ function createBranchAndCutService() {
|
|
|
278
300
|
return { applyCuts, branchAndCut };
|
|
279
301
|
}
|
|
280
302
|
|
|
303
|
+
/**
|
|
304
|
+
* Optimized cycle detector using hash-based O(1) lookup.
|
|
305
|
+
* The original checkForCycles was O(n²) - comparing every pair against all others.
|
|
306
|
+
* This version uses a Map to track where each (leaving,entering) pair occurred,
|
|
307
|
+
* making duplicate detection O(1) average case.
|
|
308
|
+
*/
|
|
309
|
+
class CycleDetector {
|
|
310
|
+
constructor() {
|
|
311
|
+
this.pairs = [];
|
|
312
|
+
this.positions = new Map();
|
|
313
|
+
}
|
|
314
|
+
add(leaving, entering) {
|
|
315
|
+
const key = `${leaving}_${entering}`;
|
|
316
|
+
const pos = this.pairs.length;
|
|
317
|
+
this.pairs.push([leaving, entering]);
|
|
318
|
+
const prevPositions = this.positions.get(key);
|
|
319
|
+
if (prevPositions === undefined) {
|
|
320
|
+
this.positions.set(key, [pos]);
|
|
321
|
+
return [];
|
|
322
|
+
}
|
|
323
|
+
// Check if any previous occurrence starts a repeating cycle
|
|
324
|
+
for (const startPos of prevPositions) {
|
|
325
|
+
const cycleLength = pos - startPos;
|
|
326
|
+
// Need at least cycleLength more elements to verify
|
|
327
|
+
if (cycleLength > this.pairs.length - pos) {
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
let cycleFound = true;
|
|
331
|
+
for (let i = 1; i < cycleLength && startPos + cycleLength + i < this.pairs.length; i++) {
|
|
332
|
+
const p1 = this.pairs[startPos + i];
|
|
333
|
+
const p2 = this.pairs[startPos + cycleLength + i];
|
|
334
|
+
if (p1[0] !== p2[0] || p1[1] !== p2[1]) {
|
|
335
|
+
cycleFound = false;
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (cycleFound) {
|
|
340
|
+
return [startPos, cycleLength];
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
prevPositions.push(pos);
|
|
344
|
+
return [];
|
|
345
|
+
}
|
|
346
|
+
}
|
|
281
347
|
function simplex() {
|
|
282
348
|
this.bounded = true;
|
|
283
349
|
this.phase1();
|
|
@@ -286,21 +352,125 @@ function simplex() {
|
|
|
286
352
|
}
|
|
287
353
|
return this;
|
|
288
354
|
}
|
|
355
|
+
/**
|
|
356
|
+
* Dual simplex algorithm for warm-starting after adding constraints.
|
|
357
|
+
*
|
|
358
|
+
* Use when: The current solution is dual feasible (reduced costs valid) but
|
|
359
|
+
* may be primal infeasible (some RHS values negative). This is common after
|
|
360
|
+
* adding bound constraints in branch-and-cut.
|
|
361
|
+
*
|
|
362
|
+
* Algorithm:
|
|
363
|
+
* 1. Find a basic variable with negative value (leaving variable)
|
|
364
|
+
* 2. Find entering variable using dual ratio test
|
|
365
|
+
* 3. Pivot to restore primal feasibility
|
|
366
|
+
* 4. Repeat until all basic variables are non-negative
|
|
367
|
+
*
|
|
368
|
+
* @returns Number of iterations, or -1 if dual infeasible
|
|
369
|
+
*/
|
|
370
|
+
function dualSimplex() {
|
|
371
|
+
const matrix = this.matrix;
|
|
372
|
+
const width = this.width;
|
|
373
|
+
const rhsColumn = this.rhsColumn;
|
|
374
|
+
const lastColumn = width - 1;
|
|
375
|
+
const lastRow = this.height - 1;
|
|
376
|
+
const precision = this.precision;
|
|
377
|
+
const negPrecision = -precision;
|
|
378
|
+
let iterations = 0;
|
|
379
|
+
const maxIterations = 10000; // Safety limit
|
|
380
|
+
while (iterations < maxIterations) {
|
|
381
|
+
// Step 1: Find leaving variable (row with most negative RHS)
|
|
382
|
+
let leavingRow = 0;
|
|
383
|
+
let minRHS = negPrecision;
|
|
384
|
+
for (let r = 1; r <= lastRow; r++) {
|
|
385
|
+
const rhsValue = matrix[r * width + rhsColumn];
|
|
386
|
+
if (rhsValue < minRHS) {
|
|
387
|
+
minRHS = rhsValue;
|
|
388
|
+
leavingRow = r;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
// If no negative RHS, we're primal feasible - done!
|
|
392
|
+
if (leavingRow === 0) {
|
|
393
|
+
this.feasible = true;
|
|
394
|
+
this.setEvaluation();
|
|
395
|
+
return iterations;
|
|
396
|
+
}
|
|
397
|
+
// Step 2: Find entering variable using dual ratio test
|
|
398
|
+
// For each non-basic variable j with a_ij < 0 (negative coefficient in leaving row),
|
|
399
|
+
// compute ratio = reduced_cost[j] / |a_ij|
|
|
400
|
+
// Choose the one with minimum ratio (to maintain dual feasibility)
|
|
401
|
+
let enteringColumn = 0;
|
|
402
|
+
let minRatio = Infinity;
|
|
403
|
+
const leavingRowOffset = leavingRow * width;
|
|
404
|
+
for (let c = 1; c <= lastColumn; c++) {
|
|
405
|
+
const coefficient = matrix[leavingRowOffset + c];
|
|
406
|
+
// Only consider columns with negative coefficient in leaving row
|
|
407
|
+
if (coefficient < negPrecision) {
|
|
408
|
+
// Reduced cost is in row 0 (cost row)
|
|
409
|
+
const reducedCost = matrix[c];
|
|
410
|
+
// For minimization, reduced costs should be >= 0 for optimality
|
|
411
|
+
// Ratio test: reducedCost / |coefficient|
|
|
412
|
+
if (reducedCost >= negPrecision) {
|
|
413
|
+
const ratio = reducedCost / -coefficient;
|
|
414
|
+
if (ratio < minRatio) {
|
|
415
|
+
minRatio = ratio;
|
|
416
|
+
enteringColumn = c;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
// If no entering column found, the problem is dual infeasible (primal unbounded)
|
|
422
|
+
if (enteringColumn === 0) {
|
|
423
|
+
this.feasible = false;
|
|
424
|
+
return -1;
|
|
425
|
+
}
|
|
426
|
+
// Step 3: Pivot
|
|
427
|
+
this.pivot(leavingRow, enteringColumn);
|
|
428
|
+
iterations++;
|
|
429
|
+
}
|
|
430
|
+
// Hit iteration limit - something went wrong
|
|
431
|
+
this.feasible = false;
|
|
432
|
+
return iterations;
|
|
433
|
+
}
|
|
289
434
|
function phase1() {
|
|
290
435
|
const debugCheckForCycles = this.model.checkForCycles;
|
|
291
|
-
const
|
|
436
|
+
const cycleDetector = debugCheckForCycles ? new CycleDetector() : null;
|
|
292
437
|
const matrix = this.matrix;
|
|
293
438
|
const width = this.width;
|
|
294
439
|
const rhsColumn = this.rhsColumn;
|
|
295
440
|
const lastColumn = this.width - 1;
|
|
296
441
|
const lastRow = this.height - 1;
|
|
297
|
-
|
|
442
|
+
const precision = this.precision;
|
|
443
|
+
const negPrecision = -precision;
|
|
444
|
+
// Cache arrays for faster access in hot loops
|
|
445
|
+
const unrestrictedVars = this.unrestrictedVars;
|
|
446
|
+
const varIndexByRow = this.varIndexByRow;
|
|
447
|
+
const varIndexByCol = this.varIndexByCol;
|
|
448
|
+
// Anti-cycling: on degenerate problems, the max-quotient pivot rule
|
|
449
|
+
// can oscillate, corrupting the matrix. We detect stalling early
|
|
450
|
+
// (after SAVE_THRESHOLD iterations without RHS improvement) and save
|
|
451
|
+
// the matrix state. If still stuck after maxQuotientLimit iterations,
|
|
452
|
+
// we restore the clean matrix and switch to Bland's rule. The lazy
|
|
453
|
+
// save avoids the copy cost on the vast majority of phase1 calls
|
|
454
|
+
// that converge quickly.
|
|
455
|
+
const SAVE_THRESHOLD = 10;
|
|
456
|
+
const maxQuotientLimit = Math.max(lastRow, lastColumn);
|
|
298
457
|
let iterations = 0;
|
|
458
|
+
let useBland = false;
|
|
459
|
+
let initialRHS = -Infinity;
|
|
460
|
+
let savedMatrix = null;
|
|
461
|
+
let savedVarIndexByRow = null;
|
|
462
|
+
let savedVarIndexByCol = null;
|
|
463
|
+
let savedRowByVarIndex = null;
|
|
464
|
+
let savedColByVarIndex = null;
|
|
299
465
|
while (true) {
|
|
466
|
+
// Find leaving row (most negative RHS among restricted basic vars).
|
|
467
|
+
// Unrestricted variables can validly have negative values, so rows
|
|
468
|
+
// where the basic variable is unrestricted are not infeasible.
|
|
300
469
|
let leavingRowIndex = 0;
|
|
301
|
-
let rhsValue =
|
|
470
|
+
let rhsValue = negPrecision;
|
|
302
471
|
for (let r = 1; r <= lastRow; r++) {
|
|
303
|
-
|
|
472
|
+
if (unrestrictedVars[varIndexByRow[r]] === true)
|
|
473
|
+
continue;
|
|
304
474
|
const value = matrix[r * width + rhsColumn];
|
|
305
475
|
if (value < rhsValue) {
|
|
306
476
|
rhsValue = value;
|
|
@@ -311,18 +481,95 @@ function phase1() {
|
|
|
311
481
|
this.feasible = true;
|
|
312
482
|
return iterations;
|
|
313
483
|
}
|
|
484
|
+
// Detect non-convergence and apply anti-cycling.
|
|
485
|
+
if (!useBland && iterations > 0 && rhsValue <= initialRHS) {
|
|
486
|
+
if (iterations >= SAVE_THRESHOLD && savedMatrix === null) {
|
|
487
|
+
// Stalling detected: save matrix for potential rollback
|
|
488
|
+
savedMatrix = matrix.slice();
|
|
489
|
+
savedVarIndexByRow = varIndexByRow.slice();
|
|
490
|
+
savedVarIndexByCol = varIndexByCol.slice();
|
|
491
|
+
savedRowByVarIndex = this.rowByVarIndex.slice();
|
|
492
|
+
savedColByVarIndex = this.colByVarIndex.slice();
|
|
493
|
+
}
|
|
494
|
+
if (iterations >= maxQuotientLimit) {
|
|
495
|
+
// Still stuck after generous budget: restore and switch
|
|
496
|
+
useBland = true;
|
|
497
|
+
if (savedMatrix) {
|
|
498
|
+
matrix.set(savedMatrix);
|
|
499
|
+
for (let i = 0; i < savedVarIndexByRow.length; i++) {
|
|
500
|
+
varIndexByRow[i] = savedVarIndexByRow[i];
|
|
501
|
+
}
|
|
502
|
+
for (let i = 0; i < savedVarIndexByCol.length; i++) {
|
|
503
|
+
varIndexByCol[i] = savedVarIndexByCol[i];
|
|
504
|
+
}
|
|
505
|
+
for (let i = 0; i < savedRowByVarIndex.length; i++) {
|
|
506
|
+
this.rowByVarIndex[i] = savedRowByVarIndex[i];
|
|
507
|
+
}
|
|
508
|
+
for (let i = 0; i < savedColByVarIndex.length; i++) {
|
|
509
|
+
this.colByVarIndex[i] = savedColByVarIndex[i];
|
|
510
|
+
}
|
|
511
|
+
iterations = 0;
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
if (iterations === 0) {
|
|
517
|
+
initialRHS = rhsValue;
|
|
518
|
+
}
|
|
519
|
+
// Find entering column.
|
|
520
|
+
// Prefer columns with negative coefficient in leaving row (these directly
|
|
521
|
+
// fix the infeasibility by making RHS positive after pivot).
|
|
522
|
+
// Only fall back to unrestricted variables with positive coefficient when
|
|
523
|
+
// no negative coefficient column exists - this "swaps" the infeasibility
|
|
524
|
+
// to an unrestricted variable (which is allowed to be negative).
|
|
314
525
|
let enteringColumn = 0;
|
|
315
|
-
let maxQuotient = -Infinity;
|
|
316
|
-
const costRowOffset = 0; // row 0
|
|
317
526
|
const leavingRowOffset = leavingRowIndex * width;
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
const
|
|
323
|
-
if (
|
|
324
|
-
maxQuotient = quotient;
|
|
527
|
+
if (useBland) {
|
|
528
|
+
// Bland's rule: pick the first eligible column (smallest index).
|
|
529
|
+
// First pass: negative coefficients only (directly fix infeasibility)
|
|
530
|
+
for (let c = 1; c <= lastColumn; c++) {
|
|
531
|
+
const coefficient = matrix[leavingRowOffset + c];
|
|
532
|
+
if (coefficient < negPrecision) {
|
|
325
533
|
enteringColumn = c;
|
|
534
|
+
break;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
// Fallback: unrestricted with non-zero coefficient (swap infeasibility)
|
|
538
|
+
if (enteringColumn === 0) {
|
|
539
|
+
for (let c = 1; c <= lastColumn; c++) {
|
|
540
|
+
const coefficient = matrix[leavingRowOffset + c];
|
|
541
|
+
if (unrestrictedVars[varIndexByCol[c]] === true &&
|
|
542
|
+
(coefficient < negPrecision || coefficient > precision)) {
|
|
543
|
+
enteringColumn = c;
|
|
544
|
+
break;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
else {
|
|
550
|
+
// Max-quotient rule: faster in practice but can oscillate on
|
|
551
|
+
// degenerate problems.
|
|
552
|
+
// First pass: negative coefficients only
|
|
553
|
+
let maxQuotient = -Infinity;
|
|
554
|
+
for (let c = 1; c <= lastColumn; c++) {
|
|
555
|
+
const coefficient = matrix[leavingRowOffset + c];
|
|
556
|
+
if (coefficient < negPrecision) {
|
|
557
|
+
const quotient = -matrix[c] / coefficient;
|
|
558
|
+
if (maxQuotient < quotient) {
|
|
559
|
+
maxQuotient = quotient;
|
|
560
|
+
enteringColumn = c;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
// Fallback: unrestricted with non-zero coefficient
|
|
565
|
+
if (enteringColumn === 0) {
|
|
566
|
+
for (let c = 1; c <= lastColumn; c++) {
|
|
567
|
+
const coefficient = matrix[leavingRowOffset + c];
|
|
568
|
+
if (unrestrictedVars[varIndexByCol[c]] === true &&
|
|
569
|
+
(coefficient < negPrecision || coefficient > precision)) {
|
|
570
|
+
enteringColumn = c;
|
|
571
|
+
break;
|
|
572
|
+
}
|
|
326
573
|
}
|
|
327
574
|
}
|
|
328
575
|
}
|
|
@@ -330,12 +577,8 @@ function phase1() {
|
|
|
330
577
|
this.feasible = false;
|
|
331
578
|
return iterations;
|
|
332
579
|
}
|
|
333
|
-
if (
|
|
334
|
-
|
|
335
|
-
this.varIndexByRow[leavingRowIndex],
|
|
336
|
-
this.varIndexByCol[enteringColumn],
|
|
337
|
-
]);
|
|
338
|
-
const cycleData = this.checkForCycles(varIndexesCycle);
|
|
580
|
+
if (cycleDetector) {
|
|
581
|
+
const cycleData = cycleDetector.add(varIndexByRow[leavingRowIndex], varIndexByCol[enteringColumn]);
|
|
339
582
|
if (cycleData.length > 0) {
|
|
340
583
|
this.model.messages.push("Cycle in phase 1");
|
|
341
584
|
this.model.messages.push("Start :" + cycleData[0]);
|
|
@@ -350,18 +593,38 @@ function phase1() {
|
|
|
350
593
|
}
|
|
351
594
|
function phase2() {
|
|
352
595
|
const debugCheckForCycles = this.model.checkForCycles;
|
|
353
|
-
const
|
|
596
|
+
const cycleDetector = debugCheckForCycles ? new CycleDetector() : null;
|
|
354
597
|
const matrix = this.matrix;
|
|
355
598
|
const width = this.width;
|
|
356
599
|
const rhsColumn = this.rhsColumn;
|
|
357
600
|
const lastColumn = this.width - 1;
|
|
358
601
|
const lastRow = this.height - 1;
|
|
359
602
|
const precision = this.precision;
|
|
603
|
+
const negPrecision = -precision;
|
|
360
604
|
const nOptionalObjectives = this.optionalObjectives.length;
|
|
361
605
|
let optionalCostsColumns = null;
|
|
606
|
+
// Cache arrays for faster access in hot loops
|
|
607
|
+
const unrestrictedVars = this.unrestrictedVars;
|
|
608
|
+
const varIndexByCol = this.varIndexByCol;
|
|
609
|
+
const varIndexByRow = this.varIndexByRow;
|
|
610
|
+
// Note: costRowIndex is always 0, so we access matrix[c] directly
|
|
362
611
|
let iterations = 0;
|
|
363
612
|
let reducedCost;
|
|
364
613
|
let unrestricted;
|
|
614
|
+
// Anti-cycling for phase 2: if the objective stalls (no meaningful
|
|
615
|
+
// improvement over several hundred iterations), the simplex is
|
|
616
|
+
// cycling through degenerate vertices. Switch to Bland's rule which
|
|
617
|
+
// guarantees termination in exact arithmetic. In floating-point,
|
|
618
|
+
// even Bland's can cycle indefinitely, so we also impose a limit:
|
|
619
|
+
// if Bland's runs for lastRow iterations without improving the
|
|
620
|
+
// objective, we accept the current solution as optimal.
|
|
621
|
+
const PHASE2_WINDOW = 100;
|
|
622
|
+
const PHASE2_STALE_LIMIT = 5; // consecutive stale windows before switching
|
|
623
|
+
let useBland = false;
|
|
624
|
+
let windowStartObj = matrix[rhsColumn];
|
|
625
|
+
let staleWindows = 0;
|
|
626
|
+
let blandStartIter = 0;
|
|
627
|
+
let blandStartObj = 0;
|
|
365
628
|
// Partial pricing setup
|
|
366
629
|
// Batch size: use configured value or auto-compute (sqrt of columns, min 50, max 500)
|
|
367
630
|
const nColumns = lastColumn;
|
|
@@ -371,14 +634,68 @@ function phase2() {
|
|
|
371
634
|
// For small problems, just scan everything (no benefit from partial pricing)
|
|
372
635
|
const usePartialPricing = nColumns > batchSize * 2;
|
|
373
636
|
while (true) {
|
|
374
|
-
const costRowOffset = this.costRowIndex * width;
|
|
375
637
|
if (nOptionalObjectives > 0) {
|
|
376
638
|
optionalCostsColumns = [];
|
|
377
639
|
}
|
|
640
|
+
// Detect degenerate cycling: check every WINDOW iterations
|
|
641
|
+
// whether the objective has made meaningful progress.
|
|
642
|
+
if (!useBland && iterations > 0 && iterations % PHASE2_WINDOW === 0) {
|
|
643
|
+
const currentObj = matrix[rhsColumn];
|
|
644
|
+
const delta = Math.abs(currentObj - windowStartObj);
|
|
645
|
+
const scale = Math.max(1, Math.abs(windowStartObj));
|
|
646
|
+
if (delta / scale < 1e-10) {
|
|
647
|
+
staleWindows++;
|
|
648
|
+
if (staleWindows >= PHASE2_STALE_LIMIT) {
|
|
649
|
+
useBland = true;
|
|
650
|
+
blandStartIter = iterations;
|
|
651
|
+
blandStartObj = currentObj;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
else {
|
|
655
|
+
staleWindows = 0;
|
|
656
|
+
}
|
|
657
|
+
windowStartObj = currentObj;
|
|
658
|
+
}
|
|
659
|
+
// Bland's termination: in floating-point arithmetic, Bland's rule
|
|
660
|
+
// can cycle through degenerate bases indefinitely. If it hasn't
|
|
661
|
+
// improved the objective after lastRow pivots, accept the current
|
|
662
|
+
// solution (reduced costs are within floating-point noise of zero).
|
|
663
|
+
if (useBland && iterations - blandStartIter > lastRow) {
|
|
664
|
+
const currentObj = matrix[rhsColumn];
|
|
665
|
+
const delta = Math.abs(currentObj - blandStartObj);
|
|
666
|
+
const scale = Math.max(1, Math.abs(blandStartObj));
|
|
667
|
+
if (delta / scale < 1e-10) {
|
|
668
|
+
this.setEvaluation();
|
|
669
|
+
this.simplexIters += 1;
|
|
670
|
+
return iterations;
|
|
671
|
+
}
|
|
672
|
+
// Objective did improve; reset the counter
|
|
673
|
+
blandStartIter = iterations;
|
|
674
|
+
blandStartObj = currentObj;
|
|
675
|
+
}
|
|
378
676
|
let enteringColumn = 0;
|
|
379
677
|
let enteringValue = precision;
|
|
380
678
|
let isReducedCostNegative = false;
|
|
381
|
-
if (
|
|
679
|
+
if (useBland) {
|
|
680
|
+
// Bland's rule: pick first eligible column (smallest index)
|
|
681
|
+
for (let c = 1; c <= lastColumn; c++) {
|
|
682
|
+
reducedCost = matrix[c];
|
|
683
|
+
unrestricted = unrestrictedVars[varIndexByCol[c]] === true;
|
|
684
|
+
if (unrestricted && reducedCost < 0) {
|
|
685
|
+
enteringColumn = c;
|
|
686
|
+
enteringValue = -reducedCost;
|
|
687
|
+
isReducedCostNegative = true;
|
|
688
|
+
break;
|
|
689
|
+
}
|
|
690
|
+
if (reducedCost > precision) {
|
|
691
|
+
enteringColumn = c;
|
|
692
|
+
enteringValue = reducedCost;
|
|
693
|
+
isReducedCostNegative = false;
|
|
694
|
+
break;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
else if (usePartialPricing) {
|
|
382
699
|
// Partial pricing: scan columns in batches
|
|
383
700
|
const startBatch = this.pricingBatchStart;
|
|
384
701
|
let batchesScanned = 0;
|
|
@@ -388,10 +705,10 @@ function phase2() {
|
|
|
388
705
|
const batchStart = this.pricingBatchStart;
|
|
389
706
|
const batchEnd = Math.min(batchStart + batchSize - 1, lastColumn);
|
|
390
707
|
for (let c = batchStart; c <= batchEnd; c++) {
|
|
391
|
-
reducedCost = matrix[costRowOffset
|
|
392
|
-
unrestricted =
|
|
708
|
+
reducedCost = matrix[c]; // costRowOffset is 0
|
|
709
|
+
unrestricted = unrestrictedVars[varIndexByCol[c]] === true;
|
|
393
710
|
if (nOptionalObjectives > 0 &&
|
|
394
|
-
|
|
711
|
+
negPrecision < reducedCost &&
|
|
395
712
|
reducedCost < precision) {
|
|
396
713
|
optionalCostsColumns === null || optionalCostsColumns === void 0 ? void 0 : optionalCostsColumns.push(c);
|
|
397
714
|
continue;
|
|
@@ -422,10 +739,10 @@ function phase2() {
|
|
|
422
739
|
else {
|
|
423
740
|
// Full pricing for small problems
|
|
424
741
|
for (let c = 1; c <= lastColumn; c++) {
|
|
425
|
-
reducedCost = matrix[costRowOffset
|
|
426
|
-
unrestricted =
|
|
742
|
+
reducedCost = matrix[c]; // costRowOffset is 0
|
|
743
|
+
unrestricted = unrestrictedVars[varIndexByCol[c]] === true;
|
|
427
744
|
if (nOptionalObjectives > 0 &&
|
|
428
|
-
|
|
745
|
+
negPrecision < reducedCost &&
|
|
429
746
|
reducedCost < precision) {
|
|
430
747
|
optionalCostsColumns === null || optionalCostsColumns === void 0 ? void 0 : optionalCostsColumns.push(c);
|
|
431
748
|
continue;
|
|
@@ -457,8 +774,8 @@ function phase2() {
|
|
|
457
774
|
for (let i = 0; i < optionalCostsColumns.length; i++) {
|
|
458
775
|
const c = optionalCostsColumns[i];
|
|
459
776
|
reducedCost = reducedCosts[c];
|
|
460
|
-
unrestricted =
|
|
461
|
-
if (
|
|
777
|
+
unrestricted = unrestrictedVars[varIndexByCol[c]] === true;
|
|
778
|
+
if (negPrecision < reducedCost && reducedCost < precision) {
|
|
462
779
|
optionalCostsColumns2.push(c);
|
|
463
780
|
continue;
|
|
464
781
|
}
|
|
@@ -487,15 +804,14 @@ function phase2() {
|
|
|
487
804
|
}
|
|
488
805
|
let leavingRow = 0;
|
|
489
806
|
let minQuotient = Infinity;
|
|
490
|
-
this.varIndexByRow;
|
|
491
807
|
for (let r = 1; r <= lastRow; r++) {
|
|
492
808
|
const rowOffset = r * width;
|
|
493
809
|
const rhsValue = matrix[rowOffset + rhsColumn];
|
|
494
810
|
const colValue = matrix[rowOffset + enteringColumn];
|
|
495
|
-
if (
|
|
811
|
+
if (negPrecision < colValue && colValue < precision) {
|
|
496
812
|
continue;
|
|
497
813
|
}
|
|
498
|
-
if (colValue > 0 && precision > rhsValue && rhsValue >
|
|
814
|
+
if (colValue > 0 && precision > rhsValue && rhsValue > negPrecision) {
|
|
499
815
|
minQuotient = 0;
|
|
500
816
|
leavingRow = r;
|
|
501
817
|
break;
|
|
@@ -509,15 +825,11 @@ function phase2() {
|
|
|
509
825
|
if (minQuotient === Infinity) {
|
|
510
826
|
this.evaluation = -Infinity;
|
|
511
827
|
this.bounded = false;
|
|
512
|
-
this.unboundedVarIndex =
|
|
828
|
+
this.unboundedVarIndex = varIndexByCol[enteringColumn];
|
|
513
829
|
return iterations;
|
|
514
830
|
}
|
|
515
|
-
if (
|
|
516
|
-
|
|
517
|
-
this.varIndexByRow[leavingRow],
|
|
518
|
-
this.varIndexByCol[enteringColumn],
|
|
519
|
-
]);
|
|
520
|
-
const cycleData = this.checkForCycles(varIndexesCycle);
|
|
831
|
+
if (cycleDetector) {
|
|
832
|
+
const cycleData = cycleDetector.add(varIndexByRow[leavingRow], varIndexByCol[enteringColumn]);
|
|
521
833
|
if (cycleData.length > 0) {
|
|
522
834
|
this.model.messages.push("Cycle in phase 2");
|
|
523
835
|
this.model.messages.push("Start :" + cycleData[0]);
|
|
@@ -530,13 +842,20 @@ function phase2() {
|
|
|
530
842
|
iterations += 1;
|
|
531
843
|
}
|
|
532
844
|
}
|
|
533
|
-
// Pre-allocated
|
|
534
|
-
|
|
845
|
+
// Pre-allocated typed arrays for pivot optimization (better cache performance)
|
|
846
|
+
let nonZeroColumns = new Int32Array(1024);
|
|
847
|
+
let pivotRowCache = new Float64Array(1024);
|
|
535
848
|
function pivot(pivotRowIndex, pivotColumnIndex) {
|
|
536
849
|
const matrix = this.matrix;
|
|
537
850
|
const width = this.width;
|
|
851
|
+
// Ensure work arrays are large enough
|
|
852
|
+
if (width > nonZeroColumns.length) {
|
|
853
|
+
nonZeroColumns = new Int32Array(width * 2);
|
|
854
|
+
pivotRowCache = new Float64Array(width * 2);
|
|
855
|
+
}
|
|
538
856
|
const pivotRowOffset = pivotRowIndex * width;
|
|
539
857
|
const quotient = matrix[pivotRowOffset + pivotColumnIndex];
|
|
858
|
+
const invQuotient = 1 / quotient;
|
|
540
859
|
const height = this.height;
|
|
541
860
|
const leavingBasicIndex = this.varIndexByRow[pivotRowIndex];
|
|
542
861
|
const enteringBasicIndex = this.varIndexByCol[pivotColumnIndex];
|
|
@@ -546,22 +865,24 @@ function pivot(pivotRowIndex, pivotColumnIndex) {
|
|
|
546
865
|
this.rowByVarIndex[leavingBasicIndex] = -1;
|
|
547
866
|
this.colByVarIndex[enteringBasicIndex] = -1;
|
|
548
867
|
this.colByVarIndex[leavingBasicIndex] = pivotColumnIndex;
|
|
549
|
-
// Normalize pivot row
|
|
868
|
+
// Normalize pivot row, track non-zero columns, and cache values for locality
|
|
550
869
|
let nNonZeroColumns = 0;
|
|
551
870
|
for (let c = 0; c < width; c++) {
|
|
552
871
|
const idx = pivotRowOffset + c;
|
|
553
872
|
const val = matrix[idx];
|
|
554
873
|
if (!(val >= -1e-16 && val <= 1e-16)) {
|
|
555
|
-
|
|
874
|
+
const normalized = val / quotient;
|
|
875
|
+
matrix[idx] = normalized;
|
|
556
876
|
nonZeroColumns[nNonZeroColumns] = c;
|
|
877
|
+
pivotRowCache[nNonZeroColumns] = normalized;
|
|
557
878
|
nNonZeroColumns++;
|
|
558
879
|
}
|
|
559
880
|
else {
|
|
560
881
|
matrix[idx] = 0;
|
|
561
882
|
}
|
|
562
883
|
}
|
|
563
|
-
matrix[pivotRowOffset + pivotColumnIndex] =
|
|
564
|
-
// Update all other rows
|
|
884
|
+
matrix[pivotRowOffset + pivotColumnIndex] = invQuotient;
|
|
885
|
+
// Update all other rows using cached pivot row values
|
|
565
886
|
for (let r = 0; r < height; r++) {
|
|
566
887
|
if (r !== pivotRowIndex) {
|
|
567
888
|
const rowOffset = r * width;
|
|
@@ -569,13 +890,16 @@ function pivot(pivotRowIndex, pivotColumnIndex) {
|
|
|
569
890
|
if (!(pivotColVal >= -1e-16 && pivotColVal <= 1e-16)) {
|
|
570
891
|
const coefficient = pivotColVal;
|
|
571
892
|
if (!(coefficient >= -1e-16 && coefficient <= 1e-16)) {
|
|
893
|
+
// Use cached pivot row values for better cache locality
|
|
572
894
|
for (let i = 0; i < nNonZeroColumns; i++) {
|
|
573
895
|
const c = nonZeroColumns[i];
|
|
574
|
-
const v0 =
|
|
896
|
+
const v0 = pivotRowCache[i];
|
|
897
|
+
// Inner zero check is critical for numerical stability
|
|
575
898
|
if (!(v0 >= -1e-16 && v0 <= 1e-16)) {
|
|
576
|
-
matrix[rowOffset + c]
|
|
899
|
+
matrix[rowOffset + c] -= coefficient * v0;
|
|
577
900
|
}
|
|
578
901
|
else if (v0 !== 0) {
|
|
902
|
+
// Clean up near-zero values in pivot row
|
|
579
903
|
matrix[pivotRowOffset + c] = 0;
|
|
580
904
|
}
|
|
581
905
|
}
|
|
@@ -587,7 +911,7 @@ function pivot(pivotRowIndex, pivotColumnIndex) {
|
|
|
587
911
|
}
|
|
588
912
|
}
|
|
589
913
|
}
|
|
590
|
-
// Update optional objectives
|
|
914
|
+
// Update optional objectives using cached pivot row values
|
|
591
915
|
const optionalObjectives = this.optionalObjectives;
|
|
592
916
|
const nOptionalObjectives = optionalObjectives.length;
|
|
593
917
|
if (nOptionalObjectives > 0) {
|
|
@@ -597,12 +921,9 @@ function pivot(pivotRowIndex, pivotColumnIndex) {
|
|
|
597
921
|
if (coefficient !== 0) {
|
|
598
922
|
for (let i = 0; i < nNonZeroColumns; i++) {
|
|
599
923
|
const c = nonZeroColumns[i];
|
|
600
|
-
|
|
601
|
-
if (v0 !== 0) {
|
|
602
|
-
reducedCosts[c] = reducedCosts[c] - coefficient * v0;
|
|
603
|
-
}
|
|
924
|
+
reducedCosts[c] -= coefficient * pivotRowCache[i];
|
|
604
925
|
}
|
|
605
|
-
reducedCosts[pivotColumnIndex] = -coefficient
|
|
926
|
+
reducedCosts[pivotColumnIndex] = -coefficient * invQuotient;
|
|
606
927
|
}
|
|
607
928
|
}
|
|
608
929
|
}
|
|
@@ -634,642 +955,6 @@ function checkForCycles(varIndexes) {
|
|
|
634
955
|
return [];
|
|
635
956
|
}
|
|
636
957
|
|
|
637
|
-
/**
|
|
638
|
-
* @file src/tableau/sparse-matrix.ts
|
|
639
|
-
* @description Compressed Sparse Column (CSC) matrix implementation
|
|
640
|
-
*
|
|
641
|
-
* Optimized for column operations which are frequent in the simplex algorithm.
|
|
642
|
-
* Provides O(nnz) pivot operations instead of O(n*m) for dense matrices.
|
|
643
|
-
*
|
|
644
|
-
* CSC Storage Format:
|
|
645
|
-
* - values: non-zero values stored column by column
|
|
646
|
-
* - rowIndices: row index for each value
|
|
647
|
-
* - colPointers: starting index in values/rowIndices for each column
|
|
648
|
-
*
|
|
649
|
-
* Column c's entries are at indices colPointers[c] to colPointers[c+1]-1
|
|
650
|
-
*/
|
|
651
|
-
class SparseMatrix {
|
|
652
|
-
constructor(nRows, nCols, precision = 1e-12) {
|
|
653
|
-
this.nRows = nRows;
|
|
654
|
-
this.nCols = nCols;
|
|
655
|
-
this.precision = precision;
|
|
656
|
-
// Initialize empty sparse matrix
|
|
657
|
-
this.values = [];
|
|
658
|
-
this.rowIndices = [];
|
|
659
|
-
this.colPointers = new Array(nCols + 1).fill(0);
|
|
660
|
-
// Workspace for row/column operations
|
|
661
|
-
this.workVector = new Float64Array(Math.max(nRows, nCols));
|
|
662
|
-
this.workIndices = [];
|
|
663
|
-
}
|
|
664
|
-
/**
|
|
665
|
-
* Create sparse matrix from dense Float64Array
|
|
666
|
-
*/
|
|
667
|
-
static fromDense(dense, width, height, precision = 1e-12) {
|
|
668
|
-
const sparse = new SparseMatrix(height, width, precision);
|
|
669
|
-
const values = [];
|
|
670
|
-
const rowIndices = [];
|
|
671
|
-
const colPointers = [0];
|
|
672
|
-
// Build CSC by iterating column by column
|
|
673
|
-
for (let c = 0; c < width; c++) {
|
|
674
|
-
for (let r = 0; r < height; r++) {
|
|
675
|
-
const val = dense[r * width + c];
|
|
676
|
-
if (val < -precision || val > precision) {
|
|
677
|
-
values.push(val);
|
|
678
|
-
rowIndices.push(r);
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
colPointers.push(values.length);
|
|
682
|
-
}
|
|
683
|
-
sparse.values = values;
|
|
684
|
-
sparse.rowIndices = rowIndices;
|
|
685
|
-
sparse.colPointers = colPointers;
|
|
686
|
-
return sparse;
|
|
687
|
-
}
|
|
688
|
-
/**
|
|
689
|
-
* Convert to dense Float64Array
|
|
690
|
-
*/
|
|
691
|
-
toDense() {
|
|
692
|
-
const dense = new Float64Array(this.nRows * this.nCols);
|
|
693
|
-
for (let c = 0; c < this.nCols; c++) {
|
|
694
|
-
const start = this.colPointers[c];
|
|
695
|
-
const end = this.colPointers[c + 1];
|
|
696
|
-
for (let i = start; i < end; i++) {
|
|
697
|
-
const r = this.rowIndices[i];
|
|
698
|
-
dense[r * this.nCols + c] = this.values[i];
|
|
699
|
-
}
|
|
700
|
-
}
|
|
701
|
-
return dense;
|
|
702
|
-
}
|
|
703
|
-
/**
|
|
704
|
-
* Get value at (row, col)
|
|
705
|
-
*/
|
|
706
|
-
get(row, col) {
|
|
707
|
-
const start = this.colPointers[col];
|
|
708
|
-
const end = this.colPointers[col + 1];
|
|
709
|
-
// Binary search for row in this column
|
|
710
|
-
let lo = start;
|
|
711
|
-
let hi = end;
|
|
712
|
-
while (lo < hi) {
|
|
713
|
-
const mid = (lo + hi) >>> 1;
|
|
714
|
-
if (this.rowIndices[mid] < row) {
|
|
715
|
-
lo = mid + 1;
|
|
716
|
-
}
|
|
717
|
-
else {
|
|
718
|
-
hi = mid;
|
|
719
|
-
}
|
|
720
|
-
}
|
|
721
|
-
if (lo < end && this.rowIndices[lo] === row) {
|
|
722
|
-
return this.values[lo];
|
|
723
|
-
}
|
|
724
|
-
return 0;
|
|
725
|
-
}
|
|
726
|
-
/**
|
|
727
|
-
* Set value at (row, col)
|
|
728
|
-
* Handles insertion and removal of entries
|
|
729
|
-
*/
|
|
730
|
-
set(row, col, value) {
|
|
731
|
-
const start = this.colPointers[col];
|
|
732
|
-
const end = this.colPointers[col + 1];
|
|
733
|
-
const isZero = value >= -this.precision && value <= this.precision;
|
|
734
|
-
// Binary search for insertion point
|
|
735
|
-
let lo = start;
|
|
736
|
-
let hi = end;
|
|
737
|
-
while (lo < hi) {
|
|
738
|
-
const mid = (lo + hi) >>> 1;
|
|
739
|
-
if (this.rowIndices[mid] < row) {
|
|
740
|
-
lo = mid + 1;
|
|
741
|
-
}
|
|
742
|
-
else {
|
|
743
|
-
hi = mid;
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
const exists = lo < end && this.rowIndices[lo] === row;
|
|
747
|
-
if (exists) {
|
|
748
|
-
if (isZero) {
|
|
749
|
-
// Remove existing entry
|
|
750
|
-
this.values.splice(lo, 1);
|
|
751
|
-
this.rowIndices.splice(lo, 1);
|
|
752
|
-
// Update column pointers
|
|
753
|
-
for (let c = col + 1; c <= this.nCols; c++) {
|
|
754
|
-
this.colPointers[c]--;
|
|
755
|
-
}
|
|
756
|
-
}
|
|
757
|
-
else {
|
|
758
|
-
// Update existing entry
|
|
759
|
-
this.values[lo] = value;
|
|
760
|
-
}
|
|
761
|
-
}
|
|
762
|
-
else if (!isZero) {
|
|
763
|
-
// Insert new entry
|
|
764
|
-
this.values.splice(lo, 0, value);
|
|
765
|
-
this.rowIndices.splice(lo, 0, row);
|
|
766
|
-
// Update column pointers
|
|
767
|
-
for (let c = col + 1; c <= this.nCols; c++) {
|
|
768
|
-
this.colPointers[c]++;
|
|
769
|
-
}
|
|
770
|
-
}
|
|
771
|
-
}
|
|
772
|
-
/**
|
|
773
|
-
* Get column as sparse vector
|
|
774
|
-
*/
|
|
775
|
-
getColumn(col) {
|
|
776
|
-
const start = this.colPointers[col];
|
|
777
|
-
const end = this.colPointers[col + 1];
|
|
778
|
-
const len = end - start;
|
|
779
|
-
return {
|
|
780
|
-
indices: this.rowIndices.slice(start, end),
|
|
781
|
-
values: this.values.slice(start, end),
|
|
782
|
-
length: len,
|
|
783
|
-
};
|
|
784
|
-
}
|
|
785
|
-
/**
|
|
786
|
-
* Get row as sparse vector (less efficient - requires full scan)
|
|
787
|
-
*/
|
|
788
|
-
getRow(row) {
|
|
789
|
-
const indices = [];
|
|
790
|
-
const values = [];
|
|
791
|
-
for (let c = 0; c < this.nCols; c++) {
|
|
792
|
-
const val = this.get(row, c);
|
|
793
|
-
if (val !== 0) {
|
|
794
|
-
indices.push(c);
|
|
795
|
-
values.push(val);
|
|
796
|
-
}
|
|
797
|
-
}
|
|
798
|
-
return { indices, values, length: indices.length };
|
|
799
|
-
}
|
|
800
|
-
/**
|
|
801
|
-
* Get row into pre-allocated dense array (more efficient for repeated access)
|
|
802
|
-
* Returns array of non-zero column indices
|
|
803
|
-
*/
|
|
804
|
-
getRowDense(row, output) {
|
|
805
|
-
const nonZeroCols = [];
|
|
806
|
-
// Clear relevant portion
|
|
807
|
-
output.fill(0, 0, this.nCols);
|
|
808
|
-
for (let c = 0; c < this.nCols; c++) {
|
|
809
|
-
const start = this.colPointers[c];
|
|
810
|
-
const end = this.colPointers[c + 1];
|
|
811
|
-
// Binary search for row
|
|
812
|
-
let lo = start;
|
|
813
|
-
let hi = end;
|
|
814
|
-
while (lo < hi) {
|
|
815
|
-
const mid = (lo + hi) >>> 1;
|
|
816
|
-
if (this.rowIndices[mid] < row) {
|
|
817
|
-
lo = mid + 1;
|
|
818
|
-
}
|
|
819
|
-
else {
|
|
820
|
-
hi = mid;
|
|
821
|
-
}
|
|
822
|
-
}
|
|
823
|
-
if (lo < end && this.rowIndices[lo] === row) {
|
|
824
|
-
output[c] = this.values[lo];
|
|
825
|
-
nonZeroCols.push(c);
|
|
826
|
-
}
|
|
827
|
-
}
|
|
828
|
-
return nonZeroCols;
|
|
829
|
-
}
|
|
830
|
-
/**
|
|
831
|
-
* Add a new row (expands matrix vertically)
|
|
832
|
-
*/
|
|
833
|
-
addRow() {
|
|
834
|
-
this.nRows++;
|
|
835
|
-
if (this.workVector.length < this.nRows) {
|
|
836
|
-
this.workVector = new Float64Array(this.nRows * 2);
|
|
837
|
-
}
|
|
838
|
-
}
|
|
839
|
-
/**
|
|
840
|
-
* Add a new column (expands matrix horizontally)
|
|
841
|
-
*/
|
|
842
|
-
addColumn() {
|
|
843
|
-
this.colPointers.push(this.colPointers[this.nCols]);
|
|
844
|
-
this.nCols++;
|
|
845
|
-
if (this.workVector.length < this.nCols) {
|
|
846
|
-
this.workVector = new Float64Array(this.nCols * 2);
|
|
847
|
-
}
|
|
848
|
-
}
|
|
849
|
-
/**
|
|
850
|
-
* Set entire column from sparse vector
|
|
851
|
-
*/
|
|
852
|
-
setColumn(col, sparseCol) {
|
|
853
|
-
const oldStart = this.colPointers[col];
|
|
854
|
-
const oldEnd = this.colPointers[col + 1];
|
|
855
|
-
const oldLen = oldEnd - oldStart;
|
|
856
|
-
const newLen = sparseCol.length;
|
|
857
|
-
const diff = newLen - oldLen;
|
|
858
|
-
if (diff !== 0) {
|
|
859
|
-
// Need to resize
|
|
860
|
-
if (diff > 0) {
|
|
861
|
-
// Make room
|
|
862
|
-
for (let i = 0; i < diff; i++) {
|
|
863
|
-
this.values.splice(oldEnd, 0, 0);
|
|
864
|
-
this.rowIndices.splice(oldEnd, 0, 0);
|
|
865
|
-
}
|
|
866
|
-
}
|
|
867
|
-
else {
|
|
868
|
-
// Remove excess
|
|
869
|
-
this.values.splice(oldStart, -diff);
|
|
870
|
-
this.rowIndices.splice(oldStart, -diff);
|
|
871
|
-
}
|
|
872
|
-
// Update column pointers
|
|
873
|
-
for (let c = col + 1; c <= this.nCols; c++) {
|
|
874
|
-
this.colPointers[c] += diff;
|
|
875
|
-
}
|
|
876
|
-
}
|
|
877
|
-
// Copy new values
|
|
878
|
-
const start = this.colPointers[col];
|
|
879
|
-
for (let i = 0; i < newLen; i++) {
|
|
880
|
-
this.values[start + i] = sparseCol.values[i];
|
|
881
|
-
this.rowIndices[start + i] = sparseCol.indices[i];
|
|
882
|
-
}
|
|
883
|
-
}
|
|
884
|
-
/**
|
|
885
|
-
* Number of non-zero entries
|
|
886
|
-
*/
|
|
887
|
-
get nnz() {
|
|
888
|
-
return this.values.length;
|
|
889
|
-
}
|
|
890
|
-
/**
|
|
891
|
-
* Density (fraction of non-zeros)
|
|
892
|
-
*/
|
|
893
|
-
get density() {
|
|
894
|
-
return this.nnz / (this.nRows * this.nCols);
|
|
895
|
-
}
|
|
896
|
-
/**
|
|
897
|
-
* Scale a row by a factor
|
|
898
|
-
*/
|
|
899
|
-
scaleRow(row, factor) {
|
|
900
|
-
for (let c = 0; c < this.nCols; c++) {
|
|
901
|
-
const start = this.colPointers[c];
|
|
902
|
-
const end = this.colPointers[c + 1];
|
|
903
|
-
for (let i = start; i < end; i++) {
|
|
904
|
-
if (this.rowIndices[i] === row) {
|
|
905
|
-
this.values[i] *= factor;
|
|
906
|
-
break;
|
|
907
|
-
}
|
|
908
|
-
}
|
|
909
|
-
}
|
|
910
|
-
}
|
|
911
|
-
/**
|
|
912
|
-
* Add scaled row to another row: targetRow += factor * sourceRow
|
|
913
|
-
* This is the core operation in Gaussian elimination / pivot
|
|
914
|
-
*/
|
|
915
|
-
addScaledRow(targetRow, sourceRow, factor) {
|
|
916
|
-
if (factor >= -this.precision && factor <= this.precision) {
|
|
917
|
-
return;
|
|
918
|
-
}
|
|
919
|
-
// Get source row as dense for efficiency
|
|
920
|
-
const sourceNonZeros = this.getRowDense(sourceRow, this.workVector);
|
|
921
|
-
// Add scaled source to target
|
|
922
|
-
for (const c of sourceNonZeros) {
|
|
923
|
-
const sourceVal = this.workVector[c];
|
|
924
|
-
const newVal = this.get(targetRow, c) + factor * sourceVal;
|
|
925
|
-
this.set(targetRow, c, newVal);
|
|
926
|
-
}
|
|
927
|
-
}
|
|
928
|
-
/**
|
|
929
|
-
* Perform pivot operation at (pivotRow, pivotCol)
|
|
930
|
-
* This is optimized for sparse operations
|
|
931
|
-
*/
|
|
932
|
-
pivot(pivotRow, pivotCol) {
|
|
933
|
-
const pivotVal = this.get(pivotRow, pivotCol);
|
|
934
|
-
if (pivotVal === 0) {
|
|
935
|
-
throw new Error("Cannot pivot on zero element");
|
|
936
|
-
}
|
|
937
|
-
const invPivot = 1 / pivotVal;
|
|
938
|
-
// Get pivot row as dense
|
|
939
|
-
const pivotRowNonZeros = this.getRowDense(pivotRow, this.workVector);
|
|
940
|
-
// Scale pivot row
|
|
941
|
-
for (const c of pivotRowNonZeros) {
|
|
942
|
-
this.workVector[c] *= invPivot;
|
|
943
|
-
}
|
|
944
|
-
this.workVector[pivotCol] = invPivot;
|
|
945
|
-
// Update pivot row in matrix
|
|
946
|
-
for (const c of pivotRowNonZeros) {
|
|
947
|
-
this.set(pivotRow, c, this.workVector[c]);
|
|
948
|
-
}
|
|
949
|
-
this.set(pivotRow, pivotCol, invPivot);
|
|
950
|
-
// Get pivot column to find rows that need updating
|
|
951
|
-
const pivotColData = this.getColumn(pivotCol);
|
|
952
|
-
// Update all other rows
|
|
953
|
-
for (let i = 0; i < pivotColData.length; i++) {
|
|
954
|
-
const r = pivotColData.indices[i];
|
|
955
|
-
if (r === pivotRow)
|
|
956
|
-
continue;
|
|
957
|
-
const coefficient = pivotColData.values[i];
|
|
958
|
-
if (coefficient >= -this.precision && coefficient <= this.precision)
|
|
959
|
-
continue;
|
|
960
|
-
// row[r] -= coefficient * pivotRow
|
|
961
|
-
for (const c of pivotRowNonZeros) {
|
|
962
|
-
if (c === pivotCol)
|
|
963
|
-
continue;
|
|
964
|
-
const pivotRowVal = this.workVector[c];
|
|
965
|
-
if (pivotRowVal >= -this.precision && pivotRowVal <= this.precision)
|
|
966
|
-
continue;
|
|
967
|
-
const newVal = this.get(r, c) - coefficient * pivotRowVal;
|
|
968
|
-
this.set(r, c, newVal);
|
|
969
|
-
}
|
|
970
|
-
// Set pivot column value for this row
|
|
971
|
-
this.set(r, pivotCol, -coefficient * invPivot);
|
|
972
|
-
}
|
|
973
|
-
return pivotRowNonZeros;
|
|
974
|
-
}
|
|
975
|
-
/**
|
|
976
|
-
* Clone the matrix
|
|
977
|
-
*/
|
|
978
|
-
clone() {
|
|
979
|
-
const copy = new SparseMatrix(this.nRows, this.nCols, this.precision);
|
|
980
|
-
copy.values = this.values.slice();
|
|
981
|
-
copy.rowIndices = this.rowIndices.slice();
|
|
982
|
-
copy.colPointers = this.colPointers.slice();
|
|
983
|
-
return copy;
|
|
984
|
-
}
|
|
985
|
-
}
|
|
986
|
-
|
|
987
|
-
/**
|
|
988
|
-
* @file src/tableau/sparse-simplex.ts
|
|
989
|
-
* @description Sparse simplex algorithm for large LP problems
|
|
990
|
-
*
|
|
991
|
-
* Optimized simplex implementation that operates directly on SparseMatrix
|
|
992
|
-
* storage. Converts the dense tableau to CSC format, solves, and converts
|
|
993
|
-
* back for solution extraction.
|
|
994
|
-
*
|
|
995
|
-
* Provides significant speedup for large, sparse problems where most
|
|
996
|
-
* coefficients are zero.
|
|
997
|
-
*/
|
|
998
|
-
/**
|
|
999
|
-
* Convert dense tableau to sparse and solve
|
|
1000
|
-
*/
|
|
1001
|
-
function sparseSimplex() {
|
|
1002
|
-
// Convert to sparse
|
|
1003
|
-
const sparse = SparseMatrix.fromDense(this.matrix, this.width, this.height, this.precision);
|
|
1004
|
-
this.bounded = true;
|
|
1005
|
-
// Run sparse phase 1
|
|
1006
|
-
sparsePhase1.call(this, sparse);
|
|
1007
|
-
if (this.feasible === true) {
|
|
1008
|
-
// Run sparse phase 2
|
|
1009
|
-
sparsePhase2.call(this, sparse);
|
|
1010
|
-
}
|
|
1011
|
-
// Copy results back to dense matrix (needed for solution extraction)
|
|
1012
|
-
const dense = sparse.toDense();
|
|
1013
|
-
this.matrix.set(dense);
|
|
1014
|
-
return this;
|
|
1015
|
-
}
|
|
1016
|
-
/**
|
|
1017
|
-
* Sparse Phase 1: Find a basic feasible solution
|
|
1018
|
-
*/
|
|
1019
|
-
function sparsePhase1(sparse) {
|
|
1020
|
-
const debugCheckForCycles = this.model.checkForCycles;
|
|
1021
|
-
const varIndexesCycle = [];
|
|
1022
|
-
const rhsColumn = this.rhsColumn;
|
|
1023
|
-
const lastColumn = this.width - 1;
|
|
1024
|
-
const lastRow = this.height - 1;
|
|
1025
|
-
const precision = this.precision;
|
|
1026
|
-
let unrestricted;
|
|
1027
|
-
let iterations = 0;
|
|
1028
|
-
while (true) {
|
|
1029
|
-
// Find leaving row (most negative RHS)
|
|
1030
|
-
let leavingRowIndex = 0;
|
|
1031
|
-
let rhsValue = -precision;
|
|
1032
|
-
for (let r = 1; r <= lastRow; r++) {
|
|
1033
|
-
const value = sparse.get(r, rhsColumn);
|
|
1034
|
-
if (value < rhsValue) {
|
|
1035
|
-
rhsValue = value;
|
|
1036
|
-
leavingRowIndex = r;
|
|
1037
|
-
}
|
|
1038
|
-
}
|
|
1039
|
-
if (leavingRowIndex === 0) {
|
|
1040
|
-
this.feasible = true;
|
|
1041
|
-
return iterations;
|
|
1042
|
-
}
|
|
1043
|
-
// Find entering column
|
|
1044
|
-
let enteringColumn = 0;
|
|
1045
|
-
let maxQuotient = -Infinity;
|
|
1046
|
-
// Get leaving row as sparse
|
|
1047
|
-
const leavingRowData = sparse.getRow(leavingRowIndex);
|
|
1048
|
-
for (let i = 0; i < leavingRowData.length; i++) {
|
|
1049
|
-
const c = leavingRowData.indices[i];
|
|
1050
|
-
if (c === 0 || c === rhsColumn)
|
|
1051
|
-
continue;
|
|
1052
|
-
const coefficient = leavingRowData.values[i];
|
|
1053
|
-
unrestricted = this.unrestrictedVars[this.varIndexByCol[c]] === true;
|
|
1054
|
-
if (unrestricted || coefficient < -precision) {
|
|
1055
|
-
const costValue = sparse.get(0, c);
|
|
1056
|
-
const quotient = -costValue / coefficient;
|
|
1057
|
-
if (maxQuotient < quotient) {
|
|
1058
|
-
maxQuotient = quotient;
|
|
1059
|
-
enteringColumn = c;
|
|
1060
|
-
}
|
|
1061
|
-
}
|
|
1062
|
-
}
|
|
1063
|
-
// Also check columns with zero in leaving row but non-zero cost
|
|
1064
|
-
for (let c = 1; c <= lastColumn; c++) {
|
|
1065
|
-
if (c === rhsColumn)
|
|
1066
|
-
continue;
|
|
1067
|
-
const coefficient = sparse.get(leavingRowIndex, c);
|
|
1068
|
-
unrestricted = this.unrestrictedVars[this.varIndexByCol[c]] === true;
|
|
1069
|
-
if (unrestricted || coefficient < -precision) {
|
|
1070
|
-
const costValue = sparse.get(0, c);
|
|
1071
|
-
const quotient = -costValue / coefficient;
|
|
1072
|
-
if (maxQuotient < quotient) {
|
|
1073
|
-
maxQuotient = quotient;
|
|
1074
|
-
enteringColumn = c;
|
|
1075
|
-
}
|
|
1076
|
-
}
|
|
1077
|
-
}
|
|
1078
|
-
if (enteringColumn === 0) {
|
|
1079
|
-
this.feasible = false;
|
|
1080
|
-
return iterations;
|
|
1081
|
-
}
|
|
1082
|
-
if (debugCheckForCycles) {
|
|
1083
|
-
varIndexesCycle.push([
|
|
1084
|
-
this.varIndexByRow[leavingRowIndex],
|
|
1085
|
-
this.varIndexByCol[enteringColumn],
|
|
1086
|
-
]);
|
|
1087
|
-
const cycleData = this.checkForCycles(varIndexesCycle);
|
|
1088
|
-
if (cycleData.length > 0) {
|
|
1089
|
-
this.model.messages.push("Cycle in phase 1");
|
|
1090
|
-
this.feasible = false;
|
|
1091
|
-
return iterations;
|
|
1092
|
-
}
|
|
1093
|
-
}
|
|
1094
|
-
// Perform pivot
|
|
1095
|
-
sparsePivot.call(this, sparse, leavingRowIndex, enteringColumn);
|
|
1096
|
-
iterations += 1;
|
|
1097
|
-
}
|
|
1098
|
-
}
|
|
1099
|
-
/**
|
|
1100
|
-
* Sparse Phase 2: Optimize the objective
|
|
1101
|
-
*/
|
|
1102
|
-
function sparsePhase2(sparse) {
|
|
1103
|
-
const debugCheckForCycles = this.model.checkForCycles;
|
|
1104
|
-
const varIndexesCycle = [];
|
|
1105
|
-
const rhsColumn = this.rhsColumn;
|
|
1106
|
-
const lastColumn = this.width - 1;
|
|
1107
|
-
this.height - 1;
|
|
1108
|
-
const precision = this.precision;
|
|
1109
|
-
this.optionalObjectives.length;
|
|
1110
|
-
let iterations = 0;
|
|
1111
|
-
let reducedCost;
|
|
1112
|
-
let unrestricted;
|
|
1113
|
-
// Partial pricing for sparse
|
|
1114
|
-
const batchSize = Math.min(500, Math.max(50, Math.floor(Math.sqrt(lastColumn))));
|
|
1115
|
-
let batchStart = 1;
|
|
1116
|
-
while (true) {
|
|
1117
|
-
const costRow = 0;
|
|
1118
|
-
let enteringColumn = 0;
|
|
1119
|
-
let enteringValue = precision;
|
|
1120
|
-
let isReducedCostNegative = false;
|
|
1121
|
-
// Partial pricing: scan columns in batches
|
|
1122
|
-
let batchesScanned = 0;
|
|
1123
|
-
const totalBatches = Math.ceil(lastColumn / batchSize);
|
|
1124
|
-
while (enteringColumn === 0 && batchesScanned < totalBatches) {
|
|
1125
|
-
const batchEnd = Math.min(batchStart + batchSize - 1, lastColumn);
|
|
1126
|
-
for (let c = batchStart; c <= batchEnd; c++) {
|
|
1127
|
-
if (c === rhsColumn)
|
|
1128
|
-
continue;
|
|
1129
|
-
reducedCost = sparse.get(costRow, c);
|
|
1130
|
-
unrestricted = this.unrestrictedVars[this.varIndexByCol[c]] === true;
|
|
1131
|
-
if (unrestricted && reducedCost < 0) {
|
|
1132
|
-
if (-reducedCost > enteringValue) {
|
|
1133
|
-
enteringValue = -reducedCost;
|
|
1134
|
-
enteringColumn = c;
|
|
1135
|
-
isReducedCostNegative = true;
|
|
1136
|
-
}
|
|
1137
|
-
continue;
|
|
1138
|
-
}
|
|
1139
|
-
if (reducedCost > enteringValue) {
|
|
1140
|
-
enteringValue = reducedCost;
|
|
1141
|
-
enteringColumn = c;
|
|
1142
|
-
isReducedCostNegative = false;
|
|
1143
|
-
}
|
|
1144
|
-
}
|
|
1145
|
-
batchStart = batchEnd >= lastColumn ? 1 : batchEnd + 1;
|
|
1146
|
-
batchesScanned++;
|
|
1147
|
-
}
|
|
1148
|
-
if (enteringColumn === 0) {
|
|
1149
|
-
this.setEvaluation();
|
|
1150
|
-
this.simplexIters += 1;
|
|
1151
|
-
return iterations;
|
|
1152
|
-
}
|
|
1153
|
-
// Find leaving row using ratio test
|
|
1154
|
-
let leavingRow = 0;
|
|
1155
|
-
let minQuotient = Infinity;
|
|
1156
|
-
// Get entering column as sparse
|
|
1157
|
-
const enteringColData = sparse.getColumn(enteringColumn);
|
|
1158
|
-
for (let i = 0; i < enteringColData.length; i++) {
|
|
1159
|
-
const r = enteringColData.indices[i];
|
|
1160
|
-
if (r === 0)
|
|
1161
|
-
continue; // Skip cost row
|
|
1162
|
-
const colValue = enteringColData.values[i];
|
|
1163
|
-
if (colValue >= -precision && colValue <= precision)
|
|
1164
|
-
continue;
|
|
1165
|
-
const rhsValue = sparse.get(r, rhsColumn);
|
|
1166
|
-
if (colValue > 0 && precision > rhsValue && rhsValue > -precision) {
|
|
1167
|
-
minQuotient = 0;
|
|
1168
|
-
leavingRow = r;
|
|
1169
|
-
break;
|
|
1170
|
-
}
|
|
1171
|
-
const quotient = isReducedCostNegative ? -rhsValue / colValue : rhsValue / colValue;
|
|
1172
|
-
if (quotient > precision && minQuotient > quotient) {
|
|
1173
|
-
minQuotient = quotient;
|
|
1174
|
-
leavingRow = r;
|
|
1175
|
-
}
|
|
1176
|
-
}
|
|
1177
|
-
if (minQuotient === Infinity) {
|
|
1178
|
-
this.evaluation = -Infinity;
|
|
1179
|
-
this.bounded = false;
|
|
1180
|
-
this.unboundedVarIndex = this.varIndexByCol[enteringColumn];
|
|
1181
|
-
return iterations;
|
|
1182
|
-
}
|
|
1183
|
-
if (debugCheckForCycles) {
|
|
1184
|
-
varIndexesCycle.push([
|
|
1185
|
-
this.varIndexByRow[leavingRow],
|
|
1186
|
-
this.varIndexByCol[enteringColumn],
|
|
1187
|
-
]);
|
|
1188
|
-
const cycleData = this.checkForCycles(varIndexesCycle);
|
|
1189
|
-
if (cycleData.length > 0) {
|
|
1190
|
-
this.model.messages.push("Cycle in phase 2");
|
|
1191
|
-
this.feasible = false;
|
|
1192
|
-
return iterations;
|
|
1193
|
-
}
|
|
1194
|
-
}
|
|
1195
|
-
// Perform pivot
|
|
1196
|
-
sparsePivot.call(this, sparse, leavingRow, enteringColumn);
|
|
1197
|
-
iterations += 1;
|
|
1198
|
-
}
|
|
1199
|
-
}
|
|
1200
|
-
/**
|
|
1201
|
-
* Sparse pivot operation
|
|
1202
|
-
*/
|
|
1203
|
-
function sparsePivot(sparse, pivotRowIndex, pivotColumnIndex) {
|
|
1204
|
-
// Update basis tracking
|
|
1205
|
-
const leavingBasicIndex = this.varIndexByRow[pivotRowIndex];
|
|
1206
|
-
const enteringBasicIndex = this.varIndexByCol[pivotColumnIndex];
|
|
1207
|
-
this.varIndexByRow[pivotRowIndex] = enteringBasicIndex;
|
|
1208
|
-
this.varIndexByCol[pivotColumnIndex] = leavingBasicIndex;
|
|
1209
|
-
this.rowByVarIndex[enteringBasicIndex] = pivotRowIndex;
|
|
1210
|
-
this.rowByVarIndex[leavingBasicIndex] = -1;
|
|
1211
|
-
this.colByVarIndex[enteringBasicIndex] = -1;
|
|
1212
|
-
this.colByVarIndex[leavingBasicIndex] = pivotColumnIndex;
|
|
1213
|
-
// Perform sparse pivot
|
|
1214
|
-
sparse.pivot(pivotRowIndex, pivotColumnIndex);
|
|
1215
|
-
// Handle optional objectives (these are stored separately, not in sparse matrix)
|
|
1216
|
-
const optionalObjectives = this.optionalObjectives;
|
|
1217
|
-
const nOptionalObjectives = optionalObjectives.length;
|
|
1218
|
-
if (nOptionalObjectives > 0) {
|
|
1219
|
-
const pivotVal = sparse.get(pivotRowIndex, pivotColumnIndex);
|
|
1220
|
-
const pivotRowData = sparse.getRow(pivotRowIndex);
|
|
1221
|
-
for (let o = 0; o < nOptionalObjectives; o++) {
|
|
1222
|
-
const reducedCosts = optionalObjectives[o].reducedCosts;
|
|
1223
|
-
const coefficient = reducedCosts[pivotColumnIndex];
|
|
1224
|
-
if (coefficient !== 0) {
|
|
1225
|
-
for (let i = 0; i < pivotRowData.length; i++) {
|
|
1226
|
-
const c = pivotRowData.indices[i];
|
|
1227
|
-
const v0 = pivotRowData.values[i];
|
|
1228
|
-
if (v0 !== 0) {
|
|
1229
|
-
reducedCosts[c] = reducedCosts[c] - coefficient * v0;
|
|
1230
|
-
}
|
|
1231
|
-
}
|
|
1232
|
-
reducedCosts[pivotColumnIndex] = -coefficient * pivotVal;
|
|
1233
|
-
}
|
|
1234
|
-
}
|
|
1235
|
-
}
|
|
1236
|
-
}
|
|
1237
|
-
/**
|
|
1238
|
-
* Threshold for using sparse (total cells)
|
|
1239
|
-
* Currently disabled - CSC format overhead exceeds benefits.
|
|
1240
|
-
* A dual-indexed (CSC+CSR) approach would be needed for efficient sparse simplex.
|
|
1241
|
-
*/
|
|
1242
|
-
const SPARSE_THRESHOLD = Infinity; // Disabled
|
|
1243
|
-
/**
|
|
1244
|
-
* Density threshold - only use sparse if below this density
|
|
1245
|
-
*/
|
|
1246
|
-
const DENSITY_THRESHOLD = 0.1; // 10%
|
|
1247
|
-
/**
|
|
1248
|
-
* Check if sparse mode should be used for this tableau
|
|
1249
|
-
*/
|
|
1250
|
-
function shouldUseSparse(tableau) {
|
|
1251
|
-
const totalCells = tableau.width * tableau.height;
|
|
1252
|
-
if (totalCells < SPARSE_THRESHOLD) {
|
|
1253
|
-
return false;
|
|
1254
|
-
}
|
|
1255
|
-
// Estimate density from constraint rows (skip row 0 which is cost row and often dense)
|
|
1256
|
-
const matrix = tableau.matrix;
|
|
1257
|
-
const width = tableau.width;
|
|
1258
|
-
const startRow = 1;
|
|
1259
|
-
const sampleRows = Math.min(20, tableau.height - 1);
|
|
1260
|
-
let nonZeros = 0;
|
|
1261
|
-
for (let r = startRow; r < startRow + sampleRows; r++) {
|
|
1262
|
-
const rowOffset = r * width;
|
|
1263
|
-
for (let c = 0; c < width; c++) {
|
|
1264
|
-
if (matrix[rowOffset + c] !== 0) {
|
|
1265
|
-
nonZeros++;
|
|
1266
|
-
}
|
|
1267
|
-
}
|
|
1268
|
-
}
|
|
1269
|
-
const estimatedDensity = nonZeros / (sampleRows * width);
|
|
1270
|
-
return estimatedDensity < DENSITY_THRESHOLD;
|
|
1271
|
-
}
|
|
1272
|
-
|
|
1273
958
|
class Variable {
|
|
1274
959
|
constructor(id, cost, index, priority) {
|
|
1275
960
|
this.id = id;
|
|
@@ -1354,6 +1039,7 @@ class Constraint {
|
|
|
1354
1039
|
setVariableCoefficient(newCoefficient, variable) {
|
|
1355
1040
|
const varIndex = variable.index;
|
|
1356
1041
|
if (varIndex === -1) {
|
|
1042
|
+
// eslint-disable-next-line no-console
|
|
1357
1043
|
console.warn("[Constraint.setVariableCoefficient] Trying to change coefficient of inexistant variable.");
|
|
1358
1044
|
return;
|
|
1359
1045
|
}
|
|
@@ -1436,44 +1122,52 @@ function addCutConstraints(cutConstraints) {
|
|
|
1436
1122
|
const heightWithCuts = height + nCutConstraints;
|
|
1437
1123
|
const width = this.width;
|
|
1438
1124
|
const lastColumn = width - 1;
|
|
1439
|
-
// Grow the matrix to accommodate new rows
|
|
1125
|
+
// Grow the matrix to accommodate new rows (with over-allocation to reduce reallocation frequency)
|
|
1440
1126
|
const oldMatrix = this.matrix;
|
|
1441
1127
|
const newSize = heightWithCuts * width;
|
|
1442
1128
|
if (oldMatrix.length < newSize) {
|
|
1443
|
-
|
|
1129
|
+
// Over-allocate by 50% to reduce future reallocations
|
|
1130
|
+
const allocSize = Math.ceil(newSize * 1.5);
|
|
1131
|
+
const newMatrix = new Float64Array(allocSize);
|
|
1444
1132
|
newMatrix.set(oldMatrix);
|
|
1445
1133
|
this.matrix = newMatrix;
|
|
1446
1134
|
}
|
|
1447
1135
|
const matrix = this.matrix;
|
|
1448
1136
|
this.height = heightWithCuts;
|
|
1449
1137
|
this.nVars = this.width + this.height - 2;
|
|
1138
|
+
// Cache array references for faster access in loop
|
|
1139
|
+
const rhsColumn = this.rhsColumn;
|
|
1140
|
+
const rowByVarIndex = this.rowByVarIndex;
|
|
1141
|
+
const colByVarIndex = this.colByVarIndex;
|
|
1142
|
+
const varIndexByRow = this.varIndexByRow;
|
|
1143
|
+
const variablesPerIndex = this.variablesPerIndex;
|
|
1450
1144
|
for (let h = 0; h < nCutConstraints; h += 1) {
|
|
1451
1145
|
const cut = cutConstraints[h];
|
|
1452
1146
|
const cutRow = height + h;
|
|
1453
1147
|
const cutRowOffset = cutRow * width;
|
|
1454
1148
|
const sign = cut.type === "min" ? -1 : 1;
|
|
1455
1149
|
const varIndex = cut.varIndex;
|
|
1456
|
-
let varRowIndex =
|
|
1150
|
+
let varRowIndex = rowByVarIndex[varIndex];
|
|
1457
1151
|
if (varRowIndex === -1) {
|
|
1458
|
-
matrix[cutRowOffset +
|
|
1152
|
+
matrix[cutRowOffset + rhsColumn] = sign * cut.value;
|
|
1459
1153
|
for (let c = 1; c <= lastColumn; c += 1) {
|
|
1460
1154
|
matrix[cutRowOffset + c] = 0;
|
|
1461
1155
|
}
|
|
1462
|
-
matrix[cutRowOffset +
|
|
1156
|
+
matrix[cutRowOffset + colByVarIndex[varIndex]] = sign;
|
|
1463
1157
|
}
|
|
1464
1158
|
else {
|
|
1465
1159
|
const varRowOffset = varRowIndex * width;
|
|
1466
|
-
const varValue = matrix[varRowOffset +
|
|
1467
|
-
matrix[cutRowOffset +
|
|
1160
|
+
const varValue = matrix[varRowOffset + rhsColumn];
|
|
1161
|
+
matrix[cutRowOffset + rhsColumn] = sign * (cut.value - varValue);
|
|
1468
1162
|
for (let c = 1; c <= lastColumn; c += 1) {
|
|
1469
1163
|
matrix[cutRowOffset + c] = -sign * matrix[varRowOffset + c];
|
|
1470
1164
|
}
|
|
1471
1165
|
}
|
|
1472
1166
|
varRowIndex = this.getNewElementIndex();
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1167
|
+
varIndexByRow[cutRow] = varRowIndex;
|
|
1168
|
+
rowByVarIndex[varRowIndex] = cutRow;
|
|
1169
|
+
colByVarIndex[varRowIndex] = -1;
|
|
1170
|
+
variablesPerIndex[varRowIndex] = new SlackVariable("s" + varRowIndex, varRowIndex);
|
|
1477
1171
|
this.nVars += 1;
|
|
1478
1172
|
}
|
|
1479
1173
|
}
|
|
@@ -1495,10 +1189,12 @@ function addLowerBoundMIRCut(rowIndex) {
|
|
|
1495
1189
|
}
|
|
1496
1190
|
const height = this.height;
|
|
1497
1191
|
const newRowOffset = height * width;
|
|
1498
|
-
// Grow matrix to add new row
|
|
1192
|
+
// Grow matrix to add new row (with over-allocation to reduce reallocation frequency)
|
|
1499
1193
|
const newSize = (height + 1) * width;
|
|
1500
1194
|
if (matrix.length < newSize) {
|
|
1501
|
-
|
|
1195
|
+
// Over-allocate by 50% to reduce future reallocations
|
|
1196
|
+
const allocSize = Math.ceil(newSize * 1.5);
|
|
1197
|
+
const newMatrix = new Float64Array(allocSize);
|
|
1502
1198
|
newMatrix.set(matrix);
|
|
1503
1199
|
this.matrix = newMatrix;
|
|
1504
1200
|
}
|
|
@@ -1510,18 +1206,23 @@ function addLowerBoundMIRCut(rowIndex) {
|
|
|
1510
1206
|
this.rowByVarIndex[slackVarIndex] = height;
|
|
1511
1207
|
this.colByVarIndex[slackVarIndex] = -1;
|
|
1512
1208
|
this.variablesPerIndex[slackVarIndex] = new SlackVariable("s" + slackVarIndex, slackVarIndex);
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1209
|
+
const rhsColumn = this.rhsColumn;
|
|
1210
|
+
mat[newRowOffset + rhsColumn] = Math.floor(rhsValue);
|
|
1211
|
+
// Cache array references for faster access in hot loop
|
|
1212
|
+
const variablesPerIndex = this.variablesPerIndex;
|
|
1213
|
+
const varIndexByCol = this.varIndexByCol;
|
|
1214
|
+
const varIndexByColLen = varIndexByCol.length;
|
|
1215
|
+
const oneMinusFrac = 1 - fractionalPart;
|
|
1216
|
+
for (let colIndex = 1; colIndex < varIndexByColLen; colIndex += 1) {
|
|
1217
|
+
const variable = variablesPerIndex[varIndexByCol[colIndex]];
|
|
1516
1218
|
const coefficient = mat[cutRowOffset + colIndex];
|
|
1517
1219
|
if (variable !== undefined && variable.isInteger) {
|
|
1518
|
-
const
|
|
1519
|
-
|
|
1520
|
-
(1 - fractionalPart);
|
|
1220
|
+
const floorCoeff = Math.floor(coefficient);
|
|
1221
|
+
const termCoeff = floorCoeff + Math.max(0, coefficient - floorCoeff - fractionalPart) / oneMinusFrac;
|
|
1521
1222
|
mat[newRowOffset + colIndex] = termCoeff;
|
|
1522
1223
|
}
|
|
1523
1224
|
else {
|
|
1524
|
-
mat[newRowOffset + colIndex] = Math.min(0, coefficient /
|
|
1225
|
+
mat[newRowOffset + colIndex] = Math.min(0, coefficient / oneMinusFrac);
|
|
1525
1226
|
}
|
|
1526
1227
|
}
|
|
1527
1228
|
for (let c = 0; c < width; c += 1) {
|
|
@@ -1547,10 +1248,12 @@ function addUpperBoundMIRCut(rowIndex) {
|
|
|
1547
1248
|
}
|
|
1548
1249
|
const height = this.height;
|
|
1549
1250
|
const newRowOffset = height * width;
|
|
1550
|
-
// Grow matrix to add new row
|
|
1251
|
+
// Grow matrix to add new row (with over-allocation to reduce reallocation frequency)
|
|
1551
1252
|
const newSize = (height + 1) * width;
|
|
1552
1253
|
if (matrix.length < newSize) {
|
|
1553
|
-
|
|
1254
|
+
// Over-allocate by 50% to reduce future reallocations
|
|
1255
|
+
const allocSize = Math.ceil(newSize * 1.5);
|
|
1256
|
+
const newMatrix = new Float64Array(allocSize);
|
|
1554
1257
|
newMatrix.set(matrix);
|
|
1555
1258
|
this.matrix = newMatrix;
|
|
1556
1259
|
}
|
|
@@ -1562,9 +1265,15 @@ function addUpperBoundMIRCut(rowIndex) {
|
|
|
1562
1265
|
this.rowByVarIndex[slackVarIndex] = height;
|
|
1563
1266
|
this.colByVarIndex[slackVarIndex] = -1;
|
|
1564
1267
|
this.variablesPerIndex[slackVarIndex] = new SlackVariable("s" + slackVarIndex, slackVarIndex);
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1268
|
+
const rhsColumn = this.rhsColumn;
|
|
1269
|
+
mat[newRowOffset + rhsColumn] = -fractionalPart;
|
|
1270
|
+
// Cache array references for faster access in hot loop
|
|
1271
|
+
const variablesPerIndex = this.variablesPerIndex;
|
|
1272
|
+
const varIndexByCol = this.varIndexByCol;
|
|
1273
|
+
const varIndexByColLen = varIndexByCol.length;
|
|
1274
|
+
const oneMinusFrac = 1 - fractionalPart;
|
|
1275
|
+
for (let colIndex = 1; colIndex < varIndexByColLen; colIndex += 1) {
|
|
1276
|
+
const variable = variablesPerIndex[varIndexByCol[colIndex]];
|
|
1568
1277
|
const coefficient = mat[cutRowOffset + colIndex];
|
|
1569
1278
|
const termCoeff = coefficient - Math.floor(coefficient);
|
|
1570
1279
|
if (variable !== undefined && variable.isInteger) {
|
|
@@ -1575,9 +1284,7 @@ function addUpperBoundMIRCut(rowIndex) {
|
|
|
1575
1284
|
}
|
|
1576
1285
|
else {
|
|
1577
1286
|
mat[newRowOffset + colIndex] =
|
|
1578
|
-
coefficient >= 0
|
|
1579
|
-
? -coefficient
|
|
1580
|
-
: (coefficient * fractionalPart) / (1 - fractionalPart);
|
|
1287
|
+
coefficient >= 0 ? -coefficient : (coefficient * fractionalPart) / oneMinusFrac;
|
|
1581
1288
|
}
|
|
1582
1289
|
}
|
|
1583
1290
|
return true;
|
|
@@ -1952,12 +1659,15 @@ function isIntegral() {
|
|
|
1952
1659
|
const rhsColumn = this.rhsColumn;
|
|
1953
1660
|
const integerVariables = this.model.integerVariables;
|
|
1954
1661
|
const nIntegerVars = integerVariables.length;
|
|
1662
|
+
// Cache array reference for faster access in hot loop
|
|
1663
|
+
const rowByVarIndex = this.rowByVarIndex;
|
|
1664
|
+
const precision = this.precision;
|
|
1955
1665
|
for (let v = 0; v < nIntegerVars; v++) {
|
|
1956
1666
|
const varIndex = integerVariables[v].index;
|
|
1957
|
-
const row =
|
|
1667
|
+
const row = rowByVarIndex[varIndex];
|
|
1958
1668
|
if (row !== -1) {
|
|
1959
1669
|
const value = matrix[row * width + rhsColumn];
|
|
1960
|
-
if (Math.abs(value - Math.round(value)) >
|
|
1670
|
+
if (Math.abs(value - Math.round(value)) > precision) {
|
|
1961
1671
|
return false;
|
|
1962
1672
|
}
|
|
1963
1673
|
}
|
|
@@ -1973,12 +1683,17 @@ function computeFractionalVolume(ignoreIntegerValues) {
|
|
|
1973
1683
|
const width = this.width;
|
|
1974
1684
|
const matrix = this.matrix;
|
|
1975
1685
|
const rhsColumn = this.rhsColumn;
|
|
1976
|
-
|
|
1977
|
-
|
|
1686
|
+
const height = this.height;
|
|
1687
|
+
// Cache array references for faster access in hot loop
|
|
1688
|
+
const variablesPerIndex = this.variablesPerIndex;
|
|
1689
|
+
const varIndexByRow = this.varIndexByRow;
|
|
1690
|
+
const precision = this.precision;
|
|
1691
|
+
for (let r = 1; r < height; r += 1) {
|
|
1692
|
+
const variable = variablesPerIndex[varIndexByRow[r]];
|
|
1978
1693
|
if (variable !== undefined && variable.isInteger) {
|
|
1979
1694
|
const value = matrix[r * width + rhsColumn];
|
|
1980
1695
|
const distance = Math.abs(value);
|
|
1981
|
-
if (Math.min(distance - Math.floor(distance), Math.floor(distance + 1)) <
|
|
1696
|
+
if (Math.min(distance - Math.floor(distance), Math.floor(distance + 1)) < precision) {
|
|
1982
1697
|
if (ignoreIntegerValues !== true) {
|
|
1983
1698
|
return 0;
|
|
1984
1699
|
}
|
|
@@ -2007,9 +1722,11 @@ function getMostFractionalVar() {
|
|
|
2007
1722
|
const rhsColumn = this.rhsColumn;
|
|
2008
1723
|
const integerVars = this.model.integerVariables;
|
|
2009
1724
|
const nIntegerVars = integerVars.length;
|
|
1725
|
+
// Cache array reference for faster access in hot loop
|
|
1726
|
+
const rowByVarIndex = this.rowByVarIndex;
|
|
2010
1727
|
for (let v = 0; v < nIntegerVars; v += 1) {
|
|
2011
1728
|
const varIndex = integerVars[v].index;
|
|
2012
|
-
const row =
|
|
1729
|
+
const row = rowByVarIndex[varIndex];
|
|
2013
1730
|
if (row !== -1) {
|
|
2014
1731
|
const varValue = matrix[row * width + rhsColumn];
|
|
2015
1732
|
const fraction = Math.abs(varValue - Math.round(varValue));
|
|
@@ -2226,13 +1943,7 @@ class Tableau {
|
|
|
2226
1943
|
}
|
|
2227
1944
|
// ========== Core Simplex Operations ==========
|
|
2228
1945
|
simplex() {
|
|
2229
|
-
|
|
2230
|
-
if (shouldUseSparse(this)) {
|
|
2231
|
-
sparseSimplex.call(this);
|
|
2232
|
-
}
|
|
2233
|
-
else {
|
|
2234
|
-
simplex.call(this);
|
|
2235
|
-
}
|
|
1946
|
+
simplex.call(this);
|
|
2236
1947
|
return this;
|
|
2237
1948
|
}
|
|
2238
1949
|
phase1() {
|
|
@@ -2241,6 +1952,14 @@ class Tableau {
|
|
|
2241
1952
|
phase2() {
|
|
2242
1953
|
return phase2.call(this);
|
|
2243
1954
|
}
|
|
1955
|
+
/**
|
|
1956
|
+
* Dual simplex for warm-starting after adding bound constraints.
|
|
1957
|
+
* Use when solution is dual feasible but may be primal infeasible.
|
|
1958
|
+
* @returns Number of iterations, or -1 if dual infeasible
|
|
1959
|
+
*/
|
|
1960
|
+
dualSimplex() {
|
|
1961
|
+
return dualSimplex.call(this);
|
|
1962
|
+
}
|
|
2244
1963
|
pivot(pivotRowIndex, pivotColumnIndex) {
|
|
2245
1964
|
pivot.call(this, pivotRowIndex, pivotColumnIndex);
|
|
2246
1965
|
}
|
|
@@ -2902,6 +2621,7 @@ class Model {
|
|
|
2902
2621
|
_removeConstraint(constraint) {
|
|
2903
2622
|
const idx = this.constraints.indexOf(constraint);
|
|
2904
2623
|
if (idx === -1) {
|
|
2624
|
+
// eslint-disable-next-line no-console
|
|
2905
2625
|
console.warn("[Model.removeConstraint] Constraint not present in model");
|
|
2906
2626
|
return;
|
|
2907
2627
|
}
|
|
@@ -2931,6 +2651,7 @@ class Model {
|
|
|
2931
2651
|
removeVariable(variable) {
|
|
2932
2652
|
const idx = this.variables.indexOf(variable);
|
|
2933
2653
|
if (idx === -1) {
|
|
2654
|
+
// eslint-disable-next-line no-console
|
|
2934
2655
|
console.warn("[Model.removeVariable] Variable not present in model");
|
|
2935
2656
|
return;
|
|
2936
2657
|
}
|
|
@@ -3050,11 +2771,20 @@ class Model {
|
|
|
3050
2771
|
const unrestrictedVarIds = jsonModel.unrestricted || {};
|
|
3051
2772
|
// Instantiating variables and constraint terms
|
|
3052
2773
|
const objectiveName = jsonModel.optimize;
|
|
2774
|
+
// Check if objectiveName exists as a coefficient key in any variable.
|
|
2775
|
+
// If not, and it matches a variable name, the user wants to optimize
|
|
2776
|
+
// that variable directly (implicit cost of 1).
|
|
2777
|
+
const objectiveIsAttribute = variableIds.some((id) => objectiveName in variables[id]);
|
|
2778
|
+
const objectiveIsVariable = !objectiveIsAttribute && variableIds.includes(objectiveName);
|
|
3053
2779
|
for (let v = 0; v < nVariables; v += 1) {
|
|
3054
2780
|
// Creation of the variables
|
|
3055
2781
|
const variableId = variableIds[v];
|
|
3056
2782
|
const variableConstraints = variables[variableId];
|
|
3057
|
-
const cost =
|
|
2783
|
+
const cost = objectiveIsVariable
|
|
2784
|
+
? variableId === objectiveName
|
|
2785
|
+
? 1
|
|
2786
|
+
: 0
|
|
2787
|
+
: variableConstraints[objectiveName] || 0;
|
|
3058
2788
|
const isBinary = !!binaryVarIds[variableId];
|
|
3059
2789
|
const isInteger = !!integerVarIds[variableId] || isBinary;
|
|
3060
2790
|
const isUnrestricted = !!unrestrictedVarIds[variableId];
|
|
@@ -3409,7 +3139,7 @@ function Polyopt(solver, model) {
|
|
|
3409
3139
|
}
|
|
3410
3140
|
}
|
|
3411
3141
|
// Strip metadata so each Pareto vertex only contains value-bearing fields.
|
|
3412
|
-
const { feasible, result, bounded, ...paretoPayload } = solution;
|
|
3142
|
+
const { feasible: _feasible, result: _result, bounded: _bounded, ...paretoPayload } = solution;
|
|
3413
3143
|
paretoVertices.push(paretoPayload);
|
|
3414
3144
|
}
|
|
3415
3145
|
// Derive equality constraints that represent the averaged objective values.
|
|
@@ -3735,8 +3465,16 @@ var ReformatLP = /*@__PURE__*/getDefaultExportFromCjs(reformat);
|
|
|
3735
3465
|
function createCut$1(type, varIndex, value) {
|
|
3736
3466
|
return { type, varIndex, value };
|
|
3737
3467
|
}
|
|
3738
|
-
function createBranch$1(relaxedEvaluation, cuts, depth) {
|
|
3739
|
-
return {
|
|
3468
|
+
function createBranch$1(relaxedEvaluation, cuts, depth, branchVarIndex, branchDirection, branchFractionality, parentEvaluation) {
|
|
3469
|
+
return {
|
|
3470
|
+
relaxedEvaluation,
|
|
3471
|
+
cuts,
|
|
3472
|
+
depth,
|
|
3473
|
+
branchVarIndex,
|
|
3474
|
+
branchDirection,
|
|
3475
|
+
branchFractionality,
|
|
3476
|
+
parentEvaluation,
|
|
3477
|
+
};
|
|
3740
3478
|
}
|
|
3741
3479
|
/**
|
|
3742
3480
|
* Enhanced branch-and-cut with:
|
|
@@ -3745,7 +3483,8 @@ function createBranch$1(relaxedEvaluation, cuts, depth) {
|
|
|
3745
3483
|
* - Diving heuristic for quick feasible solutions
|
|
3746
3484
|
*/
|
|
3747
3485
|
function createEnhancedBranchAndCutService(options = {}) {
|
|
3748
|
-
const { nodeSelection = "hybrid", branching = "pseudocost", useDiving = true,
|
|
3486
|
+
const { nodeSelection = "hybrid", branching = "pseudocost", useDiving: _useDiving = true, // Reserved for future diving heuristic
|
|
3487
|
+
strongBranchingCandidates = 5, } = options;
|
|
3749
3488
|
// Pseudocost data per variable index
|
|
3750
3489
|
const pseudoCosts = new Map();
|
|
3751
3490
|
const getPseudoCost = (varIndex) => {
|
|
@@ -3778,7 +3517,7 @@ function createEnhancedBranchAndCutService(options = {}) {
|
|
|
3778
3517
|
// Product score (like SCIP's default)
|
|
3779
3518
|
return Math.max(upEstimate, 1e-6) * Math.max(downEstimate, 1e-6);
|
|
3780
3519
|
};
|
|
3781
|
-
const selectBranchingVariable = (tableau,
|
|
3520
|
+
const selectBranchingVariable = (tableau, _currentEval) => {
|
|
3782
3521
|
const width = tableau.width;
|
|
3783
3522
|
const matrix = tableau.matrix;
|
|
3784
3523
|
const rhsColumn = tableau.rhsColumn;
|
|
@@ -3922,11 +3661,11 @@ function createEnhancedBranchAndCutService(options = {}) {
|
|
|
3922
3661
|
else {
|
|
3923
3662
|
break;
|
|
3924
3663
|
}
|
|
3925
|
-
if (activeBranch.relaxedEvaluation
|
|
3664
|
+
if (activeBranch.relaxedEvaluation >= bestEvaluation) {
|
|
3926
3665
|
continue;
|
|
3927
3666
|
}
|
|
3928
3667
|
const cuts = activeBranch.cuts;
|
|
3929
|
-
|
|
3668
|
+
tableau.evaluation;
|
|
3930
3669
|
applyCuts(tableau, cuts);
|
|
3931
3670
|
iterations++;
|
|
3932
3671
|
if (!tableau.feasible) {
|
|
@@ -3937,12 +3676,12 @@ function createEnhancedBranchAndCutService(options = {}) {
|
|
|
3937
3676
|
continue;
|
|
3938
3677
|
}
|
|
3939
3678
|
// Update pseudocosts based on observed improvement
|
|
3940
|
-
if (
|
|
3941
|
-
|
|
3942
|
-
|
|
3943
|
-
|
|
3944
|
-
const
|
|
3945
|
-
updatePseudoCost(
|
|
3679
|
+
if (activeBranch.branchVarIndex !== undefined &&
|
|
3680
|
+
activeBranch.branchDirection !== undefined &&
|
|
3681
|
+
activeBranch.branchFractionality !== undefined &&
|
|
3682
|
+
activeBranch.parentEvaluation !== undefined) {
|
|
3683
|
+
const improvement = Math.abs(evaluation - activeBranch.parentEvaluation);
|
|
3684
|
+
updatePseudoCost(activeBranch.branchVarIndex, activeBranch.branchDirection, improvement, activeBranch.branchFractionality);
|
|
3946
3685
|
}
|
|
3947
3686
|
if (evaluation === bestEvaluation) {
|
|
3948
3687
|
let isCurrentEvaluationWorse = true;
|
|
@@ -4021,22 +3760,24 @@ function createEnhancedBranchAndCutService(options = {}) {
|
|
|
4021
3760
|
cutsLow.push(cut);
|
|
4022
3761
|
}
|
|
4023
3762
|
}
|
|
4024
|
-
const
|
|
4025
|
-
const
|
|
4026
|
-
const
|
|
3763
|
+
const ceilVal = Math.ceil(varValue);
|
|
3764
|
+
const floorVal = Math.floor(varValue);
|
|
3765
|
+
const fracUp = ceilVal - varValue; // Distance to ceil
|
|
3766
|
+
const fracDown = varValue - floorVal; // Distance to floor
|
|
3767
|
+
const cutHigh = createCut$1("min", varIndex, ceilVal);
|
|
4027
3768
|
cutsHigh.push(cutHigh);
|
|
4028
|
-
const cutLow = createCut$1("max", varIndex,
|
|
3769
|
+
const cutLow = createCut$1("max", varIndex, floorVal);
|
|
4029
3770
|
cutsLow.push(cutLow);
|
|
4030
3771
|
const newDepth = activeBranch.depth + 1;
|
|
4031
3772
|
if (useDepthFirst) {
|
|
4032
3773
|
// Push in reverse order so 'up' branch is explored first
|
|
4033
3774
|
// (often better for minimization with binary vars)
|
|
4034
|
-
depthFirstStack.push(createBranch$1(evaluation, cutsLow, newDepth));
|
|
4035
|
-
depthFirstStack.push(createBranch$1(evaluation, cutsHigh, newDepth));
|
|
3775
|
+
depthFirstStack.push(createBranch$1(evaluation, cutsLow, newDepth, varIndex, "down", fracDown, evaluation));
|
|
3776
|
+
depthFirstStack.push(createBranch$1(evaluation, cutsHigh, newDepth, varIndex, "up", fracUp, evaluation));
|
|
4036
3777
|
}
|
|
4037
3778
|
else {
|
|
4038
|
-
branches.push(createBranch$1(evaluation, cutsHigh, newDepth));
|
|
4039
|
-
branches.push(createBranch$1(evaluation, cutsLow, newDepth));
|
|
3779
|
+
branches.push(createBranch$1(evaluation, cutsHigh, newDepth, varIndex, "up", fracUp, evaluation));
|
|
3780
|
+
branches.push(createBranch$1(evaluation, cutsLow, newDepth, varIndex, "down", fracDown, evaluation));
|
|
4040
3781
|
}
|
|
4041
3782
|
}
|
|
4042
3783
|
}
|
|
@@ -4274,7 +4015,7 @@ function createIncrementalBranchAndCutService(options = {}) {
|
|
|
4274
4015
|
else {
|
|
4275
4016
|
break;
|
|
4276
4017
|
}
|
|
4277
|
-
if (activeBranch.relaxedEvaluation
|
|
4018
|
+
if (activeBranch.relaxedEvaluation >= bestEvaluation) {
|
|
4278
4019
|
continue;
|
|
4279
4020
|
}
|
|
4280
4021
|
const parentEval = tableau.evaluation;
|