javascript-lp-solver 1.0.0 → 1.0.1

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 CHANGED
@@ -169,15 +169,15 @@ worker.postMessage(model);
169
169
 
170
170
  ## Performance
171
171
 
172
- Representative benchmarks on a modern laptop:
173
-
174
- | Problem | Variables | Constraints | Integers | Time |
175
- | ---------------- | --------- | ----------- | -------- | ----- |
176
- | Large Farm MIP | 100 | 35 | 100 | 16ms |
177
- | Monster LP | 552 | 600 | 0 | 18ms |
178
- | Monster II MIP | 924 | 888 | 112 | 308ms |
179
- | Stock Cutting | 31 | 5 | 31 | 1.4ms |
180
- | Vendor Selection | 1640 | 1641 | 0 | 1.2s |
172
+ Representative benchmarks (Intel i5-10400 @ 2.9GHz, Node.js 22, 10 runs averaged):
173
+
174
+ | Problem | Variables | Constraints | Integers | Avg Time |
175
+ | ---------------- | --------- | ----------- | -------- | -------- |
176
+ | Large Farm MIP | 100 | 35 | 100 | 67ms |
177
+ | Monster LP | 552 | 600 | 0 | 5ms |
178
+ | Monster II MIP | 924 | 888 | 112 | 121ms |
179
+ | Stock Cutting | 31 | 5 | 31 | 7ms |
180
+ | Vendor Selection | 1640 | 1641 | 40 | 606ms |
181
181
 
182
182
  ## Documentation
183
183
 
@@ -51,6 +51,22 @@ class BranchMinHeap {
51
51
  this.heap = new Array(initialCapacity);
52
52
  this.size = 0;
53
53
  this.seqCounter = 0;
54
+ this.pool = new Array(64);
55
+ this.poolSize = 0;
56
+ }
57
+ allocEntry(branch, seq) {
58
+ if (this.poolSize > 0) {
59
+ const entry = this.pool[--this.poolSize];
60
+ entry.branch = branch;
61
+ entry.seq = seq;
62
+ return entry;
63
+ }
64
+ return { branch, seq };
65
+ }
66
+ freeEntry(entry) {
67
+ if (this.poolSize < 256) {
68
+ this.pool[this.poolSize++] = entry;
69
+ }
54
70
  }
