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/dist/index.d.ts CHANGED
@@ -370,6 +370,12 @@ declare class Tableau {
370
370
  simplex(): this;
371
371
  phase1(): number;
372
372
  phase2(): number;
373
+ /**
374
+ * Dual simplex for warm-starting after adding bound constraints.
375
+ * Use when solution is dual feasible but may be primal infeasible.
376
+ * @returns Number of iterations, or -1 if dual infeasible
377
+ */
378
+ dualSimplex(): number;
373
379
  pivot(pivotRowIndex: number, pivotColumnIndex: number): void;
374
380
  checkForCycles(varIndexes: Array<[number, number]>): number[];
375
381
  countIntegerValues(): number;
package/dist/index.mjs CHANGED
@@ -54,6 +54,22 @@ class BranchMinHeap {
54
54
  this.heap = new Array(initialCapacity);
55
55
  this.size = 0;
56
56
  this.seqCounter = 0;
57
+ this.pool = new Array(64);
58
+ this.poolSize = 0;
59
+ }
60
+ allocEntry(branch, seq) {
61
+ if (this.poolSize > 0) {
62
+ const entry = this.pool[--this.poolSize];
63
+ entry.branch = branch;
64
+ entry.seq = seq;
65
+ return entry;
66
+ }
67
+ return { branch, seq };
68
+ }
69
+ freeEntry(entry) {
70
+ if (this.poolSize < 256) {
71
+ this.pool[this.poolSize++] = entry;
72
+ }
57
73
  }
58
74
  get length() {
59
75
  return this.size;
@@ -81,7 +97,7 @@ class BranchMinHeap {
81
97
  if (idx >= heap.length) {
82
98
  heap.length = heap.length * 2;
83
99
  }
84
- const entry = { branch, seq: this.seqCounter++ };
100
+ const entry = this.allocEntry(branch, this.seqCounter++);
85
101
  // Bubble up
86
102
  while (idx > 0) {
87
103
  const parentIdx = (idx - 1) >> 1;
@@ -99,8 +115,11 @@ class BranchMinHeap {
99
115
  return undefined;
100
116
  }
101
117
  const heap = this.heap;
102
- const result = heap[0].branch;
118
+ const poppedEntry = heap[0];
119
+ const result = poppedEntry.branch;
103
120
  this.size--;
121
+ // Return entry to pool
122
+ this.freeEntry(poppedEntry);
104
123
  if (this.size === 0) {
105
124
  return result;
106
125
  }
@@ -168,7 +187,10 @@ function createBranchAndCutService() {
168
187
  let bestEvaluation = Infinity;
169
188
  let bestBranch = null;
170
189
  const bestOptionalObjectivesEvaluations = [];
171
- for (let oInit = 0; oInit < tableau.optionalObjectives.length; oInit += 1) {
190
+ // Cache optionalObjectives reference to avoid repeated property lookups in hot loop
191
+ const optionalObjectives = tableau.optionalObjectives;
192
+ const nOptionalObjectives = optionalObjectives.length;
193
+ for (let oInit = 0; oInit < nOptionalObjectives; oInit += 1) {
172
194
  bestOptionalObjectivesEvaluations.push(Infinity);
173
195
  }
174
196
  const branch = createBranch$2(-Infinity, []);
@@ -202,13 +224,13 @@ function createBranchAndCutService() {
202
224
  }
203
225
  if (evaluation === bestEvaluation) {
204
226
  let isCurrentEvaluationWorse = true;
205
- for (let o = 0; o < tableau.optionalObjectives.length; o += 1) {
206
- if (tableau.optionalObjectives[o].reducedCosts[0] >
207
- bestOptionalObjectivesEvaluations[o]) {
227
+ for (let o = 0; o < nOptionalObjectives; o += 1) {
228
+ const currentCost = optionalObjectives[o].reducedCosts[0];
229
+ const bestCost = bestOptionalObjectivesEvaluations[o];
230
+ if (currentCost > bestCost) {
208
231
  break;
209
232
  }
210
- else if (tableau.optionalObjectives[o].reducedCosts[0] <
211
- bestOptionalObjectivesEvaluations[o]) {
233
+ else if (currentCost < bestCost) {
212
234
  isCurrentEvaluationWorse = false;
213
235
  break;
214
236
  }
@@ -225,9 +247,9 @@ function createBranchAndCutService() {
225
247
  }
226
248
  bestBranch = activeBranch;
227
249
  bestEvaluation = evaluation;
228
- for (let oCopy = 0; oCopy < tableau.optionalObjectives.length; oCopy += 1) {
250
+ for (let oCopy = 0; oCopy < nOptionalObjectives; oCopy += 1) {
229
251
  bestOptionalObjectivesEvaluations[oCopy] =
230
- tableau.optionalObjectives[oCopy].reducedCosts[0];
252
+ optionalObjectives[oCopy].reducedCosts[0];
231
253
  }
232
254
  if ((_e = tableau.model) === null || _e === void 0 ? void 0 : _e.keep_solutions) {
233
255
  const nowSolution = tableau.model.tableau.getSolution();
@@ -245,6 +267,7 @@ function createBranchAndCutService() {
245
267
  }
246
268
  const variable = tableau.getMostFractionalVar();
247
269
  const varIndex = variable.index;
270
+ const varValue = variable.value;
248
271
  const cutsHigh = [];
249
272
  const cutsLow = [];
250
273
  const nCuts = cuts.length;
@@ -263,11 +286,9 @@ function createBranchAndCutService() {
263
286
  cutsLow.push(cut);
264
287
  }
265
288
  }
266
- const min = Math.ceil(variable.value);
267
- const max = Math.floor(variable.value);
268
- const cutHigh = createCut$2("min", varIndex, min);
289
+ const cutHigh = createCut$2("min", varIndex, Math.ceil(varValue));
269
290
  cutsHigh.push(cutHigh);
270
- const cutLow = createCut$2("max", varIndex, max);
291
+ const cutLow = createCut$2("max", varIndex, Math.floor(varValue));
271
292
  cutsLow.push(cutLow);
272
293
  branches.push(createBranch$2(evaluation, cutsHigh));
273
294
  branches.push(createBranch$2(evaluation, cutsLow));
@@ -281,6 +302,50 @@ function createBranchAndCutService() {
281
302
  return { applyCuts, branchAndCut };
282
303
  }
283
304
 
305
+ /**
306
+ * Optimized cycle detector using hash-based O(1) lookup.
307
+ * The original checkForCycles was O(n²) - comparing every pair against all others.
308
+ * This version uses a Map to track where each (leaving,entering) pair occurred,
309
+ * making duplicate detection O(1) average case.
310
+ */
311
+ class CycleDetector {
312
+ constructor() {
313
+ this.pairs = [];
314
+ this.positions = new Map();
315
+ }
316
+ add(leaving, entering) {
317
+ const key = `${leaving}_${entering}`;
318
+ const pos = this.pairs.length;
319
+ this.pairs.push([leaving, entering]);
320
+ const prevPositions = this.positions.get(key);
321
+ if (prevPositions === undefined) {
322
+ this.positions.set(key, [pos]);
323
+ return [];
324
+ }
325
+ // Check if any previous occurrence starts a repeating cycle
326
+ for (const startPos of prevPositions) {
327
+ const cycleLength = pos - startPos;
328
+ // Need at least cycleLength more elements to verify
329
+ if (cycleLength > this.pairs.length - pos) {
330
+ continue;
331
+ }
332
+ let cycleFound = true;
333
+ for (let i = 1; i < cycleLength && startPos + cycleLength + i < this.pairs.length; i++) {
334
+ const p1 = this.pairs[startPos + i];
335
+ const p2 = this.pairs[startPos + cycleLength + i];
336
+ if (p1[0] !== p2[0] || p1[1] !== p2[1]) {
337
+ cycleFound = false;
338
+ break;
339
+ }
340
+ }
341
+ if (cycleFound) {
342
+ return [startPos, cycleLength];
343
+ }
344
+ }
345
+ prevPositions.push(pos);
346
+ return [];
347
+ }
348
+ }
284
349
  function simplex() {
285
350
  this.bounded = true;
286
351
  this.phase1();
@@ -289,21 +354,106 @@ function simplex() {
289
354
  }
290
355
  return this;
291
356
  }
357
+ /**
358
+ * Dual simplex algorithm for warm-starting after adding constraints.
359
+ *
360
+ * Use when: The current solution is dual feasible (reduced costs valid) but
361
+ * may be primal infeasible (some RHS values negative). This is common after
362
+ * adding bound constraints in branch-and-cut.
363
+ *
364
+ * Algorithm:
365
+ * 1. Find a basic variable with negative value (leaving variable)
366
+ * 2. Find entering variable using dual ratio test
367
+ * 3. Pivot to restore primal feasibility
368
+ * 4. Repeat until all basic variables are non-negative
369
+ *
370
+ * @returns Number of iterations, or -1 if dual infeasible
371
+ */
372
+ function dualSimplex() {
373
+ const matrix = this.matrix;
374
+ const width = this.width;
375
+ const rhsColumn = this.rhsColumn;
376
+ const lastColumn = width - 1;
377
+ const lastRow = this.height - 1;
378
+ const precision = this.precision;
379
+ const negPrecision = -precision;
380
+ let iterations = 0;
381
+ const maxIterations = 10000; // Safety limit
382
+ while (iterations < maxIterations) {
383
+ // Step 1: Find leaving variable (row with most negative RHS)
384
+ let leavingRow = 0;
385
+ let minRHS = negPrecision;
386
+ for (let r = 1; r <= lastRow; r++) {
387
+ const rhsValue = matrix[r * width + rhsColumn];
388
+ if (rhsValue < minRHS) {
389
+ minRHS = rhsValue;
390
+ leavingRow = r;
391
+ }
392
+ }
393
+ // If no negative RHS, we're primal feasible - done!
394
+ if (leavingRow === 0) {
395
+ this.feasible = true;
396
+ this.setEvaluation();
397
+ return iterations;
398
+ }
399
+ // Step 2: Find entering variable using dual ratio test
400
+ // For each non-basic variable j with a_ij < 0 (negative coefficient in leaving row),
401
+ // compute ratio = reduced_cost[j] / |a_ij|
402
+ // Choose the one with minimum ratio (to maintain dual feasibility)
403
+ let enteringColumn = 0;
404
+ let minRatio = Infinity;
405
+ const leavingRowOffset = leavingRow * width;
406
+ for (let c = 1; c <= lastColumn; c++) {
407
+ const coefficient = matrix[leavingRowOffset + c];
408
+ // Only consider columns with negative coefficient in leaving row
409
+ if (coefficient < negPrecision) {
410
+ // Reduced cost is in row 0 (cost row)
411
+ const reducedCost = matrix[c];
412
+ // For minimization, reduced costs should be >= 0 for optimality
413
+ // Ratio test: reducedCost / |coefficient|
414
+ if (reducedCost >= negPrecision) {
415
+ const ratio = reducedCost / -coefficient;
416
+ if (ratio < minRatio) {
417
+ minRatio = ratio;
418
+ enteringColumn = c;
419
+ }
420
+ }
421
+ }
422
+ }
423
+ // If no entering column found, the problem is dual infeasible (primal unbounded)
424
+ if (enteringColumn === 0) {
425
+ this.feasible = false;
426
+ return -1;
427
+ }
428
+ // Step 3: Pivot
429
+ this.pivot(leavingRow, enteringColumn);
430
+ iterations++;
431
+ }
432
+ // Hit iteration limit - something went wrong
433
+ this.feasible = false;
434
+ return iterations;
435
+ }
292
436
  function phase1() {
293
437
  const debugCheckForCycles = this.model.checkForCycles;
294
- const varIndexesCycle = [];
438
+ const cycleDetector = debugCheckForCycles ? new CycleDetector() : null;
295
439
  const matrix = this.matrix;
296
440
  const width = this.width;
297
441
  const rhsColumn = this.rhsColumn;
298
442
  const lastColumn = this.width - 1;
299
443
  const lastRow = this.height - 1;
444
+ const precision = this.precision;
445
+ const negPrecision = -precision;
446
+ // Cache arrays for faster access in hot loops
447
+ const unrestrictedVars = this.unrestrictedVars;
448
+ const varIndexByRow = this.varIndexByRow;
449
+ const varIndexByCol = this.varIndexByCol;
300
450
  let unrestricted;
301
451
  let iterations = 0;
302
452
  while (true) {
453
+ // Find leaving row (most negative RHS)
303
454
  let leavingRowIndex = 0;
304
- let rhsValue = -this.precision;
455
+ let rhsValue = negPrecision;
305
456
  for (let r = 1; r <= lastRow; r++) {
306
- unrestricted = this.unrestrictedVars[this.varIndexByRow[r]] === true;
307
457
  const value = matrix[r * width + rhsColumn];
308
458
  if (value < rhsValue) {
309
459
  rhsValue = value;
@@ -314,15 +464,15 @@ function phase1() {
314
464
  this.feasible = true;
315
465
  return iterations;
316
466
  }
467
+ // Find entering column
317
468
  let enteringColumn = 0;
318
469
  let maxQuotient = -Infinity;
319
- const costRowOffset = 0; // row 0
320
470
  const leavingRowOffset = leavingRowIndex * width;
321
471
  for (let c = 1; c <= lastColumn; c++) {
322
472
  const coefficient = matrix[leavingRowOffset + c];
323
- unrestricted = this.unrestrictedVars[this.varIndexByCol[c]] === true;
324
- if (unrestricted || coefficient < -this.precision) {
325
- const quotient = -matrix[costRowOffset + c] / coefficient;
473
+ unrestricted = unrestrictedVars[varIndexByCol[c]] === true;
474
+ if (unrestricted || coefficient < negPrecision) {
475
+ const quotient = -matrix[c] / coefficient; // costRowOffset is 0
326
476
  if (maxQuotient < quotient) {
327
477
  maxQuotient = quotient;
328
478
  enteringColumn = c;
@@ -333,12 +483,8 @@ function phase1() {
333
483
  this.feasible = false;
334
484
  return iterations;
335
485
  }
336
- if (debugCheckForCycles) {
337
- varIndexesCycle.push([
338
- this.varIndexByRow[leavingRowIndex],
339
- this.varIndexByCol[enteringColumn],
340
- ]);
341
- const cycleData = this.checkForCycles(varIndexesCycle);
486
+ if (cycleDetector) {
487
+ const cycleData = cycleDetector.add(varIndexByRow[leavingRowIndex], varIndexByCol[enteringColumn]);
342
488
  if (cycleData.length > 0) {
343
489
  this.model.messages.push("Cycle in phase 1");
344
490
  this.model.messages.push("Start :" + cycleData[0]);
@@ -353,15 +499,21 @@ function phase1() {
353
499
  }
354
500
  function phase2() {
355
501
  const debugCheckForCycles = this.model.checkForCycles;
356
- const varIndexesCycle = [];
502
+ const cycleDetector = debugCheckForCycles ? new CycleDetector() : null;
357
503
  const matrix = this.matrix;
358
504
  const width = this.width;
359
505
  const rhsColumn = this.rhsColumn;
360
506
  const lastColumn = this.width - 1;
361
507
  const lastRow = this.height - 1;
362
508
  const precision = this.precision;
509
+ const negPrecision = -precision;
363
510
  const nOptionalObjectives = this.optionalObjectives.length;
364
511
  let optionalCostsColumns = null;
512
+ // Cache arrays for faster access in hot loops
513
+ const unrestrictedVars = this.unrestrictedVars;
514
+ const varIndexByCol = this.varIndexByCol;
515
+ const varIndexByRow = this.varIndexByRow;
516
+ // Note: costRowIndex is always 0, so we access matrix[c] directly
365
517
  let iterations = 0;
366
518
  let reducedCost;
367
519
  let unrestricted;
@@ -374,7 +526,6 @@ function phase2() {
374
526
  // For small problems, just scan everything (no benefit from partial pricing)
375
527
  const usePartialPricing = nColumns > batchSize * 2;
376
528
  while (true) {
377
- const costRowOffset = this.costRowIndex * width;
378
529
  if (nOptionalObjectives > 0) {
379
530
  optionalCostsColumns = [];
380
531
  }
@@ -391,10 +542,10 @@ function phase2() {
391
542
  const batchStart = this.pricingBatchStart;
392
543
  const batchEnd = Math.min(batchStart + batchSize - 1, lastColumn);
393
544
  for (let c = batchStart; c <= batchEnd; c++) {
394
- reducedCost = matrix[costRowOffset + c];
395
- unrestricted = this.unrestrictedVars[this.varIndexByCol[c]] === true;
545
+ reducedCost = matrix[c]; // costRowOffset is 0
546
+ unrestricted = unrestrictedVars[varIndexByCol[c]] === true;
396
547
  if (nOptionalObjectives > 0 &&
397
- -precision < reducedCost &&
548
+ negPrecision < reducedCost &&
398
549
  reducedCost < precision) {
399
550
  optionalCostsColumns === null || optionalCostsColumns === void 0 ? void 0 : optionalCostsColumns.push(c);
400
551
  continue;
@@ -425,10 +576,10 @@ function phase2() {
425
576
  else {
426
577
  // Full pricing for small problems
427
578
  for (let c = 1; c <= lastColumn; c++) {
428
- reducedCost = matrix[costRowOffset + c];
429
- unrestricted = this.unrestrictedVars[this.varIndexByCol[c]] === true;
579
+ reducedCost = matrix[c]; // costRowOffset is 0
580
+ unrestricted = unrestrictedVars[varIndexByCol[c]] === true;
430
581
  if (nOptionalObjectives > 0 &&
431
- -precision < reducedCost &&
582
+ negPrecision < reducedCost &&
432
583
  reducedCost < precision) {
433
584
  optionalCostsColumns === null || optionalCostsColumns === void 0 ? void 0 : optionalCostsColumns.push(c);
434
585
  continue;
@@ -460,8 +611,8 @@ function phase2() {
460
611
  for (let i = 0; i < optionalCostsColumns.length; i++) {
461
612
  const c = optionalCostsColumns[i];
462
613
  reducedCost = reducedCosts[c];
463
- unrestricted = this.unrestrictedVars[this.varIndexByCol[c]] === true;
464
- if (-precision < reducedCost && reducedCost < precision) {
614
+ unrestricted = unrestrictedVars[varIndexByCol[c]] === true;
615
+ if (negPrecision < reducedCost && reducedCost < precision) {
465
616
  optionalCostsColumns2.push(c);
466
617
  continue;
467
618
  }
@@ -490,15 +641,14 @@ function phase2() {
490
641
  }
491
642
  let leavingRow = 0;
492
643
  let minQuotient = Infinity;
493
- this.varIndexByRow;
494
644
  for (let r = 1; r <= lastRow; r++) {
495
645
  const rowOffset = r * width;
496
646
  const rhsValue = matrix[rowOffset + rhsColumn];
497
647
  const colValue = matrix[rowOffset + enteringColumn];
498
- if (-precision < colValue && colValue < precision) {
648
+ if (negPrecision < colValue && colValue < precision) {
499
649
  continue;
500
650
  }
501
- if (colValue > 0 && precision > rhsValue && rhsValue > -precision) {
651
+ if (colValue > 0 && precision > rhsValue && rhsValue > negPrecision) {
502
652
  minQuotient = 0;
503
653
  leavingRow = r;
504
654
  break;
@@ -512,15 +662,11 @@ function phase2() {
512
662
  if (minQuotient === Infinity) {
513
663
  this.evaluation = -Infinity;
514
664
  this.bounded = false;
515
- this.unboundedVarIndex = this.varIndexByCol[enteringColumn];
665
+ this.unboundedVarIndex = varIndexByCol[enteringColumn];
516
666
  return iterations;
517
667
  }
518
- if (debugCheckForCycles) {
519
- varIndexesCycle.push([
520
- this.varIndexByRow[leavingRow],
521
- this.varIndexByCol[enteringColumn],
522
- ]);
523
- const cycleData = this.checkForCycles(varIndexesCycle);
668
+ if (cycleDetector) {
669
+ const cycleData = cycleDetector.add(varIndexByRow[leavingRow], varIndexByCol[enteringColumn]);
524
670
  if (cycleData.length > 0) {
525
671
  this.model.messages.push("Cycle in phase 2");
526
672
  this.model.messages.push("Start :" + cycleData[0]);
@@ -533,13 +679,20 @@ function phase2() {
533
679
  iterations += 1;
534
680
  }
535
681
  }
536
- // Pre-allocated array for non-zero column tracking
537
- const nonZeroColumns = [];
682
+ // Pre-allocated typed arrays for pivot optimization (better cache performance)
683
+ let nonZeroColumns = new Int32Array(1024);
684
+ let pivotRowCache = new Float64Array(1024);
538
685
  function pivot(pivotRowIndex, pivotColumnIndex) {
539
686
  const matrix = this.matrix;
540
687
  const width = this.width;
688
+ // Ensure work arrays are large enough
689
+ if (width > nonZeroColumns.length) {
690
+ nonZeroColumns = new Int32Array(width * 2);
691
+ pivotRowCache = new Float64Array(width * 2);
692
+ }
541
693
  const pivotRowOffset = pivotRowIndex * width;
542
694
  const quotient = matrix[pivotRowOffset + pivotColumnIndex];
695
+ const invQuotient = 1 / quotient;
543
696
  const height = this.height;
544
697
  const leavingBasicIndex = this.varIndexByRow[pivotRowIndex];
545
698
  const enteringBasicIndex = this.varIndexByCol[pivotColumnIndex];
@@ -549,22 +702,24 @@ function pivot(pivotRowIndex, pivotColumnIndex) {
549
702
  this.rowByVarIndex[leavingBasicIndex] = -1;
550
703
  this.colByVarIndex[enteringBasicIndex] = -1;
551
704
  this.colByVarIndex[leavingBasicIndex] = pivotColumnIndex;
552
- // Normalize pivot row and track non-zero columns
705
+ // Normalize pivot row, track non-zero columns, and cache values for locality
553
706
  let nNonZeroColumns = 0;
554
707
  for (let c = 0; c < width; c++) {
555
708
  const idx = pivotRowOffset + c;
556
709
  const val = matrix[idx];
557
710
  if (!(val >= -1e-16 && val <= 1e-16)) {
558
- matrix[idx] = val / quotient;
711
+ const normalized = val / quotient;
712
+ matrix[idx] = normalized;
559
713
  nonZeroColumns[nNonZeroColumns] = c;
714
+ pivotRowCache[nNonZeroColumns] = normalized;
560
715
  nNonZeroColumns++;
561
716
  }
562
717
  else {
563
718
  matrix[idx] = 0;
564
719
  }
565
720
  }
566
- matrix[pivotRowOffset + pivotColumnIndex] = 1 / quotient;
567
- // Update all other rows
721
+ matrix[pivotRowOffset + pivotColumnIndex] = invQuotient;
722
+ // Update all other rows using cached pivot row values
568
723
  for (let r = 0; r < height; r++) {
569
724
  if (r !== pivotRowIndex) {
570
725
  const rowOffset = r * width;
@@ -572,13 +727,16 @@ function pivot(pivotRowIndex, pivotColumnIndex) {
572
727
  if (!(pivotColVal >= -1e-16 && pivotColVal <= 1e-16)) {
573
728
  const coefficient = pivotColVal;
574
729
  if (!(coefficient >= -1e-16 && coefficient <= 1e-16)) {
730
+ // Use cached pivot row values for better cache locality
575
731
  for (let i = 0; i < nNonZeroColumns; i++) {
576
732
  const c = nonZeroColumns[i];
577
- const v0 = matrix[pivotRowOffset + c];
733
+ const v0 = pivotRowCache[i];
734
+ // Inner zero check is critical for numerical stability
578
735
  if (!(v0 >= -1e-16 && v0 <= 1e-16)) {
579
- matrix[rowOffset + c] = matrix[rowOffset + c] - coefficient * v0;
736
+ matrix[rowOffset + c] -= coefficient * v0;
580
737
  }
581
738
  else if (v0 !== 0) {
739
+ // Clean up near-zero values in pivot row
582
740
  matrix[pivotRowOffset + c] = 0;
583
741
  }
584
742
  }
@@ -590,7 +748,7 @@ function pivot(pivotRowIndex, pivotColumnIndex) {
590
748
  }
591
749
  }
592
750
  }
593
- // Update optional objectives
751
+ // Update optional objectives using cached pivot row values
594
752
  const optionalObjectives = this.optionalObjectives;
595
753
  const nOptionalObjectives = optionalObjectives.length;
596
754
  if (nOptionalObjectives > 0) {
@@ -600,12 +758,9 @@ function pivot(pivotRowIndex, pivotColumnIndex) {
600
758
  if (coefficient !== 0) {
601
759
  for (let i = 0; i < nNonZeroColumns; i++) {
602
760
  const c = nonZeroColumns[i];
603
- const v0 = matrix[pivotRowOffset + c];
604
- if (v0 !== 0) {
605
- reducedCosts[c] = reducedCosts[c] - coefficient * v0;
606
- }
761
+ reducedCosts[c] -= coefficient * pivotRowCache[i];
607
762
  }
608
- reducedCosts[pivotColumnIndex] = -coefficient / quotient;
763
+ reducedCosts[pivotColumnIndex] = -coefficient * invQuotient;
609
764
  }
610
765
  }
611
766
  }
@@ -1439,11 +1594,13 @@ function addCutConstraints(cutConstraints) {
1439
1594
  const heightWithCuts = height + nCutConstraints;
1440
1595
  const width = this.width;
1441
1596
  const lastColumn = width - 1;
1442
- // Grow the matrix to accommodate new rows
1597
+ // Grow the matrix to accommodate new rows (with over-allocation to reduce reallocation frequency)
1443
1598
  const oldMatrix = this.matrix;
1444
1599
  const newSize = heightWithCuts * width;
1445
1600
  if (oldMatrix.length < newSize) {
1446
- const newMatrix = new Float64Array(newSize);
1601
+ // Over-allocate by 50% to reduce future reallocations
1602
+ const allocSize = Math.ceil(newSize * 1.5);
1603
+ const newMatrix = new Float64Array(allocSize);
1447
1604
  newMatrix.set(oldMatrix);
1448
1605
  this.matrix = newMatrix;
1449
1606
  }
@@ -1498,10 +1655,12 @@ function addLowerBoundMIRCut(rowIndex) {
1498
1655
  }
1499
1656
  const height = this.height;
1500
1657
  const newRowOffset = height * width;
1501
- // Grow matrix to add new row
1658
+ // Grow matrix to add new row (with over-allocation to reduce reallocation frequency)
1502
1659
  const newSize = (height + 1) * width;
1503
1660
  if (matrix.length < newSize) {
1504
- const newMatrix = new Float64Array(newSize);
1661
+ // Over-allocate by 50% to reduce future reallocations
1662
+ const allocSize = Math.ceil(newSize * 1.5);
1663
+ const newMatrix = new Float64Array(allocSize);
1505
1664
  newMatrix.set(matrix);
1506
1665
  this.matrix = newMatrix;
1507
1666
  }
@@ -1550,10 +1709,12 @@ function addUpperBoundMIRCut(rowIndex) {
1550
1709
  }
1551
1710
  const height = this.height;
1552
1711
  const newRowOffset = height * width;
1553
- // Grow matrix to add new row
1712
+ // Grow matrix to add new row (with over-allocation to reduce reallocation frequency)
1554
1713
  const newSize = (height + 1) * width;
1555
1714
  if (matrix.length < newSize) {
1556
- const newMatrix = new Float64Array(newSize);
1715
+ // Over-allocate by 50% to reduce future reallocations
1716
+ const allocSize = Math.ceil(newSize * 1.5);
1717
+ const newMatrix = new Float64Array(allocSize);
1557
1718
  newMatrix.set(matrix);
1558
1719
  this.matrix = newMatrix;
1559
1720
  }
@@ -2244,6 +2405,14 @@ class Tableau {
2244
2405
  phase2() {
2245
2406
  return phase2.call(this);
2246
2407
  }
2408
+ /**
2409
+ * Dual simplex for warm-starting after adding bound constraints.
2410
+ * Use when solution is dual feasible but may be primal infeasible.
2411
+ * @returns Number of iterations, or -1 if dual infeasible
2412
+ */
2413
+ dualSimplex() {
2414
+ return dualSimplex.call(this);
2415
+ }
2247
2416
  pivot(pivotRowIndex, pivotColumnIndex) {
2248
2417
  pivot.call(this, pivotRowIndex, pivotColumnIndex);
2249
2418
  }
@@ -3928,8 +4097,16 @@ function Polyopt(solver, model) {
3928
4097
  function createCut$1(type, varIndex, value) {
3929
4098
  return { type, varIndex, value };
3930
4099
  }
3931
- function createBranch$1(relaxedEvaluation, cuts, depth) {
3932
- return { relaxedEvaluation, cuts, depth };
4100
+ function createBranch$1(relaxedEvaluation, cuts, depth, branchVarIndex, branchDirection, branchFractionality, parentEvaluation) {
4101
+ return {
4102
+ relaxedEvaluation,
4103
+ cuts,
4104
+ depth,
4105
+ branchVarIndex,
4106
+ branchDirection,
4107
+ branchFractionality,
4108
+ parentEvaluation,
4109
+ };
3933
4110
  }
3934
4111
  /**
3935
4112
  * Enhanced branch-and-cut with:
@@ -3938,7 +4115,8 @@ function createBranch$1(relaxedEvaluation, cuts, depth) {
3938
4115
  * - Diving heuristic for quick feasible solutions
3939
4116
  */
3940
4117
  function createEnhancedBranchAndCutService(options = {}) {
3941
- const { nodeSelection = "hybrid", branching = "pseudocost", useDiving = true, strongBranchingCandidates = 5, } = options;
4118
+ const { nodeSelection = "hybrid", branching = "pseudocost", useDiving: _useDiving = true, // Reserved for future diving heuristic
4119
+ strongBranchingCandidates = 5, } = options;
3942
4120
  // Pseudocost data per variable index
3943
4121
  const pseudoCosts = new Map();
3944
4122
  const getPseudoCost = (varIndex) => {
@@ -4119,7 +4297,7 @@ function createEnhancedBranchAndCutService(options = {}) {
4119
4297
  continue;
4120
4298
  }
4121
4299
  const cuts = activeBranch.cuts;
4122
- const parentEval = tableau.evaluation;
4300
+ tableau.evaluation;
4123
4301
  applyCuts(tableau, cuts);
4124
4302
  iterations++;
4125
4303
  if (!tableau.feasible) {
@@ -4130,12 +4308,12 @@ function createEnhancedBranchAndCutService(options = {}) {
4130
4308
  continue;
4131
4309
  }
4132
4310
  // Update pseudocosts based on observed improvement
4133
- if (cuts.length > 0 && parentEval !== 0) {
4134
- const lastCut = cuts[cuts.length - 1];
4135
- const improvement = Math.abs(evaluation - parentEval);
4136
- // Estimate fraction (simplified)
4137
- const fraction = 0.5;
4138
- updatePseudoCost(lastCut.varIndex, lastCut.type === "min" ? "up" : "down", improvement, fraction);
4311
+ if (activeBranch.branchVarIndex !== undefined &&
4312
+ activeBranch.branchDirection !== undefined &&
4313
+ activeBranch.branchFractionality !== undefined &&
4314
+ activeBranch.parentEvaluation !== undefined) {
4315
+ const improvement = Math.abs(evaluation - activeBranch.parentEvaluation);
4316
+ updatePseudoCost(activeBranch.branchVarIndex, activeBranch.branchDirection, improvement, activeBranch.branchFractionality);
4139
4317
  }
4140
4318
  if (evaluation === bestEvaluation) {
4141
4319
  let isCurrentEvaluationWorse = true;
@@ -4214,22 +4392,24 @@ function createEnhancedBranchAndCutService(options = {}) {
4214
4392
  cutsLow.push(cut);
4215
4393
  }
4216
4394
  }
4217
- const min = Math.ceil(varValue);
4218
- const max = Math.floor(varValue);
4219
- const cutHigh = createCut$1("min", varIndex, min);
4395
+ const ceilVal = Math.ceil(varValue);
4396
+ const floorVal = Math.floor(varValue);
4397
+ const fracUp = ceilVal - varValue; // Distance to ceil
4398
+ const fracDown = varValue - floorVal; // Distance to floor
4399
+ const cutHigh = createCut$1("min", varIndex, ceilVal);
4220
4400
  cutsHigh.push(cutHigh);
4221
- const cutLow = createCut$1("max", varIndex, max);
4401
+ const cutLow = createCut$1("max", varIndex, floorVal);
4222
4402
  cutsLow.push(cutLow);
4223
4403
  const newDepth = activeBranch.depth + 1;
4224
4404
  if (useDepthFirst) {
4225
4405
  // Push in reverse order so 'up' branch is explored first
4226
4406
  // (often better for minimization with binary vars)
4227
- depthFirstStack.push(createBranch$1(evaluation, cutsLow, newDepth));
4228
- depthFirstStack.push(createBranch$1(evaluation, cutsHigh, newDepth));
4407
+ depthFirstStack.push(createBranch$1(evaluation, cutsLow, newDepth, varIndex, "down", fracDown, evaluation));
4408
+ depthFirstStack.push(createBranch$1(evaluation, cutsHigh, newDepth, varIndex, "up", fracUp, evaluation));
4229
4409
  }
4230
4410
  else {
4231
- branches.push(createBranch$1(evaluation, cutsHigh, newDepth));
4232
- branches.push(createBranch$1(evaluation, cutsLow, newDepth));
4411
+ branches.push(createBranch$1(evaluation, cutsHigh, newDepth, varIndex, "up", fracUp, evaluation));
4412
+ branches.push(createBranch$1(evaluation, cutsLow, newDepth, varIndex, "down", fracDown, evaluation));
4233
4413
  }
4234
4414
  }
4235
4415
  }