javascript-lp-solver 1.0.1 → 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 +4797 -1314
- package/dist/index.browser.mjs.map +1 -1
- package/dist/index.cjs +4812 -1311
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +765 -41
- package/dist/index.mjs +4812 -1311
- package/dist/index.mjs.map +1 -1
- package/dist/solver.global.js +4797 -1314
- 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 +23 -7
- package/dist/types/model.d.ts +171 -0
- package/dist/types/polyopt.d.ts +21 -4
- package/dist/types/shims/external.d.ts +2 -6
- 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/index.d.ts +1 -4
- 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 -55
- 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 -10
- 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 -13
- package/dist/types/tableau/tableau.d.ts +153 -0
- package/dist/types/tableau/types.d.ts +45 -21
- package/dist/types/test-utils/problem-generator.d.ts +2 -2
- package/dist/types/types/solver.d.ts +119 -0
- package/package.json +11 -2
- package/scripts/ts-node-register.js +1 -1
- 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
|
@@ -9,6 +9,55 @@
|
|
|
9
9
|
* Functions are designed to be bound to a Tableau instance via `this`.
|
|
10
10
|
*/
|
|
11
11
|
import type Tableau from "./tableau";
|
|
12
|
+
import { type BoundState } from "./bounds";
|
|
13
|
+
/**
|
|
14
|
+
* Create a deep copy of this tableau's state.
|
|
15
|
+
*
|
|
16
|
+
* Copies all mutable state (matrix, index arrays, optional objectives) while
|
|
17
|
+
* sharing immutable references (variables, model). The matrix is copied using
|
|
18
|
+
* the Float64Array constructor for maximum speed.
|
|
19
|
+
*
|
|
20
|
+
* @returns A new Tableau instance with identical state.
|
|
21
|
+
*/
|
|
12
22
|
export declare function copy(this: Tableau): Tableau;
|
|
23
|
+
/**
|
|
24
|
+
* Snapshot the current tableau state for later restoration.
|
|
25
|
+
* Used before branch-and-cut to establish a root state that branches can revert to.
|
|
26
|
+
*/
|
|
13
27
|
export declare function save(this: Tableau): void;
|
|
28
|
+
/**
|
|
29
|
+
* Restore the tableau to the previously saved state.
|
|
30
|
+
*
|
|
31
|
+
* Copies all mutable arrays from the saved snapshot back into the current tableau.
|
|
32
|
+
* Uses `Float64Array.set()` for efficient matrix restoration and explicit loops
|
|
33
|
+
* for index arrays to avoid allocation.
|
|
34
|
+
*
|
|
35
|
+
* No-op if no state has been saved.
|
|
36
|
+
*/
|
|
14
37
|
export declare function restore(this: Tableau): void;
|
|
38
|
+
/**
|
|
39
|
+
* Lightweight copy of the LP state at a branch-and-cut node: the live rows
|
|
40
|
+
* of the matrix plus the basis bookkeeping. Variables, model and optional
|
|
41
|
+
* objectives are shared with the tableau and not captured.
|
|
42
|
+
*/
|
|
43
|
+
export interface NodeSnapshot {
|
|
44
|
+
matrix: Float64Array;
|
|
45
|
+
height: number;
|
|
46
|
+
nVars: number;
|
|
47
|
+
lastElementIndex: number;
|
|
48
|
+
varIndexByRow: number[];
|
|
49
|
+
varIndexByCol: number[];
|
|
50
|
+
rowByVarIndex: number[];
|
|
51
|
+
colByVarIndex: number[];
|
|
52
|
+
availableIndexes: number[];
|
|
53
|
+
bounds: BoundState | null;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Capture the current node's LP state so a queued child can later resume
|
|
57
|
+
* from it instead of re-solving from the root.
|
|
58
|
+
*/
|
|
59
|
+
export declare function snapshot(this: Tableau, reuse?: NodeSnapshot | null): NodeSnapshot;
|
|
60
|
+
/**
|
|
61
|
+
* Load a state captured by `snapshot`. The width must not have changed.
|
|
62
|
+
*/
|
|
63
|
+
export declare function loadSnapshot(this: Tableau, snap: NodeSnapshot): void;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/tableau/bounds.ts
|
|
3
|
+
* @description Column bounds for the tableau (upper-bounding technique)
|
|
4
|
+
*
|
|
5
|
+
* Every variable keeps the classic "nonbasic means stored value 0" invariant
|
|
6
|
+
* by storing an affine image of itself:
|
|
7
|
+
*
|
|
8
|
+
* x = offset + sign * s, 0 <= s <= range, sign in {+1, -1}
|
|
9
|
+
*
|
|
10
|
+
* A variable resting at its lower bound has sign +1 and offset = lb; one
|
|
11
|
+
* resting at its upper bound is complemented (sign -1, offset = ub). Changing
|
|
12
|
+
* a bound is a substitution s = alpha + beta * s' applied to the tableau
|
|
13
|
+
* (Dantzig 1955; Chvátal, "Linear Programming", ch. 8), so pivots and the
|
|
14
|
+
* existing phase logic need no knowledge of bounds beyond the range check.
|
|
15
|
+
*
|
|
16
|
+
* Every tableau row, including the cost row, has the form
|
|
17
|
+
* basic = rhs - sum(a_c * s_c) over nonbasic columns c,
|
|
18
|
+
* so substitutions apply uniformly to all rows.
|
|
19
|
+
*/
|
|
20
|
+
import type Tableau from "./tableau";
|
|
21
|
+
/**
|
|
22
|
+
* Grow the dense bound arrays to cover varIndex, filling defaults
|
|
23
|
+
* (offset 0, sign +1, range/upper Infinity, lower 0).
|
|
24
|
+
*/
|
|
25
|
+
/** Size the bound arrays for variable indices up to `varIndex` in one allocation. */
|
|
26
|
+
export declare function ensureBoundCapacity(tableau: Tableau, varIndex: number): void;
|
|
27
|
+
/**
|
|
28
|
+
* Set the actual bounds lb <= x <= ub of a variable (lb finite).
|
|
29
|
+
*
|
|
30
|
+
* A nonbasic variable resting at its upper bound stays there (so a
|
|
31
|
+
* dual-feasible basis stays dual feasible); otherwise the variable is
|
|
32
|
+
* re-anchored at its new lower bound.
|
|
33
|
+
*
|
|
34
|
+
* @returns False if the bounds are empty (lb > ub), which makes the node infeasible.
|
|
35
|
+
*/
|
|
36
|
+
export declare function setVariableBounds(tableau: Tableau, varIndex: number, lb: number, ub: number): boolean;
|
|
37
|
+
/** Current lower bound of a variable (0 unless changed). */
|
|
38
|
+
export declare function lowerBoundOf(tableau: Tableau, varIndex: number): number;
|
|
39
|
+
/** Current upper bound of a variable (Infinity unless changed). */
|
|
40
|
+
export declare function upperBoundOf(tableau: Tableau, varIndex: number): number;
|
|
41
|
+
/** Range (ub - lb) of the stored variable, Infinity when unbounded above. */
|
|
42
|
+
export declare function rangeOf(tableau: Tableau, varIndex: number): number;
|
|
43
|
+
/** Actual value x of a variable given the current basis. */
|
|
44
|
+
export declare function actualValue(tableau: Tableau, varIndex: number): number;
|
|
45
|
+
/** Actual value of a basic variable from its stored value. */
|
|
46
|
+
export declare function toActual(tableau: Tableau, varIndex: number, stored: number): number;
|
|
47
|
+
/**
|
|
48
|
+
* Complement the basic variable of a row that sits above its range, turning
|
|
49
|
+
* the violation into an ordinary negative right-hand side.
|
|
50
|
+
*/
|
|
51
|
+
export declare function flipRowToUpper(tableau: Tableau, row: number): void;
|
|
52
|
+
/**
|
|
53
|
+
* Complement a nonbasic variable that moves to its upper bound (bound flip).
|
|
54
|
+
*/
|
|
55
|
+
export declare function flipColumnToUpper(tableau: Tableau, column: number): void;
|
|
56
|
+
/** State captured with snapshots and saved tableaux. */
|
|
57
|
+
export interface BoundState {
|
|
58
|
+
hasBounds: boolean;
|
|
59
|
+
offset: Float64Array;
|
|
60
|
+
sign: Float64Array;
|
|
61
|
+
range: Float64Array;
|
|
62
|
+
lower: Float64Array;
|
|
63
|
+
upper: Float64Array;
|
|
64
|
+
}
|
|
65
|
+
/** Capture the bound state (cheap when no bounds were ever set). */
|
|
66
|
+
export declare function captureBounds(tableau: Tableau, reuse?: BoundState | null): BoundState | null;
|
|
67
|
+
/** Restore a captured bound state (null resets to no bounds). */
|
|
68
|
+
export declare function restoreBounds(tableau: Tableau, state: BoundState | null): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -14,8 +14,22 @@
|
|
|
14
14
|
*/
|
|
15
15
|
import type Tableau from "./tableau";
|
|
16
16
|
import type { BranchCut } from "./types";
|
|
17
|
+
/**
|
|
18
|
+
* Interface for branch-and-cut solver services.
|
|
19
|
+
* Implementations provide the strategy for exploring the B&B tree.
|
|
20
|
+
*/
|
|
17
21
|
export interface BranchAndCutService {
|
|
22
|
+
/** Apply bound cuts and re-solve the LP relaxation. */
|
|
18
23
|
applyCuts(tableau: Tableau, branchingCuts: BranchCut[]): void;
|
|
24
|
+
/** Run the full branch-and-cut algorithm to find an integer-optimal solution. */
|
|
19
25
|
branchAndCut(tableau: Tableau): void;
|
|
20
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Create the default (basic) branch-and-cut service.
|
|
29
|
+
*
|
|
30
|
+
* Uses best-first node selection via a min-heap, most-fractional branching,
|
|
31
|
+
* and optional MIR cuts. Suitable for small-to-medium MIP instances.
|
|
32
|
+
*
|
|
33
|
+
* @returns A BranchAndCutService with applyCuts and branchAndCut methods.
|
|
34
|
+
*/
|
|
21
35
|
export declare function createBranchAndCutService(): BranchAndCutService;
|
|
@@ -11,7 +11,41 @@
|
|
|
11
11
|
*/
|
|
12
12
|
import type Tableau from "./tableau";
|
|
13
13
|
import type { BranchCut } from "./types";
|
|
14
|
+
/**
|
|
15
|
+
* Add bound constraints (cuts) to the tableau for branch-and-bound.
|
|
16
|
+
*
|
|
17
|
+
* Each cut adds a new row enforcing a variable bound (x >= value or x <= value).
|
|
18
|
+
* The matrix is grown with 50% over-allocation to reduce reallocation frequency.
|
|
19
|
+
* New slack variables are created for each added row.
|
|
20
|
+
*
|
|
21
|
+
* @param cutConstraints - Array of bound constraints to add.
|
|
22
|
+
*/
|
|
14
23
|
export declare function addCutConstraints(this: Tableau, cutConstraints: BranchCut[]): void;
|
|
24
|
+
/**
|
|
25
|
+
* Add a lower-bound Mixed Integer Rounding (MIR) cut from the given row.
|
|
26
|
+
*
|
|
27
|
+
* Generates a Gomory-style cut that is valid for integer solutions but
|
|
28
|
+
* removes the current fractional LP relaxation point. The cut coefficients
|
|
29
|
+
* are derived from the fractional parts of the tableau row.
|
|
30
|
+
*
|
|
31
|
+
* @param rowIndex - Row of the fractional integer variable to cut on.
|
|
32
|
+
* @returns True if a cut was successfully added, false if the row is unsuitable.
|
|
33
|
+
*/
|
|
15
34
|
export declare function addLowerBoundMIRCut(this: Tableau, rowIndex: number): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Add an upper-bound Mixed Integer Rounding (MIR) cut from the given row.
|
|
37
|
+
*
|
|
38
|
+
* Similar to addLowerBoundMIRCut but derives coefficients for the complementary
|
|
39
|
+
* direction. Effective when the fractional variable is closer to its ceiling.
|
|
40
|
+
*
|
|
41
|
+
* @param rowIndex - Row of the fractional integer variable to cut on.
|
|
42
|
+
* @returns True if a cut was successfully added, false if the row is unsuitable.
|
|
43
|
+
*/
|
|
16
44
|
export declare function addUpperBoundMIRCut(this: Tableau, rowIndex: number): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Apply MIR cuts to all rows with fractional integer basic variables.
|
|
47
|
+
*
|
|
48
|
+
* Iterates through constraint rows, adding lower-bound MIR cuts where
|
|
49
|
+
* applicable. Limited to 10 cuts per call to avoid excessive tableau growth.
|
|
50
|
+
*/
|
|
17
51
|
export declare function applyMIRCuts(this: Tableau): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,24 +1,90 @@
|
|
|
1
|
+
import type Tableau from "./tableau";
|
|
2
|
+
import type { Constraint, Variable } from "../expressions";
|
|
1
3
|
/**
|
|
2
|
-
*
|
|
3
|
-
* @description Runtime tableau modification operations
|
|
4
|
-
*
|
|
5
|
-
* Supports modifying the LP after initial solve:
|
|
6
|
-
* - Add/remove constraints
|
|
7
|
-
* - Add/remove variables
|
|
8
|
-
* - Update constraint coefficients and RHS values
|
|
9
|
-
* - Update objective function costs
|
|
4
|
+
* Ensure a variable is in the basis (basic). If non-basic, pivot it in.
|
|
10
5
|
*
|
|
11
|
-
*
|
|
6
|
+
* @param varIndex - Internal index of the variable to make basic.
|
|
7
|
+
* @returns Row index where the variable is now basic.
|
|
12
8
|
*/
|
|
13
|
-
import type Tableau from "./tableau";
|
|
14
|
-
import type { Constraint, Variable } from "../expressions";
|
|
15
9
|
export declare function putInBase(this: Tableau, varIndex: number): number;
|
|
10
|
+
/**
|
|
11
|
+
* Ensure a variable is non-basic. If currently basic, pivot it out.
|
|
12
|
+
*
|
|
13
|
+
* @param varIndex - Internal index of the variable to make non-basic.
|
|
14
|
+
* @returns Column index where the variable is now non-basic.
|
|
15
|
+
*/
|
|
16
16
|
export declare function takeOutOfBase(this: Tableau, varIndex: number): number;
|
|
17
|
+
/**
|
|
18
|
+
* Read current variable values from the tableau matrix into Variable.value.
|
|
19
|
+
* Non-basic variables get value 0; basic variables get their RHS value (rounded).
|
|
20
|
+
*/
|
|
17
21
|
export declare function updateVariableValues(this: Tableau): void;
|
|
22
|
+
/**
|
|
23
|
+
* Update a constraint's RHS value in the current tableau.
|
|
24
|
+
*
|
|
25
|
+
* If the constraint's slack is basic, directly adjusts the RHS cell.
|
|
26
|
+
* If non-basic, propagates the change through all rows using the slack column.
|
|
27
|
+
*
|
|
28
|
+
* @param constraint - The constraint to update.
|
|
29
|
+
* @param difference - Amount to subtract from the current RHS.
|
|
30
|
+
*/
|
|
18
31
|
export declare function updateRightHandSide(this: Tableau, constraint: Constraint, difference: number): void;
|
|
32
|
+
/**
|
|
33
|
+
* Update a variable's coefficient in a constraint within the active tableau.
|
|
34
|
+
*
|
|
35
|
+
* Ensures the constraint is basic (via putInBase) then applies the coefficient
|
|
36
|
+
* change. Handles both basic and non-basic target variables.
|
|
37
|
+
*
|
|
38
|
+
* @param constraint - The constraint being modified.
|
|
39
|
+
* @param variable - The variable whose coefficient is changing.
|
|
40
|
+
* @param difference - Amount to add to the current coefficient.
|
|
41
|
+
* @throws If constraint.index equals variable.index.
|
|
42
|
+
*/
|
|
19
43
|
export declare function updateConstraintCoefficient(this: Tableau, constraint: Constraint, variable: Variable, difference: number): void;
|
|
44
|
+
/**
|
|
45
|
+
* Update a variable's objective coefficient in the active tableau.
|
|
46
|
+
*
|
|
47
|
+
* If the variable is basic, propagates the cost change through the cost row
|
|
48
|
+
* (or optional objective's reduced costs). If non-basic, directly updates its column.
|
|
49
|
+
*
|
|
50
|
+
* @param variable - The variable whose cost is changing.
|
|
51
|
+
* @param difference - Amount to add to the current reduced cost.
|
|
52
|
+
*/
|
|
20
53
|
export declare function updateCost(this: Tableau, variable: Variable, difference: number): void;
|
|
54
|
+
/**
|
|
55
|
+
* Add a new constraint row to the active tableau.
|
|
56
|
+
*
|
|
57
|
+
* Appends a new row with the constraint's terms expressed in the current basis.
|
|
58
|
+
* Grows the matrix capacity with exponential growth strategy if needed.
|
|
59
|
+
* The constraint's slack variable becomes the basic variable in the new row.
|
|
60
|
+
*
|
|
61
|
+
* @param constraint - The constraint to add (with terms already populated).
|
|
62
|
+
*/
|
|
21
63
|
export declare function addConstraint(this: Tableau, constraint: Constraint): void;
|
|
64
|
+
/**
|
|
65
|
+
* Remove a constraint from the active tableau.
|
|
66
|
+
*
|
|
67
|
+
* Pivots the constraint's slack into the basis, swaps the row with the last row,
|
|
68
|
+
* and decrements the height. The slack variable's index is returned to the pool.
|
|
69
|
+
*
|
|
70
|
+
* @param constraint - The constraint to remove.
|
|
71
|
+
*/
|
|
22
72
|
export declare function removeConstraint(this: Tableau, constraint: Constraint): void;
|
|
73
|
+
/**
|
|
74
|
+
* Add a new variable (column) to the active tableau.
|
|
75
|
+
*
|
|
76
|
+
* Requires reallocating the matrix since the stride (width) changes.
|
|
77
|
+
* Sets the variable's reduced cost in the appropriate objective row.
|
|
78
|
+
*
|
|
79
|
+
* @param variable - The variable to add (cost and priority must be set).
|
|
80
|
+
*/
|
|
23
81
|
export declare function addVariable(this: Tableau, variable: Variable): void;
|
|
82
|
+
/**
|
|
83
|
+
* Remove a variable (column) from the active tableau.
|
|
84
|
+
*
|
|
85
|
+
* Pivots the variable out of the basis if basic, then overwrites its column
|
|
86
|
+
* with the last column and decrements width. The variable index is recycled.
|
|
87
|
+
*
|
|
88
|
+
* @param variable - The variable to remove.
|
|
89
|
+
*/
|
|
24
90
|
export declare function removeVariable(this: Tableau, variable: Variable): void;
|
|
@@ -12,20 +12,38 @@
|
|
|
12
12
|
*/
|
|
13
13
|
import type Tableau from "./tableau";
|
|
14
14
|
import type { BranchCut } from "./types";
|
|
15
|
+
/**
|
|
16
|
+
* Interface for the enhanced branch-and-cut service.
|
|
17
|
+
*/
|
|
15
18
|
export interface EnhancedBranchAndCutService {
|
|
19
|
+
/** Apply bound cuts and re-solve the LP relaxation. */
|
|
16
20
|
applyCuts(tableau: Tableau, branchingCuts: BranchCut[]): void;
|
|
21
|
+
/** Run enhanced branch-and-cut with configurable strategies. */
|
|
17
22
|
branchAndCut(tableau: Tableau): void;
|
|
18
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Configuration for the enhanced branch-and-cut solver.
|
|
26
|
+
*/
|
|
19
27
|
export interface BranchAndCutOptions {
|
|
28
|
+
/** Node selection strategy for tree traversal. "hybrid" starts depth-first then switches to best-first. */
|
|
20
29
|
nodeSelection?: "best-first" | "depth-first" | "hybrid";
|
|
30
|
+
/** Variable selection strategy for branching decisions. */
|
|
21
31
|
branching?: "most-fractional" | "pseudocost" | "strong";
|
|
32
|
+
/** Reserved for future diving heuristic implementation. */
|
|
22
33
|
useDiving?: boolean;
|
|
34
|
+
/** Maximum number of candidates to evaluate for strong branching. */
|
|
23
35
|
strongBranchingCandidates?: number;
|
|
24
36
|
}
|
|
25
37
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* -
|
|
38
|
+
* Create an enhanced branch-and-cut service with configurable strategies.
|
|
39
|
+
*
|
|
40
|
+
* Key improvements over the basic service:
|
|
41
|
+
* - **Pseudocost branching**: Learns from branching history to select better variables.
|
|
42
|
+
* - **Hybrid node selection**: Starts depth-first (fast feasible solutions), then
|
|
43
|
+
* switches to best-first (optimal convergence) after finding the first solution.
|
|
44
|
+
* - **Strong branching**: Evaluates multiple candidates by estimating LP improvements.
|
|
45
|
+
*
|
|
46
|
+
* @param options - Configuration for node/variable selection strategies.
|
|
47
|
+
* @returns An EnhancedBranchAndCutService instance.
|
|
30
48
|
*/
|
|
31
49
|
export declare function createEnhancedBranchAndCutService(options?: BranchAndCutOptions): EnhancedBranchAndCutService;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/tableau/gomory.ts
|
|
3
|
+
* @description Gomory mixed-integer cuts from the optimal tableau
|
|
4
|
+
*
|
|
5
|
+
* Each tableau row whose basic variable is integer but fractional yields a
|
|
6
|
+
* Gomory mixed-integer (GMI) cut (Gomory 1960; Balas et al. 1996 showed they
|
|
7
|
+
* pay off in branch-and-cut). The dense tableau stores every row of B^-1 A
|
|
8
|
+
* already, so generating a cut costs one pass over the row.
|
|
9
|
+
*
|
|
10
|
+
* Row r reads s_B + sum_j a_j s_j = b with every nonbasic s_j >= 0: with
|
|
11
|
+
* column bounds (bounds.ts) each stored variable is shifted and possibly
|
|
12
|
+
* complemented so that it sits at zero, and integer variables keep integer
|
|
13
|
+
* offsets, so integrality carries over to the stored variable. With f0 the
|
|
14
|
+
* fractional part of b and f_j that of a_j, the cut is
|
|
15
|
+
*
|
|
16
|
+
* sum_{int, f_j <= f0} f_j/f0 s_j + sum_{int, f_j > f0} (1-f_j)/(1-f0) s_j
|
|
17
|
+
* + sum_{cont, a_j > 0} a_j/f0 s_j + sum_{cont, a_j < 0} -a_j/(1-f0) s_j >= 1.
|
|
18
|
+
*
|
|
19
|
+
* Slack variables are treated as continuous (rows are equilibrated, so
|
|
20
|
+
* their slacks need not be integral), which keeps every cut valid.
|
|
21
|
+
*/
|
|
22
|
+
import type Tableau from "./tableau";
|
|
23
|
+
/**
|
|
24
|
+
* Append up to `maxCuts` GMI cuts, from the most fractional integer rows, as
|
|
25
|
+
* new tableau rows whose slack is basic and negative (violated). The basis
|
|
26
|
+
* stays dual feasible, so phase 1 re-optimizes with a few dual pivots.
|
|
27
|
+
*
|
|
28
|
+
* @returns The number of cuts added.
|
|
29
|
+
*/
|
|
30
|
+
export declare function addGomoryCuts(tableau: Tableau, maxCuts: number): number;
|
|
31
|
+
/**
|
|
32
|
+
* Append a cut row in tableau form, t = rhs - sum_c row[c] s_c with a new
|
|
33
|
+
* basic slack t >= 0 (row[0] is ignored), equilibrated by a power of two so
|
|
34
|
+
* its largest coefficient is near 1. Its slack is registered as a cut slack,
|
|
35
|
+
* so removeSlackCuts may delete the row later.
|
|
36
|
+
*
|
|
37
|
+
* @param largest - Largest |row[c]| (computed by the caller).
|
|
38
|
+
*/
|
|
39
|
+
export declare function appendCutTableauRow(tableau: Tableau, rhs: number, rowCoefficients: Float64Array, largest: number): void;
|
|
40
|
+
/**
|
|
41
|
+
* Delete cut rows at or after `firstCutRow` whose slack is basic and strictly
|
|
42
|
+
* positive: those cuts are not binding at the current optimum, so dropping
|
|
43
|
+
* their rows leaves the basis optimal while making every later pivot
|
|
44
|
+
* cheaper. Binding cuts (slack nonbasic or at zero) stay.
|
|
45
|
+
*
|
|
46
|
+
* @returns The number of rows removed.
|
|
47
|
+
*/
|
|
48
|
+
export declare function removeSlackCuts(tableau: Tableau, firstCutRow: number): number;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -12,13 +12,37 @@
|
|
|
12
12
|
*/
|
|
13
13
|
import type Tableau from "./tableau";
|
|
14
14
|
import type { BranchCut } from "./types";
|
|
15
|
+
/**
|
|
16
|
+
* Interface for the incremental branch-and-cut service.
|
|
17
|
+
*/
|
|
15
18
|
export interface IncrementalBranchAndCutService {
|
|
19
|
+
/** Apply bound cuts and re-solve the LP relaxation. */
|
|
16
20
|
applyCuts(tableau: Tableau, branchingCuts: BranchCut[]): void;
|
|
21
|
+
/** Run incremental branch-and-cut with checkpoint-based state management. */
|
|
17
22
|
branchAndCut(tableau: Tableau): void;
|
|
18
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Configuration for the incremental branch-and-cut solver.
|
|
26
|
+
*/
|
|
19
27
|
export interface IncrementalBranchAndCutOptions {
|
|
28
|
+
/** Node selection strategy for tree traversal. */
|
|
20
29
|
nodeSelection?: "best-first" | "depth-first" | "hybrid";
|
|
30
|
+
/** Variable selection strategy for branching decisions. */
|
|
21
31
|
branching?: "most-fractional" | "pseudocost" | "strong";
|
|
32
|
+
/** Maximum number of checkpoints to store (limits memory usage). */
|
|
22
33
|
maxCheckpoints?: number;
|
|
23
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Create an incremental branch-and-cut service with checkpoint-based restoration.
|
|
37
|
+
*
|
|
38
|
+
* Key optimization: instead of always restoring to root and reapplying all cuts,
|
|
39
|
+
* child nodes store a parent checkpoint and apply only their new cut. This reduces
|
|
40
|
+
* cut application cost from O(depth) to O(1) per node in depth-first traversal.
|
|
41
|
+
*
|
|
42
|
+
* **Memory trade-off**: Stores one checkpoint per active tree path (capped by maxCheckpoints).
|
|
43
|
+
* Falls back to full-restore when the checkpoint limit is exceeded.
|
|
44
|
+
*
|
|
45
|
+
* @param options - Configuration for strategies and checkpoint limits.
|
|
46
|
+
* @returns An IncrementalBranchAndCutService instance.
|
|
47
|
+
*/
|
|
24
48
|
export declare function createIncrementalBranchAndCutService(options?: IncrementalBranchAndCutOptions): IncrementalBranchAndCutService;
|
|
@@ -10,8 +10,5 @@
|
|
|
10
10
|
* - Cutting plane generation
|
|
11
11
|
*/
|
|
12
12
|
import Tableau from "./tableau";
|
|
13
|
-
export {
|
|
14
|
-
export { Solution, MilpSolution } from "./solution";
|
|
15
|
-
export type { BoundType, Branch, BranchCut, OptionalObjective, SavedState, TableauSolution, TableauSolutionSet, VariableValue, } from "./types";
|
|
13
|
+
export type { TableauSolution, TableauSolutionSet } from "./types";
|
|
16
14
|
export default Tableau;
|
|
17
|
-
export { Tableau };
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/tableau/kernel.ts
|
|
3
|
+
* @description WebAssembly SIMD kernel for the dense pivot row update
|
|
4
|
+
*
|
|
5
|
+
* The pivot's rank-1 update (row -= coefficient * pivotRow for every row) is
|
|
6
|
+
* most of the solver's runtime. V8 does not vectorize the JS loop; the same
|
|
7
|
+
* loop in WebAssembly with f64x2 runs 3-5x faster and gives bit-identical
|
|
8
|
+
* results (a multiply and a subtract per entry, no fused multiply-add).
|
|
9
|
+
*
|
|
10
|
+
* WebAssembly can only touch its own memory, so a matrix that wants the
|
|
11
|
+
* kernel is allocated inside a WebAssembly.Memory by allocMatrix, followed by
|
|
12
|
+
* scratch space for the dense pivot row. Any other matrix, and any runtime
|
|
13
|
+
* without WebAssembly SIMD, uses the JS update in simplex.ts.
|
|
14
|
+
*
|
|
15
|
+
* The module below is assembled from this text (wabt, simd enabled):
|
|
16
|
+
*
|
|
17
|
+
* (module
|
|
18
|
+
* (memory (import "e" "mem") 1)
|
|
19
|
+
* ;; normalize(row, w, quot, p): divide the pivot row (byte offset row, w entries) by quot,
|
|
20
|
+
* ;; zero entries that are or become noise (|x| <= 1e-16), store the result in the matrix
|
|
21
|
+
* ;; and in the dense scratch row at p. Returns the number of entries kept.
|
|
22
|
+
* (func (export "normalize") (param $row i32) (param $w i32) (param $quot f64) (param $p i32) (result i32)
|
|
23
|
+
* (local $c i32) (local $end i32) (local $end2 i32) (local $n i32) (local $v v128) (local $x v128) (local $keep v128) (local $q v128) (local $eps v128) (local $s f64) (local $t f64)
|
|
24
|
+
* (local.set $q (f64x2.splat (local.get $quot)))
|
|
25
|
+
* (local.set $eps (f64x2.splat (f64.const 1e-16)))
|
|
26
|
+
* (local.set $c (local.get $row))
|
|
27
|
+
* (local.set $end (i32.add (local.get $row) (i32.shl (local.get $w) (i32.const 3))))
|
|
28
|
+
* (local.set $end2 (i32.sub (local.get $end) (i32.const 15)))
|
|
29
|
+
* (block $d (loop $l
|
|
30
|
+
* (br_if $d (i32.ge_u (local.get $c) (local.get $end2)))
|
|
31
|
+
* (local.set $v (v128.load (local.get $c)))
|
|
32
|
+
* (local.set $x (f64x2.div (local.get $v) (local.get $q)))
|
|
33
|
+
* (local.set $keep (v128.not (v128.or
|
|
34
|
+
* (f64x2.le (f64x2.abs (local.get $x)) (local.get $eps))
|
|
35
|
+
* (f64x2.le (f64x2.abs (local.get $v)) (local.get $eps)))))
|
|
36
|
+
* (local.set $x (v128.and (local.get $x) (local.get $keep)))
|
|
37
|
+
* (v128.store (local.get $c) (local.get $x))
|
|
38
|
+
* (v128.store (local.get $p) (local.get $x))
|
|
39
|
+
* (local.set $n (i32.add (local.get $n) (i32.popcnt (i64x2.bitmask (local.get $keep)))))
|
|
40
|
+
* (local.set $c (i32.add (local.get $c) (i32.const 16)))
|
|
41
|
+
* (local.set $p (i32.add (local.get $p) (i32.const 16)))
|
|
42
|
+
* (br $l)))
|
|
43
|
+
* (if (i32.lt_u (local.get $c) (local.get $end)) (then
|
|
44
|
+
* (local.set $s (f64.load (local.get $c)))
|
|
45
|
+
* (local.set $t (f64.div (local.get $s) (local.get $quot)))
|
|
46
|
+
* (if (i32.or
|
|
47
|
+
* (f64.le (f64.abs (local.get $t)) (f64.const 1e-16))
|
|
48
|
+
* (f64.le (f64.abs (local.get $s)) (f64.const 1e-16)))
|
|
49
|
+
* (then (local.set $t (f64.const 0)))
|
|
50
|
+
* (else (local.set $n (i32.add (local.get $n) (i32.const 1)))))
|
|
51
|
+
* (f64.store (local.get $c) (local.get $t))
|
|
52
|
+
* (f64.store (local.get $p) (local.get $t))))
|
|
53
|
+
* (local.get $n))
|
|
54
|
+
* (func (export "update") (param $p i32) (param $w i32) (param $h i32) (param $pr i32) (param $pc i32) (param $quot f64)
|
|
55
|
+
* (local $r i32) (local $row i32) (local $c i32) (local $end i32) (local $end2 i32) (local $end4 i32) (local $coef f64) (local $v v128) (local $q i32) (local $pcaddr i32)
|
|
56
|
+
* (block $done (loop $rows
|
|
57
|
+
* (br_if $done (i32.ge_u (local.get $r) (local.get $h)))
|
|
58
|
+
* (if (i32.ne (local.get $r) (local.get $pr)) (then
|
|
59
|
+
* (local.set $row (i32.shl (i32.mul (local.get $r) (local.get $w)) (i32.const 3)))
|
|
60
|
+
* (local.set $pcaddr (i32.add (local.get $row) (i32.shl (local.get $pc) (i32.const 3))))
|
|
61
|
+
* (local.set $coef (f64.load (local.get $pcaddr)))
|
|
62
|
+
* (if (i32.eqz (f64.le (f64.abs (local.get $coef)) (f64.const 1e-16))) (then
|
|
63
|
+
* (local.set $v (f64x2.splat (local.get $coef)))
|
|
64
|
+
* (local.set $c (local.get $row))
|
|
65
|
+
* (local.set $q (local.get $p))
|
|
66
|
+
* (local.set $end (i32.add (local.get $row) (i32.shl (local.get $w) (i32.const 3))))
|
|
67
|
+
* (local.set $end2 (i32.sub (local.get $end) (i32.const 15)))
|
|
68
|
+
* (local.set $end4 (i32.sub (local.get $end) (i32.const 63)))
|
|
69
|
+
* (block $d4 (loop $l4
|
|
70
|
+
* (br_if $d4 (i32.ge_u (local.get $c) (local.get $end4)))
|
|
71
|
+
* (v128.store offset=0 (local.get $c) (f64x2.sub (v128.load offset=0 (local.get $c)) (f64x2.mul (local.get $v) (v128.load offset=0 (local.get $q)))))
|
|
72
|
+
* (v128.store offset=16 (local.get $c) (f64x2.sub (v128.load offset=16 (local.get $c)) (f64x2.mul (local.get $v) (v128.load offset=16 (local.get $q)))))
|
|
73
|
+
* (v128.store offset=32 (local.get $c) (f64x2.sub (v128.load offset=32 (local.get $c)) (f64x2.mul (local.get $v) (v128.load offset=32 (local.get $q)))))
|
|
74
|
+
* (v128.store offset=48 (local.get $c) (f64x2.sub (v128.load offset=48 (local.get $c)) (f64x2.mul (local.get $v) (v128.load offset=48 (local.get $q)))))
|
|
75
|
+
* (local.set $c (i32.add (local.get $c) (i32.const 64)))
|
|
76
|
+
* (local.set $q (i32.add (local.get $q) (i32.const 64)))
|
|
77
|
+
* (br $l4)))
|
|
78
|
+
* (block $d2 (loop $l2
|
|
79
|
+
* (br_if $d2 (i32.ge_u (local.get $c) (local.get $end2)))
|
|
80
|
+
* (v128.store (local.get $c) (f64x2.sub (v128.load (local.get $c)) (f64x2.mul (local.get $v) (v128.load (local.get $q)))))
|
|
81
|
+
* (local.set $c (i32.add (local.get $c) (i32.const 16)))
|
|
82
|
+
* (local.set $q (i32.add (local.get $q) (i32.const 16)))
|
|
83
|
+
* (br $l2)))
|
|
84
|
+
* (if (i32.lt_u (local.get $c) (local.get $end)) (then
|
|
85
|
+
* (f64.store (local.get $c) (f64.sub (f64.load (local.get $c)) (f64.mul (local.get $coef) (f64.load (local.get $q)))))))
|
|
86
|
+
* (f64.store (local.get $pcaddr) (f64.div (f64.neg (local.get $coef)) (local.get $quot)))
|
|
87
|
+
* ))
|
|
88
|
+
* ))
|
|
89
|
+
* (local.set $r (i32.add (local.get $r) (i32.const 1)))
|
|
90
|
+
* (br $rows)))
|
|
91
|
+
* )
|
|
92
|
+
* )
|
|
93
|
+
*/
|
|
94
|
+
/** update(pivotRowByteOffset, width, height, pivotRow, pivotColumn, quotient) */
|
|
95
|
+
type UpdateFunction = (pivotRowOffset: number, width: number, height: number, pivotRow: number, pivotColumn: number, quotient: number) => void;
|
|
96
|
+
/** normalize(pivotRowByteOffset, width, quotient, scratchByteOffset): entries kept */
|
|
97
|
+
type NormalizeFunction = (rowOffset: number, width: number, quotient: number, scratchOffset: number) => number;
|
|
98
|
+
/** Kernel bound to one matrix allocation. */
|
|
99
|
+
export interface PivotKernel {
|
|
100
|
+
update: UpdateFunction;
|
|
101
|
+
/**
|
|
102
|
+
* Divide the pivot row by the pivot element, zero entries that are or
|
|
103
|
+
* become noise, and leave a dense copy in pivotRow.
|
|
104
|
+
*/
|
|
105
|
+
normalize: NormalizeFunction;
|
|
106
|
+
/** Dense pivot row scratch, in the same memory as the matrix. */
|
|
107
|
+
pivotRow: Float64Array;
|
|
108
|
+
/** Byte offset of pivotRow in the memory. */
|
|
109
|
+
pivotRowOffset: number;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Allocate a zeroed matrix of `cells` doubles. When the kernel is available
|
|
113
|
+
* the matrix lives in WebAssembly memory with room for a pivot row of
|
|
114
|
+
* `width` entries after it; otherwise this is a plain Float64Array.
|
|
115
|
+
*/
|
|
116
|
+
export declare function allocMatrix(cells: number, width: number): Float64Array;
|
|
117
|
+
/** The kernel for a matrix from allocMatrix, if it has one. */
|
|
118
|
+
export declare function kernelOf(matrix: Float64Array): PivotKernel | undefined;
|
|
119
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -10,4 +10,18 @@
|
|
|
10
10
|
* Primarily used during development and debugging.
|
|
11
11
|
*/
|
|
12
12
|
import type Tableau from "./tableau";
|
|
13
|
+
/**
|
|
14
|
+
* Print the current tableau state to console for debugging.
|
|
15
|
+
*
|
|
16
|
+
* Outputs the complete matrix in a formatted table showing:
|
|
17
|
+
* - Variable names along the top (non-basic) and right side (basic)
|
|
18
|
+
* - Cost row (objective function reduced costs)
|
|
19
|
+
* - Constraint rows with RHS values
|
|
20
|
+
* - Optional objective reduced costs (if hierarchical optimization)
|
|
21
|
+
* - Current feasibility and evaluation
|
|
22
|
+
*
|
|
23
|
+
* @param message - Label to display above the tableau output.
|
|
24
|
+
* @param force - If true, print even when DEBUG_ENABLED is false.
|
|
25
|
+
* @returns The tableau instance for chaining.
|
|
26
|
+
*/
|
|
13
27
|
export declare function log(this: Tableau, message: unknown, force?: boolean): Tableau;
|
|
@@ -10,20 +10,59 @@
|
|
|
10
10
|
* reducing complexity from O(n log n) to O(log n) per operation.
|
|
11
11
|
*/
|
|
12
12
|
import type { Branch } from "./types";
|
|
13
|
+
/**
|
|
14
|
+
* Binary min-heap for branch-and-bound node selection.
|
|
15
|
+
*
|
|
16
|
+
* Orders branches by relaxed objective value (lowest first), with LIFO
|
|
17
|
+
* tie-breaking so that more recently created nodes are explored first
|
|
18
|
+
* when evaluations are equal. This mimics depth-first behavior in ties.
|
|
19
|
+
*
|
|
20
|
+
* Uses a flat array for cache efficiency and an object pool to reduce
|
|
21
|
+
* garbage collection pressure during intensive B&B traversals.
|
|
22
|
+
*/
|
|
13
23
|
export declare class BranchMinHeap {
|
|
24
|
+
/** Flat array storing heap entries in tree order. */
|
|
14
25
|
private heap;
|
|
26
|
+
/** Number of entries currently in the heap. */
|
|
15
27
|
private size;
|
|
28
|
+
/** Monotonically increasing counter for LIFO tie-breaking. */
|
|
16
29
|
private seqCounter;
|
|
30
|
+
/** Object pool for reusing HeapEntry objects. */
|
|
17
31
|
private pool;
|
|
32
|
+
/** Current number of entries in the pool. */
|
|
18
33
|
private poolSize;
|
|
34
|
+
/**
|
|
35
|
+
* @param initialCapacity - Pre-allocated array size (grows automatically if exceeded).
|
|
36
|
+
*/
|
|
19
37
|
constructor(initialCapacity?: number);
|
|
38
|
+
/** Allocate a HeapEntry, reusing from the pool if available. */
|
|
20
39
|
private allocEntry;
|
|
40
|
+
/** Return a HeapEntry to the pool for reuse (capped at 256 entries). */
|
|
21
41
|
private freeEntry;
|
|
42
|
+
/** Number of branches currently in the heap. */
|
|
22
43
|
get length(): number;
|
|
44
|
+
/** Whether the heap contains no branches. */
|
|
23
45
|
isEmpty(): boolean;
|
|
46
|
+
/** Remove all entries and reset the sequence counter. */
|
|
24
47
|
clear(): void;
|
|
48
|
+
/**
|
|
49
|
+
* Compare two entries for priority ordering.
|
|
50
|
+
* @returns True if `a` should be extracted before `b`.
|
|
51
|
+
*/
|
|
25
52
|
private isBefore;
|
|
53
|
+
/**
|
|
54
|
+
* Insert a branch into the heap. O(log n).
|
|
55
|
+
* @param branch - The B&B node to insert.
|
|
56
|
+
*/
|
|
26
57
|
push(branch: Branch): void;
|
|
58
|
+
/**
|
|
59
|
+
* Remove and return the highest-priority branch (lowest relaxed evaluation). O(log n).
|
|
60
|
+
* @returns The most promising branch, or undefined if empty.
|
|
61
|
+
*/
|
|
27
62
|
pop(): Branch | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* View the highest-priority branch without removing it. O(1).
|
|
65
|
+
* @returns The most promising branch, or undefined if empty.
|
|
66
|
+
*/
|
|
28
67
|
peek(): Branch | undefined;
|
|
29
68
|
}
|