javascript-lp-solver 1.0.3 → 1.1.0
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 +80 -12
- package/dist/index.browser.mjs +4321 -399
- package/dist/index.browser.mjs.map +1 -1
- package/dist/index.cjs +4335 -395
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +739 -14
- package/dist/index.mjs +4335 -395
- package/dist/index.mjs.map +1 -1
- package/dist/solver.global.js +4321 -399
- package/dist/solver.global.js.map +1 -1
- package/dist/types/expressions.d.ts +148 -0
- package/dist/types/external/lpsolve/main.d.ts +2 -7
- package/dist/types/external/lpsolve/reformat.d.ts +1 -6
- package/dist/types/external/main.d.ts +7 -0
- package/dist/types/implied-integers.d.ts +47 -0
- package/dist/types/main.d.ts +22 -6
- package/dist/types/model.d.ts +171 -0
- package/dist/types/polyopt.d.ts +20 -3
- package/dist/types/solver.miplib.test.d.ts +1 -0
- package/dist/types/solver.netlib.test.d.ts +1 -0
- package/dist/types/solver.property.test.d.ts +1 -0
- package/dist/types/tableau/backup.d.ts +49 -0
- package/dist/types/tableau/bounds.d.ts +68 -0
- package/dist/types/tableau/bounds.test.d.ts +1 -0
- package/dist/types/tableau/branch-and-cut.d.ts +14 -0
- package/dist/types/tableau/cutting-strategies.d.ts +34 -0
- package/dist/types/tableau/determinism.test.d.ts +1 -0
- package/dist/types/tableau/dynamic-modification.d.ts +77 -11
- package/dist/types/tableau/enhanced-branch-and-cut.d.ts +22 -4
- package/dist/types/tableau/gomory.d.ts +48 -0
- package/dist/types/tableau/gomory.test.d.ts +1 -0
- package/dist/types/tableau/incremental-branch-and-cut.d.ts +24 -0
- package/dist/types/tableau/kernel.d.ts +119 -0
- package/dist/types/tableau/kernel.test.d.ts +1 -0
- package/dist/types/tableau/log.d.ts +14 -0
- package/dist/types/tableau/min-heap.d.ts +39 -0
- package/dist/types/tableau/mip-utils.d.ts +31 -19
- package/dist/types/tableau/mir.d.ts +65 -0
- package/dist/types/tableau/mir.test.d.ts +1 -0
- package/dist/types/tableau/presolve.d.ts +12 -0
- package/dist/types/tableau/pseudocosts.d.ts +42 -0
- package/dist/types/tableau/pseudocosts.test.d.ts +1 -0
- package/dist/types/tableau/root-presolve.d.ts +33 -0
- package/dist/types/tableau/root-presolve.test.d.ts +1 -0
- package/dist/types/tableau/simplex.d.ts +46 -0
- package/dist/types/tableau/solution.d.ts +31 -12
- package/dist/types/tableau/tableau.d.ts +153 -0
- package/dist/types/tableau/types.d.ts +45 -0
- package/dist/types/types/solver.d.ts +119 -0
- package/package.json +3 -2
- package/dist/solver.cjs.js +0 -4448
- package/dist/types/tableau/branching-strategies.d.ts +0 -4
- package/dist/types/tableau/integer-properties.d.ts +0 -4
- package/dist/types/tableau/lu-factorization.d.ts +0 -61
- package/dist/types/tableau/lu-simplex.d.ts +0 -32
- package/dist/types/tableau/milp-solution.d.ts +0 -7
- package/dist/types/tableau/parallel-branch-and-cut.d.ts +0 -39
- package/dist/types/tableau/sparse-matrix.d.ts +0 -98
- package/dist/types/tableau/sparse-simplex.d.ts +0 -30
- package/dist/types/types/api.d.ts +0 -5
- package/dist/types/wasm/index.d.ts +0 -42
- /package/dist/types/{tableau/sparse-matrix.test.d.ts → implied-integers.test.d.ts} +0 -0
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file src/tableau/lu-factorization.ts
|
|
3
|
-
* @description LU factorization for efficient simplex pivoting
|
|
4
|
-
*
|
|
5
|
-
* Maintains LU factors of the basis matrix to avoid O(n*m) dense row operations.
|
|
6
|
-
* Uses Forrest-Tomlin updates to modify factors when basis changes.
|
|
7
|
-
*
|
|
8
|
-
* Key operations:
|
|
9
|
-
* - FTRAN: Solve Bx = b (used to compute pivot column)
|
|
10
|
-
* - BTRAN: Solve B^T y = c (used to compute reduced costs)
|
|
11
|
-
* - UPDATE: Modify L,U when one column of B changes
|
|
12
|
-
*/
|
|
13
|
-
/**
|
|
14
|
-
* LU Factorization with partial pivoting.
|
|
15
|
-
* Stores L and U in compact form, with permutation vector.
|
|
16
|
-
*/
|
|
17
|
-
export declare class LUFactorization {
|
|
18
|
-
readonly n: number;
|
|
19
|
-
L: Float64Array;
|
|
20
|
-
U: Float64Array;
|
|
21
|
-
perm: Int32Array;
|
|
22
|
-
permInv: Int32Array;
|
|
23
|
-
private work;
|
|
24
|
-
private etaVectors;
|
|
25
|
-
private etaIndices;
|
|
26
|
-
private etaCount;
|
|
27
|
-
private readonly maxEtaCount;
|
|
28
|
-
private readonly tol;
|
|
29
|
-
constructor(n: number);
|
|
30
|
-
/**
|
|
31
|
-
* Factorize a dense matrix A into LU with partial pivoting.
|
|
32
|
-
* A is stored row-major with given width (stride).
|
|
33
|
-
*/
|
|
34
|
-
factorize(A: Float64Array, width: number, colIndices: number[]): boolean;
|
|
35
|
-
/**
|
|
36
|
-
* FTRAN: Solve Bx = b
|
|
37
|
-
* Returns solution in the result array.
|
|
38
|
-
*/
|
|
39
|
-
ftran(b: Float64Array, result: Float64Array): void;
|
|
40
|
-
/**
|
|
41
|
-
* BTRAN: Solve B^T y = c
|
|
42
|
-
* Returns solution in the result array.
|
|
43
|
-
*/
|
|
44
|
-
btran(c: Float64Array, result: Float64Array): void;
|
|
45
|
-
/**
|
|
46
|
-
* Update factors after basis change (Forrest-Tomlin style).
|
|
47
|
-
* The column at position `leavingIdx` in the basis is replaced.
|
|
48
|
-
* `newColumn` is the entering column (after FTRAN transformation).
|
|
49
|
-
*
|
|
50
|
-
* Returns true if update succeeded, false if refactorization needed.
|
|
51
|
-
*/
|
|
52
|
-
update(leavingIdx: number, newColumn: Float64Array): boolean;
|
|
53
|
-
/**
|
|
54
|
-
* Check if refactorization is needed.
|
|
55
|
-
*/
|
|
56
|
-
needsRefactorization(): boolean;
|
|
57
|
-
/**
|
|
58
|
-
* Get the number of eta vectors (update count since last factorization).
|
|
59
|
-
*/
|
|
60
|
-
getUpdateCount(): number;
|
|
61
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file src/tableau/lu-simplex.ts
|
|
3
|
-
* @description LU-based simplex algorithm
|
|
4
|
-
*
|
|
5
|
-
* Uses LU factorization of the basis matrix instead of explicit row operations.
|
|
6
|
-
* This reduces pivot cost from O(n*m) to approximately O(n²) per pivot,
|
|
7
|
-
* plus O(n) for LU updates.
|
|
8
|
-
*
|
|
9
|
-
* Key differences from standard tableau simplex:
|
|
10
|
-
* - Stores original constraint matrix A, not B^{-1}A
|
|
11
|
-
* - Computes reduced costs via BTRAN
|
|
12
|
-
* - Computes pivot column via FTRAN
|
|
13
|
-
* - Updates LU factors instead of row operations
|
|
14
|
-
*/
|
|
15
|
-
import type Tableau from "./tableau";
|
|
16
|
-
import { LUFactorization } from "./lu-factorization";
|
|
17
|
-
/**
|
|
18
|
-
* Run LU-based simplex on the tableau.
|
|
19
|
-
* This modifies the tableau in place and returns it.
|
|
20
|
-
*/
|
|
21
|
-
export declare function luSimplex(this: Tableau): Tableau;
|
|
22
|
-
/**
|
|
23
|
-
* LU-based pivot operation.
|
|
24
|
-
* This is the key optimization - instead of O(n*m) row operations,
|
|
25
|
-
* we update the LU factors in O(n) time.
|
|
26
|
-
*/
|
|
27
|
-
export declare function luPivot(this: Tableau, lu: LUFactorization, leavingRow: number, enteringColumn: number): void;
|
|
28
|
-
/**
|
|
29
|
-
* Check if LU simplex should be used based on problem size.
|
|
30
|
-
* LU overhead is only worthwhile for larger problems.
|
|
31
|
-
*/
|
|
32
|
-
export declare function shouldUseLU(tableau: Tableau): boolean;
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type Tableau from "./tableau";
|
|
2
|
-
import Solution from "./solution";
|
|
3
|
-
declare class MilpSolution extends Solution {
|
|
4
|
-
iter: number;
|
|
5
|
-
constructor(tableau: Tableau, evaluation: number, feasible: boolean, bounded: boolean, branchAndCutIterations: number);
|
|
6
|
-
}
|
|
7
|
-
export default MilpSolution;
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file src/tableau/parallel-branch-and-cut.ts
|
|
3
|
-
* @description Parallel branch-and-cut using worker threads (Node.js only)
|
|
4
|
-
*
|
|
5
|
-
* Distributes B&B nodes across multiple worker threads for faster MIP solving.
|
|
6
|
-
* Falls back to sequential solving in browser environments.
|
|
7
|
-
*
|
|
8
|
-
* Heuristics for when to use parallel solving:
|
|
9
|
-
* - Problem has >= 10 integer variables
|
|
10
|
-
* - At least 4 branches in queue
|
|
11
|
-
*/
|
|
12
|
-
import type { Model as JsonModel } from "../types/solver";
|
|
13
|
-
/** Configuration for parallel B&B */
|
|
14
|
-
export interface ParallelConfig {
|
|
15
|
-
numWorkers?: number;
|
|
16
|
-
verbose?: boolean;
|
|
17
|
-
maxNodes?: number;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Check if parallel B&B is available
|
|
21
|
-
*/
|
|
22
|
-
export declare function isParallelAvailable(): boolean;
|
|
23
|
-
/**
|
|
24
|
-
* Solve MIP using parallel branch-and-cut
|
|
25
|
-
*/
|
|
26
|
-
export declare function solveParallel(model: JsonModel, config?: ParallelConfig): Promise<{
|
|
27
|
-
feasible: boolean;
|
|
28
|
-
result: number;
|
|
29
|
-
solution: Record<string, number>;
|
|
30
|
-
iterations: number;
|
|
31
|
-
}>;
|
|
32
|
-
/**
|
|
33
|
-
* Heuristic to decide if parallel solving is worthwhile
|
|
34
|
-
*
|
|
35
|
-
* Note: Current implementation has high overhead from worker startup and message passing.
|
|
36
|
-
* Parallel is only beneficial for very large problems with expensive LP relaxations.
|
|
37
|
-
* For most problems, sequential solving is faster.
|
|
38
|
-
*/
|
|
39
|
-
export declare function shouldUseParallel(_model: JsonModel): boolean;
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file src/tableau/sparse-matrix.ts
|
|
3
|
-
* @description Compressed Sparse Column (CSC) matrix implementation
|
|
4
|
-
*
|
|
5
|
-
* Optimized for column operations which are frequent in the simplex algorithm.
|
|
6
|
-
* Provides O(nnz) pivot operations instead of O(n*m) for dense matrices.
|
|
7
|
-
*
|
|
8
|
-
* CSC Storage Format:
|
|
9
|
-
* - values: non-zero values stored column by column
|
|
10
|
-
* - rowIndices: row index for each value
|
|
11
|
-
* - colPointers: starting index in values/rowIndices for each column
|
|
12
|
-
*
|
|
13
|
-
* Column c's entries are at indices colPointers[c] to colPointers[c+1]-1
|
|
14
|
-
*/
|
|
15
|
-
export interface SparseVector {
|
|
16
|
-
indices: number[];
|
|
17
|
-
values: number[];
|
|
18
|
-
length: number;
|
|
19
|
-
}
|
|
20
|
-
export declare class SparseMatrix {
|
|
21
|
-
values: number[];
|
|
22
|
-
rowIndices: number[];
|
|
23
|
-
colPointers: number[];
|
|
24
|
-
nRows: number;
|
|
25
|
-
nCols: number;
|
|
26
|
-
private precision;
|
|
27
|
-
private workVector;
|
|
28
|
-
private workIndices;
|
|
29
|
-
constructor(nRows: number, nCols: number, precision?: number);
|
|
30
|
-
/**
|
|
31
|
-
* Create sparse matrix from dense Float64Array
|
|
32
|
-
*/
|
|
33
|
-
static fromDense(dense: Float64Array, width: number, height: number, precision?: number): SparseMatrix;
|
|
34
|
-
/**
|
|
35
|
-
* Convert to dense Float64Array
|
|
36
|
-
*/
|
|
37
|
-
toDense(): Float64Array;
|
|
38
|
-
/**
|
|
39
|
-
* Get value at (row, col)
|
|
40
|
-
*/
|
|
41
|
-
get(row: number, col: number): number;
|
|
42
|
-
/**
|
|
43
|
-
* Set value at (row, col)
|
|
44
|
-
* Handles insertion and removal of entries
|
|
45
|
-
*/
|
|
46
|
-
set(row: number, col: number, value: number): void;
|
|
47
|
-
/**
|
|
48
|
-
* Get column as sparse vector
|
|
49
|
-
*/
|
|
50
|
-
getColumn(col: number): SparseVector;
|
|
51
|
-
/**
|
|
52
|
-
* Get row as sparse vector (less efficient - requires full scan)
|
|
53
|
-
*/
|
|
54
|
-
getRow(row: number): SparseVector;
|
|
55
|
-
/**
|
|
56
|
-
* Get row into pre-allocated dense array (more efficient for repeated access)
|
|
57
|
-
* Returns array of non-zero column indices
|
|
58
|
-
*/
|
|
59
|
-
getRowDense(row: number, output: Float64Array): number[];
|
|
60
|
-
/**
|
|
61
|
-
* Add a new row (expands matrix vertically)
|
|
62
|
-
*/
|
|
63
|
-
addRow(): void;
|
|
64
|
-
/**
|
|
65
|
-
* Add a new column (expands matrix horizontally)
|
|
66
|
-
*/
|
|
67
|
-
addColumn(): void;
|
|
68
|
-
/**
|
|
69
|
-
* Set entire column from sparse vector
|
|
70
|
-
*/
|
|
71
|
-
setColumn(col: number, sparseCol: SparseVector): void;
|
|
72
|
-
/**
|
|
73
|
-
* Number of non-zero entries
|
|
74
|
-
*/
|
|
75
|
-
get nnz(): number;
|
|
76
|
-
/**
|
|
77
|
-
* Density (fraction of non-zeros)
|
|
78
|
-
*/
|
|
79
|
-
get density(): number;
|
|
80
|
-
/**
|
|
81
|
-
* Scale a row by a factor
|
|
82
|
-
*/
|
|
83
|
-
scaleRow(row: number, factor: number): void;
|
|
84
|
-
/**
|
|
85
|
-
* Add scaled row to another row: targetRow += factor * sourceRow
|
|
86
|
-
* This is the core operation in Gaussian elimination / pivot
|
|
87
|
-
*/
|
|
88
|
-
addScaledRow(targetRow: number, sourceRow: number, factor: number): void;
|
|
89
|
-
/**
|
|
90
|
-
* Perform pivot operation at (pivotRow, pivotCol)
|
|
91
|
-
* This is optimized for sparse operations
|
|
92
|
-
*/
|
|
93
|
-
pivot(pivotRow: number, pivotCol: number): number[];
|
|
94
|
-
/**
|
|
95
|
-
* Clone the matrix
|
|
96
|
-
*/
|
|
97
|
-
clone(): SparseMatrix;
|
|
98
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file src/tableau/sparse-simplex.ts
|
|
3
|
-
* @description Sparse simplex algorithm for large LP problems
|
|
4
|
-
*
|
|
5
|
-
* Optimized simplex implementation that operates directly on SparseMatrix
|
|
6
|
-
* storage. Converts the dense tableau to CSC format, solves, and converts
|
|
7
|
-
* back for solution extraction.
|
|
8
|
-
*
|
|
9
|
-
* Provides significant speedup for large, sparse problems where most
|
|
10
|
-
* coefficients are zero.
|
|
11
|
-
*/
|
|
12
|
-
import type Tableau from "./tableau";
|
|
13
|
-
/**
|
|
14
|
-
* Convert dense tableau to sparse and solve
|
|
15
|
-
*/
|
|
16
|
-
export declare function sparseSimplex(this: Tableau): Tableau;
|
|
17
|
-
/**
|
|
18
|
-
* Threshold for using sparse (total cells)
|
|
19
|
-
* Currently disabled - CSC format overhead exceeds benefits.
|
|
20
|
-
* A dual-indexed (CSC+CSR) approach would be needed for efficient sparse simplex.
|
|
21
|
-
*/
|
|
22
|
-
export declare const SPARSE_THRESHOLD: number;
|
|
23
|
-
/**
|
|
24
|
-
* Density threshold - only use sparse if below this density
|
|
25
|
-
*/
|
|
26
|
-
export declare const DENSITY_THRESHOLD = 0.1;
|
|
27
|
-
/**
|
|
28
|
-
* Check if sparse mode should be used for this tableau
|
|
29
|
-
*/
|
|
30
|
-
export declare function shouldUseSparse(tableau: Tableau): boolean;
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { ExternalSolvers, ExternalSolverModule } from "../external/main";
|
|
2
|
-
import type { Constraint, ConstraintBound, ConstraintRelation, Model as ModelDefinition, Numeral, ObjectiveDirection, SolveOptions, SolveResult, Term, Variable, VariableCoefficients } from "./solver";
|
|
3
|
-
export type Solution<TVariable extends string = string> = SolveResult & Record<TVariable, number | undefined>;
|
|
4
|
-
export type SolverAPI = typeof import("../main").default;
|
|
5
|
-
export type { Constraint, ConstraintBound, ConstraintRelation, ExternalSolvers, ExternalSolverModule, ModelDefinition, Numeral, ObjectiveDirection, SolveOptions, SolveResult, Term, Variable, VariableCoefficients };
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file src/wasm/index.ts
|
|
3
|
-
* @description WASM solver integration
|
|
4
|
-
*
|
|
5
|
-
* Provides async initialization and synchronous solving via WebAssembly.
|
|
6
|
-
* In Node.js, auto-initializes when the module is loaded.
|
|
7
|
-
*/
|
|
8
|
-
import type { Model, SolveResult } from "../types/solver";
|
|
9
|
-
interface WasmModule {
|
|
10
|
-
solve: (modelJson: string) => string;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Initialize the WASM module from a pre-loaded wasm-bindgen module.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* // In browser:
|
|
17
|
-
* import init, * as wasm from './lp_solver_wasm.js';
|
|
18
|
-
* await init();
|
|
19
|
-
* solver.initWasm(wasm);
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* // In Node.js:
|
|
23
|
-
* const wasm = await import('./lp_solver_wasm.js');
|
|
24
|
-
* await wasm.default();
|
|
25
|
-
* solver.initWasm(wasm);
|
|
26
|
-
*/
|
|
27
|
-
export declare function initWasm(module?: WasmModule): Promise<boolean>;
|
|
28
|
-
/**
|
|
29
|
-
* Check if WASM is initialized and available.
|
|
30
|
-
*/
|
|
31
|
-
export declare function isWasmAvailable(): boolean;
|
|
32
|
-
/**
|
|
33
|
-
* Solve a model using WASM.
|
|
34
|
-
* Throws if WASM is not initialized.
|
|
35
|
-
*/
|
|
36
|
-
export declare function solveWasm(model: Model): SolveResult;
|
|
37
|
-
declare const _default: {
|
|
38
|
-
initWasm: typeof initWasm;
|
|
39
|
-
isWasmAvailable: typeof isWasmAvailable;
|
|
40
|
-
solveWasm: typeof solveWasm;
|
|
41
|
-
};
|
|
42
|
-
export default _default;
|
|
File without changes
|