55
71
  get length() {
56
72
  return this.size;
@@ -78,7 +94,7 @@ class BranchMinHeap {
78
94
  if (idx >= heap.length) {
79
95
  heap.length = heap.length * 2;
80
96
  }
81
- const entry = { branch, seq: this.seqCounter++ };
97
+ const entry = this.allocEntry(branch, this.seqCounter++);
82
98
  // Bubble up
83
99
  while (idx > 0) {
84
100
  const parentIdx = (idx - 1) >> 1;
@@ -96,8 +112,11 @@ class BranchMinHeap {
96
112
  return undefined;
97
113
  }
98
114
  const heap = this.heap;
99
- const result = heap[0].branch;
115
+ const poppedEntry = heap[0];
116
+ const result = poppedEntry.branch;
100
117
  this.size--;
118
+ // Return entry to pool
119
+ this.freeEntry(poppedEntry);
101
120
  if (this.size === 0) {
102
121
  return result;
103
122
  }
@@ -165,7 +184,10 @@ function createBranchAndCutService() {
165
184
  let bestEvaluation = Infinity;
166
185
  let bestBranch = null;
167
186
  const bestOptionalObjectivesEvaluations = [];
168
- for (let oInit = 0; oInit < tableau.optionalObjectives.length; oInit += 1) {
187
+ // Cache optionalObjectives reference to avoid repeated property lookups in hot loop
188
+ const optionalObjectives = tableau.optionalObjectives;
189
+ const nOptionalObjectives = optionalObjectives.length;
190
+ for (let oInit = 0; oInit < nOptionalObjectives; oInit += 1) {
169
191
  bestOptionalObjectivesEvaluations.push(Infinity);
170
192
  }
171
193
  const branch = createBranch$2(-Infinity, []);
@@ -199,13 +221,13 @@ function createBranchAndCutService() {
199
221
  }
200
222
  if (evaluation === bestEvaluation) {
201
223
  let isCurrentEvaluationWorse = true;
202
- for (let o = 0; o < tableau.optionalObjectives.length; o += 1) {
203
- if (tableau.optionalObjectives[o].reducedCosts[0] >
204
- bestOptionalObjectivesEvaluations[o]) {
224
+ for (let o = 0; o < nOptionalObjectives; o += 1) {
225
+ const currentCost = optionalObjectives[o].reducedCosts[0];
226
+ const bestCost = bestOptionalObjectivesEvaluations[o];
227
+ if (currentCost > bestCost) {
205
228
  break;
206
229
  }
207
- else if (tableau.optionalObjectives[o].reducedCosts[0] <
208
- bestOptionalObjectivesEvaluations[o]) {
230
+ else if (currentCost < bestCost) {
209
231
  isCurrentEvaluationWorse = false;
210
232
  break;
211
233
  }
@@ -222,9 +244,9 @@ function createBranchAndCutService() {
222
244
  }
223
245
  bestBranch = activeBranch;
224
246
  bestEvaluation = evaluation;
225
- for (let oCopy = 0; oCopy < tableau.optionalObjectives.length; oCopy += 1) {
247
+ for (let oCopy = 0; oCopy < nOptionalObjectives; oCopy += 1) {
226
248
  bestOptionalObjectivesEvaluations[oCopy] =
227
- tableau.optionalObjectives[oCopy].reducedCosts[0];
249
+ optionalObjectives[oCopy].reducedCosts[0];
228
250
  }
229
251
  if ((_e = tableau.model) === null || _e === void 0 ? void 0 : _e.keep_solutions) {
230
252
  const nowSolution = tableau.model.tableau.getSolution();
@@ -242,6 +264,7 @@ function createBranchAndCutService() {
242
264
  }
243
265
  const variable = tableau.getMostFractionalVar();
244
266
  const varIndex = variable.index;
267
+ const varValue = variable.value;
245
268
  const cutsHigh = [];
246
269
  const cutsLow = [];
247
270
  const nCuts = cuts.length;
@@ -260,11 +283,9 @@ function createBranchAndCutService() {
260
283
  cutsLow.push(cut);
261
284
  }
262
285
  }
263
- const min = Math.ceil(variable.value);
264
- const max = Math.floor(variable.value);
265
- const cutHigh = createCut$2("min", varIndex, min);
286
+ const cutHigh = createCut$2("min", varIndex, Math.ceil(varValue));
266
287
  cutsHigh.push(cutHigh);
267
- const cutLow = createCut$2("max", varIndex, max);
288
+ const cutLow = createCut$2("max", varIndex, Math.floor(varValue));
268
289
  cutsLow.push(cutLow);
269
290
  branches.push(createBranch$2(evaluation, cutsHigh));
270
291
  branches.push(createBranch$2(evaluation, cutsLow));
@@ -278,6 +299,50 @@ function createBranchAndCutService() {
278
299
  return { applyCuts, branchAndCut };
279
300
  }
280
301
 
302
+ /**
303
+ * Optimized cycle detector using hash-based O(1) lookup.
304
+ * The original checkForCycles was O(n²) - comparing every pair against all others.
305
+ * This version uses a Map to track where each (leaving,entering) pair occurred,
306
+ * making duplicate detection O(1) average case.
307
+ */
308
+ class CycleDetector {
309
+ constructor() {
310
+ this.pairs = [];
311
+ this.positions = new Map();
312
+ }
313
+ add(leaving, entering) {
314
+ const key = `${leaving}_${entering}`;
315
+ const pos = this.pairs.length;
316
+ this.pairs.push([leaving, entering]);
317
+ const prevPositions = this.positions.get(key);
318
+ if (prevPositions === undefined) {
319
+ this.positions.set(key, [pos]);
320
+ return [];
321
+ }
322
+ // Check if any previous occurrence starts a repeating cycle
323
+ for (const startPos of prevPositions) {
324
+ const cycleLength = pos - startPos;
325
+ // Need at least cycleLength more elements to verify
326
+ if (cycleLength > this.pairs.length - pos) {
327
+ continue;
328
+ }
329
+ let cycleFound = true;
330
+ for (let i = 1; i < cycleLength && startPos + cycleLength + i < this.pairs.length; i++) {
331
+ const p1 = this.pairs[startPos + i];
332
+ const p2 = this.pairs[startPos + cycleLength + i];
333
+ if (p1[0] !== p2[0] || p1[1] !== p2[1]) {
334
+ cycleFound = false;
335
+ break;
336
+ }
337
+ }
338
+ if (cycleFound) {
339
+ return [startPos, cycleLength];
340
+ }
341
+ }
342
+ prevPositions.push(pos);
343
+ return [];
344
+ }
345
+ }
281
346
  function simplex() {
282
347
  this.bounded = true;
283
348
  this.phase1();
@@ -286,21 +351,106 @@ function simplex() {
286
351
  }
287
352
  return this;
288
353
  }
354
+ /**
355
+ * Dual simplex algorithm for warm-starting after adding constraints.
356
+ *
357
+ * Use when: The current solution is dual feasible (reduced costs valid) but
358
+ * may be primal infeasible (some RHS values negative). This is common after
359
+ * adding bound constraints in branch-and-cut.
360
+ *
361
+ * Algorithm:
362
+ * 1. Find a basic variable with negative value (leaving variable)
363
+ * 2. Find entering variable using dual ratio test
364
+ * 3. Pivot to restore primal feasibility
365
+ * 4. Repeat until all basic variables are non-negative
366
+ *
367
+ * @returns Number of iterations, or -1 if dual infeasible
368
+ */
369
+ function dualSimplex() {
370
+ const matrix = this.matrix;
371
+ const width = this.width;
372
+ const rhsColumn = this.rhsColumn;
373
+ const lastColumn = width - 1;
374
+ const lastRow = this.height - 1;
375
+ const precision = this.precision;
376
+ const negPrecision = -precision;
377
+ let iterations = 0;
378
+ const maxIterations = 10000; // Safety limit
379
+ while (iterations < maxIterations) {
380
+ // Step 1: Find leaving variable (row with most negative RHS)
381
+ let leavingRow = 0;
382
+ let minRHS = negPrecision;
383
+ for (let r = 1; r <= lastRow; r++) {
384
+ const rhsValue = matrix[r * width + rhsColumn];
385
+ if (rhsValue < minRHS) {
386
+ minRHS = rhsValue;
387
+ leavingRow = r;
388
+ }
389
+ }
390
+ // If no negative RHS, we're primal feasible - done!
391
+ if (leavingRow === 0) {
392
+ this.feasible = true;
393
+ this.setEvaluation();
394
+ return iterations;
395
+ }
396
+ // Step 2: Find entering variable using dual ratio test
397
+ // For each non-basic variable j with a_ij < 0 (negative coefficient in leaving row),
398
+ // compute ratio = reduced_cost[j] / |a_ij|
399
+ // Choose the one with minimum ratio (to maintain dual feasibility)
400
+ let enteringColumn = 0;
401
+ let minRatio = Infinity;
402
+ const leavingRowOffset = leavingRow * width;
403
+ for (let c = 1; c <= lastColumn; c++) {
404
+ const coefficient = matrix[leavingRowOffset + c];
405
+ // Only consider columns with negative coefficient in leaving row
406
+ if (coefficient < negPrecision) {
407
+ // Reduced cost is in row 0 (cost row)
408
+ const reducedCost = matrix[c];
409
+ // For minimization, reduced costs should be >= 0 for optimality
410
+ // Ratio test: reducedCost / |coefficient|
411
+ if (reducedCost >= negPrecision) {
412
+ const ratio = reducedCost / -coefficient;
413
+ if (ratio < minRatio) {
414
+ minRatio = ratio;
415
+ enteringColumn = c;
416
+ }
417
+ }
418
+ }
419
+ }
420
+ // If no entering column found, the problem is dual infeasible (primal unbounded)
421
+ if (enteringColumn === 0) {
422
+ this.feasible = false;
423
+ return -1;
424
+ }
425
+ // Step 3: Pivot
426
+ this.pivot(leavingRow, enteringColumn);
427
+ iterations++;
428
+ }
429
+ // Hit iteration limit - something went wrong
430
+ this.feasible = false;
431
+ return iterations;
432
+ }
289
433
  function phase1() {
290
434
  const debugCheckForCycles = this.model.checkForCycles;
291
- const varIndexesCycle = [];
435
+ const cycleDetector = debugCheckForCycles ? new CycleDetector() : null;
292
436
  const matrix = this.matrix;
293
437
  const width = this.width;
294
438
  const rhsColumn = this.rhsColumn;
295
439
  const lastColumn = this.width - 1;
296
440
  const lastRow = this.height - 1;
441
+ const precision = this.precision;
442
+ const negPrecision = -precision;
443
+ // Cache arrays for faster access in hot loops
444
+ const unrestrictedVars = this.unrestrictedVars;
445
+ const varIndexByRow = this.varIndexByRow;
446
+ const varIndexByCol = this.varIndexByCol;
297
447
  let unrestricted;
298
448
  let iterations = 0;
299
449
  while (true) {
450
+ // Find leaving row (most negative RHS)
300
451
  let leavingRowIndex = 0;
301
- let rhsValue = -this.precision;
452
+ let rhsValue = negPrecision;
302
453
  for (let r = 1; r <= lastRow; r++) {
303
- unrestricted = this.unrestrictedVars[this.varIndexByRow[r]] === true;
304
454
  const value = matrix[r * width + rhsColumn];
305
455
  if (value < rhsValue) {
306
456
  rhsValue = value;
@@ -311,15 +461,15 @@ function phase1() {
311
461
  this.feasible = true;
312
462
  return iterations;
313
463
  }
464
+ // Find entering column
314
465
  let enteringColumn = 0;
315
466
  let maxQuotient = -Infinity;
316
- const costRowOffset = 0; // row 0
317
467
  const leavingRowOffset = leavingRowIndex * width;
318
468
  for (let c = 1; c <= lastColumn; c++) {
319
469
  const coefficient = matrix[leavingRowOffset + c];
320
- unrestricted = this.unrestrictedVars[this.varIndexByCol[c]] === true;
321
- if (unrestricted || coefficient < -this.precision) {
322
- const quotient = -matrix[costRowOffset + c] / coefficient;
470
+ unrestricted = unrestrictedVars[varIndexByCol[c]] === true;
471
+ if (unrestricted || coefficient < negPrecision) {
472
+ const quotient = -matrix[c] / coefficient; // costRowOffset is 0
323
473
  if (maxQuotient < quotient) {
324
474
  maxQuotient = quotient;
325
475
  enteringColumn = c;
@@ -330,12 +480,8 @@ function phase1() {
330
480
  this.feasible = false;
331
481
  return iterations;
332
482
  }
333
- if (debugCheckForCycles) {
334
- varIndexesCycle.push([
335
- this.varIndexByRow[leavingRowIndex],
336
- this.varIndexByCol[enteringColumn],
337
- ]);
338
- const cycleData = this.checkForCycles(varIndexesCycle);
483
+ if (cycleDetector) {
484
+ const cycleData = cycleDetector.add(varIndexByRow[leavingRowIndex], varIndexByCol[enteringColumn]);
339
485
  if (cycleData.length > 0) {
340
486
  this.model.messages.push("Cycle in phase 1");
341
487
  this.model.messages.push("Start :" + cycleData[0]);
@@ -350,15 +496,21 @@ function phase1() {
350
496
  }
351
497
  function phase2() {
352
498
  const debugCheckForCycles = this.model.checkForCycles;
353
- const varIndexesCycle = [];
499
+ const cycleDetector = debugCheckForCycles ? new CycleDetector() : null;
354
500
  const matrix = this.matrix;
355
501
  const width = this.width;
356
502
  const rhsColumn = this.rhsColumn;
357
503
  const lastColumn = this.width - 1;
358
504
  const lastRow = this.height - 1;
359
505
  const precision = this.precision;
506
+ const negPrecision = -precision;
360
507
  const nOptionalObjectives = this.optionalObjectives.length;
361
508
  let optionalCostsColumns = null;
509
+ // Cache arrays for faster access in hot loops
510
+ const unrestrictedVars = this.unrestrictedVars;
511
+ const varIndexByCol = this.varIndexByCol;
512
+ const varIndexByRow = this.varIndexByRow;
513
+ // Note: costRowIndex is always 0, so we access matrix[c] directly
362
514
  let iterations = 0;
363
515
  let reducedCost;
364
516
  let unrestricted;
@@ -371,7 +523,6 @@ function phase2() {
371
523
  // For small problems, just scan everything (no benefit from partial pricing)
372
524
  const usePartialPricing = nColumns > batchSize * 2;
373
525
  while (true) {
374
- const costRowOffset = this.costRowIndex * width;
375
526
  if (nOptionalObjectives > 0) {
376
527
  optionalCostsColumns = [];
377
528
  }
@@ -388,10 +539,10 @@ function phase2() {
388
539
  const batchStart = this.pricingBatchStart;
389
540
  const batchEnd = Math.min(batchStart + batchSize - 1, lastColumn);
390
541
  for (let c = batchStart; c <= batchEnd; c++) {
391
- reducedCost = matrix[costRowOffset + c];
392
- unrestricted = this.unrestrictedVars[this.varIndexByCol[c]] === true;
542
+ reducedCost = matrix[c]; // costRowOffset is 0
543
+ unrestricted = unrestrictedVars[varIndexByCol[c]] === true;
393
544
  if (nOptionalObjectives > 0 &&
394
- -precision < reducedCost &&
545
+ negPrecision < reducedCost &&
395
546
  reducedCost < precision) {
396
547
  optionalCostsColumns === null || optionalCostsColumns === void 0 ? void 0 : optionalCostsColumns.push(c);
397
548
  continue;
@@ -422,10 +573,10 @@ function phase2() {
422
573
  else {
423
574
  // Full pricing for small problems
424
575
  for (let c = 1; c <= lastColumn; c++) {
425
- reducedCost = matrix[costRowOffset + c];
426
- unrestricted = this.unrestrictedVars[this.varIndexByCol[c]] === true;
576
+ reducedCost = matrix[c]; // costRowOffset is 0
577
+ unrestricted = unrestrictedVars[varIndexByCol[c]] === true;
427
578
  if (nOptionalObjectives > 0 &&
428
- -precision < reducedCost &&
579
+ negPrecision < reducedCost &&
429
580
  reducedCost < precision) {
430
581
  optionalCostsColumns === null || optionalCostsColumns === void 0 ? void 0 : optionalCostsColumns.push(c);
431
582
  continue;
@@ -457,8 +608,8 @@ function phase2() {
457
608
  for (let i = 0; i < optionalCostsColumns.length; i++) {
458
609
  const c = optionalCostsColumns[i];
459
610
  reducedCost = reducedCosts[c];
460
- unrestricted = this.unrestrictedVars[this.varIndexByCol[c]] === true;
461
- if (-precision < reducedCost && reducedCost < precision) {
611
+ unrestricted = unrestrictedVars[varIndexByCol[c]] === true;
612
+ if (negPrecision < reducedCost && reducedCost < precision) {
462
613
  optionalCostsColumns2.push(c);
463
614
  continue;
464
615
  }
@@ -487,15 +638,14 @@ function phase2() {
487
638
  }
488
639
  let leavingRow = 0;
489
640
  let minQuotient = Infinity;
490
- this.varIndexByRow;
491
641
  for (let r = 1; r <= lastRow; r++) {
492
642
  const rowOffset = r * width;
493
643
  const rhsValue = matrix[rowOffset + rhsColumn];
494
644
  const colValue = matrix[rowOffset + enteringColumn];
495
- if (-precision < colValue && colValue < precision) {
645
+ if (negPrecision < colValue && colValue < precision) {
496
646
  continue;
497
647
  }
498
- if (colValue > 0 && precision > rhsValue && rhsValue > -precision) {
648
+ if (colValue > 0 && precision > rhsValue && rhsValue > negPrecision) {
499
649
  minQuotient = 0;
500
650
  leavingRow = r;
501
651
  break;
@@ -509,15 +659,11 @@ function phase2() {
509
659
  if (minQuotient === Infinity) {
510
660
  this.evaluation = -Infinity;
511
661
  this.bounded = false;
512
- this.unboundedVarIndex = this.varIndexByCol[enteringColumn];
662
+ this.unboundedVarIndex = varIndexByCol[enteringColumn];
513
663
  return iterations;
514
664
  }
515
- if (debugCheckForCycles) {
516
- varIndexesCycle.push([
517
- this.varIndexByRow[leavingRow],
518
- this.varIndexByCol[enteringColumn],
519
- ]);
520
- const cycleData = this.checkForCycles(varIndexesCycle);
665
+ if (cycleDetector) {
666
+ const cycleData = cycleDetector.add(varIndexByRow[leavingRow], varIndexByCol[enteringColumn]);
521
667
  if (cycleData.length > 0) {
522
668
  this.model.messages.push("Cycle in phase 2");
523
669
  this.model.messages.push("Start :" + cycleData[0]);
@@ -530,13 +676,20 @@ function phase2() {
530
676
  iterations += 1;
531
677
  }
532
678
  }
533
- // Pre-allocated array for non-zero column tracking
534
- const nonZeroColumns = [];
679
+ // Pre-allocated typed arrays for pivot optimization (better cache performance)
680
+ let nonZeroColumns = new Int32Array(1024);
681
+ let pivotRowCache = new Float64Array(1024);
535
682
  function pivot(pivotRowIndex, pivotColumnIndex) {
536
683
  const matrix = this.matrix;
537
684
  const width = this.width;
685
+ // Ensure work arrays are large enough
686
+ if (width > nonZeroColumns.length) {
687
+ nonZeroColumns = new Int32Array(width * 2);
688
+ pivotRowCache = new Float64Array(width * 2);
689
+ }
538
690
  const pivotRowOffset = pivotRowIndex * width;
539
691
  const quotient = matrix[pivotRowOffset + pivotColumnIndex];
692
+ const invQuotient = 1 / quotient;
540
693
  const height = this.height;
541
694
  const leavingBasicIndex = this.varIndexByRow[pivotRowIndex];
542
695
  const enteringBasicIndex = this.varIndexByCol[pivotColumnIndex];
@@ -546,22 +699,24 @@ function pivot(pivotRowIndex, pivotColumnIndex) {
546
699
  this.rowByVarIndex[leavingBasicIndex] = -1;
547
700
  this.colByVarIndex[enteringBasicIndex] = -1;
548
701
  this.colByVarIndex[leavingBasicIndex] = pivotColumnIndex;
549
- // Normalize pivot row and track non-zero columns
702
+ // Normalize pivot row, track non-zero columns, and cache values for locality
550
703
  let nNonZeroColumns = 0;
551
704
  for (let c = 0; c < width; c++) {
552
705
  const idx = pivotRowOffset + c;
553
706
  const val = matrix[idx];
554
707
  if (!(val >= -1e-16 && val <= 1e-16)) {
555
- matrix[idx] = val / quotient;
708
+ const normalized = val / quotient;
709
+ matrix[idx] = normalized;
556
710
  nonZeroColumns[nNonZeroColumns] = c;
711
+ pivotRowCache[nNonZeroColumns] = normalized;
557
712
  nNonZeroColumns++;
558
713
  }
559
714
  else {
560
715
  matrix[idx] = 0;
561
716
  }
562
717
  }
563
- matrix[pivotRowOffset + pivotColumnIndex] = 1 / quotient;
564
- // Update all other rows
718
+ matrix[pivotRowOffset + pivotColumnIndex] = invQuotient;
719
+ // Update all other rows using cached pivot row values
565
720
  for (let r = 0; r < height; r++) {
566
721
  if (r !== pivotRowIndex) {
567
722
  const rowOffset = r * width;
@@ -569,13 +724,16 @@ function pivot(pivotRowIndex, pivotColumnIndex) {
569
724
  if (!(pivotColVal >= -1e-16 && pivotColVal <= 1e-16)) {
570
725
  const coefficient = pivotColVal;
571
726
  if (!(coefficient >= -1e-16 && coefficient <= 1e-16)) {
727
+ // Use cached pivot row values for better cache locality
572
728
  for (let i = 0; i < nNonZeroColumns; i++) {
573
729
  const c = nonZeroColumns[i];
574
- const v0 = matrix[pivotRowOffset + c];
730
+ const v0 = pivotRowCache[i];
731
+ // Inner zero check is critical for numerical stability
575
732
  if (!(v0 >= -1e-16 && v0 <= 1e-16)) {
576
- matrix[rowOffset + c] = matrix[rowOffset + c] - coefficient * v0;
733
+ matrix[rowOffset + c] -= coefficient * v0;
577
734
  }
578
735
  else if (v0 !== 0) {
736
+ // Clean up near-zero values in pivot row
579
737
  matrix[pivotRowOffset + c] = 0;
580
738
  }
581
739
  }
@@ -587,7 +745,7 @@ function pivot(pivotRowIndex, pivotColumnIndex) {
587
745
  }
588
746
  }
589
747
  }
590
- // Update optional objectives
748
+ // Update optional objectives using cached pivot row values
591
749
  const optionalObjectives = this.optionalObjectives;
592
750
  const nOptionalObjectives = optionalObjectives.length;
593
751
  if (nOptionalObjectives > 0) {
@@ -597,12 +755,9 @@ function pivot(pivotRowIndex, pivotColumnIndex) {
597
755
  if (coefficient !== 0) {
598
756
  for (let i = 0; i < nNonZeroColumns; i++) {
599
757
  const c = nonZeroColumns[i];
600
- const v0 = matrix[pivotRowOffset + c];
601
- if (v0 !== 0) {
602
- reducedCosts[c] = reducedCosts[c] - coefficient * v0;
603
- }
758
+ reducedCosts[c] -= coefficient * pivotRowCache[i];
604
759
  }
605
- reducedCosts[pivotColumnIndex] = -coefficient / quotient;
760
+ reducedCosts[pivotColumnIndex] = -coefficient * invQuotient;
606
761
  }
607
762
  }
608
763
  }
@@ -1436,11 +1591,13 @@ function addCutConstraints(cutConstraints) {
1436
1591
  const heightWithCuts = height + nCutConstraints;
1437
1592
  const width = this.width;
1438
1593
  const lastColumn = width - 1;
1439
- // Grow the matrix to accommodate new rows
1594
+ // Grow the matrix to accommodate new rows (with over-allocation to reduce reallocation frequency)
1440
1595
  const oldMatrix = this.matrix;
1441
1596
  const newSize = heightWithCuts * width;
1442
1597
  if (oldMatrix.length < newSize) {
1443
- const newMatrix = new Float64Array(newSize);
1598
+ // Over-allocate by 50% to reduce future reallocations
1599
+ const allocSize = Math.ceil(newSize * 1.5);
1600
+ const newMatrix = new Float64Array(allocSize);
1444
1601
  newMatrix.set(oldMatrix);
1445
1602
  this.matrix = newMatrix;
1446
1603
  }
@@ -1495,10 +1652,12 @@ function addLowerBoundMIRCut(rowIndex) {
1495
1652
  }
1496
1653
  const height = this.height;
1497
1654
  const newRowOffset = height * width;
1498
- // Grow matrix to add new row
1655
+ // Grow matrix to add new row (with over-allocation to reduce reallocation frequency)
1499
1656
  const newSize = (height + 1) * width;
1500
1657
  if (matrix.length < newSize) {
1501
- const newMatrix = new Float64Array(newSize);
1658
+ // Over-allocate by 50% to reduce future reallocations
1659
+ const allocSize = Math.ceil(newSize * 1.5);
1660
+ const newMatrix = new Float64Array(allocSize);
1502
1661
  newMatrix.set(matrix);
1503
1662
  this.matrix = newMatrix;
1504
1663
  }
@@ -1547,10 +1706,12 @@ function addUpperBoundMIRCut(rowIndex) {
1547
1706
  }
1548
1707
  const height = this.height;
1549
1708
  const newRowOffset = height * width;
1550
- // Grow matrix to add new row
1709
+ // Grow matrix to add new row (with over-allocation to reduce reallocation frequency)
1551
1710
  const newSize = (height + 1) * width;
1552
1711
  if (matrix.length < newSize) {
1553
- const newMatrix = new Float64Array(newSize);
1712
+ // Over-allocate by 50% to reduce future reallocations
1713
+ const allocSize = Math.ceil(newSize * 1.5);
1714
+ const newMatrix = new Float64Array(allocSize);
1554
1715
  newMatrix.set(matrix);
1555
1716
  this.matrix = newMatrix;
1556
1717
  }
@@ -2241,6 +2402,14 @@ class Tableau {
2241
2402
  phase2() {
2242
2403
  return phase2.call(this);
2243
2404
  }
2405
+ /**
2406
+ * Dual simplex for warm-starting after adding bound constraints.
2407
+ * Use when solution is dual feasible but may be primal infeasible.
2408
+ * @returns Number of iterations, or -1 if dual infeasible
2409
+ */
2410
+ dualSimplex() {
2411
+ return dualSimplex.call(this);
2412
+ }
2244
2413
  pivot(pivotRowIndex, pivotColumnIndex) {
2245
2414
  pivot.call(this, pivotRowIndex, pivotColumnIndex);
2246
2415
  }
@@ -3735,8 +3904,16 @@ var ReformatLP = /*@__PURE__*/getDefaultExportFromCjs(reformat);
3735
3904
  function createCut$1(type, varIndex, value) {
3736
3905
  return { type, varIndex, value };
3737
3906
  }
3738
- function createBranch$1(relaxedEvaluation, cuts, depth) {
3739
- return { relaxedEvaluation, cuts, depth };
3907
+ function createBranch$1(relaxedEvaluation, cuts, depth, branchVarIndex, branchDirection, branchFractionality, parentEvaluation) {
3908
+ return {
3909
+ relaxedEvaluation,
3910
+ cuts,
3911
+ depth,
3912
+ branchVarIndex,
3913
+ branchDirection,
3914
+ branchFractionality,
3915
+ parentEvaluation,
3916
+ };
3740
3917
  }
3741
3918
  /**
3742
3919
  * Enhanced branch-and-cut with:
@@ -3745,7 +3922,8 @@ function createBranch$1(relaxedEvaluation, cuts, depth) {
3745
3922
  * - Diving heuristic for quick feasible solutions
3746
3923
  */
3747
3924
  function createEnhancedBranchAndCutService(options = {}) {
3748
- const { nodeSelection = "hybrid", branching = "pseudocost", useDiving = true, strongBranchingCandidates = 5, } = options;
3925
+ const { nodeSelection = "hybrid", branching = "pseudocost", useDiving: _useDiving = true, // Reserved for future diving heuristic
3926
+ strongBranchingCandidates = 5, } = options;
3749
3927
  // Pseudocost data per variable index
3750
3928
  const pseudoCosts = new Map();
3751
3929
  const getPseudoCost = (varIndex) => {
@@ -3926,7 +4104,7 @@ function createEnhancedBranchAndCutService(options = {}) {
3926
4104
  continue;
3927
4105
  }
3928
4106
  const cuts = activeBranch.cuts;
3929
- const parentEval = tableau.evaluation;
4107
+ tableau.evaluation;
3930
4108
  applyCuts(tableau, cuts);
3931
4109
  iterations++;
3932
4110
  if (!tableau.feasible) {
@@ -3937,12 +4115,12 @@ function createEnhancedBranchAndCutService(options = {}) {
3937
4115
  continue;
3938
4116
  }
3939
4117
  // Update pseudocosts based on observed improvement
3940
- if (cuts.length > 0 && parentEval !== 0) {
3941
- const lastCut = cuts[cuts.length - 1];
3942
- const improvement = Math.abs(evaluation - parentEval);
3943
- // Estimate fraction (simplified)
3944
- const fraction = 0.5;
3945
- updatePseudoCost(lastCut.varIndex, lastCut.type === "min" ? "up" : "down", improvement, fraction);
4118
+ if (activeBranch.branchVarIndex !== undefined &&
4119
+ activeBranch.branchDirection !== undefined &&
4120
+ activeBranch.branchFractionality !== undefined &&
4121
+ activeBranch.parentEvaluation !== undefined) {
4122
+ const improvement = Math.abs(evaluation - activeBranch.parentEvaluation);
4123
+ updatePseudoCost(activeBranch.branchVarIndex, activeBranch.branchDirection, improvement, activeBranch.branchFractionality);
3946
4124
  }
3947
4125
  if (evaluation === bestEvaluation) {
3948
4126
  let isCurrentEvaluationWorse = true;
@@ -4021,22 +4199,24 @@ function createEnhancedBranchAndCutService(options = {}) {
4021
4199
  cutsLow.push(cut);
4022
4200
  }
4023
4201
  }
4024
- const min = Math.ceil(varValue);
4025
- const max = Math.floor(varValue);
4026
- const cutHigh = createCut$1("min", varIndex, min);
4202
+ const ceilVal = Math.ceil(varValue);
4203
+ const floorVal = Math.floor(varValue);
4204
+ const fracUp = ceilVal - varValue; // Distance to ceil
4205
+ const fracDown = varValue - floorVal; // Distance to floor
4206
+ const cutHigh = createCut$1("min", varIndex, ceilVal);
4027
4207
  cutsHigh.push(cutHigh);
4028
- const cutLow = createCut$1("max", varIndex, max);
4208
+ const cutLow = createCut$1("max", varIndex, floorVal);
4029
4209
  cutsLow.push(cutLow);
4030
4210
  const newDepth = activeBranch.depth + 1;
4031
4211
  if (useDepthFirst) {
4032
4212
  // Push in reverse order so 'up' branch is explored first
4033
4213
  // (often better for minimization with binary vars)
4034
- depthFirstStack.push(createBranch$1(evaluation, cutsLow, newDepth));
4035
- depthFirstStack.push(createBranch$1(evaluation, cutsHigh, newDepth));
4214
+ depthFirstStack.push(createBranch$1(evaluation, cutsLow, newDepth, varIndex, "down", fracDown, evaluation));
4215
+ depthFirstStack.push(createBranch$1(evaluation, cutsHigh, newDepth, varIndex, "up", fracUp, evaluation));
4036
4216
  }
4037
4217
  else {
4038
- branches.push(createBranch$1(evaluation, cutsHigh, newDepth));
4039
- branches.push(createBranch$1(evaluation, cutsLow, newDepth));
4218
+ branches.push(createBranch$1(evaluation, cutsHigh, newDepth, varIndex, "up", fracUp, evaluation));
4219
+ branches.push(createBranch$1(evaluation, cutsLow, newDepth, varIndex, "down", fracDown, evaluation));
4040
4220
  }
4041
4221
  }
4042
4222
  }