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.cjs
CHANGED
|
@@ -164,15 +164,16 @@ function createBranchAndCutService() {
|
|
|
164
164
|
tableau.addCutConstraints(branchingCuts);
|
|
165
165
|
tableau.simplex();
|
|
166
166
|
if ((_a = tableau.model) === null || _a === void 0 ? void 0 : _a.useMIRCuts) {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
167
|
+
// Optimization: reuse previous "after" as next "before" to avoid redundant computation
|
|
168
|
+
let fractionalVolume = tableau.computeFractionalVolume(true);
|
|
169
|
+
while (fractionalVolume > 0) {
|
|
170
170
|
tableau.applyMIRCuts();
|
|
171
171
|
tableau.simplex();
|
|
172
172
|
const fractionalVolumeAfter = tableau.computeFractionalVolume(true);
|
|
173
|
-
if (fractionalVolumeAfter >= 0.9 *
|
|
174
|
-
|
|
173
|
+
if (fractionalVolumeAfter >= 0.9 * fractionalVolume) {
|
|
174
|
+
break;
|
|
175
175
|
}
|
|
176
|
+
fractionalVolume = fractionalVolumeAfter;
|
|
176
177
|
}
|
|
177
178
|
}
|
|
178
179
|
};
|
|
@@ -211,7 +212,7 @@ function createBranchAndCutService() {
|
|
|
211
212
|
}
|
|
212
213
|
}
|
|
213
214
|
const activeBranch = branches.pop();
|
|
214
|
-
if (activeBranch.relaxedEvaluation
|
|
215
|
+
if (activeBranch.relaxedEvaluation >= bestEvaluation) {
|
|
215
216
|
continue;
|
|
216
217
|
}
|
|
217
218
|
const cuts = activeBranch.cuts;
|
|
@@ -449,13 +450,32 @@ function phase1() {
|
|
|
449
450
|
const unrestrictedVars = this.unrestrictedVars;
|
|
450
451
|
const varIndexByRow = this.varIndexByRow;
|
|
451
452
|
const varIndexByCol = this.varIndexByCol;
|
|
452
|
-
|
|
453
|
+
// Anti-cycling: on degenerate problems, the max-quotient pivot rule
|
|
454
|
+
// can oscillate, corrupting the matrix. We detect stalling early
|
|
455
|
+
// (after SAVE_THRESHOLD iterations without RHS improvement) and save
|
|
456
|
+
// the matrix state. If still stuck after maxQuotientLimit iterations,
|
|
457
|
+
// we restore the clean matrix and switch to Bland's rule. The lazy
|
|
458
|
+
// save avoids the copy cost on the vast majority of phase1 calls
|
|
459
|
+
// that converge quickly.
|
|
460
|
+
const SAVE_THRESHOLD = 10;
|
|
461
|
+
const maxQuotientLimit = Math.max(lastRow, lastColumn);
|
|
453
462
|
let iterations = 0;
|
|
463
|
+
let useBland = false;
|
|
464
|
+
let initialRHS = -Infinity;
|
|
465
|
+
let savedMatrix = null;
|
|
466
|
+
let savedVarIndexByRow = null;
|
|
467
|
+
let savedVarIndexByCol = null;
|
|
468
|
+
let savedRowByVarIndex = null;
|
|
469
|
+
let savedColByVarIndex = null;
|
|
454
470
|
while (true) {
|
|
455
|
-
// Find leaving row (most negative RHS)
|
|
471
|
+
// Find leaving row (most negative RHS among restricted basic vars).
|
|
472
|
+
// Unrestricted variables can validly have negative values, so rows
|
|
473
|
+
// where the basic variable is unrestricted are not infeasible.
|
|
456
474
|
let leavingRowIndex = 0;
|
|
457
475
|
let rhsValue = negPrecision;
|
|
458
476
|
for (let r = 1; r <= lastRow; r++) {
|
|
477
|
+
if (unrestrictedVars[varIndexByRow[r]] === true)
|
|
478
|
+
continue;
|
|
459
479
|
const value = matrix[r * width + rhsColumn];
|
|
460
480
|
if (value < rhsValue) {
|
|
461
481
|
rhsValue = value;
|
|
@@ -466,18 +486,95 @@ function phase1() {
|
|
|
466
486
|
this.feasible = true;
|
|
467
487
|
return iterations;
|
|
468
488
|
}
|
|
469
|
-
//
|
|
489
|
+
// Detect non-convergence and apply anti-cycling.
|
|
490
|
+
if (!useBland && iterations > 0 && rhsValue <= initialRHS) {
|
|
491
|
+
if (iterations >= SAVE_THRESHOLD && savedMatrix === null) {
|
|
492
|
+
// Stalling detected: save matrix for potential rollback
|
|
493
|
+
savedMatrix = matrix.slice();
|
|
494
|
+
savedVarIndexByRow = varIndexByRow.slice();
|
|
495
|
+
savedVarIndexByCol = varIndexByCol.slice();
|
|
496
|
+
savedRowByVarIndex = this.rowByVarIndex.slice();
|
|
497
|
+
savedColByVarIndex = this.colByVarIndex.slice();
|
|
498
|
+
}
|
|
499
|
+
if (iterations >= maxQuotientLimit) {
|
|
500
|
+
// Still stuck after generous budget: restore and switch
|
|
501
|
+
useBland = true;
|
|
502
|
+
if (savedMatrix) {
|
|
503
|
+
matrix.set(savedMatrix);
|
|
504
|
+
for (let i = 0; i < savedVarIndexByRow.length; i++) {
|
|
505
|
+
varIndexByRow[i] = savedVarIndexByRow[i];
|
|
506
|
+
}
|
|
507
|
+
for (let i = 0; i < savedVarIndexByCol.length; i++) {
|
|
508
|
+
varIndexByCol[i] = savedVarIndexByCol[i];
|
|
509
|
+
}
|
|
510
|
+
for (let i = 0; i < savedRowByVarIndex.length; i++) {
|
|
511
|
+
this.rowByVarIndex[i] = savedRowByVarIndex[i];
|
|
512
|
+
}
|
|
513
|
+
for (let i = 0; i < savedColByVarIndex.length; i++) {
|
|
514
|
+
this.colByVarIndex[i] = savedColByVarIndex[i];
|
|
515
|
+
}
|
|
516
|
+
iterations = 0;
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
if (iterations === 0) {
|
|
522
|
+
initialRHS = rhsValue;
|
|
523
|
+
}
|
|
524
|
+
// Find entering column.
|
|
525
|
+
// Prefer columns with negative coefficient in leaving row (these directly
|
|
526
|
+
// fix the infeasibility by making RHS positive after pivot).
|
|
527
|
+
// Only fall back to unrestricted variables with positive coefficient when
|
|
528
|
+
// no negative coefficient column exists - this "swaps" the infeasibility
|
|
529
|
+
// to an unrestricted variable (which is allowed to be negative).
|
|
470
530
|
let enteringColumn = 0;
|
|
471
|
-
let maxQuotient = -Infinity;
|
|
472
531
|
const leavingRowOffset = leavingRowIndex * width;
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
const
|
|
478
|
-
if (
|
|
479
|
-
maxQuotient = quotient;
|
|
532
|
+
if (useBland) {
|
|
533
|
+
// Bland's rule: pick the first eligible column (smallest index).
|
|
534
|
+
// First pass: negative coefficients only (directly fix infeasibility)
|
|
535
|
+
for (let c = 1; c <= lastColumn; c++) {
|
|
536
|
+
const coefficient = matrix[leavingRowOffset + c];
|
|
537
|
+
if (coefficient < negPrecision) {
|
|
480
538
|
enteringColumn = c;
|
|
539
|
+
break;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
// Fallback: unrestricted with non-zero coefficient (swap infeasibility)
|
|
543
|
+
if (enteringColumn === 0) {
|
|
544
|
+
for (let c = 1; c <= lastColumn; c++) {
|
|
545
|
+
const coefficient = matrix[leavingRowOffset + c];
|
|
546
|
+
if (unrestrictedVars[varIndexByCol[c]] === true &&
|
|
547
|
+
(coefficient < negPrecision || coefficient > precision)) {
|
|
548
|
+
enteringColumn = c;
|
|
549
|
+
break;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
else {
|
|
555
|
+
// Max-quotient rule: faster in practice but can oscillate on
|
|
556
|
+
// degenerate problems.
|
|
557
|
+
// First pass: negative coefficients only
|
|
558
|
+
let maxQuotient = -Infinity;
|
|
559
|
+
for (let c = 1; c <= lastColumn; c++) {
|
|
560
|
+
const coefficient = matrix[leavingRowOffset + c];
|
|
561
|
+
if (coefficient < negPrecision) {
|
|
562
|
+
const quotient = -matrix[c] / coefficient;
|
|
563
|
+
if (maxQuotient < quotient) {
|
|
564
|
+
maxQuotient = quotient;
|
|
565
|
+
enteringColumn = c;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
// Fallback: unrestricted with non-zero coefficient
|
|
570
|
+
if (enteringColumn === 0) {
|
|
571
|
+
for (let c = 1; c <= lastColumn; c++) {
|
|
572
|
+
const coefficient = matrix[leavingRowOffset + c];
|
|
573
|
+
if (unrestrictedVars[varIndexByCol[c]] === true &&
|
|
574
|
+
(coefficient < negPrecision || coefficient > precision)) {
|
|
575
|
+
enteringColumn = c;
|
|
576
|
+
break;
|
|
577
|
+
}
|
|
481
578
|
}
|
|
482
579
|
}
|
|
483
580
|
}
|
|
@@ -519,6 +616,20 @@ function phase2() {
|
|
|
519
616
|
let iterations = 0;
|
|
520
617
|
let reducedCost;
|
|
521
618
|
let unrestricted;
|
|
619
|
+
// Anti-cycling for phase 2: if the objective stalls (no meaningful
|
|
620
|
+
// improvement over several hundred iterations), the simplex is
|
|
621
|
+
// cycling through degenerate vertices. Switch to Bland's rule which
|
|
622
|
+
// guarantees termination in exact arithmetic. In floating-point,
|
|
623
|
+
// even Bland's can cycle indefinitely, so we also impose a limit:
|
|
624
|
+
// if Bland's runs for lastRow iterations without improving the
|
|
625
|
+
// objective, we accept the current solution as optimal.
|
|
626
|
+
const PHASE2_WINDOW = 100;
|
|
627
|
+
const PHASE2_STALE_LIMIT = 5; // consecutive stale windows before switching
|
|
628
|
+
let useBland = false;
|
|
629
|
+
let windowStartObj = matrix[rhsColumn];
|
|
630
|
+
let staleWindows = 0;
|
|
631
|
+
let blandStartIter = 0;
|
|
632
|
+
let blandStartObj = 0;
|
|
522
633
|
// Partial pricing setup
|
|
523
634
|
// Batch size: use configured value or auto-compute (sqrt of columns, min 50, max 500)
|
|
524
635
|
const nColumns = lastColumn;
|
|
@@ -531,10 +642,65 @@ function phase2() {
|
|
|
531
642
|
if (nOptionalObjectives > 0) {
|
|
532
643
|
optionalCostsColumns = [];
|
|
533
644
|
}
|
|
645
|
+
// Detect degenerate cycling: check every WINDOW iterations
|
|
646
|
+
// whether the objective has made meaningful progress.
|
|
647
|
+
if (!useBland && iterations > 0 && iterations % PHASE2_WINDOW === 0) {
|
|
648
|
+
const currentObj = matrix[rhsColumn];
|
|
649
|
+
const delta = Math.abs(currentObj - windowStartObj);
|
|
650
|
+
const scale = Math.max(1, Math.abs(windowStartObj));
|
|
651
|
+
if (delta / scale < 1e-10) {
|
|
652
|
+
staleWindows++;
|
|
653
|
+
if (staleWindows >= PHASE2_STALE_LIMIT) {
|
|
654
|
+
useBland = true;
|
|
655
|
+
blandStartIter = iterations;
|
|
656
|
+
blandStartObj = currentObj;
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
else {
|
|
660
|
+
staleWindows = 0;
|
|
661
|
+
}
|
|
662
|
+
windowStartObj = currentObj;
|
|
663
|
+
}
|
|
664
|
+
// Bland's termination: in floating-point arithmetic, Bland's rule
|
|
665
|
+
// can cycle through degenerate bases indefinitely. If it hasn't
|
|
666
|
+
// improved the objective after lastRow pivots, accept the current
|
|
667
|
+
// solution (reduced costs are within floating-point noise of zero).
|
|
668
|
+
if (useBland && iterations - blandStartIter > lastRow) {
|
|
669
|
+
const currentObj = matrix[rhsColumn];
|
|
670
|
+
const delta = Math.abs(currentObj - blandStartObj);
|
|
671
|
+
const scale = Math.max(1, Math.abs(blandStartObj));
|
|
672
|
+
if (delta / scale < 1e-10) {
|
|
673
|
+
this.setEvaluation();
|
|
674
|
+
this.simplexIters += 1;
|
|
675
|
+
return iterations;
|
|
676
|
+
}
|
|
677
|
+
// Objective did improve; reset the counter
|
|
678
|
+
blandStartIter = iterations;
|
|
679
|
+
blandStartObj = currentObj;
|
|
680
|
+
}
|
|
534
681
|
let enteringColumn = 0;
|
|
535
682
|
let enteringValue = precision;
|
|
536
683
|
let isReducedCostNegative = false;
|
|
537
|
-
if (
|
|
684
|
+
if (useBland) {
|
|
685
|
+
// Bland's rule: pick first eligible column (smallest index)
|
|
686
|
+
for (let c = 1; c <= lastColumn; c++) {
|
|
687
|
+
reducedCost = matrix[c];
|
|
688
|
+
unrestricted = unrestrictedVars[varIndexByCol[c]] === true;
|
|
689
|
+
if (unrestricted && reducedCost < 0) {
|
|
690
|
+
enteringColumn = c;
|
|
691
|
+
enteringValue = -reducedCost;
|
|
692
|
+
isReducedCostNegative = true;
|
|
693
|
+
break;
|
|
694
|
+
}
|
|
695
|
+
if (reducedCost > precision) {
|
|
696
|
+
enteringColumn = c;
|
|
697
|
+
enteringValue = reducedCost;
|
|
698
|
+
isReducedCostNegative = false;
|
|
699
|
+
break;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
else if (usePartialPricing) {
|
|
538
704
|
// Partial pricing: scan columns in batches
|
|
539
705
|
const startBatch = this.pricingBatchStart;
|
|
540
706
|
let batchesScanned = 0;
|
|
@@ -794,642 +960,6 @@ function checkForCycles(varIndexes) {
|
|
|
794
960
|
return [];
|
|
795
961
|
}
|
|
796
962
|
|
|
797
|
-
/**
|
|
798
|
-
* @file src/tableau/sparse-matrix.ts
|
|
799
|
-
* @description Compressed Sparse Column (CSC) matrix implementation
|
|
800
|
-
*
|
|
801
|
-
* Optimized for column operations which are frequent in the simplex algorithm.
|
|
802
|
-
* Provides O(nnz) pivot operations instead of O(n*m) for dense matrices.
|
|
803
|
-
*
|
|
804
|
-
* CSC Storage Format:
|
|
805
|
-
* - values: non-zero values stored column by column
|
|
806
|
-
* - rowIndices: row index for each value
|
|
807
|
-
* - colPointers: starting index in values/rowIndices for each column
|
|
808
|
-
*
|
|
809
|
-
* Column c's entries are at indices colPointers[c] to colPointers[c+1]-1
|
|
810
|
-
*/
|
|
811
|
-
class SparseMatrix {
|
|
812
|
-
constructor(nRows, nCols, precision = 1e-12) {
|
|
813
|
-
this.nRows = nRows;
|
|
814
|
-
this.nCols = nCols;
|
|
815
|
-
this.precision = precision;
|
|
816
|
-
// Initialize empty sparse matrix
|
|
817
|
-
this.values = [];
|
|
818
|
-
this.rowIndices = [];
|
|
819
|
-
this.colPointers = new Array(nCols + 1).fill(0);
|
|
820
|
-
// Workspace for row/column operations
|
|
821
|
-
this.workVector = new Float64Array(Math.max(nRows, nCols));
|
|
822
|
-
this.workIndices = [];
|
|
823
|
-
}
|
|
824
|
-
/**
|
|
825
|
-
* Create sparse matrix from dense Float64Array
|
|
826
|
-
*/
|
|
827
|
-
static fromDense(dense, width, height, precision = 1e-12) {
|
|
828
|
-
const sparse = new SparseMatrix(height, width, precision);
|
|
829
|
-
const values = [];
|
|
830
|
-
const rowIndices = [];
|
|
831
|
-
const colPointers = [0];
|
|
832
|
-
// Build CSC by iterating column by column
|
|
833
|
-
for (let c = 0; c < width; c++) {
|
|
834
|
-
for (let r = 0; r < height; r++) {
|
|
835
|
-
const val = dense[r * width + c];
|
|
836
|
-
if (val < -precision || val > precision) {
|
|
837
|
-
values.push(val);
|
|
838
|
-
rowIndices.push(r);
|
|
839
|
-
}
|
|
840
|
-
}
|
|
841
|
-
colPointers.push(values.length);
|
|
842
|
-
}
|
|
843
|
-
sparse.values = values;
|
|
844
|
-
sparse.rowIndices = rowIndices;
|
|
845
|
-
sparse.colPointers = colPointers;
|
|
846
|
-
return sparse;
|
|
847
|
-
}
|
|
848
|
-
/**
|
|
849
|
-
* Convert to dense Float64Array
|
|
850
|
-
*/
|
|
851
|
-
toDense() {
|
|
852
|
-
const dense = new Float64Array(this.nRows * this.nCols);
|
|
853
|
-
for (let c = 0; c < this.nCols; c++) {
|
|
854
|
-
const start = this.colPointers[c];
|
|
855
|
-
const end = this.colPointers[c + 1];
|
|
856
|
-
for (let i = start; i < end; i++) {
|
|
857
|
-
const r = this.rowIndices[i];
|
|
858
|
-
dense[r * this.nCols + c] = this.values[i];
|
|
859
|
-
}
|
|
860
|
-
}
|
|
861
|
-
return dense;
|
|
862
|
-
}
|
|
863
|
-
/**
|
|
864
|
-
* Get value at (row, col)
|
|
865
|
-
*/
|
|
866
|
-
get(row, col) {
|
|
867
|
-
const start = this.colPointers[col];
|
|
868
|
-
const end = this.colPointers[col + 1];
|
|
869
|
-
// Binary search for row in this column
|
|
870
|
-
let lo = start;
|
|
871
|
-
let hi = end;
|
|
872
|
-
while (lo < hi) {
|
|
873
|
-
const mid = (lo + hi) >>> 1;
|
|
874
|
-
if (this.rowIndices[mid] < row) {
|
|
875
|
-
lo = mid + 1;
|
|
876
|
-
}
|
|
877
|
-
else {
|
|
878
|
-
hi = mid;
|
|
879
|
-
}
|
|
880
|
-
}
|
|
881
|
-
if (lo < end && this.rowIndices[lo] === row) {
|
|
882
|
-
return this.values[lo];
|
|
883
|
-
}
|
|
884
|
-
return 0;
|
|
885
|
-
}
|
|
886
|
-
/**
|
|
887
|
-
* Set value at (row, col)
|
|
888
|
-
* Handles insertion and removal of entries
|
|
889
|
-
*/
|
|
890
|
-
set(row, col, value) {
|
|
891
|
-
const start = this.colPointers[col];
|
|
892
|
-
const end = this.colPointers[col + 1];
|
|
893
|
-
const isZero = value >= -this.precision && value <= this.precision;
|
|
894
|
-
// Binary search for insertion point
|
|
895
|
-
let lo = start;
|
|
896
|
-
let hi = end;
|
|
897
|
-
while (lo < hi) {
|
|
898
|
-
const mid = (lo + hi) >>> 1;
|
|
899
|
-
if (this.rowIndices[mid] < row) {
|
|
900
|
-
lo = mid + 1;
|
|
901
|
-
}
|
|
902
|
-
else {
|
|
903
|
-
hi = mid;
|
|
904
|
-
}
|
|
905
|
-
}
|
|
906
|
-
const exists = lo < end && this.rowIndices[lo] === row;
|
|
907
|
-
if (exists) {
|
|
908
|
-
if (isZero) {
|
|
909
|
-
// Remove existing entry
|
|
910
|
-
this.values.splice(lo, 1);
|
|
911
|
-
this.rowIndices.splice(lo, 1);
|
|
912
|
-
// Update column pointers
|
|
913
|
-
for (let c = col + 1; c <= this.nCols; c++) {
|
|
914
|
-
this.colPointers[c]--;
|
|
915
|
-
}
|
|
916
|
-
}
|
|
917
|
-
else {
|
|
918
|
-
// Update existing entry
|
|
919
|
-
this.values[lo] = value;
|
|
920
|
-
}
|
|
921
|
-
}
|
|
922
|
-
else if (!isZero) {
|
|
923
|
-
// Insert new entry
|
|
924
|
-
this.values.splice(lo, 0, value);
|
|
925
|
-
this.rowIndices.splice(lo, 0, row);
|
|
926
|
-
// Update column pointers
|
|
927
|
-
for (let c = col + 1; c <= this.nCols; c++) {
|
|
928
|
-
this.colPointers[c]++;
|
|
929
|
-
}
|
|
930
|
-
}
|
|
931
|
-
}
|
|
932
|
-
/**
|
|
933
|
-
* Get column as sparse vector
|
|
934
|
-
*/
|
|
935
|
-
getColumn(col) {
|
|
936
|
-
const start = this.colPointers[col];
|
|
937
|
-
const end = this.colPointers[col + 1];
|
|
938
|
-
const len = end - start;
|
|
939
|
-
return {
|
|
940
|
-
indices: this.rowIndices.slice(start, end),
|
|
941
|
-
values: this.values.slice(start, end),
|
|
942
|
-
length: len,
|
|
943
|
-
};
|
|
944
|
-
}
|
|
945
|
-
/**
|
|
946
|
-
* Get row as sparse vector (less efficient - requires full scan)
|
|
947
|
-
*/
|
|
948
|
-
getRow(row) {
|
|
949
|
-
const indices = [];
|
|
950
|
-
const values = [];
|
|
951
|
-
for (let c = 0; c < this.nCols; c++) {
|
|
952
|
-
const val = this.get(row, c);
|
|
953
|
-
if (val !== 0) {
|
|
954
|
-
indices.push(c);
|
|
955
|
-
values.push(val);
|
|
956
|
-
}
|
|
957
|
-
}
|
|
958
|
-
return { indices, values, length: indices.length };
|
|
959
|
-
}
|
|
960
|
-
/**
|
|
961
|
-
* Get row into pre-allocated dense array (more efficient for repeated access)
|
|
962
|
-
* Returns array of non-zero column indices
|
|
963
|
-
*/
|
|
964
|
-
getRowDense(row, output) {
|
|
965
|
-
const nonZeroCols = [];
|
|
966
|
-
// Clear relevant portion
|
|
967
|
-
output.fill(0, 0, this.nCols);
|
|
968
|
-
for (let c = 0; c < this.nCols; c++) {
|
|
969
|
-
const start = this.colPointers[c];
|
|
970
|
-
const end = this.colPointers[c + 1];
|
|
971
|
-
// Binary search for row
|
|
972
|
-
let lo = start;
|
|
973
|
-
let hi = end;
|
|
974
|
-
while (lo < hi) {
|
|
975
|
-
const mid = (lo + hi) >>> 1;
|
|
976
|
-
if (this.rowIndices[mid] < row) {
|
|
977
|
-
lo = mid + 1;
|
|
978
|
-
}
|
|
979
|
-
else {
|
|
980
|
-
hi = mid;
|
|
981
|
-
}
|
|
982
|
-
}
|
|
983
|
-
if (lo < end && this.rowIndices[lo] === row) {
|
|
984
|
-
output[c] = this.values[lo];
|
|
985
|
-
nonZeroCols.push(c);
|
|
986
|
-
}
|
|
987
|
-
}
|
|
988
|
-
return nonZeroCols;
|
|
989
|
-
}
|
|
990
|
-
/**
|
|
991
|
-
* Add a new row (expands matrix vertically)
|
|
992
|
-
*/
|
|
993
|
-
addRow() {
|
|
994
|
-
this.nRows++;
|
|
995
|
-
if (this.workVector.length < this.nRows) {
|
|
996
|
-
this.workVector = new Float64Array(this.nRows * 2);
|
|
997
|
-
}
|
|
998
|
-
}
|
|
999
|
-
/**
|
|
1000
|
-
* Add a new column (expands matrix horizontally)
|
|
1001
|
-
*/
|
|
1002
|
-
addColumn() {
|
|
1003
|
-
this.colPointers.push(this.colPointers[this.nCols]);
|
|
1004
|
-
this.nCols++;
|
|
1005
|
-
if (this.workVector.length < this.nCols) {
|
|
1006
|
-
this.workVector = new Float64Array(this.nCols * 2);
|
|
1007
|
-
}
|
|
1008
|
-
}
|
|
1009
|
-
/**
|
|
1010
|
-
* Set entire column from sparse vector
|
|
1011
|
-
*/
|
|
1012
|
-
setColumn(col, sparseCol) {
|
|
1013
|
-
const oldStart = this.colPointers[col];
|
|
1014
|
-
const oldEnd = this.colPointers[col + 1];
|
|
1015
|
-
const oldLen = oldEnd - oldStart;
|
|
1016
|
-
const newLen = sparseCol.length;
|
|
1017
|
-
const diff = newLen - oldLen;
|
|
1018
|
-
if (diff !== 0) {
|
|
1019
|
-
// Need to resize
|
|
1020
|
-
if (diff > 0) {
|
|
1021
|
-
// Make room
|
|
1022
|
-
for (let i = 0; i < diff; i++) {
|
|
1023
|
-
this.values.splice(oldEnd, 0, 0);
|
|
1024
|
-
this.rowIndices.splice(oldEnd, 0, 0);
|
|
1025
|
-
}
|
|
1026
|
-
}
|
|
1027
|
-
else {
|
|
1028
|
-
// Remove excess
|
|
1029
|
-
this.values.splice(oldStart, -diff);
|
|
1030
|
-
this.rowIndices.splice(oldStart, -diff);
|
|
1031
|
-
}
|
|
1032
|
-
// Update column pointers
|
|
1033
|
-
for (let c = col + 1; c <= this.nCols; c++) {
|
|
1034
|
-
this.colPointers[c] += diff;
|
|
1035
|
-
}
|
|
1036
|
-
}
|
|
1037
|
-
// Copy new values
|
|
1038
|
-
const start = this.colPointers[col];
|
|
1039
|
-
for (let i = 0; i < newLen; i++) {
|
|
1040
|
-
this.values[start + i] = sparseCol.values[i];
|
|
1041
|
-
this.rowIndices[start + i] = sparseCol.indices[i];
|
|
1042
|
-
}
|
|
1043
|
-
}
|
|
1044
|
-
/**
|
|
1045
|
-
* Number of non-zero entries
|
|
1046
|
-
*/
|
|
1047
|
-
get nnz() {
|
|
1048
|
-
return this.values.length;
|
|
1049
|
-
}
|
|
1050
|
-
/**
|
|
1051
|
-
* Density (fraction of non-zeros)
|
|
1052
|
-
*/
|
|
1053
|
-
get density() {
|
|
1054
|
-
return this.nnz / (this.nRows * this.nCols);
|
|
1055
|
-
}
|
|
1056
|
-
/**
|
|
1057
|
-
* Scale a row by a factor
|
|
1058
|
-
*/
|
|
1059
|
-
scaleRow(row, factor) {
|
|
1060
|
-
for (let c = 0; c < this.nCols; c++) {
|
|
1061
|
-
const start = this.colPointers[c];
|
|
1062
|
-
const end = this.colPointers[c + 1];
|
|
1063
|
-
for (let i = start; i < end; i++) {
|
|
1064
|
-
if (this.rowIndices[i] === row) {
|
|
1065
|
-
this.values[i] *= factor;
|
|
1066
|
-
break;
|
|
1067
|
-
}
|
|
1068
|
-
}
|
|
1069
|
-
}
|
|
1070
|
-
}
|
|
1071
|
-
/**
|
|
1072
|
-
* Add scaled row to another row: targetRow += factor * sourceRow
|
|
1073
|
-
* This is the core operation in Gaussian elimination / pivot
|
|
1074
|
-
*/
|
|
1075
|
-
addScaledRow(targetRow, sourceRow, factor) {
|
|
1076
|
-
if (factor >= -this.precision && factor <= this.precision) {
|
|
1077
|
-
return;
|
|
1078
|
-
}
|
|
1079
|
-
// Get source row as dense for efficiency
|
|
1080
|
-
const sourceNonZeros = this.getRowDense(sourceRow, this.workVector);
|
|
1081
|
-
// Add scaled source to target
|
|
1082
|
-
for (const c of sourceNonZeros) {
|
|
1083
|
-
const sourceVal = this.workVector[c];
|
|
1084
|
-
const newVal = this.get(targetRow, c) + factor * sourceVal;
|
|
1085
|
-
this.set(targetRow, c, newVal);
|
|
1086
|
-
}
|
|
1087
|
-
}
|
|
1088
|
-
/**
|
|
1089
|
-
* Perform pivot operation at (pivotRow, pivotCol)
|
|
1090
|
-
* This is optimized for sparse operations
|
|
1091
|
-
*/
|
|
1092
|
-
pivot(pivotRow, pivotCol) {
|
|
1093
|
-
const pivotVal = this.get(pivotRow, pivotCol);
|
|
1094
|
-
if (pivotVal === 0) {
|
|
1095
|
-
throw new Error("Cannot pivot on zero element");
|
|
1096
|
-
}
|
|
1097
|
-
const invPivot = 1 / pivotVal;
|
|
1098
|
-
// Get pivot row as dense
|
|
1099
|
-
const pivotRowNonZeros = this.getRowDense(pivotRow, this.workVector);
|
|
1100
|
-
// Scale pivot row
|
|
1101
|
-
for (const c of pivotRowNonZeros) {
|
|
1102
|
-
this.workVector[c] *= invPivot;
|
|
1103
|
-
}
|
|
1104
|
-
this.workVector[pivotCol] = invPivot;
|
|
1105
|
-
// Update pivot row in matrix
|
|
1106
|
-
for (const c of pivotRowNonZeros) {
|
|
1107
|
-
this.set(pivotRow, c, this.workVector[c]);
|
|
1108
|
-
}
|
|
1109
|
-
this.set(pivotRow, pivotCol, invPivot);
|
|
1110
|
-
// Get pivot column to find rows that need updating
|
|
1111
|
-
const pivotColData = this.getColumn(pivotCol);
|
|
1112
|
-
// Update all other rows
|
|
1113
|
-
for (let i = 0; i < pivotColData.length; i++) {
|
|
1114
|
-
const r = pivotColData.indices[i];
|
|
1115
|
-
if (r === pivotRow)
|
|
1116
|
-
continue;
|
|
1117
|
-
const coefficient = pivotColData.values[i];
|
|
1118
|
-
if (coefficient >= -this.precision && coefficient <= this.precision)
|
|
1119
|
-
continue;
|
|
1120
|
-
// row[r] -= coefficient * pivotRow
|
|
1121
|
-
for (const c of pivotRowNonZeros) {
|
|
1122
|
-
if (c === pivotCol)
|
|
1123
|
-
continue;
|
|
1124
|
-
const pivotRowVal = this.workVector[c];
|
|
1125
|
-
if (pivotRowVal >= -this.precision && pivotRowVal <= this.precision)
|
|
1126
|
-
continue;
|
|
1127
|
-
const newVal = this.get(r, c) - coefficient * pivotRowVal;
|
|
1128
|
-
this.set(r, c, newVal);
|
|
1129
|
-
}
|
|
1130
|
-
// Set pivot column value for this row
|
|
1131
|
-
this.set(r, pivotCol, -coefficient * invPivot);
|
|
1132
|
-
}
|
|
1133
|
-
return pivotRowNonZeros;
|
|
1134
|
-
}
|
|
1135
|
-
/**
|
|
1136
|
-
* Clone the matrix
|
|
1137
|
-
*/
|
|
1138
|
-
clone() {
|
|
1139
|
-
const copy = new SparseMatrix(this.nRows, this.nCols, this.precision);
|
|
1140
|
-
copy.values = this.values.slice();
|
|
1141
|
-
copy.rowIndices = this.rowIndices.slice();
|
|
1142
|
-
copy.colPointers = this.colPointers.slice();
|
|
1143
|
-
return copy;
|
|
1144
|
-
}
|
|
1145
|
-
}
|
|
1146
|
-
|
|
1147
|
-
/**
|
|
1148
|
-
* @file src/tableau/sparse-simplex.ts
|
|
1149
|
-
* @description Sparse simplex algorithm for large LP problems
|
|
1150
|
-
*
|
|
1151
|
-
* Optimized simplex implementation that operates directly on SparseMatrix
|
|
1152
|
-
* storage. Converts the dense tableau to CSC format, solves, and converts
|
|
1153
|
-
* back for solution extraction.
|
|
1154
|
-
*
|
|
1155
|
-
* Provides significant speedup for large, sparse problems where most
|
|
1156
|
-
* coefficients are zero.
|
|
1157
|
-
*/
|
|
1158
|
-
/**
|
|
1159
|
-
* Convert dense tableau to sparse and solve
|
|
1160
|
-
*/
|
|
1161
|
-
function sparseSimplex() {
|
|
1162
|
-
// Convert to sparse
|
|
1163
|
-
const sparse = SparseMatrix.fromDense(this.matrix, this.width, this.height, this.precision);
|
|
1164
|
-
this.bounded = true;
|
|
1165
|
-
// Run sparse phase 1
|
|
1166
|
-
sparsePhase1.call(this, sparse);
|
|
1167
|
-
if (this.feasible === true) {
|
|
1168
|
-
// Run sparse phase 2
|
|
1169
|
-
sparsePhase2.call(this, sparse);
|
|
1170
|
-
}
|
|
1171
|
-
// Copy results back to dense matrix (needed for solution extraction)
|
|
1172
|
-
const dense = sparse.toDense();
|
|
1173
|
-
this.matrix.set(dense);
|
|
1174
|
-
return this;
|
|
1175
|
-
}
|
|
1176
|
-
/**
|
|
1177
|
-
* Sparse Phase 1: Find a basic feasible solution
|
|
1178
|
-
*/
|
|
1179
|
-
function sparsePhase1(sparse) {
|
|
1180
|
-
const debugCheckForCycles = this.model.checkForCycles;
|
|
1181
|
-
const varIndexesCycle = [];
|
|
1182
|
-
const rhsColumn = this.rhsColumn;
|
|
1183
|
-
const lastColumn = this.width - 1;
|
|
1184
|
-
const lastRow = this.height - 1;
|
|
1185
|
-
const precision = this.precision;
|
|
1186
|
-
let unrestricted;
|
|
1187
|
-
let iterations = 0;
|
|
1188
|
-
while (true) {
|
|
1189
|
-
// Find leaving row (most negative RHS)
|
|
1190
|
-
let leavingRowIndex = 0;
|
|
1191
|
-
let rhsValue = -precision;
|
|
1192
|
-
for (let r = 1; r <= lastRow; r++) {
|
|
1193
|
-
const value = sparse.get(r, rhsColumn);
|
|
1194
|
-
if (value < rhsValue) {
|
|
1195
|
-
rhsValue = value;
|
|
1196
|
-
leavingRowIndex = r;
|
|
1197
|
-
}
|
|
1198
|
-
}
|
|
1199
|
-
if (leavingRowIndex === 0) {
|
|
1200
|
-
this.feasible = true;
|
|
1201
|
-
return iterations;
|
|
1202
|
-
}
|
|
1203
|
-
// Find entering column
|
|
1204
|
-
let enteringColumn = 0;
|
|
1205
|
-
let maxQuotient = -Infinity;
|
|
1206
|
-
// Get leaving row as sparse
|
|
1207
|
-
const leavingRowData = sparse.getRow(leavingRowIndex);
|
|
1208
|
-
for (let i = 0; i < leavingRowData.length; i++) {
|
|
1209
|
-
const c = leavingRowData.indices[i];
|
|
1210
|
-
if (c === 0 || c === rhsColumn)
|
|
1211
|
-
continue;
|
|
1212
|
-
const coefficient = leavingRowData.values[i];
|
|
1213
|
-
unrestricted = this.unrestrictedVars[this.varIndexByCol[c]] === true;
|
|
1214
|
-
if (unrestricted || coefficient < -precision) {
|
|
1215
|
-
const costValue = sparse.get(0, c);
|
|
1216
|
-
const quotient = -costValue / coefficient;
|
|
1217
|
-
if (maxQuotient < quotient) {
|
|
1218
|
-
maxQuotient = quotient;
|
|
1219
|
-
enteringColumn = c;
|
|
1220
|
-
}
|
|
1221
|
-
}
|
|
1222
|
-
}
|
|
1223
|
-
// Also check columns with zero in leaving row but non-zero cost
|
|
1224
|
-
for (let c = 1; c <= lastColumn; c++) {
|
|
1225
|
-
if (c === rhsColumn)
|
|
1226
|
-
continue;
|
|
1227
|
-
const coefficient = sparse.get(leavingRowIndex, c);
|
|
1228
|
-
unrestricted = this.unrestrictedVars[this.varIndexByCol[c]] === true;
|
|
1229
|
-
if (unrestricted || coefficient < -precision) {
|
|
1230
|
-
const costValue = sparse.get(0, c);
|
|
1231
|
-
const quotient = -costValue / coefficient;
|
|
1232
|
-
if (maxQuotient < quotient) {
|
|
1233
|
-
maxQuotient = quotient;
|
|
1234
|
-
enteringColumn = c;
|
|
1235
|
-
}
|
|
1236
|
-
}
|
|
1237
|
-
}
|
|
1238
|
-
if (enteringColumn === 0) {
|
|
1239
|
-
this.feasible = false;
|
|
1240
|
-
return iterations;
|
|
1241
|
-
}
|
|
1242
|
-
if (debugCheckForCycles) {
|
|
1243
|
-
varIndexesCycle.push([
|
|
1244
|
-
this.varIndexByRow[leavingRowIndex],
|
|
1245
|
-
this.varIndexByCol[enteringColumn],
|
|
1246
|
-
]);
|
|
1247
|
-
const cycleData = this.checkForCycles(varIndexesCycle);
|
|
1248
|
-
if (cycleData.length > 0) {
|
|
1249
|
-
this.model.messages.push("Cycle in phase 1");
|
|
1250
|
-
this.feasible = false;
|
|
1251
|
-
return iterations;
|
|
1252
|
-
}
|
|
1253
|
-
}
|
|
1254
|
-
// Perform pivot
|
|
1255
|
-
sparsePivot.call(this, sparse, leavingRowIndex, enteringColumn);
|
|
1256
|
-
iterations += 1;
|
|
1257
|
-
}
|
|
1258
|
-
}
|
|
1259
|
-
/**
|
|
1260
|
-
* Sparse Phase 2: Optimize the objective
|
|
1261
|
-
*/
|
|
1262
|
-
function sparsePhase2(sparse) {
|
|
1263
|
-
const debugCheckForCycles = this.model.checkForCycles;
|
|
1264
|
-
const varIndexesCycle = [];
|
|
1265
|
-
const rhsColumn = this.rhsColumn;
|
|
1266
|
-
const lastColumn = this.width - 1;
|
|
1267
|
-
this.height - 1;
|
|
1268
|
-
const precision = this.precision;
|
|
1269
|
-
this.optionalObjectives.length;
|
|
1270
|
-
let iterations = 0;
|
|
1271
|
-
let reducedCost;
|
|
1272
|
-
let unrestricted;
|
|
1273
|
-
// Partial pricing for sparse
|
|
1274
|
-
const batchSize = Math.min(500, Math.max(50, Math.floor(Math.sqrt(lastColumn))));
|
|
1275
|
-
let batchStart = 1;
|
|
1276
|
-
while (true) {
|
|
1277
|
-
const costRow = 0;
|
|
1278
|
-
let enteringColumn = 0;
|
|
1279
|
-
let enteringValue = precision;
|
|
1280
|
-
let isReducedCostNegative = false;
|
|
1281
|
-
// Partial pricing: scan columns in batches
|
|
1282
|
-
let batchesScanned = 0;
|
|
1283
|
-
const totalBatches = Math.ceil(lastColumn / batchSize);
|
|
1284
|
-
while (enteringColumn === 0 && batchesScanned < totalBatches) {
|
|
1285
|
-
const batchEnd = Math.min(batchStart + batchSize - 1, lastColumn);
|
|
1286
|
-
for (let c = batchStart; c <= batchEnd; c++) {
|
|
1287
|
-
if (c === rhsColumn)
|
|
1288
|
-
continue;
|
|
1289
|
-
reducedCost = sparse.get(costRow, c);
|
|
1290
|
-
unrestricted = this.unrestrictedVars[this.varIndexByCol[c]] === true;
|
|
1291
|
-
if (unrestricted && reducedCost < 0) {
|
|
1292
|
-
if (-reducedCost > enteringValue) {
|
|
1293
|
-
enteringValue = -reducedCost;
|
|
1294
|
-
enteringColumn = c;
|
|
1295
|
-
isReducedCostNegative = true;
|
|
1296
|
-
}
|
|
1297
|
-
continue;
|
|
1298
|
-
}
|
|
1299
|
-
if (reducedCost > enteringValue) {
|
|
1300
|
-
enteringValue = reducedCost;
|
|
1301
|
-
enteringColumn = c;
|
|
1302
|
-
isReducedCostNegative = false;
|
|
1303
|
-
}
|
|
1304
|
-
}
|
|
1305
|
-
batchStart = batchEnd >= lastColumn ? 1 : batchEnd + 1;
|
|
1306
|
-
batchesScanned++;
|
|
1307
|
-
}
|
|
1308
|
-
if (enteringColumn === 0) {
|
|
1309
|
-
this.setEvaluation();
|
|
1310
|
-
this.simplexIters += 1;
|
|
1311
|
-
return iterations;
|
|
1312
|
-
}
|
|
1313
|
-
// Find leaving row using ratio test
|
|
1314
|
-
let leavingRow = 0;
|
|
1315
|
-
let minQuotient = Infinity;
|
|
1316
|
-
// Get entering column as sparse
|
|
1317
|
-
const enteringColData = sparse.getColumn(enteringColumn);
|
|
1318
|
-
for (let i = 0; i < enteringColData.length; i++) {
|
|
1319
|
-
const r = enteringColData.indices[i];
|
|
1320
|
-
if (r === 0)
|
|
1321
|
-
continue; // Skip cost row
|
|
1322
|
-
const colValue = enteringColData.values[i];
|
|
1323
|
-
if (colValue >= -precision && colValue <= precision)
|
|
1324
|
-
continue;
|
|
1325
|
-
const rhsValue = sparse.get(r, rhsColumn);
|
|
1326
|
-
if (colValue > 0 && precision > rhsValue && rhsValue > -precision) {
|
|
1327
|
-
minQuotient = 0;
|
|
1328
|
-
leavingRow = r;
|
|
1329
|
-
break;
|
|
1330
|
-
}
|
|
1331
|
-
const quotient = isReducedCostNegative ? -rhsValue / colValue : rhsValue / colValue;
|
|
1332
|
-
if (quotient > precision && minQuotient > quotient) {
|
|
1333
|
-
minQuotient = quotient;
|
|
1334
|
-
leavingRow = r;
|
|
1335
|
-
}
|
|
1336
|
-
}
|
|
1337
|
-
if (minQuotient === Infinity) {
|
|
1338
|
-
this.evaluation = -Infinity;
|
|
1339
|
-
this.bounded = false;
|
|
1340
|
-
this.unboundedVarIndex = this.varIndexByCol[enteringColumn];
|
|
1341
|
-
return iterations;
|
|
1342
|
-
}
|
|
1343
|
-
if (debugCheckForCycles) {
|
|
1344
|
-
varIndexesCycle.push([
|
|
1345
|
-
this.varIndexByRow[leavingRow],
|
|
1346
|
-
this.varIndexByCol[enteringColumn],
|
|
1347
|
-
]);
|
|
1348
|
-
const cycleData = this.checkForCycles(varIndexesCycle);
|
|
1349
|
-
if (cycleData.length > 0) {
|
|
1350
|
-
this.model.messages.push("Cycle in phase 2");
|
|
1351
|
-
this.feasible = false;
|
|
1352
|
-
return iterations;
|
|
1353
|
-
}
|
|
1354
|
-
}
|
|
1355
|
-
// Perform pivot
|
|
1356
|
-
sparsePivot.call(this, sparse, leavingRow, enteringColumn);
|
|
1357
|
-
iterations += 1;
|
|
1358
|
-
}
|
|
1359
|
-
}
|
|
1360
|
-
/**
|
|
1361
|
-
* Sparse pivot operation
|
|
1362
|
-
*/
|
|
1363
|
-
function sparsePivot(sparse, pivotRowIndex, pivotColumnIndex) {
|
|
1364
|
-
// Update basis tracking
|
|
1365
|
-
const leavingBasicIndex = this.varIndexByRow[pivotRowIndex];
|
|
1366
|
-
const enteringBasicIndex = this.varIndexByCol[pivotColumnIndex];
|
|
1367
|
-
this.varIndexByRow[pivotRowIndex] = enteringBasicIndex;
|
|
1368
|
-
this.varIndexByCol[pivotColumnIndex] = leavingBasicIndex;
|
|
1369
|
-
this.rowByVarIndex[enteringBasicIndex] = pivotRowIndex;
|
|
1370
|
-
this.rowByVarIndex[leavingBasicIndex] = -1;
|
|
1371
|
-
this.colByVarIndex[enteringBasicIndex] = -1;
|
|
1372
|
-
this.colByVarIndex[leavingBasicIndex] = pivotColumnIndex;
|
|
1373
|
-
// Perform sparse pivot
|
|
1374
|
-
sparse.pivot(pivotRowIndex, pivotColumnIndex);
|
|
1375
|
-
// Handle optional objectives (these are stored separately, not in sparse matrix)
|
|
1376
|
-
const optionalObjectives = this.optionalObjectives;
|
|
1377
|
-
const nOptionalObjectives = optionalObjectives.length;
|
|
1378
|
-
if (nOptionalObjectives > 0) {
|
|
1379
|
-
const pivotVal = sparse.get(pivotRowIndex, pivotColumnIndex);
|
|
1380
|
-
const pivotRowData = sparse.getRow(pivotRowIndex);
|
|
1381
|
-
for (let o = 0; o < nOptionalObjectives; o++) {
|
|
1382
|
-
const reducedCosts = optionalObjectives[o].reducedCosts;
|
|
1383
|
-
const coefficient = reducedCosts[pivotColumnIndex];
|
|
1384
|
-
if (coefficient !== 0) {
|
|
1385
|
-
for (let i = 0; i < pivotRowData.length; i++) {
|
|
1386
|
-
const c = pivotRowData.indices[i];
|
|
1387
|
-
const v0 = pivotRowData.values[i];
|
|
1388
|
-
if (v0 !== 0) {
|
|
1389
|
-
reducedCosts[c] = reducedCosts[c] - coefficient * v0;
|
|
1390
|
-
}
|
|
1391
|
-
}
|
|
1392
|
-
reducedCosts[pivotColumnIndex] = -coefficient * pivotVal;
|
|
1393
|
-
}
|
|
1394
|
-
}
|
|
1395
|
-
}
|
|
1396
|
-
}
|
|
1397
|
-
/**
|
|
1398
|
-
* Threshold for using sparse (total cells)
|
|
1399
|
-
* Currently disabled - CSC format overhead exceeds benefits.
|
|
1400
|
-
* A dual-indexed (CSC+CSR) approach would be needed for efficient sparse simplex.
|
|
1401
|
-
*/
|
|
1402
|
-
const SPARSE_THRESHOLD = Infinity; // Disabled
|
|
1403
|
-
/**
|
|
1404
|
-
* Density threshold - only use sparse if below this density
|
|
1405
|
-
*/
|
|
1406
|
-
const DENSITY_THRESHOLD = 0.1; // 10%
|
|
1407
|
-
/**
|
|
1408
|
-
* Check if sparse mode should be used for this tableau
|
|
1409
|
-
*/
|
|
1410
|
-
function shouldUseSparse(tableau) {
|
|
1411
|
-
const totalCells = tableau.width * tableau.height;
|
|
1412
|
-
if (totalCells < SPARSE_THRESHOLD) {
|
|
1413
|
-
return false;
|
|
1414
|
-
}
|
|
1415
|
-
// Estimate density from constraint rows (skip row 0 which is cost row and often dense)
|
|
1416
|
-
const matrix = tableau.matrix;
|
|
1417
|
-
const width = tableau.width;
|
|
1418
|
-
const startRow = 1;
|
|
1419
|
-
const sampleRows = Math.min(20, tableau.height - 1);
|
|
1420
|
-
let nonZeros = 0;
|
|
1421
|
-
for (let r = startRow; r < startRow + sampleRows; r++) {
|
|
1422
|
-
const rowOffset = r * width;
|
|
1423
|
-
for (let c = 0; c < width; c++) {
|
|
1424
|
-
if (matrix[rowOffset + c] !== 0) {
|
|
1425
|
-
nonZeros++;
|
|
1426
|
-
}
|
|
1427
|
-
}
|
|
1428
|
-
}
|
|
1429
|
-
const estimatedDensity = nonZeros / (sampleRows * width);
|
|
1430
|
-
return estimatedDensity < DENSITY_THRESHOLD;
|
|
1431
|
-
}
|
|
1432
|
-
|
|
1433
963
|
class Variable {
|
|
1434
964
|
constructor(id, cost, index, priority) {
|
|
1435
965
|
this.id = id;
|
|
@@ -1514,6 +1044,7 @@ class Constraint {
|
|
|
1514
1044
|
setVariableCoefficient(newCoefficient, variable) {
|
|
1515
1045
|
const varIndex = variable.index;
|
|
1516
1046
|
if (varIndex === -1) {
|
|
1047
|
+
// eslint-disable-next-line no-console
|
|
1517
1048
|
console.warn("[Constraint.setVariableCoefficient] Trying to change coefficient of inexistant variable.");
|
|
1518
1049
|
return;
|
|
1519
1050
|
}
|
|
@@ -1609,33 +1140,39 @@ function addCutConstraints(cutConstraints) {
|
|
|
1609
1140
|
const matrix = this.matrix;
|
|
1610
1141
|
this.height = heightWithCuts;
|
|
1611
1142
|
this.nVars = this.width + this.height - 2;
|
|
1143
|
+
// Cache array references for faster access in loop
|
|
1144
|
+
const rhsColumn = this.rhsColumn;
|
|
1145
|
+
const rowByVarIndex = this.rowByVarIndex;
|
|
1146
|
+
const colByVarIndex = this.colByVarIndex;
|
|
1147
|
+
const varIndexByRow = this.varIndexByRow;
|
|
1148
|
+
const variablesPerIndex = this.variablesPerIndex;
|
|
1612
1149
|
for (let h = 0; h < nCutConstraints; h += 1) {
|
|
1613
1150
|
const cut = cutConstraints[h];
|
|
1614
1151
|
const cutRow = height + h;
|
|
1615
1152
|
const cutRowOffset = cutRow * width;
|
|
1616
1153
|
const sign = cut.type === "min" ? -1 : 1;
|
|
1617
1154
|
const varIndex = cut.varIndex;
|
|
1618
|
-
let varRowIndex =
|
|
1155
|
+
let varRowIndex = rowByVarIndex[varIndex];
|
|
1619
1156
|
if (varRowIndex === -1) {
|
|
1620
|
-
matrix[cutRowOffset +
|
|
1157
|
+
matrix[cutRowOffset + rhsColumn] = sign * cut.value;
|
|
1621
1158
|
for (let c = 1; c <= lastColumn; c += 1) {
|
|
1622
1159
|
matrix[cutRowOffset + c] = 0;
|
|
1623
1160
|
}
|
|
1624
|
-
matrix[cutRowOffset +
|
|
1161
|
+
matrix[cutRowOffset + colByVarIndex[varIndex]] = sign;
|
|
1625
1162
|
}
|
|
1626
1163
|
else {
|
|
1627
1164
|
const varRowOffset = varRowIndex * width;
|
|
1628
|
-
const varValue = matrix[varRowOffset +
|
|
1629
|
-
matrix[cutRowOffset +
|
|
1165
|
+
const varValue = matrix[varRowOffset + rhsColumn];
|
|
1166
|
+
matrix[cutRowOffset + rhsColumn] = sign * (cut.value - varValue);
|
|
1630
1167
|
for (let c = 1; c <= lastColumn; c += 1) {
|
|
1631
1168
|
matrix[cutRowOffset + c] = -sign * matrix[varRowOffset + c];
|
|
1632
1169
|
}
|
|
1633
1170
|
}
|
|
1634
1171
|
varRowIndex = this.getNewElementIndex();
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1172
|
+
varIndexByRow[cutRow] = varRowIndex;
|
|
1173
|
+
rowByVarIndex[varRowIndex] = cutRow;
|
|
1174
|
+
colByVarIndex[varRowIndex] = -1;
|
|
1175
|
+
variablesPerIndex[varRowIndex] = new SlackVariable("s" + varRowIndex, varRowIndex);
|
|
1639
1176
|
this.nVars += 1;
|
|
1640
1177
|
}
|
|
1641
1178
|
}
|
|
@@ -1674,18 +1211,23 @@ function addLowerBoundMIRCut(rowIndex) {
|
|
|
1674
1211
|
this.rowByVarIndex[slackVarIndex] = height;
|
|
1675
1212
|
this.colByVarIndex[slackVarIndex] = -1;
|
|
1676
1213
|
this.variablesPerIndex[slackVarIndex] = new SlackVariable("s" + slackVarIndex, slackVarIndex);
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1214
|
+
const rhsColumn = this.rhsColumn;
|
|
1215
|
+
mat[newRowOffset + rhsColumn] = Math.floor(rhsValue);
|
|
1216
|
+
// Cache array references for faster access in hot loop
|
|
1217
|
+
const variablesPerIndex = this.variablesPerIndex;
|
|
1218
|
+
const varIndexByCol = this.varIndexByCol;
|
|
1219
|
+
const varIndexByColLen = varIndexByCol.length;
|
|
1220
|
+
const oneMinusFrac = 1 - fractionalPart;
|
|
1221
|
+
for (let colIndex = 1; colIndex < varIndexByColLen; colIndex += 1) {
|
|
1222
|
+
const variable = variablesPerIndex[varIndexByCol[colIndex]];
|
|
1680
1223
|
const coefficient = mat[cutRowOffset + colIndex];
|
|
1681
1224
|
if (variable !== undefined && variable.isInteger) {
|
|
1682
|
-
const
|
|
1683
|
-
|
|
1684
|
-
(1 - fractionalPart);
|
|
1225
|
+
const floorCoeff = Math.floor(coefficient);
|
|
1226
|
+
const termCoeff = floorCoeff + Math.max(0, coefficient - floorCoeff - fractionalPart) / oneMinusFrac;
|
|
1685
1227
|
mat[newRowOffset + colIndex] = termCoeff;
|
|
1686
1228
|
}
|
|
1687
1229
|
else {
|
|
1688
|
-
mat[newRowOffset + colIndex] = Math.min(0, coefficient /
|
|
1230
|
+
mat[newRowOffset + colIndex] = Math.min(0, coefficient / oneMinusFrac);
|
|
1689
1231
|
}
|
|
1690
1232
|
}
|
|
1691
1233
|
for (let c = 0; c < width; c += 1) {
|
|
@@ -1728,9 +1270,15 @@ function addUpperBoundMIRCut(rowIndex) {
|
|
|
1728
1270
|
this.rowByVarIndex[slackVarIndex] = height;
|
|
1729
1271
|
this.colByVarIndex[slackVarIndex] = -1;
|
|
1730
1272
|
this.variablesPerIndex[slackVarIndex] = new SlackVariable("s" + slackVarIndex, slackVarIndex);
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1273
|
+
const rhsColumn = this.rhsColumn;
|
|
1274
|
+
mat[newRowOffset + rhsColumn] = -fractionalPart;
|
|
1275
|
+
// Cache array references for faster access in hot loop
|
|
1276
|
+
const variablesPerIndex = this.variablesPerIndex;
|
|
1277
|
+
const varIndexByCol = this.varIndexByCol;
|
|
1278
|
+
const varIndexByColLen = varIndexByCol.length;
|
|
1279
|
+
const oneMinusFrac = 1 - fractionalPart;
|
|
1280
|
+
for (let colIndex = 1; colIndex < varIndexByColLen; colIndex += 1) {
|
|
1281
|
+
const variable = variablesPerIndex[varIndexByCol[colIndex]];
|
|
1734
1282
|
const coefficient = mat[cutRowOffset + colIndex];
|
|
1735
1283
|
const termCoeff = coefficient - Math.floor(coefficient);
|
|
1736
1284
|
if (variable !== undefined && variable.isInteger) {
|
|
@@ -1741,9 +1289,7 @@ function addUpperBoundMIRCut(rowIndex) {
|
|
|
1741
1289
|
}
|
|
1742
1290
|
else {
|
|
1743
1291
|
mat[newRowOffset + colIndex] =
|
|
1744
|
-
coefficient >= 0
|
|
1745
|
-
? -coefficient
|
|
1746
|
-
: (coefficient * fractionalPart) / (1 - fractionalPart);
|
|
1292
|
+
coefficient >= 0 ? -coefficient : (coefficient * fractionalPart) / oneMinusFrac;
|
|
1747
1293
|
}
|
|
1748
1294
|
}
|
|
1749
1295
|
return true;
|
|
@@ -2118,12 +1664,15 @@ function isIntegral() {
|
|
|
2118
1664
|
const rhsColumn = this.rhsColumn;
|
|
2119
1665
|
const integerVariables = this.model.integerVariables;
|
|
2120
1666
|
const nIntegerVars = integerVariables.length;
|
|
1667
|
+
// Cache array reference for faster access in hot loop
|
|
1668
|
+
const rowByVarIndex = this.rowByVarIndex;
|
|
1669
|
+
const precision = this.precision;
|
|
2121
1670
|
for (let v = 0; v < nIntegerVars; v++) {
|
|
2122
1671
|
const varIndex = integerVariables[v].index;
|
|
2123
|
-
const row =
|
|
1672
|
+
const row = rowByVarIndex[varIndex];
|
|
2124
1673
|
if (row !== -1) {
|
|
2125
1674
|
const value = matrix[row * width + rhsColumn];
|
|
2126
|
-
if (Math.abs(value - Math.round(value)) >
|
|
1675
|
+
if (Math.abs(value - Math.round(value)) > precision) {
|
|
2127
1676
|
return false;
|
|
2128
1677
|
}
|
|
2129
1678
|
}
|
|
@@ -2139,12 +1688,17 @@ function computeFractionalVolume(ignoreIntegerValues) {
|
|
|
2139
1688
|
const width = this.width;
|
|
2140
1689
|
const matrix = this.matrix;
|
|
2141
1690
|
const rhsColumn = this.rhsColumn;
|
|
2142
|
-
|
|
2143
|
-
|
|
1691
|
+
const height = this.height;
|
|
1692
|
+
// Cache array references for faster access in hot loop
|
|
1693
|
+
const variablesPerIndex = this.variablesPerIndex;
|
|
1694
|
+
const varIndexByRow = this.varIndexByRow;
|
|
1695
|
+
const precision = this.precision;
|
|
1696
|
+
for (let r = 1; r < height; r += 1) {
|
|
1697
|
+
const variable = variablesPerIndex[varIndexByRow[r]];
|
|
2144
1698
|
if (variable !== undefined && variable.isInteger) {
|
|
2145
1699
|
const value = matrix[r * width + rhsColumn];
|
|
2146
1700
|
const distance = Math.abs(value);
|
|
2147
|
-
if (Math.min(distance - Math.floor(distance), Math.floor(distance + 1)) <
|
|
1701
|
+
if (Math.min(distance - Math.floor(distance), Math.floor(distance + 1)) < precision) {
|
|
2148
1702
|
if (ignoreIntegerValues !== true) {
|
|
2149
1703
|
return 0;
|
|
2150
1704
|
}
|
|
@@ -2173,9 +1727,11 @@ function getMostFractionalVar() {
|
|
|
2173
1727
|
const rhsColumn = this.rhsColumn;
|
|
2174
1728
|
const integerVars = this.model.integerVariables;
|
|
2175
1729
|
const nIntegerVars = integerVars.length;
|
|
1730
|
+
// Cache array reference for faster access in hot loop
|
|
1731
|
+
const rowByVarIndex = this.rowByVarIndex;
|
|
2176
1732
|
for (let v = 0; v < nIntegerVars; v += 1) {
|
|
2177
1733
|
const varIndex = integerVars[v].index;
|
|
2178
|
-
const row =
|
|
1734
|
+
const row = rowByVarIndex[varIndex];
|
|
2179
1735
|
if (row !== -1) {
|
|
2180
1736
|
const varValue = matrix[row * width + rhsColumn];
|
|
2181
1737
|
const fraction = Math.abs(varValue - Math.round(varValue));
|
|
@@ -2392,13 +1948,7 @@ class Tableau {
|
|
|
2392
1948
|
}
|
|
2393
1949
|
// ========== Core Simplex Operations ==========
|
|
2394
1950
|
simplex() {
|
|
2395
|
-
|
|
2396
|
-
if (shouldUseSparse(this)) {
|
|
2397
|
-
sparseSimplex.call(this);
|
|
2398
|
-
}
|
|
2399
|
-
else {
|
|
2400
|
-
simplex.call(this);
|
|
2401
|
-
}
|
|
1951
|
+
simplex.call(this);
|
|
2402
1952
|
return this;
|
|
2403
1953
|
}
|
|
2404
1954
|
phase1() {
|
|
@@ -3076,6 +2626,7 @@ class Model {
|
|
|
3076
2626
|
_removeConstraint(constraint) {
|
|
3077
2627
|
const idx = this.constraints.indexOf(constraint);
|
|
3078
2628
|
if (idx === -1) {
|
|
2629
|
+
// eslint-disable-next-line no-console
|
|
3079
2630
|
console.warn("[Model.removeConstraint] Constraint not present in model");
|
|
3080
2631
|
return;
|
|
3081
2632
|
}
|
|
@@ -3105,6 +2656,7 @@ class Model {
|
|
|
3105
2656
|
removeVariable(variable) {
|
|
3106
2657
|
const idx = this.variables.indexOf(variable);
|
|
3107
2658
|
if (idx === -1) {
|
|
2659
|
+
// eslint-disable-next-line no-console
|
|
3108
2660
|
console.warn("[Model.removeVariable] Variable not present in model");
|
|
3109
2661
|
return;
|
|
3110
2662
|
}
|
|
@@ -3224,11 +2776,20 @@ class Model {
|
|
|
3224
2776
|
const unrestrictedVarIds = jsonModel.unrestricted || {};
|
|
3225
2777
|
// Instantiating variables and constraint terms
|
|
3226
2778
|
const objectiveName = jsonModel.optimize;
|
|
2779
|
+
// Check if objectiveName exists as a coefficient key in any variable.
|
|
2780
|
+
// If not, and it matches a variable name, the user wants to optimize
|
|
2781
|
+
// that variable directly (implicit cost of 1).
|
|
2782
|
+
const objectiveIsAttribute = variableIds.some((id) => objectiveName in variables[id]);
|
|
2783
|
+
const objectiveIsVariable = !objectiveIsAttribute && variableIds.includes(objectiveName);
|
|
3227
2784
|
for (let v = 0; v < nVariables; v += 1) {
|
|
3228
2785
|
// Creation of the variables
|
|
3229
2786
|
const variableId = variableIds[v];
|
|
3230
2787
|
const variableConstraints = variables[variableId];
|
|
3231
|
-
const cost =
|
|
2788
|
+
const cost = objectiveIsVariable
|
|
2789
|
+
? variableId === objectiveName
|
|
2790
|
+
? 1
|
|
2791
|
+
: 0
|
|
2792
|
+
: variableConstraints[objectiveName] || 0;
|
|
3232
2793
|
const isBinary = !!binaryVarIds[variableId];
|
|
3233
2794
|
const isInteger = !!integerVarIds[variableId] || isBinary;
|
|
3234
2795
|
const isUnrestricted = !!unrestrictedVarIds[variableId];
|
|
@@ -4073,7 +3634,7 @@ function Polyopt(solver, model) {
|
|
|
4073
3634
|
}
|
|
4074
3635
|
}
|
|
4075
3636
|
// Strip metadata so each Pareto vertex only contains value-bearing fields.
|
|
4076
|
-
const { feasible, result, bounded, ...paretoPayload } = solution;
|
|
3637
|
+
const { feasible: _feasible, result: _result, bounded: _bounded, ...paretoPayload } = solution;
|
|
4077
3638
|
paretoVertices.push(paretoPayload);
|
|
4078
3639
|
}
|
|
4079
3640
|
// Derive equality constraints that represent the averaged objective values.
|
|
@@ -4151,7 +3712,7 @@ function createEnhancedBranchAndCutService(options = {}) {
|
|
|
4151
3712
|
// Product score (like SCIP's default)
|
|
4152
3713
|
return Math.max(upEstimate, 1e-6) * Math.max(downEstimate, 1e-6);
|
|
4153
3714
|
};
|
|
4154
|
-
const selectBranchingVariable = (tableau,
|
|
3715
|
+
const selectBranchingVariable = (tableau, _currentEval) => {
|
|
4155
3716
|
const width = tableau.width;
|
|
4156
3717
|
const matrix = tableau.matrix;
|
|
4157
3718
|
const rhsColumn = tableau.rhsColumn;
|
|
@@ -4295,7 +3856,7 @@ function createEnhancedBranchAndCutService(options = {}) {
|
|
|
4295
3856
|
else {
|
|
4296
3857
|
break;
|
|
4297
3858
|
}
|
|
4298
|
-
if (activeBranch.relaxedEvaluation
|
|
3859
|
+
if (activeBranch.relaxedEvaluation >= bestEvaluation) {
|
|
4299
3860
|
continue;
|
|
4300
3861
|
}
|
|
4301
3862
|
const cuts = activeBranch.cuts;
|
|
@@ -4649,7 +4210,7 @@ function createIncrementalBranchAndCutService(options = {}) {
|
|
|
4649
4210
|
else {
|
|
4650
4211
|
break;
|
|
4651
4212
|
}
|
|
4652
|
-
if (activeBranch.relaxedEvaluation
|
|
4213
|
+
if (activeBranch.relaxedEvaluation >= bestEvaluation) {
|
|
4653
4214
|
continue;
|
|
4654
4215
|
}
|
|
4655
4216
|
const parentEval = tableau.evaluation;
|