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