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