javascript-lp-solver 1.0.1 → 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 +5 -5
- package/dist/index.browser.mjs +255 -694
- package/dist/index.browser.mjs.map +1 -1
- package/dist/index.cjs +255 -694
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +32 -33
- package/dist/index.mjs +255 -694
- package/dist/index.mjs.map +1 -1
- package/dist/solver.global.js +255 -694
- 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/mip-utils.d.ts +0 -36
- package/dist/types/tableau/presolve.d.ts +0 -10
- package/dist/types/tableau/solution.d.ts +0 -1
- package/dist/types/tableau/types.d.ts +0 -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
|
@@ -159,15 +159,16 @@ function createBranchAndCutService() {
|
|
|
159
159
|
tableau.addCutConstraints(branchingCuts);
|
|
160
160
|
tableau.simplex();
|
|
161
161
|
if ((_a = tableau.model) === null || _a === void 0 ? void 0 : _a.useMIRCuts) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
162
|
+
// Optimization: reuse previous "after" as next "before" to avoid redundant computation
|
|
163
|
+
let fractionalVolume = tableau.computeFractionalVolume(true);
|
|
164
|
+
while (fractionalVolume > 0) {
|
|
165
165
|
tableau.applyMIRCuts();
|
|
166
166
|
tableau.simplex();
|
|
167
167
|
const fractionalVolumeAfter = tableau.computeFractionalVolume(true);
|
|
168
|
-
if (fractionalVolumeAfter >= 0.9 *
|
|
169
|
-
|
|
168
|
+
if (fractionalVolumeAfter >= 0.9 * fractionalVolume) {
|
|
169
|
+
break;
|
|
170
170
|
}
|
|
171
|
+
fractionalVolume = fractionalVolumeAfter;
|
|
171
172
|
}
|
|
172
173
|
}
|
|
173
174
|
};
|
|
@@ -206,7 +207,7 @@ function createBranchAndCutService() {
|
|
|
206
207
|
}
|
|
207
208
|
}
|
|
208
209
|
const activeBranch = branches.pop();
|
|
209
|
-
if (activeBranch.relaxedEvaluation
|
|
210
|
+
if (activeBranch.relaxedEvaluation >= bestEvaluation) {
|
|
210
211
|
continue;
|
|
211
212
|
}
|
|
212
213
|
const cuts = activeBranch.cuts;
|
|
@@ -444,13 +445,32 @@ function phase1() {
|
|
|
444
445
|
const unrestrictedVars = this.unrestrictedVars;
|
|
445
446
|
const varIndexByRow = this.varIndexByRow;
|
|
446
447
|
const varIndexByCol = this.varIndexByCol;
|
|
447
|
-
|
|
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);
|
|
448
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;
|
|
449
465
|
while (true) {
|
|
450
|
-
// Find leaving row (most negative RHS)
|
|
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.
|
|
451
469
|
let leavingRowIndex = 0;
|
|
452
470
|
let rhsValue = negPrecision;
|
|
453
471
|
for (let r = 1; r <= lastRow; r++) {
|
|
472
|
+
if (unrestrictedVars[varIndexByRow[r]] === true)
|
|
473
|
+
continue;
|
|
454
474
|
const value = matrix[r * width + rhsColumn];
|
|
455
475
|
if (value < rhsValue) {
|
|
456
476
|
rhsValue = value;
|
|
@@ -461,18 +481,95 @@ function phase1() {
|
|
|
461
481
|
this.feasible = true;
|
|
462
482
|
return iterations;
|
|
463
483
|
}
|
|
464
|
-
//
|
|
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).
|
|
465
525
|
let enteringColumn = 0;
|
|
466
|
-
let maxQuotient = -Infinity;
|
|
467
526
|
const leavingRowOffset = leavingRowIndex * width;
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
const
|
|
473
|
-
if (
|
|
474
|
-
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) {
|
|
475
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
|
+
}
|
|
476
573
|
}
|
|
477
574
|
}
|
|
478
575
|
}
|
|
@@ -514,6 +611,20 @@ function phase2() {
|
|
|
514
611
|
let iterations = 0;
|
|
515
612
|
let reducedCost;
|
|
516
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;
|
|
517
628
|
// Partial pricing setup
|
|
518
629
|
// Batch size: use configured value or auto-compute (sqrt of columns, min 50, max 500)
|
|
519
630
|
const nColumns = lastColumn;
|
|
@@ -526,10 +637,65 @@ function phase2() {
|
|
|
526
637
|
if (nOptionalObjectives > 0) {
|
|
527
638
|
optionalCostsColumns = [];
|
|
528
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
|
+
}
|
|
529
676
|
let enteringColumn = 0;
|
|
530
677
|
let enteringValue = precision;
|
|
531
678
|
let isReducedCostNegative = false;
|
|
532
|
-
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) {
|
|
533
699
|
// Partial pricing: scan columns in batches
|
|
534
700
|
const startBatch = this.pricingBatchStart;
|
|
535
701
|
let batchesScanned = 0;
|
|
@@ -789,642 +955,6 @@ function checkForCycles(varIndexes) {
|
|
|
789
955
|
return [];
|
|
790
956
|
}
|
|
791
957
|
|
|
792
|
-
/**
|
|
793
|
-
* @file src/tableau/sparse-matrix.ts
|
|
794
|
-
* @description Compressed Sparse Column (CSC) matrix implementation
|
|
795
|
-
*
|
|
796
|
-
* Optimized for column operations which are frequent in the simplex algorithm.
|
|
797
|
-
* Provides O(nnz) pivot operations instead of O(n*m) for dense matrices.
|
|
798
|
-
*
|
|
799
|
-
* CSC Storage Format:
|
|
800
|
-
* - values: non-zero values stored column by column
|
|
801
|
-
* - rowIndices: row index for each value
|
|
802
|
-
* - colPointers: starting index in values/rowIndices for each column
|
|
803
|
-
*
|
|
804
|
-
* Column c's entries are at indices colPointers[c] to colPointers[c+1]-1
|
|
805
|
-
*/
|
|
806
|
-
class SparseMatrix {
|
|
807
|
-
constructor(nRows, nCols, precision = 1e-12) {
|
|
808
|
-
this.nRows = nRows;
|
|
809
|
-
this.nCols = nCols;
|
|
810
|
-
this.precision = precision;
|
|
811
|
-
// Initialize empty sparse matrix
|
|
812
|
-
this.values = [];
|
|
813
|
-
this.rowIndices = [];
|
|
814
|
-
this.colPointers = new Array(nCols + 1).fill(0);
|
|
815
|
-
// Workspace for row/column operations
|
|
816
|
-
this.workVector = new Float64Array(Math.max(nRows, nCols));
|
|
817
|
-
this.workIndices = [];
|
|
818
|
-
}
|
|
819
|
-
/**
|
|
820
|
-
* Create sparse matrix from dense Float64Array
|
|
821
|
-
*/
|
|
822
|
-
static fromDense(dense, width, height, precision = 1e-12) {
|
|
823
|
-
const sparse = new SparseMatrix(height, width, precision);
|
|
824
|
-
const values = [];
|
|
825
|
-
const rowIndices = [];
|
|
826
|
-
const colPointers = [0];
|
|
827
|
-
// Build CSC by iterating column by column
|
|
828
|
-
for (let c = 0; c < width; c++) {
|
|
829
|
-
for (let r = 0; r < height; r++) {
|
|
830
|
-
const val = dense[r * width + c];
|
|
831
|
-
if (val < -precision || val > precision) {
|
|
832
|
-
values.push(val);
|
|
833
|
-
rowIndices.push(r);
|
|
834
|
-
}
|
|
835
|
-
}
|
|
836
|
-
colPointers.push(values.length);
|
|
837
|
-
}
|
|
838
|
-
sparse.values = values;
|
|
839
|
-
sparse.rowIndices = rowIndices;
|
|
840
|
-
sparse.colPointers = colPointers;
|
|
841
|
-
return sparse;
|
|
842
|
-
}
|
|
843
|
-
/**
|
|
844
|
-
* Convert to dense Float64Array
|
|
845
|
-
*/
|
|
846
|
-
toDense() {
|
|
847
|
-
const dense = new Float64Array(this.nRows * this.nCols);
|
|
848
|
-
for (let c = 0; c < this.nCols; c++) {
|
|
849
|
-
const start = this.colPointers[c];
|
|
850
|
-
const end = this.colPointers[c + 1];
|
|
851
|
-
for (let i = start; i < end; i++) {
|
|
852
|
-
const r = this.rowIndices[i];
|
|
853
|
-
dense[r * this.nCols + c] = this.values[i];
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
|
-
return dense;
|
|
857
|
-
}
|
|
858
|
-
/**
|
|
859
|
-
* Get value at (row, col)
|
|
860
|
-
*/
|
|
861
|
-
get(row, col) {
|
|
862
|
-
const start = this.colPointers[col];
|
|
863
|
-
const end = this.colPointers[col + 1];
|
|
864
|
-
// Binary search for row in this column
|
|
865
|
-
let lo = start;
|
|
866
|
-
let hi = end;
|
|
867
|
-
while (lo < hi) {
|
|
868
|
-
const mid = (lo + hi) >>> 1;
|
|
869
|
-
if (this.rowIndices[mid] < row) {
|
|
870
|
-
lo = mid + 1;
|
|
871
|
-
}
|
|
872
|
-
else {
|
|
873
|
-
hi = mid;
|
|
874
|
-
}
|
|
875
|
-
}
|
|
876
|
-
if (lo < end && this.rowIndices[lo] === row) {
|
|
877
|
-
return this.values[lo];
|
|
878
|
-
}
|
|
879
|
-
return 0;
|
|
880
|
-
}
|
|
881
|
-
/**
|
|
882
|
-
* Set value at (row, col)
|
|
883
|
-
* Handles insertion and removal of entries
|
|
884
|
-
*/
|
|
885
|
-
set(row, col, value) {
|
|
886
|
-
const start = this.colPointers[col];
|
|
887
|
-
const end = this.colPointers[col + 1];
|
|
888
|
-
const isZero = value >= -this.precision && value <= this.precision;
|
|
889
|
-
// Binary search for insertion point
|
|
890
|
-
let lo = start;
|
|
891
|
-
let hi = end;
|
|
892
|
-
while (lo < hi) {
|
|
893
|
-
const mid = (lo + hi) >>> 1;
|
|
894
|
-
if (this.rowIndices[mid] < row) {
|
|
895
|
-
lo = mid + 1;
|
|
896
|
-
}
|
|
897
|
-
else {
|
|
898
|
-
hi = mid;
|
|
899
|
-
}
|
|
900
|
-
}
|
|
901
|
-
const exists = lo < end && this.rowIndices[lo] === row;
|
|
902
|
-
if (exists) {
|
|
903
|
-
if (isZero) {
|
|
904
|
-
// Remove existing entry
|
|
905
|
-
this.values.splice(lo, 1);
|
|
906
|
-
this.rowIndices.splice(lo, 1);
|
|
907
|
-
// Update column pointers
|
|
908
|
-
for (let c = col + 1; c <= this.nCols; c++) {
|
|
909
|
-
this.colPointers[c]--;
|
|
910
|
-
}
|
|
911
|
-
}
|
|
912
|
-
else {
|
|
913
|
-
// Update existing entry
|
|
914
|
-
this.values[lo] = value;
|
|
915
|
-
}
|
|
916
|
-
}
|
|
917
|
-
else if (!isZero) {
|
|
918
|
-
// Insert new entry
|
|
919
|
-
this.values.splice(lo, 0, value);
|
|
920
|
-
this.rowIndices.splice(lo, 0, row);
|
|
921
|
-
// Update column pointers
|
|
922
|
-
for (let c = col + 1; c <= this.nCols; c++) {
|
|
923
|
-
this.colPointers[c]++;
|
|
924
|
-
}
|
|
925
|
-
}
|
|
926
|
-
}
|
|
927
|
-
/**
|
|
928
|
-
* Get column as sparse vector
|
|
929
|
-
*/
|
|
930
|
-
getColumn(col) {
|
|
931
|
-
const start = this.colPointers[col];
|
|
932
|
-
const end = this.colPointers[col + 1];
|
|
933
|
-
const len = end - start;
|
|
934
|
-
return {
|
|
935
|
-
indices: this.rowIndices.slice(start, end),
|
|
936
|
-
values: this.values.slice(start, end),
|
|
937
|
-
length: len,
|
|
938
|
-
};
|
|
939
|
-
}
|
|
940
|
-
/**
|
|
941
|
-
* Get row as sparse vector (less efficient - requires full scan)
|
|
942
|
-
*/
|
|
943
|
-
getRow(row) {
|
|
944
|
-
const indices = [];
|
|
945
|
-
const values = [];
|
|
946
|
-
for (let c = 0; c < this.nCols; c++) {
|
|
947
|
-
const val = this.get(row, c);
|
|
948
|
-
if (val !== 0) {
|
|
949
|
-
indices.push(c);
|
|
950
|
-
values.push(val);
|
|
951
|
-
}
|
|
952
|
-
}
|
|
953
|
-
return { indices, values, length: indices.length };
|
|
954
|
-
}
|
|
955
|
-
/**
|
|
956
|
-
* Get row into pre-allocated dense array (more efficient for repeated access)
|
|
957
|
-
* Returns array of non-zero column indices
|
|
958
|
-
*/
|
|
959
|
-
getRowDense(row, output) {
|
|
960
|
-
const nonZeroCols = [];
|
|
961
|
-
// Clear relevant portion
|
|
962
|
-
output.fill(0, 0, this.nCols);
|
|
963
|
-
for (let c = 0; c < this.nCols; c++) {
|
|
964
|
-
const start = this.colPointers[c];
|
|
965
|
-
const end = this.colPointers[c + 1];
|
|
966
|
-
// Binary search for row
|
|
967
|
-
let lo = start;
|
|
968
|
-
let hi = end;
|
|
969
|
-
while (lo < hi) {
|
|
970
|
-
const mid = (lo + hi) >>> 1;
|
|
971
|
-
if (this.rowIndices[mid] < row) {
|
|
972
|
-
lo = mid + 1;
|
|
973
|
-
}
|
|
974
|
-
else {
|
|
975
|
-
hi = mid;
|
|
976
|
-
}
|
|
977
|
-
}
|
|
978
|
-
if (lo < end && this.rowIndices[lo] === row) {
|
|
979
|
-
output[c] = this.values[lo];
|
|
980
|
-
nonZeroCols.push(c);
|
|
981
|
-
}
|
|
982
|
-
}
|
|
983
|
-
return nonZeroCols;
|
|
984
|
-
}
|
|
985
|
-
/**
|
|
986
|
-
* Add a new row (expands matrix vertically)
|
|
987
|
-
*/
|
|
988
|
-
addRow() {
|
|
989
|
-
this.nRows++;
|
|
990
|
-
if (this.workVector.length < this.nRows) {
|
|
991
|
-
this.workVector = new Float64Array(this.nRows * 2);
|
|
992
|
-
}
|
|
993
|
-
}
|
|
994
|
-
/**
|
|
995
|
-
* Add a new column (expands matrix horizontally)
|
|
996
|
-
*/
|
|
997
|
-
addColumn() {
|
|
998
|
-
this.colPointers.push(this.colPointers[this.nCols]);
|
|
999
|
-
this.nCols++;
|
|
1000
|
-
if (this.workVector.length < this.nCols) {
|
|
1001
|
-
this.workVector = new Float64Array(this.nCols * 2);
|
|
1002
|
-
}
|
|
1003
|
-
}
|
|
1004
|
-
/**
|
|
1005
|
-
* Set entire column from sparse vector
|
|
1006
|
-
*/
|
|
1007
|
-
setColumn(col, sparseCol) {
|
|
1008
|
-
const oldStart = this.colPointers[col];
|
|
1009
|
-
const oldEnd = this.colPointers[col + 1];
|
|
1010
|
-
const oldLen = oldEnd - oldStart;
|
|
1011
|
-
const newLen = sparseCol.length;
|
|
1012
|
-
const diff = newLen - oldLen;
|
|
1013
|
-
if (diff !== 0) {
|
|
1014
|
-
// Need to resize
|
|
1015
|
-
if (diff > 0) {
|
|
1016
|
-
// Make room
|
|
1017
|
-
for (let i = 0; i < diff; i++) {
|
|
1018
|
-
this.values.splice(oldEnd, 0, 0);
|
|
1019
|
-
this.rowIndices.splice(oldEnd, 0, 0);
|
|
1020
|
-
}
|
|
1021
|
-
}
|
|
1022
|
-
else {
|
|
1023
|
-
// Remove excess
|
|
1024
|
-
this.values.splice(oldStart, -diff);
|
|
1025
|
-
this.rowIndices.splice(oldStart, -diff);
|
|
1026
|
-
}
|
|
1027
|
-
// Update column pointers
|
|
1028
|
-
for (let c = col + 1; c <= this.nCols; c++) {
|
|
1029
|
-
this.colPointers[c] += diff;
|
|
1030
|
-
}
|
|
1031
|
-
}
|
|
1032
|
-
// Copy new values
|
|
1033
|
-
const start = this.colPointers[col];
|
|
1034
|
-
for (let i = 0; i < newLen; i++) {
|
|
1035
|
-
this.values[start + i] = sparseCol.values[i];
|
|
1036
|
-
this.rowIndices[start + i] = sparseCol.indices[i];
|
|
1037
|
-
}
|
|
1038
|
-
}
|
|
1039
|
-
/**
|
|
1040
|
-
* Number of non-zero entries
|
|
1041
|
-
*/
|
|
1042
|
-
get nnz() {
|
|
1043
|
-
return this.values.length;
|
|
1044
|
-
}
|
|
1045
|
-
/**
|
|
1046
|
-
* Density (fraction of non-zeros)
|
|
1047
|
-
*/
|
|
1048
|
-
get density() {
|
|
1049
|
-
return this.nnz / (this.nRows * this.nCols);
|
|
1050
|
-
}
|
|
1051
|
-
/**
|
|
1052
|
-
* Scale a row by a factor
|
|
1053
|
-
*/
|
|
1054
|
-
scaleRow(row, factor) {
|
|
1055
|
-
for (let c = 0; c < this.nCols; c++) {
|
|
1056
|
-
const start = this.colPointers[c];
|
|
1057
|
-
const end = this.colPointers[c + 1];
|
|
1058
|
-
for (let i = start; i < end; i++) {
|
|
1059
|
-
if (this.rowIndices[i] === row) {
|
|
1060
|
-
this.values[i] *= factor;
|
|
1061
|
-
break;
|
|
1062
|
-
}
|
|
1063
|
-
}
|
|
1064
|
-
}
|
|
1065
|
-
}
|
|
1066
|
-
/**
|
|
1067
|
-
* Add scaled row to another row: targetRow += factor * sourceRow
|
|
1068
|
-
* This is the core operation in Gaussian elimination / pivot
|
|
1069
|
-
*/
|
|
1070
|
-
addScaledRow(targetRow, sourceRow, factor) {
|
|
1071
|
-
if (factor >= -this.precision && factor <= this.precision) {
|
|
1072
|
-
return;
|
|
1073
|
-
}
|
|
1074
|
-
// Get source row as dense for efficiency
|
|
1075
|
-
const sourceNonZeros = this.getRowDense(sourceRow, this.workVector);
|
|
1076
|
-
// Add scaled source to target
|
|
1077
|
-
for (const c of sourceNonZeros) {
|
|
1078
|
-
const sourceVal = this.workVector[c];
|
|
1079
|
-
const newVal = this.get(targetRow, c) + factor * sourceVal;
|
|
1080
|
-
this.set(targetRow, c, newVal);
|
|
1081
|
-
}
|
|
1082
|
-
}
|
|
1083
|
-
/**
|
|
1084
|
-
* Perform pivot operation at (pivotRow, pivotCol)
|
|
1085
|
-
* This is optimized for sparse operations
|
|
1086
|
-
*/
|
|
1087
|
-
pivot(pivotRow, pivotCol) {
|
|
1088
|
-
const pivotVal = this.get(pivotRow, pivotCol);
|
|
1089
|
-
if (pivotVal === 0) {
|
|
1090
|
-
throw new Error("Cannot pivot on zero element");
|
|
1091
|
-
}
|
|
1092
|
-
const invPivot = 1 / pivotVal;
|
|
1093
|
-
// Get pivot row as dense
|
|
1094
|
-
const pivotRowNonZeros = this.getRowDense(pivotRow, this.workVector);
|
|
1095
|
-
// Scale pivot row
|
|
1096
|
-
for (const c of pivotRowNonZeros) {
|
|
1097
|
-
this.workVector[c] *= invPivot;
|
|
1098
|
-
}
|
|
1099
|
-
this.workVector[pivotCol] = invPivot;
|
|
1100
|
-
// Update pivot row in matrix
|
|
1101
|
-
for (const c of pivotRowNonZeros) {
|
|
1102
|
-
this.set(pivotRow, c, this.workVector[c]);
|
|
1103
|
-
}
|
|
1104
|
-
this.set(pivotRow, pivotCol, invPivot);
|
|
1105
|
-
// Get pivot column to find rows that need updating
|
|
1106
|
-
const pivotColData = this.getColumn(pivotCol);
|
|
1107
|
-
// Update all other rows
|
|
1108
|
-
for (let i = 0; i < pivotColData.length; i++) {
|
|
1109
|
-
const r = pivotColData.indices[i];
|
|
1110
|
-
if (r === pivotRow)
|
|
1111
|
-
continue;
|
|
1112
|
-
const coefficient = pivotColData.values[i];
|
|
1113
|
-
if (coefficient >= -this.precision && coefficient <= this.precision)
|
|
1114
|
-
continue;
|
|
1115
|
-
// row[r] -= coefficient * pivotRow
|
|
1116
|
-
for (const c of pivotRowNonZeros) {
|
|
1117
|
-
if (c === pivotCol)
|
|
1118
|
-
continue;
|
|
1119
|
-
const pivotRowVal = this.workVector[c];
|
|
1120
|
-
if (pivotRowVal >= -this.precision && pivotRowVal <= this.precision)
|
|
1121
|
-
continue;
|
|
1122
|
-
const newVal = this.get(r, c) - coefficient * pivotRowVal;
|
|
1123
|
-
this.set(r, c, newVal);
|
|
1124
|
-
}
|
|
1125
|
-
// Set pivot column value for this row
|
|
1126
|
-
this.set(r, pivotCol, -coefficient * invPivot);
|
|
1127
|
-
}
|
|
1128
|
-
return pivotRowNonZeros;
|
|
1129
|
-
}
|
|
1130
|
-
/**
|
|
1131
|
-
* Clone the matrix
|
|
1132
|
-
*/
|
|
1133
|
-
clone() {
|
|
1134
|
-
const copy = new SparseMatrix(this.nRows, this.nCols, this.precision);
|
|
1135
|
-
copy.values = this.values.slice();
|
|
1136
|
-
copy.rowIndices = this.rowIndices.slice();
|
|
1137
|
-
copy.colPointers = this.colPointers.slice();
|
|
1138
|
-
return copy;
|
|
1139
|
-
}
|
|
1140
|
-
}
|
|
1141
|
-
|
|
1142
|
-
/**
|
|
1143
|
-
* @file src/tableau/sparse-simplex.ts
|
|
1144
|
-
* @description Sparse simplex algorithm for large LP problems
|
|
1145
|
-
*
|
|
1146
|
-
* Optimized simplex implementation that operates directly on SparseMatrix
|
|
1147
|
-
* storage. Converts the dense tableau to CSC format, solves, and converts
|
|
1148
|
-
* back for solution extraction.
|
|
1149
|
-
*
|
|
1150
|
-
* Provides significant speedup for large, sparse problems where most
|
|
1151
|
-
* coefficients are zero.
|
|
1152
|
-
*/
|
|
1153
|
-
/**
|
|
1154
|
-
* Convert dense tableau to sparse and solve
|
|
1155
|
-
*/
|
|
1156
|
-
function sparseSimplex() {
|
|
1157
|
-
// Convert to sparse
|
|
1158
|
-
const sparse = SparseMatrix.fromDense(this.matrix, this.width, this.height, this.precision);
|
|
1159
|
-
this.bounded = true;
|
|
1160
|
-
// Run sparse phase 1
|
|
1161
|
-
sparsePhase1.call(this, sparse);
|
|
1162
|
-
if (this.feasible === true) {
|
|
1163
|
-
// Run sparse phase 2
|
|
1164
|
-
sparsePhase2.call(this, sparse);
|
|
1165
|
-
}
|
|
1166
|
-
// Copy results back to dense matrix (needed for solution extraction)
|
|
1167
|
-
const dense = sparse.toDense();
|
|
1168
|
-
this.matrix.set(dense);
|
|
1169
|
-
return this;
|
|
1170
|
-
}
|
|
1171
|
-
/**
|
|
1172
|
-
* Sparse Phase 1: Find a basic feasible solution
|
|
1173
|
-
*/
|
|
1174
|
-
function sparsePhase1(sparse) {
|
|
1175
|
-
const debugCheckForCycles = this.model.checkForCycles;
|
|
1176
|
-
const varIndexesCycle = [];
|
|
1177
|
-
const rhsColumn = this.rhsColumn;
|
|
1178
|
-
const lastColumn = this.width - 1;
|
|
1179
|
-
const lastRow = this.height - 1;
|
|
1180
|
-
const precision = this.precision;
|
|
1181
|
-
let unrestricted;
|
|
1182
|
-
let iterations = 0;
|
|
1183
|
-
while (true) {
|
|
1184
|
-
// Find leaving row (most negative RHS)
|
|
1185
|
-
let leavingRowIndex = 0;
|
|
1186
|
-
let rhsValue = -precision;
|
|
1187
|
-
for (let r = 1; r <= lastRow; r++) {
|
|
1188
|
-
const value = sparse.get(r, rhsColumn);
|
|
1189
|
-
if (value < rhsValue) {
|
|
1190
|
-
rhsValue = value;
|
|
1191
|
-
leavingRowIndex = r;
|
|
1192
|
-
}
|
|
1193
|
-
}
|
|
1194
|
-
if (leavingRowIndex === 0) {
|
|
1195
|
-
this.feasible = true;
|
|
1196
|
-
return iterations;
|
|
1197
|
-
}
|
|
1198
|
-
// Find entering column
|
|
1199
|
-
let enteringColumn = 0;
|
|
1200
|
-
let maxQuotient = -Infinity;
|
|
1201
|
-
// Get leaving row as sparse
|
|
1202
|
-
const leavingRowData = sparse.getRow(leavingRowIndex);
|
|
1203
|
-
for (let i = 0; i < leavingRowData.length; i++) {
|
|
1204
|
-
const c = leavingRowData.indices[i];
|
|
1205
|
-
if (c === 0 || c === rhsColumn)
|
|
1206
|
-
continue;
|
|
1207
|
-
const coefficient = leavingRowData.values[i];
|
|
1208
|
-
unrestricted = this.unrestrictedVars[this.varIndexByCol[c]] === true;
|
|
1209
|
-
if (unrestricted || coefficient < -precision) {
|
|
1210
|
-
const costValue = sparse.get(0, c);
|
|
1211
|
-
const quotient = -costValue / coefficient;
|
|
1212
|
-
if (maxQuotient < quotient) {
|
|
1213
|
-
maxQuotient = quotient;
|
|
1214
|
-
enteringColumn = c;
|
|
1215
|
-
}
|
|
1216
|
-
}
|
|
1217
|
-
}
|
|
1218
|
-
// Also check columns with zero in leaving row but non-zero cost
|
|
1219
|
-
for (let c = 1; c <= lastColumn; c++) {
|
|
1220
|
-
if (c === rhsColumn)
|
|
1221
|
-
continue;
|
|
1222
|
-
const coefficient = sparse.get(leavingRowIndex, c);
|
|
1223
|
-
unrestricted = this.unrestrictedVars[this.varIndexByCol[c]] === true;
|
|
1224
|
-
if (unrestricted || coefficient < -precision) {
|
|
1225
|
-
const costValue = sparse.get(0, c);
|
|
1226
|
-
const quotient = -costValue / coefficient;
|
|
1227
|
-
if (maxQuotient < quotient) {
|
|
1228
|
-
maxQuotient = quotient;
|
|
1229
|
-
enteringColumn = c;
|
|
1230
|
-
}
|
|
1231
|
-
}
|
|
1232
|
-
}
|
|
1233
|
-
if (enteringColumn === 0) {
|
|
1234
|
-
this.feasible = false;
|
|
1235
|
-
return iterations;
|
|
1236
|
-
}
|
|
1237
|
-
if (debugCheckForCycles) {
|
|
1238
|
-
varIndexesCycle.push([
|
|
1239
|
-
this.varIndexByRow[leavingRowIndex],
|
|
1240
|
-
this.varIndexByCol[enteringColumn],
|
|
1241
|
-
]);
|
|
1242
|
-
const cycleData = this.checkForCycles(varIndexesCycle);
|
|
1243
|
-
if (cycleData.length > 0) {
|
|
1244
|
-
this.model.messages.push("Cycle in phase 1");
|
|
1245
|
-
this.feasible = false;
|
|
1246
|
-
return iterations;
|
|
1247
|
-
}
|
|
1248
|
-
}
|
|
1249
|
-
// Perform pivot
|
|
1250
|
-
sparsePivot.call(this, sparse, leavingRowIndex, enteringColumn);
|
|
1251
|
-
iterations += 1;
|
|
1252
|
-
}
|
|
1253
|
-
}
|
|
1254
|
-
/**
|
|
1255
|
-
* Sparse Phase 2: Optimize the objective
|
|
1256
|
-
*/
|
|
1257
|
-
function sparsePhase2(sparse) {
|
|
1258
|
-
const debugCheckForCycles = this.model.checkForCycles;
|
|
1259
|
-
const varIndexesCycle = [];
|
|
1260
|
-
const rhsColumn = this.rhsColumn;
|
|
1261
|
-
const lastColumn = this.width - 1;
|
|
1262
|
-
this.height - 1;
|
|
1263
|
-
const precision = this.precision;
|
|
1264
|
-
this.optionalObjectives.length;
|
|
1265
|
-
let iterations = 0;
|
|
1266
|
-
let reducedCost;
|
|
1267
|
-
let unrestricted;
|
|
1268
|
-
// Partial pricing for sparse
|
|
1269
|
-
const batchSize = Math.min(500, Math.max(50, Math.floor(Math.sqrt(lastColumn))));
|
|
1270
|
-
let batchStart = 1;
|
|
1271
|
-
while (true) {
|
|
1272
|
-
const costRow = 0;
|
|
1273
|
-
let enteringColumn = 0;
|
|
1274
|
-
let enteringValue = precision;
|
|
1275
|
-
let isReducedCostNegative = false;
|
|
1276
|
-
// Partial pricing: scan columns in batches
|
|
1277
|
-
let batchesScanned = 0;
|
|
1278
|
-
const totalBatches = Math.ceil(lastColumn / batchSize);
|
|
1279
|
-
while (enteringColumn === 0 && batchesScanned < totalBatches) {
|
|
1280
|
-
const batchEnd = Math.min(batchStart + batchSize - 1, lastColumn);
|
|
1281
|
-
for (let c = batchStart; c <= batchEnd; c++) {
|
|
1282
|
-
if (c === rhsColumn)
|
|
1283
|
-
continue;
|
|
1284
|
-
reducedCost = sparse.get(costRow, c);
|
|
1285
|
-
unrestricted = this.unrestrictedVars[this.varIndexByCol[c]] === true;
|
|
1286
|
-
if (unrestricted && reducedCost < 0) {
|
|
1287
|
-
if (-reducedCost > enteringValue) {
|
|
1288
|
-
enteringValue = -reducedCost;
|
|
1289
|
-
enteringColumn = c;
|
|
1290
|
-
isReducedCostNegative = true;
|
|
1291
|
-
}
|
|
1292
|
-
continue;
|
|
1293
|
-
}
|
|
1294
|
-
if (reducedCost > enteringValue) {
|
|
1295
|
-
enteringValue = reducedCost;
|
|
1296
|
-
enteringColumn = c;
|
|
1297
|
-
isReducedCostNegative = false;
|
|
1298
|
-
}
|
|
1299
|
-
}
|
|
1300
|
-
batchStart = batchEnd >= lastColumn ? 1 : batchEnd + 1;
|
|
1301
|
-
batchesScanned++;
|
|
1302
|
-
}
|
|
1303
|
-
if (enteringColumn === 0) {
|
|
1304
|
-
this.setEvaluation();
|
|
1305
|
-
this.simplexIters += 1;
|
|
1306
|
-
return iterations;
|
|
1307
|
-
}
|
|
1308
|
-
// Find leaving row using ratio test
|
|
1309
|
-
let leavingRow = 0;
|
|
1310
|
-
let minQuotient = Infinity;
|
|
1311
|
-
// Get entering column as sparse
|
|
1312
|
-
const enteringColData = sparse.getColumn(enteringColumn);
|
|
1313
|
-
for (let i = 0; i < enteringColData.length; i++) {
|
|
1314
|
-
const r = enteringColData.indices[i];
|
|
1315
|
-
if (r === 0)
|
|
1316
|
-
continue; // Skip cost row
|
|
1317
|
-
const colValue = enteringColData.values[i];
|
|
1318
|
-
if (colValue >= -precision && colValue <= precision)
|
|
1319
|
-
continue;
|
|
1320
|
-
const rhsValue = sparse.get(r, rhsColumn);
|
|
1321
|
-
if (colValue > 0 && precision > rhsValue && rhsValue > -precision) {
|
|
1322
|
-
minQuotient = 0;
|
|
1323
|
-
leavingRow = r;
|
|
1324
|
-
break;
|
|
1325
|
-
}
|
|
1326
|
-
const quotient = isReducedCostNegative ? -rhsValue / colValue : rhsValue / colValue;
|
|
1327
|
-
if (quotient > precision && minQuotient > quotient) {
|
|
1328
|
-
minQuotient = quotient;
|
|
1329
|
-
leavingRow = r;
|
|
1330
|
-
}
|
|
1331
|
-
}
|
|
1332
|
-
if (minQuotient === Infinity) {
|
|
1333
|
-
this.evaluation = -Infinity;
|
|
1334
|
-
this.bounded = false;
|
|
1335
|
-
this.unboundedVarIndex = this.varIndexByCol[enteringColumn];
|
|
1336
|
-
return iterations;
|
|
1337
|
-
}
|
|
1338
|
-
if (debugCheckForCycles) {
|
|
1339
|
-
varIndexesCycle.push([
|
|
1340
|
-
this.varIndexByRow[leavingRow],
|
|
1341
|
-
this.varIndexByCol[enteringColumn],
|
|
1342
|
-
]);
|
|
1343
|
-
const cycleData = this.checkForCycles(varIndexesCycle);
|
|
1344
|
-
if (cycleData.length > 0) {
|
|
1345
|
-
this.model.messages.push("Cycle in phase 2");
|
|
1346
|
-
this.feasible = false;
|
|
1347
|
-
return iterations;
|
|
1348
|
-
}
|
|
1349
|
-
}
|
|
1350
|
-
// Perform pivot
|
|
1351
|
-
sparsePivot.call(this, sparse, leavingRow, enteringColumn);
|
|
1352
|
-
iterations += 1;
|
|
1353
|
-
}
|
|
1354
|
-
}
|
|
1355
|
-
/**
|
|
1356
|
-
* Sparse pivot operation
|
|
1357
|
-
*/
|
|
1358
|
-
function sparsePivot(sparse, pivotRowIndex, pivotColumnIndex) {
|
|
1359
|
-
// Update basis tracking
|
|
1360
|
-
const leavingBasicIndex = this.varIndexByRow[pivotRowIndex];
|
|
1361
|
-
const enteringBasicIndex = this.varIndexByCol[pivotColumnIndex];
|
|
1362
|
-
this.varIndexByRow[pivotRowIndex] = enteringBasicIndex;
|
|
1363
|
-
this.varIndexByCol[pivotColumnIndex] = leavingBasicIndex;
|
|
1364
|
-
this.rowByVarIndex[enteringBasicIndex] = pivotRowIndex;
|
|
1365
|
-
this.rowByVarIndex[leavingBasicIndex] = -1;
|
|
1366
|
-
this.colByVarIndex[enteringBasicIndex] = -1;
|
|
1367
|
-
this.colByVarIndex[leavingBasicIndex] = pivotColumnIndex;
|
|
1368
|
-
// Perform sparse pivot
|
|
1369
|
-
sparse.pivot(pivotRowIndex, pivotColumnIndex);
|
|
1370
|
-
// Handle optional objectives (these are stored separately, not in sparse matrix)
|
|
1371
|
-
const optionalObjectives = this.optionalObjectives;
|
|
1372
|
-
const nOptionalObjectives = optionalObjectives.length;
|
|
1373
|
-
if (nOptionalObjectives > 0) {
|
|
1374
|
-
const pivotVal = sparse.get(pivotRowIndex, pivotColumnIndex);
|
|
1375
|
-
const pivotRowData = sparse.getRow(pivotRowIndex);
|
|
1376
|
-
for (let o = 0; o < nOptionalObjectives; o++) {
|
|
1377
|
-
const reducedCosts = optionalObjectives[o].reducedCosts;
|
|
1378
|
-
const coefficient = reducedCosts[pivotColumnIndex];
|
|
1379
|
-
if (coefficient !== 0) {
|
|
1380
|
-
for (let i = 0; i < pivotRowData.length; i++) {
|
|
1381
|
-
const c = pivotRowData.indices[i];
|
|
1382
|
-
const v0 = pivotRowData.values[i];
|
|
1383
|
-
if (v0 !== 0) {
|
|
1384
|
-
reducedCosts[c] = reducedCosts[c] - coefficient * v0;
|
|
1385
|
-
}
|
|
1386
|
-
}
|
|
1387
|
-
reducedCosts[pivotColumnIndex] = -coefficient * pivotVal;
|
|
1388
|
-
}
|
|
1389
|
-
}
|
|
1390
|
-
}
|
|
1391
|
-
}
|
|
1392
|
-
/**
|
|
1393
|
-
* Threshold for using sparse (total cells)
|
|
1394
|
-
* Currently disabled - CSC format overhead exceeds benefits.
|
|
1395
|
-
* A dual-indexed (CSC+CSR) approach would be needed for efficient sparse simplex.
|
|
1396
|
-
*/
|
|
1397
|
-
const SPARSE_THRESHOLD = Infinity; // Disabled
|
|
1398
|
-
/**
|
|
1399
|
-
* Density threshold - only use sparse if below this density
|
|
1400
|
-
*/
|
|
1401
|
-
const DENSITY_THRESHOLD = 0.1; // 10%
|
|
1402
|
-
/**
|
|
1403
|
-
* Check if sparse mode should be used for this tableau
|
|
1404
|
-
*/
|
|
1405
|
-
function shouldUseSparse(tableau) {
|
|
1406
|
-
const totalCells = tableau.width * tableau.height;
|
|
1407
|
-
if (totalCells < SPARSE_THRESHOLD) {
|
|
1408
|
-
return false;
|
|
1409
|
-
}
|
|
1410
|
-
// Estimate density from constraint rows (skip row 0 which is cost row and often dense)
|
|
1411
|
-
const matrix = tableau.matrix;
|
|
1412
|
-
const width = tableau.width;
|
|
1413
|
-
const startRow = 1;
|
|
1414
|
-
const sampleRows = Math.min(20, tableau.height - 1);
|
|
1415
|
-
let nonZeros = 0;
|
|
1416
|
-
for (let r = startRow; r < startRow + sampleRows; r++) {
|
|
1417
|
-
const rowOffset = r * width;
|
|
1418
|
-
for (let c = 0; c < width; c++) {
|
|
1419
|
-
if (matrix[rowOffset + c] !== 0) {
|
|
1420
|
-
nonZeros++;
|
|
1421
|
-
}
|
|
1422
|
-
}
|
|
1423
|
-
}
|
|
1424
|
-
const estimatedDensity = nonZeros / (sampleRows * width);
|
|
1425
|
-
return estimatedDensity < DENSITY_THRESHOLD;
|
|
1426
|
-
}
|
|
1427
|
-
|
|
1428
958
|
class Variable {
|
|
1429
959
|
constructor(id, cost, index, priority) {
|
|
1430
960
|
this.id = id;
|
|
@@ -1509,6 +1039,7 @@ class Constraint {
|
|
|
1509
1039
|
setVariableCoefficient(newCoefficient, variable) {
|
|
1510
1040
|
const varIndex = variable.index;
|
|
1511
1041
|
if (varIndex === -1) {
|
|
1042
|
+
// eslint-disable-next-line no-console
|
|
1512
1043
|
console.warn("[Constraint.setVariableCoefficient] Trying to change coefficient of inexistant variable.");
|
|
1513
1044
|
return;
|
|
1514
1045
|
}
|
|
@@ -1604,33 +1135,39 @@ function addCutConstraints(cutConstraints) {
|
|
|
1604
1135
|
const matrix = this.matrix;
|
|
1605
1136
|
this.height = heightWithCuts;
|
|
1606
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;
|
|
1607
1144
|
for (let h = 0; h < nCutConstraints; h += 1) {
|
|
1608
1145
|
const cut = cutConstraints[h];
|
|
1609
1146
|
const cutRow = height + h;
|
|
1610
1147
|
const cutRowOffset = cutRow * width;
|
|
1611
1148
|
const sign = cut.type === "min" ? -1 : 1;
|
|
1612
1149
|
const varIndex = cut.varIndex;
|
|
1613
|
-
let varRowIndex =
|
|
1150
|
+
let varRowIndex = rowByVarIndex[varIndex];
|
|
1614
1151
|
if (varRowIndex === -1) {
|
|
1615
|
-
matrix[cutRowOffset +
|
|
1152
|
+
matrix[cutRowOffset + rhsColumn] = sign * cut.value;
|
|
1616
1153
|
for (let c = 1; c <= lastColumn; c += 1) {
|
|
1617
1154
|
matrix[cutRowOffset + c] = 0;
|
|
1618
1155
|
}
|
|
1619
|
-
matrix[cutRowOffset +
|
|
1156
|
+
matrix[cutRowOffset + colByVarIndex[varIndex]] = sign;
|
|
1620
1157
|
}
|
|
1621
1158
|
else {
|
|
1622
1159
|
const varRowOffset = varRowIndex * width;
|
|
1623
|
-
const varValue = matrix[varRowOffset +
|
|
1624
|
-
matrix[cutRowOffset +
|
|
1160
|
+
const varValue = matrix[varRowOffset + rhsColumn];
|
|
1161
|
+
matrix[cutRowOffset + rhsColumn] = sign * (cut.value - varValue);
|
|
1625
1162
|
for (let c = 1; c <= lastColumn; c += 1) {
|
|
1626
1163
|
matrix[cutRowOffset + c] = -sign * matrix[varRowOffset + c];
|
|
1627
1164
|
}
|
|
1628
1165
|
}
|
|
1629
1166
|
varRowIndex = this.getNewElementIndex();
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1167
|
+
varIndexByRow[cutRow] = varRowIndex;
|
|
1168
|
+
rowByVarIndex[varRowIndex] = cutRow;
|
|
1169
|
+
colByVarIndex[varRowIndex] = -1;
|
|
1170
|
+
variablesPerIndex[varRowIndex] = new SlackVariable("s" + varRowIndex, varRowIndex);
|
|
1634
1171
|
this.nVars += 1;
|
|
1635
1172
|
}
|
|
1636
1173
|
}
|
|
@@ -1669,18 +1206,23 @@ function addLowerBoundMIRCut(rowIndex) {
|
|
|
1669
1206
|
this.rowByVarIndex[slackVarIndex] = height;
|
|
1670
1207
|
this.colByVarIndex[slackVarIndex] = -1;
|
|
1671
1208
|
this.variablesPerIndex[slackVarIndex] = new SlackVariable("s" + slackVarIndex, slackVarIndex);
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
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]];
|
|
1675
1218
|
const coefficient = mat[cutRowOffset + colIndex];
|
|
1676
1219
|
if (variable !== undefined && variable.isInteger) {
|
|
1677
|
-
const
|
|
1678
|
-
|
|
1679
|
-
(1 - fractionalPart);
|
|
1220
|
+
const floorCoeff = Math.floor(coefficient);
|
|
1221
|
+
const termCoeff = floorCoeff + Math.max(0, coefficient - floorCoeff - fractionalPart) / oneMinusFrac;
|
|
1680
1222
|
mat[newRowOffset + colIndex] = termCoeff;
|
|
1681
1223
|
}
|
|
1682
1224
|
else {
|
|
1683
|
-
mat[newRowOffset + colIndex] = Math.min(0, coefficient /
|
|
1225
|
+
mat[newRowOffset + colIndex] = Math.min(0, coefficient / oneMinusFrac);
|
|
1684
1226
|
}
|
|
1685
1227
|
}
|
|
1686
1228
|
for (let c = 0; c < width; c += 1) {
|
|
@@ -1723,9 +1265,15 @@ function addUpperBoundMIRCut(rowIndex) {
|
|
|
1723
1265
|
this.rowByVarIndex[slackVarIndex] = height;
|
|
1724
1266
|
this.colByVarIndex[slackVarIndex] = -1;
|
|
1725
1267
|
this.variablesPerIndex[slackVarIndex] = new SlackVariable("s" + slackVarIndex, slackVarIndex);
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
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]];
|
|
1729
1277
|
const coefficient = mat[cutRowOffset + colIndex];
|
|
1730
1278
|
const termCoeff = coefficient - Math.floor(coefficient);
|
|
1731
1279
|
if (variable !== undefined && variable.isInteger) {
|
|
@@ -1736,9 +1284,7 @@ function addUpperBoundMIRCut(rowIndex) {
|
|
|
1736
1284
|
}
|
|
1737
1285
|
else {
|
|
1738
1286
|
mat[newRowOffset + colIndex] =
|
|
1739
|
-
coefficient >= 0
|
|
1740
|
-
? -coefficient
|
|
1741
|
-
: (coefficient * fractionalPart) / (1 - fractionalPart);
|
|
1287
|
+
coefficient >= 0 ? -coefficient : (coefficient * fractionalPart) / oneMinusFrac;
|
|
1742
1288
|
}
|
|
1743
1289
|
}
|
|
1744
1290
|
return true;
|
|
@@ -2113,12 +1659,15 @@ function isIntegral() {
|
|
|
2113
1659
|
const rhsColumn = this.rhsColumn;
|
|
2114
1660
|
const integerVariables = this.model.integerVariables;
|
|
2115
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;
|
|
2116
1665
|
for (let v = 0; v < nIntegerVars; v++) {
|
|
2117
1666
|
const varIndex = integerVariables[v].index;
|
|
2118
|
-
const row =
|
|
1667
|
+
const row = rowByVarIndex[varIndex];
|
|
2119
1668
|
if (row !== -1) {
|
|
2120
1669
|
const value = matrix[row * width + rhsColumn];
|
|
2121
|
-
if (Math.abs(value - Math.round(value)) >
|
|
1670
|
+
if (Math.abs(value - Math.round(value)) > precision) {
|
|
2122
1671
|
return false;
|
|
2123
1672
|
}
|
|
2124
1673
|
}
|
|
@@ -2134,12 +1683,17 @@ function computeFractionalVolume(ignoreIntegerValues) {
|
|
|
2134
1683
|
const width = this.width;
|
|
2135
1684
|
const matrix = this.matrix;
|
|
2136
1685
|
const rhsColumn = this.rhsColumn;
|
|
2137
|
-
|
|
2138
|
-
|
|
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]];
|
|
2139
1693
|
if (variable !== undefined && variable.isInteger) {
|
|
2140
1694
|
const value = matrix[r * width + rhsColumn];
|
|
2141
1695
|
const distance = Math.abs(value);
|
|
2142
|
-
if (Math.min(distance - Math.floor(distance), Math.floor(distance + 1)) <
|
|
1696
|
+
if (Math.min(distance - Math.floor(distance), Math.floor(distance + 1)) < precision) {
|
|
2143
1697
|
if (ignoreIntegerValues !== true) {
|
|
2144
1698
|
return 0;
|
|
2145
1699
|
}
|
|
@@ -2168,9 +1722,11 @@ function getMostFractionalVar() {
|
|
|
2168
1722
|
const rhsColumn = this.rhsColumn;
|
|
2169
1723
|
const integerVars = this.model.integerVariables;
|
|
2170
1724
|
const nIntegerVars = integerVars.length;
|
|
1725
|
+
// Cache array reference for faster access in hot loop
|
|
1726
|
+
const rowByVarIndex = this.rowByVarIndex;
|
|
2171
1727
|
for (let v = 0; v < nIntegerVars; v += 1) {
|
|
2172
1728
|
const varIndex = integerVars[v].index;
|
|
2173
|
-
const row =
|
|
1729
|
+
const row = rowByVarIndex[varIndex];
|
|
2174
1730
|
if (row !== -1) {
|
|
2175
1731
|
const varValue = matrix[row * width + rhsColumn];
|
|
2176
1732
|
const fraction = Math.abs(varValue - Math.round(varValue));
|
|
@@ -2387,13 +1943,7 @@ class Tableau {
|
|
|
2387
1943
|
}
|
|
2388
1944
|
// ========== Core Simplex Operations ==========
|
|
2389
1945
|
simplex() {
|
|
2390
|
-
|
|
2391
|
-
if (shouldUseSparse(this)) {
|
|
2392
|
-
sparseSimplex.call(this);
|
|
2393
|
-
}
|
|
2394
|
-
else {
|
|
2395
|
-
simplex.call(this);
|
|
2396
|
-
}
|
|
1946
|
+
simplex.call(this);
|
|
2397
1947
|
return this;
|
|
2398
1948
|
}
|
|
2399
1949
|
phase1() {
|
|
@@ -3071,6 +2621,7 @@ class Model {
|
|
|
3071
2621
|
_removeConstraint(constraint) {
|
|
3072
2622
|
const idx = this.constraints.indexOf(constraint);
|
|
3073
2623
|
if (idx === -1) {
|
|
2624
|
+
// eslint-disable-next-line no-console
|
|
3074
2625
|
console.warn("[Model.removeConstraint] Constraint not present in model");
|
|
3075
2626
|
return;
|
|
3076
2627
|
}
|
|
@@ -3100,6 +2651,7 @@ class Model {
|
|
|
3100
2651
|
removeVariable(variable) {
|
|
3101
2652
|
const idx = this.variables.indexOf(variable);
|
|
3102
2653
|
if (idx === -1) {
|
|
2654
|
+
// eslint-disable-next-line no-console
|
|
3103
2655
|
console.warn("[Model.removeVariable] Variable not present in model");
|
|
3104
2656
|
return;
|
|
3105
2657
|
}
|
|
@@ -3219,11 +2771,20 @@ class Model {
|
|
|
3219
2771
|
const unrestrictedVarIds = jsonModel.unrestricted || {};
|
|
3220
2772
|
// Instantiating variables and constraint terms
|
|
3221
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);
|
|
3222
2779
|
for (let v = 0; v < nVariables; v += 1) {
|
|
3223
2780
|
// Creation of the variables
|
|
3224
2781
|
const variableId = variableIds[v];
|
|
3225
2782
|
const variableConstraints = variables[variableId];
|
|
3226
|
-
const cost =
|
|
2783
|
+
const cost = objectiveIsVariable
|
|
2784
|
+
? variableId === objectiveName
|
|
2785
|
+
? 1
|
|
2786
|
+
: 0
|
|
2787
|
+
: variableConstraints[objectiveName] || 0;
|
|
3227
2788
|
const isBinary = !!binaryVarIds[variableId];
|
|
3228
2789
|
const isInteger = !!integerVarIds[variableId] || isBinary;
|
|
3229
2790
|
const isUnrestricted = !!unrestrictedVarIds[variableId];
|
|
@@ -3578,7 +3139,7 @@ function Polyopt(solver, model) {
|
|
|
3578
3139
|
}
|
|
3579
3140
|
}
|
|
3580
3141
|
// Strip metadata so each Pareto vertex only contains value-bearing fields.
|
|
3581
|
-
const { feasible, result, bounded, ...paretoPayload } = solution;
|
|
3142
|
+
const { feasible: _feasible, result: _result, bounded: _bounded, ...paretoPayload } = solution;
|
|
3582
3143
|
paretoVertices.push(paretoPayload);
|
|
3583
3144
|
}
|
|
3584
3145
|
// Derive equality constraints that represent the averaged objective values.
|
|
@@ -3956,7 +3517,7 @@ function createEnhancedBranchAndCutService(options = {}) {
|
|
|
3956
3517
|
// Product score (like SCIP's default)
|
|
3957
3518
|
return Math.max(upEstimate, 1e-6) * Math.max(downEstimate, 1e-6);
|
|
3958
3519
|
};
|
|
3959
|
-
const selectBranchingVariable = (tableau,
|
|
3520
|
+
const selectBranchingVariable = (tableau, _currentEval) => {
|
|
3960
3521
|
const width = tableau.width;
|
|
3961
3522
|
const matrix = tableau.matrix;
|
|
3962
3523
|
const rhsColumn = tableau.rhsColumn;
|
|
@@ -4100,7 +3661,7 @@ function createEnhancedBranchAndCutService(options = {}) {
|
|
|
4100
3661
|
else {
|
|
4101
3662
|
break;
|
|
4102
3663
|
}
|
|
4103
|
-
if (activeBranch.relaxedEvaluation
|
|
3664
|
+
if (activeBranch.relaxedEvaluation >= bestEvaluation) {
|
|
4104
3665
|
continue;
|
|
4105
3666
|
}
|
|
4106
3667
|
const cuts = activeBranch.cuts;
|
|
@@ -4454,7 +4015,7 @@ function createIncrementalBranchAndCutService(options = {}) {
|
|
|
4454
4015
|
else {
|
|
4455
4016
|
break;
|
|
4456
4017
|
}
|
|
4457
|
-
if (activeBranch.relaxedEvaluation
|
|
4018
|
+
if (activeBranch.relaxedEvaluation >= bestEvaluation) {
|
|
4458
4019
|
continue;
|
|
4459
4020
|
}
|
|
4460
4021
|
const parentEval = tableau.evaluation;
|