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,37 +1,49 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file src/tableau/mip-utils.ts
|
|
3
|
-
* @description Mixed-integer programming utility functions
|
|
4
|
-
*
|
|
5
|
-
* Provides helper functions for MIP solving:
|
|
6
|
-
* - Checking integrality of current solution
|
|
7
|
-
* - Variable selection for branching (most fractional, etc.)
|
|
8
|
-
* - Fractional volume computation
|
|
9
|
-
*
|
|
10
|
-
* Functions are designed to be bound to a Tableau instance via `this`.
|
|
11
|
-
*/
|
|
12
1
|
import type Tableau from "./tableau";
|
|
13
2
|
import type { VariableValue } from "./types";
|
|
3
|
+
/** Distance from a numeric value to its nearest integer. */
|
|
4
|
+
export declare function integerDistance(value: number): number;
|
|
14
5
|
/**
|
|
15
6
|
* Count how many integer variables currently have integral values.
|
|
7
|
+
* A value is considered integral if its distance to the nearest integer
|
|
8
|
+
* is within the tableau's precision tolerance.
|
|
9
|
+
*
|
|
10
|
+
* @returns Number of integer variables with integral current values.
|
|
16
11
|
*/
|
|
17
12
|
export declare function countIntegerValues(this: Tableau): number;
|
|
18
13
|
/**
|
|
19
|
-
* Check if all integer variables have integral values.
|
|
20
|
-
*
|
|
14
|
+
* Check if all integer variables have integral values within precision.
|
|
15
|
+
*
|
|
16
|
+
* @returns True if the current solution satisfies all integrality constraints.
|
|
21
17
|
*/
|
|
22
18
|
export declare function isIntegral(this: Tableau): boolean;
|
|
23
19
|
/**
|
|
24
|
-
* Compute a measure of
|
|
25
|
-
*
|
|
20
|
+
* Compute a measure of total fractionality across all integer variables.
|
|
21
|
+
*
|
|
22
|
+
* The "volume" is the product of fractional values. Used to evaluate cutting
|
|
23
|
+
* plane effectiveness: a decreasing volume indicates the LP relaxation is
|
|
24
|
+
* tightening toward integrality.
|
|
25
|
+
*
|
|
26
|
+
* @param ignoreIntegerValues - If true, skip already-integral variables (don't return 0 early).
|
|
27
|
+
* @returns Product of fractional variable values, or 0 if all are integral.
|
|
26
28
|
*/
|
|
27
29
|
export declare function computeFractionalVolume(this: Tableau, ignoreIntegerValues?: boolean): number;
|
|
28
30
|
/**
|
|
29
|
-
* Select the integer variable with the most fractional value.
|
|
30
|
-
*
|
|
31
|
+
* Select the integer variable with the most fractional value for branching.
|
|
32
|
+
*
|
|
33
|
+
* This is the default branching strategy. Picks the variable whose current
|
|
34
|
+
* value is farthest from the nearest integer (closest to 0.5 fractionality),
|
|
35
|
+
* creating the most balanced branching.
|
|
36
|
+
*
|
|
37
|
+
* @returns The selected variable's index and value, or nulls if all are integral.
|
|
31
38
|
*/
|
|
32
39
|
export declare function getMostFractionalVar(this: Tableau): VariableValue;
|
|
33
40
|
/**
|
|
34
|
-
* Select the fractional integer variable with the lowest
|
|
35
|
-
*
|
|
41
|
+
* Select the fractional integer variable with the lowest objective coefficient.
|
|
42
|
+
*
|
|
43
|
+
* Alternative branching strategy that prioritizes variables with less
|
|
44
|
+
* objective impact, potentially leading to smaller objective degradation
|
|
45
|
+
* in child nodes.
|
|
46
|
+
*
|
|
47
|
+
* @returns The selected variable's index and value, or nulls if all are integral.
|
|
36
48
|
*/
|
|
37
49
|
export declare function getFractionalVarWithLowestCost(this: Tableau): VariableValue;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/tableau/mir.ts
|
|
3
|
+
* @description Complemented mixed-integer rounding (c-MIR) cuts on the original rows
|
|
4
|
+
*
|
|
5
|
+
* Marchand and Wolsey (2001): take a row sum_j a_j x_j <= b (or an
|
|
6
|
+
* aggregation of a few rows that eliminates continuous variables strictly
|
|
7
|
+
* inside their bounds), substitute every variable by a bound so that it is
|
|
8
|
+
* nonnegative (integers possibly complemented to their upper bound), relax
|
|
9
|
+
* continuous terms with positive coefficient, divide by a candidate delta and
|
|
10
|
+
* apply mixed-integer rounding:
|
|
11
|
+
*
|
|
12
|
+
* sum_int F(a_j / delta) y_j - s / (delta (1 - f0)) <= floor(beta / delta),
|
|
13
|
+
* F(d) = floor(d) + max(0, frac(d) - f0) / (1 - f0), f0 = frac(beta / delta),
|
|
14
|
+
*
|
|
15
|
+
* where s >= 0 collects the continuous terms with negative coefficient.
|
|
16
|
+
* Knapsack covers and flow covers are special cases, which is why this one
|
|
17
|
+
* separator closes most of the root gap on fixed-charge models (MIPLIB fiber,
|
|
18
|
+
* vpm2, qnet1) where Gomory cuts barely move the bound.
|
|
19
|
+
*
|
|
20
|
+
* Rows are captured in x-space right after root presolve, while the tableau
|
|
21
|
+
* still holds them, so cuts are globally valid. A cut sum_j alpha_j x_j <= beta
|
|
22
|
+
* is then mapped into the current tableau by substituting each basic
|
|
23
|
+
* variable's row (addLinearCut).
|
|
24
|
+
*/
|
|
25
|
+
import type Tableau from "./tableau";
|
|
26
|
+
/** A row sum_j coef[k] x_{index[k]} <= rhs over structural variables. */
|
|
27
|
+
interface SparseRow {
|
|
28
|
+
index: number[];
|
|
29
|
+
coef: number[];
|
|
30
|
+
rhs: number;
|
|
31
|
+
}
|
|
32
|
+
export interface OriginalRows {
|
|
33
|
+
rows: SparseRow[];
|
|
34
|
+
/** Rows containing each structural variable. */
|
|
35
|
+
rowsOf: Map<number, number[]>;
|
|
36
|
+
/**
|
|
37
|
+
* Variable upper bounds x <= u y (x continuous, y integer), from rows
|
|
38
|
+
* a x + c y <= 0 with a > 0 > c: the substitution behind flow covers.
|
|
39
|
+
*/
|
|
40
|
+
vub: Map<number, {
|
|
41
|
+
y: number;
|
|
42
|
+
u: number;
|
|
43
|
+
}>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Capture the rows of a fresh tableau (every structural nonbasic, every row
|
|
47
|
+
* slack basic) in x-space. Rows touching free variables are skipped.
|
|
48
|
+
*/
|
|
49
|
+
export declare function captureOriginalRows(tableau: Tableau): OriginalRows | null;
|
|
50
|
+
/** A cut sum alpha_j x_j <= beta with its efficacy at the LP point. */
|
|
51
|
+
export interface Cut {
|
|
52
|
+
alpha: Map<number, number>;
|
|
53
|
+
beta: number;
|
|
54
|
+
efficacy: number;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Separate c-MIR cuts from the captured rows at the current LP optimum and
|
|
58
|
+
* append up to `maxCuts` of the most efficacious ones to the tableau.
|
|
59
|
+
*
|
|
60
|
+
* @returns The number of cuts added.
|
|
61
|
+
*/
|
|
62
|
+
export declare function addMirCuts(tableau: Tableau, original: OriginalRows, maxCuts: number): number;
|
|
63
|
+
/** c-MIR cuts from the captured original rows at the current LP optimum. */
|
|
64
|
+
export declare function separateMirCuts(tableau: Tableau, original: OriginalRows): Cut[];
|
|
65
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -12,17 +12,29 @@
|
|
|
12
12
|
*/
|
|
13
13
|
import type Model from "../model";
|
|
14
14
|
import type { Variable, Constraint } from "../expressions";
|
|
15
|
+
/**
|
|
16
|
+
* Results from the presolve preprocessing phase.
|
|
17
|
+
* Contains all reductions discovered and an infeasibility flag.
|
|
18
|
+
*/
|
|
15
19
|
export interface PresolveResult {
|
|
20
|
+
/** Variables proven to have a unique feasible value. */
|
|
16
21
|
fixedVariables: Map<Variable, number>;
|
|
22
|
+
/** Constraints proven to be redundant (always satisfied). */
|
|
17
23
|
removedConstraints: Set<Constraint>;
|
|
24
|
+
/** Variables with tightened lower/upper bounds from constraint analysis. */
|
|
18
25
|
tightenedBounds: Map<Variable, {
|
|
19
26
|
lower?: number;
|
|
20
27
|
upper?: number;
|
|
21
28
|
}>;
|
|
29
|
+
/** Set to true if preprocessing proves the problem has no feasible solution. */
|
|
22
30
|
isInfeasible: boolean;
|
|
31
|
+
/** Summary statistics of reductions applied. */
|
|
23
32
|
stats: {
|
|
33
|
+
/** Number of variables fixed to a constant value. */
|
|
24
34
|
variablesFixed: number;
|
|
35
|
+
/** Number of constraints removed as redundant. */
|
|
25
36
|
constraintsRemoved: number;
|
|
37
|
+
/** Number of variable bounds tightened. */
|
|
26
38
|
boundsTightened: number;
|
|
27
39
|
};
|
|
28
40
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/tableau/pseudocosts.ts
|
|
3
|
+
* @description Pseudocosts for branching-variable selection
|
|
4
|
+
*
|
|
5
|
+
* A pseudocost is the average objective degradation per unit of change when
|
|
6
|
+
* a variable is branched on in one direction (Bénichou et al. 1971). Branching
|
|
7
|
+
* on x = 2.3 down moves it by f = 0.3 and up by 1 - f = 0.7; if the child's
|
|
8
|
+
* LP bound worsens by g, the observation is g / f (down) or g / (1 - f) (up).
|
|
9
|
+
*
|
|
10
|
+
* Candidates are scored with the product rule (Achterberg 2007, ch. 5):
|
|
11
|
+
* max(down gain, eps) * max(up gain, eps), which prefers variables that
|
|
12
|
+
* degrade both children over ones that degrade only one.
|
|
13
|
+
*/
|
|
14
|
+
/** Observations per direction before a pseudocost is trusted (SCIP's default η_rel is 8). */
|
|
15
|
+
export declare const RELIABILITY = 4;
|
|
16
|
+
export declare class Pseudocosts {
|
|
17
|
+
private upSum;
|
|
18
|
+
private upCount;
|
|
19
|
+
private downSum;
|
|
20
|
+
private downCount;
|
|
21
|
+
private totalUp;
|
|
22
|
+
private totalUpCount;
|
|
23
|
+
private totalDown;
|
|
24
|
+
private totalDownCount;
|
|
25
|
+
constructor(nVars: number);
|
|
26
|
+
/**
|
|
27
|
+
* Record one branching observation.
|
|
28
|
+
* @param distance - How far the branch moved the variable (f down, 1 - f up).
|
|
29
|
+
* @param gain - Child bound minus parent bound (>= 0 in the minimization form).
|
|
30
|
+
*/
|
|
31
|
+
record(varIndex: number, up: boolean, distance: number, gain: number): void;
|
|
32
|
+
/** Fewer observations of the two directions. */
|
|
33
|
+
reliability(varIndex: number): number;
|
|
34
|
+
/** Per-unit up pseudocost; the average over all variables if none observed yet. */
|
|
35
|
+
up(varIndex: number): number;
|
|
36
|
+
/** Per-unit down pseudocost; the average over all variables if none observed yet. */
|
|
37
|
+
down(varIndex: number): number;
|
|
38
|
+
/** Product-rule score of branching on a variable with fractional part f. */
|
|
39
|
+
score(varIndex: number, f: number): number;
|
|
40
|
+
}
|
|
41
|
+
/** Product rule over the two children's (estimated or measured) gains. */
|
|
42
|
+
export declare function productScore(downGain: number, upGain: number): number;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/tableau/root-presolve.ts
|
|
3
|
+
* @description Bound propagation and coefficient tightening on the root tableau
|
|
4
|
+
*
|
|
5
|
+
* Runs on a freshly built tableau (every structural variable nonbasic, every
|
|
6
|
+
* row slack basic), where row r reads sum_j a_rj s_j <= b_r over stored
|
|
7
|
+
* variables s_j in [0, range_j] (bounds.ts shifts and complements each
|
|
8
|
+
* variable so it sits at zero).
|
|
9
|
+
*
|
|
10
|
+
* - Bound propagation (activity-based, as in every MIP presolve): the row's
|
|
11
|
+
* minimum activity without s_j bounds s_j above (a_rj > 0) or below
|
|
12
|
+
* (a_rj < 0). Integer bounds are rounded inward. Implied bounds never change
|
|
13
|
+
* the feasible set, only make it explicit.
|
|
14
|
+
* - Coefficient tightening (Savelsbergh 1994): for a binary s_k whose row is
|
|
15
|
+
* redundant at one of its values, the coefficient (and right-hand side) can
|
|
16
|
+
* shrink so the row stays the same at both values while the LP relaxation
|
|
17
|
+
* tightens. The classic case is a big-M row x - M y <= 0 with x <= u:
|
|
18
|
+
* M shrinks to u.
|
|
19
|
+
*
|
|
20
|
+
* It works on internal tableau rows only, so the user's model is untouched.
|
|
21
|
+
*/
|
|
22
|
+
import type Tableau from "./tableau";
|
|
23
|
+
export interface RootPresolveResult {
|
|
24
|
+
/** False if propagation proved the bounds empty. */
|
|
25
|
+
feasible: boolean;
|
|
26
|
+
tightenedBounds: number;
|
|
27
|
+
tightenedCoefficients: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Tighten bounds and big-M coefficients of a fresh tableau in place.
|
|
31
|
+
* Does nothing unless every structural column is nonbasic.
|
|
32
|
+
*/
|
|
33
|
+
export declare function presolveRootTableau(tableau: Tableau): RootPresolveResult;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -10,6 +10,14 @@
|
|
|
10
10
|
* Uses partial pricing for large problems to improve performance.
|
|
11
11
|
*/
|
|
12
12
|
import type Tableau from "./tableau";
|
|
13
|
+
/**
|
|
14
|
+
* Run the full two-phase simplex algorithm.
|
|
15
|
+
*
|
|
16
|
+
* Phase 1 finds a basic feasible solution (or proves infeasibility).
|
|
17
|
+
* Phase 2 optimizes the objective (or proves unboundedness).
|
|
18
|
+
*
|
|
19
|
+
* @returns The tableau instance for chaining.
|
|
20
|
+
*/
|
|
13
21
|
export declare function simplex(this: Tableau): Tableau;
|
|
14
22
|
/**
|
|
15
23
|
* Dual simplex algorithm for warm-starting after adding constraints.
|
|
@@ -27,7 +35,45 @@ export declare function simplex(this: Tableau): Tableau;
|
|
|
27
35
|
* @returns Number of iterations, or -1 if dual infeasible
|
|
28
36
|
*/
|
|
29
37
|
export declare function dualSimplex(this: Tableau): number;
|
|
38
|
+
/**
|
|
39
|
+
* Phase 1: Find an initial basic feasible solution.
|
|
40
|
+
*
|
|
41
|
+
* Pivots to eliminate negative RHS values (infeasible basic variables).
|
|
42
|
+
* Uses max-quotient rule for fast convergence, with anti-cycling fallback
|
|
43
|
+
* to Bland's rule if degenerate stalling is detected.
|
|
44
|
+
*
|
|
45
|
+
* @returns Number of pivot iterations performed.
|
|
46
|
+
*/
|
|
30
47
|
export declare function phase1(this: Tableau): number;
|
|
48
|
+
/**
|
|
49
|
+
* Phase 2: Optimize the objective function.
|
|
50
|
+
*
|
|
51
|
+
* Pivots to improve the objective by selecting entering variables with
|
|
52
|
+
* positive reduced costs. Uses partial pricing on large problems (batched
|
|
53
|
+
* column scanning) and falls back to Bland's rule on degenerate cycling.
|
|
54
|
+
*
|
|
55
|
+
* Handles hierarchical (multi-priority) objectives by checking optional
|
|
56
|
+
* objective reduced costs when the primary objective is at optimality.
|
|
57
|
+
*
|
|
58
|
+
* @returns Number of pivot iterations performed.
|
|
59
|
+
*/
|
|
31
60
|
export declare function phase2(this: Tableau): number;
|
|
61
|
+
/**
|
|
62
|
+
* Perform a single simplex pivot operation.
|
|
63
|
+
*
|
|
64
|
+
* Swaps the entering variable (non-basic, in column) with the leaving variable
|
|
65
|
+
* (basic, in row). Updates the entire matrix using cached pivot row values
|
|
66
|
+
* for better cache locality. Also updates optional objective reduced costs.
|
|
67
|
+
*
|
|
68
|
+
* @param pivotRowIndex - Row of the leaving variable.
|
|
69
|
+
* @param pivotColumnIndex - Column of the entering variable.
|
|
70
|
+
*/
|
|
32
71
|
export declare function pivot(this: Tableau, pivotRowIndex: number, pivotColumnIndex: number): void;
|
|
72
|
+
/**
|
|
73
|
+
* Legacy O(n^2) cycle detection by brute-force pair comparison.
|
|
74
|
+
* Superseded by CycleDetector but retained for API compatibility.
|
|
75
|
+
*
|
|
76
|
+
* @param varIndexes - Array of [leaving, entering] pairs from pivot history.
|
|
77
|
+
* @returns [startIndex, cycleLength] if cycle found, empty array otherwise.
|
|
78
|
+
*/
|
|
33
79
|
export declare function checkForCycles(this: Tableau, varIndexes: Array<[number, number]>): number[];
|
|
@@ -1,35 +1,54 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file src/tableau/solution.ts
|
|
3
|
-
* @description Solution classes for LP and MIP results
|
|
4
|
-
*
|
|
5
|
-
* Provides solution containers for optimization results:
|
|
6
|
-
* - Solution: Base class for continuous LP problems
|
|
7
|
-
* - MilpSolution: Extended class for mixed-integer problems
|
|
8
|
-
*
|
|
9
|
-
* Solutions include feasibility status, objective value, and variable values.
|
|
10
|
-
*/
|
|
11
1
|
import type Tableau from "./tableau";
|
|
12
2
|
import type { TableauSolutionSet } from "./types";
|
|
13
3
|
/**
|
|
14
4
|
* Represents a solution to a linear programming problem.
|
|
5
|
+
*
|
|
6
|
+
* Contains the feasibility status, objective value, and provides a method
|
|
7
|
+
* to extract variable values from the tableau's current state.
|
|
15
8
|
*/
|
|
16
9
|
export declare class Solution {
|
|
10
|
+
/** Whether the solver found a feasible solution. */
|
|
17
11
|
feasible: boolean;
|
|
12
|
+
/** Optimal objective function value (in the model's original direction). */
|
|
18
13
|
evaluation: number;
|
|
14
|
+
/** Whether the problem is bounded (false = unbounded objective). */
|
|
19
15
|
bounded: boolean;
|
|
16
|
+
/** Reference to the source tableau (used by generateSolutionSet). */
|
|
20
17
|
_tableau: Tableau;
|
|
18
|
+
/** Cached variable-to-value mapping (populated by generateSolutionSet). */
|
|
21
19
|
solutionSet: TableauSolutionSet;
|
|
20
|
+
/**
|
|
21
|
+
* @param tableau - The solved tableau containing variable values.
|
|
22
|
+
* @param evaluation - Objective value (already sign-corrected for min/max).
|
|
23
|
+
* @param feasible - Whether a feasible solution exists.
|
|
24
|
+
* @param bounded - Whether the objective is bounded.
|
|
25
|
+
*/
|
|
22
26
|
constructor(tableau: Tableau, evaluation: number, feasible: boolean, bounded: boolean);
|
|
23
27
|
/**
|
|
24
|
-
*
|
|
28
|
+
* Extract variable values from the tableau's current state.
|
|
29
|
+
*
|
|
30
|
+
* Iterates over all basic variables, skipping slack variables, and
|
|
31
|
+
* rounds values to the tableau's precision to avoid floating-point noise.
|
|
32
|
+
*
|
|
33
|
+
* @returns A map from variable ID to its solution value.
|
|
25
34
|
*/
|
|
26
35
|
generateSolutionSet(): TableauSolutionSet;
|
|
27
36
|
}
|
|
28
37
|
/**
|
|
29
38
|
* Represents a solution to a mixed-integer programming problem.
|
|
30
|
-
*
|
|
39
|
+
*
|
|
40
|
+
* Extends the base Solution with the number of branch-and-cut iterations
|
|
41
|
+
* performed, which is useful for performance analysis and debugging.
|
|
31
42
|
*/
|
|
32
43
|
export declare class MilpSolution extends Solution {
|
|
44
|
+
/** Number of branch-and-cut iterations (nodes explored) to find this solution. */
|
|
33
45
|
iter: number;
|
|
46
|
+
/**
|
|
47
|
+
* @param tableau - The solved tableau.
|
|
48
|
+
* @param evaluation - Objective value.
|
|
49
|
+
* @param feasible - Whether an integer-feasible solution was found.
|
|
50
|
+
* @param bounded - Whether the problem is bounded.
|
|
51
|
+
* @param branchAndCutIterations - Number of B&C nodes explored.
|
|
52
|
+
*/
|
|
34
53
|
constructor(tableau: Tableau, evaluation: number, feasible: boolean, bounded: boolean, branchAndCutIterations: number);
|
|
35
54
|
}
|
|
@@ -2,42 +2,126 @@ import type Model from "../model";
|
|
|
2
2
|
import type { Constraint, Variable } from "../expressions";
|
|
3
3
|
import type { BranchCut, OptionalObjective, TableauSolution, VariableValue } from "./types";
|
|
4
4
|
import type { BranchAndCutService } from "./branch-and-cut";
|
|
5
|
+
import * as backupOps from "./backup";
|
|
6
|
+
/**
|
|
7
|
+
* Core simplex tableau data structure.
|
|
8
|
+
*
|
|
9
|
+
* Stores the LP problem in matrix form (Float64Array for numerical precision)
|
|
10
|
+
* and provides methods for simplex operations, branch-and-cut, and dynamic
|
|
11
|
+
* model modification. Each row represents a basic variable; each column
|
|
12
|
+
* represents a non-basic variable. The first row is the cost (objective) row;
|
|
13
|
+
* the first column is the RHS column.
|
|
14
|
+
*
|
|
15
|
+
* This class delegates its method implementations to separate modules
|
|
16
|
+
* (simplex, cutting-strategies, backup, etc.) for maintainability.
|
|
17
|
+
*/
|
|
5
18
|
export default class Tableau {
|
|
19
|
+
/** The model this tableau was built from (null until setModel is called). */
|
|
6
20
|
model: Model | null;
|
|
21
|
+
/** Flat row-major matrix storage. Element at (row, col) is matrix[row * width + col]. */
|
|
7
22
|
matrix: Float64Array;
|
|
23
|
+
/** Number of columns (non-basic variables + RHS column). */
|
|
8
24
|
width: number;
|
|
25
|
+
/** Number of rows (basic variables + cost row). */
|
|
9
26
|
height: number;
|
|
27
|
+
/** Row index of the objective function (always 0). */
|
|
10
28
|
costRowIndex: number;
|
|
29
|
+
/** Column index of the RHS values (always 0). */
|
|
11
30
|
rhsColumn: number;
|
|
31
|
+
/** Lookup table: internal variable index -> Variable object. */
|
|
12
32
|
variablesPerIndex: Array<Variable | undefined>;
|
|
33
|
+
/** Set of variable indices that are unrestricted (may be negative). */
|
|
13
34
|
unrestrictedVars: Record<number, boolean>;
|
|
35
|
+
/** Whether the current solution is feasible. Set to false by phase 1 if no BFS found. */
|
|
14
36
|
feasible: boolean;
|
|
37
|
+
/** Current objective function value (negated internally for minimization). */
|
|
15
38
|
evaluation: number;
|
|
39
|
+
/** Total simplex iterations performed across all phases. */
|
|
16
40
|
simplexIters: number;
|
|
41
|
+
/** Maps row index -> variable index of the basic variable in that row. */
|
|
17
42
|
varIndexByRow: number[];
|
|
43
|
+
/** Maps column index -> variable index of the non-basic variable in that column. */
|
|
18
44
|
varIndexByCol: number[];
|
|
45
|
+
/** Maps variable index -> row index (-1 if non-basic). */
|
|
19
46
|
rowByVarIndex: number[];
|
|
47
|
+
/** Maps variable index -> column index (-1 if basic). */
|
|
20
48
|
colByVarIndex: number[];
|
|
49
|
+
/** Numerical precision tolerance for integrality and zero comparisons. */
|
|
21
50
|
precision: number;
|
|
51
|
+
/** Secondary objectives sorted by priority (for hierarchical optimization). */
|
|
22
52
|
optionalObjectives: OptionalObjective[];
|
|
53
|
+
/** Lookup: priority level -> optional objective. */
|
|
23
54
|
objectivesByPriority: Record<number, OptionalObjective>;
|
|
55
|
+
/** Alias for objectivesByPriority (legacy compatibility). */
|
|
24
56
|
optionalObjectivePerPriority: Record<number, OptionalObjective>;
|
|
57
|
+
/** Saved tableau state for branch-and-cut restoration. */
|
|
25
58
|
savedState: Tableau | null;
|
|
59
|
+
/** Pivots performed so far (deterministic work measure for branch-and-cut). */
|
|
60
|
+
pivotCount: number;
|
|
61
|
+
/**
|
|
62
|
+
* When positive, a dual phase 1 from a dual-feasible basis stops after
|
|
63
|
+
* this many iterations and reports its objective (a valid bound) with
|
|
64
|
+
* iterationLimited set. Used by strong branching, which only needs bounds.
|
|
65
|
+
*/
|
|
66
|
+
dualIterationLimit: number;
|
|
67
|
+
/** Whether the last simplex() stopped at dualIterationLimit. */
|
|
68
|
+
iterationLimited: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Row equilibration factor per constraint (indexed by the constraint's
|
|
71
|
+
* slack variable index; absent means 1). Each constraint row is stored
|
|
72
|
+
* multiplied by a power of two that brings its largest coefficient near
|
|
73
|
+
* 1, so the slack is measured on a comparable scale to the structurals
|
|
74
|
+
* and the fixed tolerances mean the same thing on every row.
|
|
75
|
+
*/
|
|
76
|
+
rowScale: Float64Array;
|
|
77
|
+
/** True once any column bound has been set (see bounds.ts). */
|
|
78
|
+
hasBounds: boolean;
|
|
79
|
+
/** A column bound from the model had an empty range (lb > ub). */
|
|
80
|
+
hasEmptyColumnBound: boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Per variable index (dense, allocated on first use; see bounds.ts):
|
|
83
|
+
* x = boundOffset + boundSign * stored, stored in [0, boundRange],
|
|
84
|
+
* actual bounds [boundLower, boundUpper].
|
|
85
|
+
*/
|
|
86
|
+
boundOffset: Float64Array;
|
|
87
|
+
boundSign: Float64Array;
|
|
88
|
+
boundRange: Float64Array;
|
|
89
|
+
boundLower: Float64Array;
|
|
90
|
+
boundUpper: Float64Array;
|
|
91
|
+
/** Pool of recycled variable/constraint indices. */
|
|
26
92
|
availableIndexes: number[];
|
|
93
|
+
/** Next fresh index to assign (when pool is empty). */
|
|
27
94
|
lastElementIndex: number;
|
|
95
|
+
/** All decision variables in column order. */
|
|
28
96
|
variables: Variable[];
|
|
97
|
+
/** Total number of variable/constraint indices allocated. */
|
|
29
98
|
nVars: number;
|
|
99
|
+
/** Whether the problem is bounded (set to false if unbounded ray detected). */
|
|
30
100
|
bounded: boolean;
|
|
101
|
+
/** Index of the unbounded variable (if bounded is false). */
|
|
31
102
|
unboundedVarIndex: number | null;
|
|
103
|
+
/** Number of B&B nodes explored in the last branchAndCut call. */
|
|
32
104
|
branchAndCutIterations: number;
|
|
105
|
+
/** Best possible objective value (LP relaxation at root). */
|
|
33
106
|
bestPossibleEval: number;
|
|
107
|
+
/** Set to true when the current solution satisfies all integrality constraints. */
|
|
34
108
|
__isIntegral?: boolean;
|
|
109
|
+
/** Starting column for partial pricing sweep in phase 2. */
|
|
35
110
|
pricingBatchStart: number;
|
|
111
|
+
/** Batch size for partial pricing (0 = auto-compute based on problem size). */
|
|
36
112
|
pricingBatchSize: number;
|
|
113
|
+
/** The branch-and-cut strategy used for MIP solving. */
|
|
37
114
|
readonly branchAndCutService: BranchAndCutService;
|
|
115
|
+
/**
|
|
116
|
+
* @param precision - Numerical tolerance for zero/integrality tests (default: 1e-8).
|
|
117
|
+
* @param branchAndCutService - Custom B&C strategy (default: basic best-first).
|
|
118
|
+
*/
|
|
38
119
|
constructor(precision?: number, branchAndCutService?: BranchAndCutService);
|
|
120
|
+
/** Run two-phase simplex to find the optimal LP solution. */
|
|
39
121
|
simplex(): this;
|
|
122
|
+
/** Phase 1: Find a basic feasible solution. @returns Pivot iteration count. */
|
|
40
123
|
phase1(): number;
|
|
124
|
+
/** Phase 2: Optimize the objective. @returns Pivot iteration count. */
|
|
41
125
|
phase2(): number;
|
|
42
126
|
/**
|
|
43
127
|
* Dual simplex for warm-starting after adding bound constraints.
|
|
@@ -45,40 +129,109 @@ export default class Tableau {
|
|
|
45
129
|
* @returns Number of iterations, or -1 if dual infeasible
|
|
46
130
|
*/
|
|
47
131
|
dualSimplex(): number;
|
|
132
|
+
/**
|
|
133
|
+
* Perform a single simplex pivot.
|
|
134
|
+
* @param pivotRowIndex - Row of the leaving variable.
|
|
135
|
+
* @param pivotColumnIndex - Column of the entering variable.
|
|
136
|
+
*/
|
|
48
137
|
pivot(pivotRowIndex: number, pivotColumnIndex: number): void;
|
|
138
|
+
/** Detect pivot cycles in a history of (leaving, entering) pairs. */
|
|
49
139
|
checkForCycles(varIndexes: Array<[number, number]>): number[];
|
|
140
|
+
/** Count how many integer variables currently have integral values. */
|
|
50
141
|
countIntegerValues(): number;
|
|
142
|
+
/** Check if all integer variables have integral values within precision. */
|
|
51
143
|
isIntegral(): boolean;
|
|
144
|
+
/**
|
|
145
|
+
* Compute sum of fractionalities of integer variables.
|
|
146
|
+
* @param ignoreIntegerValues - If true, skip variables that are already integral.
|
|
147
|
+
* @returns Total fractional volume (0 = all integral).
|
|
148
|
+
*/
|
|
52
149
|
computeFractionalVolume(ignoreIntegerValues?: boolean): number;
|
|
150
|
+
/** Add bound constraints and grow the tableau as needed. */
|
|
53
151
|
addCutConstraints(branchingCuts: BranchCut[]): void;
|
|
152
|
+
/** Apply MIR cuts to all fractional integer rows. */
|
|
54
153
|
applyMIRCuts(): void;
|
|
154
|
+
/** Add a lower-bound MIR cut from the given row. @returns True if cut was added. */
|
|
55
155
|
addLowerBoundMIRCut(rowIndex: number): boolean;
|
|
156
|
+
/** Add an upper-bound MIR cut from the given row. @returns True if cut was added. */
|
|
56
157
|
addUpperBoundMIRCut(rowIndex: number): boolean;
|
|
158
|
+
/** Find the integer variable with the most fractional current value. */
|
|
57
159
|
getMostFractionalVar(): VariableValue;
|
|
160
|
+
/** Find the fractional integer variable with the lowest objective cost. */
|
|
58
161
|
getFractionalVarWithLowestCost(): VariableValue;
|
|
162
|
+
/** Pivot a variable into the basis. @returns Its new row index. */
|
|
59
163
|
putInBase(varIndex: number): number;
|
|
164
|
+
/** Pivot a variable out of the basis. @returns Its new column index. */
|
|
60
165
|
takeOutOfBase(varIndex: number): number;
|
|
166
|
+
/** Read variable values from the matrix into Variable.value. */
|
|
61
167
|
updateVariableValues(): void;
|
|
168
|
+
/** Update a constraint's RHS in the active tableau. */
|
|
62
169
|
updateRightHandSide(constraint: Constraint, difference: number): void;
|
|
170
|
+
/** Update a variable's coefficient in a constraint. */
|
|
63
171
|
updateConstraintCoefficient(constraint: Constraint, variable: Variable, difference: number): void;
|
|
172
|
+
/** Update a variable's objective coefficient. */
|
|
64
173
|
updateCost(variable: Variable, difference: number): void;
|
|
174
|
+
/** Add a new constraint row to the tableau. */
|
|
65
175
|
addConstraint(constraint: Constraint): void;
|
|
176
|
+
/** Remove a constraint row from the tableau. */
|
|
66
177
|
removeConstraint(constraint: Constraint): void;
|
|
178
|
+
/** Add a new variable column to the tableau. */
|
|
67
179
|
addVariable(variable: Variable): void;
|
|
180
|
+
/** Remove a variable column from the tableau. */
|
|
68
181
|
removeVariable(variable: Variable): void;
|
|
182
|
+
/** Create a deep copy of this tableau's mutable state. */
|
|
69
183
|
copy(): Tableau;
|
|
184
|
+
/** Snapshot the current state for later restoration. */
|
|
70
185
|
save(): void;
|
|
186
|
+
/** Restore the tableau to its previously saved state. */
|
|
71
187
|
restore(): void;
|
|
188
|
+
/** Capture the current node's LP state (see backup.snapshot). */
|
|
189
|
+
snapshot(reuse?: backupOps.NodeSnapshot | null): backupOps.NodeSnapshot;
|
|
190
|
+
/** Load a node LP state captured by snapshot(). */
|
|
191
|
+
loadSnapshot(snap: backupOps.NodeSnapshot): void;
|
|
192
|
+
/** Print the tableau matrix to console for debugging. */
|
|
72
193
|
log(message: unknown): this;
|
|
194
|
+
/** Apply branch cuts and re-solve the LP relaxation. */
|
|
73
195
|
applyCuts(branchingCuts: BranchCut[]): void;
|
|
196
|
+
/** Run branch-and-cut to find an integer-optimal solution. */
|
|
74
197
|
branchAndCut(): void;
|
|
198
|
+
/**
|
|
199
|
+
* Solve the problem: run B&C for MIP or simplex for LP, then extract the solution.
|
|
200
|
+
* @returns The complete solution object.
|
|
201
|
+
*/
|
|
75
202
|
solve(): TableauSolution;
|
|
203
|
+
/** Build a Solution or MilpSolution from the current tableau state. */
|
|
76
204
|
getSolution(): TableauSolution;
|
|
205
|
+
/**
|
|
206
|
+
* Register or update a secondary (optional) objective at the given priority.
|
|
207
|
+
* @param priority - Priority level (lower = optimized first).
|
|
208
|
+
* @param column - Column index for the reduced cost entry.
|
|
209
|
+
* @param cost - Reduced cost value for this variable in this objective.
|
|
210
|
+
*/
|
|
77
211
|
setOptionalObjective(priority: number, column: number, cost: number): void;
|
|
212
|
+
/**
|
|
213
|
+
* Allocate and initialize the tableau's matrix and index arrays.
|
|
214
|
+
* Called by setModel; should not be called directly.
|
|
215
|
+
*
|
|
216
|
+
* @param width - Number of columns (variables + 1).
|
|
217
|
+
* @param height - Number of rows (constraints + 1).
|
|
218
|
+
* @param variables - All decision variables.
|
|
219
|
+
* @param unrestrictedVars - Set of unrestricted variable indices.
|
|
220
|
+
*/
|
|
78
221
|
initialize(width: number, height: number, variables: Variable[], unrestrictedVars: Record<number, boolean>): void;
|
|
222
|
+
/** Populate the matrix with objective coefficients and constraint data from the model. */
|
|
79
223
|
_resetMatrix(): void;
|
|
224
|
+
/**
|
|
225
|
+
* Initialize the tableau from a Model instance.
|
|
226
|
+
* Allocates the matrix and populates it with the model's data.
|
|
227
|
+
* @param model - The optimization model to load.
|
|
228
|
+
* @returns This tableau for chaining.
|
|
229
|
+
*/
|
|
80
230
|
setModel(model: Model): this;
|
|
231
|
+
/** Allocate a new variable/constraint index (reuses from pool if available). */
|
|
81
232
|
getNewElementIndex(): number;
|
|
233
|
+
/** Compute the fraction of non-zero entries in the matrix (for sparsity analysis). */
|
|
82
234
|
density(): number;
|
|
235
|
+
/** Read the objective value from the matrix, round to precision, and store in this.evaluation. */
|
|
83
236
|
setEvaluation(): void;
|
|
84
237
|
}
|