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
|
@@ -13,65 +13,213 @@
|
|
|
13
13
|
* These classes support the fluent API: model.smallerThan(10).addTerm(2, x)
|
|
14
14
|
*/
|
|
15
15
|
import type Model from "./model";
|
|
16
|
+
/**
|
|
17
|
+
* Priority level for hierarchical optimization.
|
|
18
|
+
* Numeric values represent custom priority levels (lower = higher priority).
|
|
19
|
+
* String values map to predefined levels: "required" (0), "strong" (1), "medium" (2), "weak" (3).
|
|
20
|
+
*/
|
|
16
21
|
export type Priority = number | "required" | "strong" | "medium" | "weak";
|
|
22
|
+
/**
|
|
23
|
+
* A continuous decision variable in the optimization problem.
|
|
24
|
+
*
|
|
25
|
+
* Variables represent quantities the solver can adjust. Each variable has
|
|
26
|
+
* an objective coefficient (`cost`), a solution `value`, and a `priority`
|
|
27
|
+
* level for hierarchical optimization.
|
|
28
|
+
*/
|
|
17
29
|
export declare class Variable {
|
|
30
|
+
/** Unique string identifier (used in solution output). */
|
|
18
31
|
id: string;
|
|
32
|
+
/** Objective function coefficient. */
|
|
19
33
|
cost: number;
|
|
34
|
+
/** Internal index in the tableau's variable/column arrays. */
|
|
20
35
|
index: number;
|
|
36
|
+
/** Current solution value (set after solving). */
|
|
21
37
|
value: number;
|
|
38
|
+
/** Priority level for hierarchical optimization (0 = primary objective). */
|
|
22
39
|
priority: number;
|
|
40
|
+
/** Whether this variable is restricted to integer values. */
|
|
23
41
|
isInteger?: true;
|
|
42
|
+
/** Whether this is a slack variable (internal, not a decision variable). */
|
|
24
43
|
isSlack?: true;
|
|
44
|
+
/**
|
|
45
|
+
* @param id - Unique identifier for this variable.
|
|
46
|
+
* @param cost - Objective function coefficient.
|
|
47
|
+
* @param index - Internal tableau index.
|
|
48
|
+
* @param priority - Priority level for hierarchical optimization.
|
|
49
|
+
*/
|
|
25
50
|
constructor(id: string, cost: number, index: number, priority: number);
|
|
26
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* A decision variable restricted to integer values.
|
|
54
|
+
* The branch-and-cut algorithm ensures integrality in the final solution.
|
|
55
|
+
*/
|
|
27
56
|
export declare class IntegerVariable extends Variable {
|
|
28
57
|
isInteger: true;
|
|
29
58
|
constructor(id: string, cost: number, index: number, priority: number);
|
|
30
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* An internal variable added to convert inequality constraints to equalities.
|
|
62
|
+
* Slack variables have zero cost and priority and are excluded from solution output.
|
|
63
|
+
*/
|
|
31
64
|
export declare class SlackVariable extends Variable {
|
|
32
65
|
isSlack: true;
|
|
66
|
+
/**
|
|
67
|
+
* @param id - Identifier (typically "s" + index).
|
|
68
|
+
* @param index - Internal tableau index.
|
|
69
|
+
*/
|
|
33
70
|
constructor(id: string, index: number);
|
|
34
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* A single term in a linear expression: a variable multiplied by a coefficient.
|
|
74
|
+
*/
|
|
35
75
|
export declare class Term {
|
|
76
|
+
/** The variable in this term. */
|
|
36
77
|
variable: Variable;
|
|
78
|
+
/** The scalar multiplier for the variable. */
|
|
37
79
|
coefficient: number;
|
|
38
80
|
constructor(variable: Variable, coefficient: number);
|
|
39
81
|
}
|
|
40
82
|
type RelaxationModel = Model & {
|
|
41
83
|
addVariable(cost: number, id: string, isInteger?: boolean, isUnrestricted?: boolean, priority?: number): Variable;
|
|
42
84
|
};
|
|
85
|
+
/**
|
|
86
|
+
* Create a relaxation variable for a soft constraint.
|
|
87
|
+
*
|
|
88
|
+
* Relaxation variables allow constraints to be violated at a cost in the objective.
|
|
89
|
+
* The `weight` controls how expensive violation is; `priority` controls hierarchical ordering.
|
|
90
|
+
*
|
|
91
|
+
* @param model - The model to add the relaxation variable to.
|
|
92
|
+
* @param weight - Cost per unit of constraint violation (default: 1).
|
|
93
|
+
* @param priority - Priority level; "required" (or 0) means no relaxation is created.
|
|
94
|
+
* @returns The relaxation variable, or null if priority is "required".
|
|
95
|
+
*/
|
|
43
96
|
export declare function createRelaxationVariable(model: RelaxationModel, weight?: number, priority?: Priority): Variable | null;
|
|
97
|
+
/**
|
|
98
|
+
* A linear inequality constraint: sum(terms) <= rhs (upper bound) or sum(terms) >= rhs (lower bound).
|
|
99
|
+
*
|
|
100
|
+
* Constraints are created via `model.smallerThan()` or `model.greaterThan()` and populated
|
|
101
|
+
* with terms via the fluent `addTerm()` method. Internally, the constraint is stored
|
|
102
|
+
* in standard form with a slack variable.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* // x + 2y <= 10
|
|
107
|
+
* const c = model.smallerThan(10);
|
|
108
|
+
* c.addTerm(1, x).addTerm(2, y);
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
44
111
|
export declare class Constraint {
|
|
112
|
+
/** Slack variable converting this inequality to an equality for the tableau. */
|
|
45
113
|
slack: SlackVariable;
|
|
114
|
+
/** Internal constraint index in the tableau. */
|
|
46
115
|
index: number;
|
|
116
|
+
/** Reference to the parent model (used for dynamic coefficient updates). */
|
|
47
117
|
model: RelaxationModel;
|
|
118
|
+
/** Right-hand side value. */
|
|
48
119
|
rhs: number;
|
|
120
|
+
/** If true, this is a <= constraint; if false, a >= constraint. */
|
|
49
121
|
isUpperBound: boolean;
|
|
122
|
+
/** All terms on the left-hand side. */
|
|
50
123
|
terms: Term[];
|
|
124
|
+
/** Fast lookup of terms by variable index for coefficient updates. */
|
|
51
125
|
termsByVarIndex: Record<number, Term>;
|
|
126
|
+
/** Relaxation variable for soft constraint violation (null if hard). */
|
|
52
127
|
relaxation: Variable | null;
|
|
128
|
+
/**
|
|
129
|
+
* @param rhs - Right-hand side value.
|
|
130
|
+
* @param isUpperBound - True for <= constraint, false for >=.
|
|
131
|
+
* @param index - Internal index assigned by the tableau.
|
|
132
|
+
* @param model - Parent model for registering coefficient updates.
|
|
133
|
+
*/
|
|
53
134
|
constructor(rhs: number, isUpperBound: boolean, index: number, model: RelaxationModel);
|
|
135
|
+
/**
|
|
136
|
+
* Add a term (coefficient * variable) to this constraint.
|
|
137
|
+
* If the variable already has a term, the coefficients are summed.
|
|
138
|
+
*
|
|
139
|
+
* @param coefficient - Scalar multiplier for the variable.
|
|
140
|
+
* @param variable - The decision variable.
|
|
141
|
+
* @returns This constraint for method chaining.
|
|
142
|
+
*/
|
|
54
143
|
addTerm(coefficient: number, variable: Variable): this;
|
|
144
|
+
/** @deprecated Not yet implemented. Placeholder for future term removal. */
|
|
55
145
|
removeTerm(_term: Term): this;
|
|
146
|
+
/**
|
|
147
|
+
* Update the right-hand side value of this constraint.
|
|
148
|
+
* Propagates the change to the tableau if already initialized.
|
|
149
|
+
*
|
|
150
|
+
* @param newRhs - New RHS value.
|
|
151
|
+
* @returns This constraint for method chaining.
|
|
152
|
+
*/
|
|
56
153
|
setRightHandSide(newRhs: number): this;
|
|
154
|
+
/**
|
|
155
|
+
* Set the coefficient of a variable in this constraint.
|
|
156
|
+
* Creates a new term if the variable doesn't already appear.
|
|
157
|
+
*
|
|
158
|
+
* @param newCoefficient - The new coefficient value.
|
|
159
|
+
* @param variable - The variable whose coefficient to set.
|
|
160
|
+
* @returns This constraint for chaining, or void if the variable has no index.
|
|
161
|
+
*/
|
|
57
162
|
setVariableCoefficient(newCoefficient: number, variable: Variable): this | void;
|
|
163
|
+
/**
|
|
164
|
+
* Make this constraint "soft" by adding a relaxation variable.
|
|
165
|
+
* Violation of the constraint incurs a penalty in the objective.
|
|
166
|
+
*
|
|
167
|
+
* @param weight - Cost per unit of violation (default: 1).
|
|
168
|
+
* @param priority - Priority level for hierarchical optimization.
|
|
169
|
+
*/
|
|
58
170
|
relax(weight?: number, priority?: Priority): void;
|
|
171
|
+
/**
|
|
172
|
+
* Apply a relaxation variable to this constraint.
|
|
173
|
+
* Adds the variable with coefficient +1 (lower bound) or -1 (upper bound).
|
|
174
|
+
*/
|
|
59
175
|
_relax(relaxationVariable: Variable | null): void;
|
|
60
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* An equality constraint represented as two paired inequalities (upper + lower bound).
|
|
179
|
+
*
|
|
180
|
+
* Created via `model.equal(rhs)`. Operations like `addTerm()` and `relax()` are
|
|
181
|
+
* delegated to both underlying constraints simultaneously.
|
|
182
|
+
*/
|
|
61
183
|
export declare class Equality {
|
|
184
|
+
/** The <= constraint (sum <= rhs). */
|
|
62
185
|
upperBound: Constraint;
|
|
186
|
+
/** The >= constraint (sum >= rhs). */
|
|
63
187
|
lowerBound: Constraint;
|
|
188
|
+
/** Reference to the parent model. */
|
|
64
189
|
model: RelaxationModel;
|
|
190
|
+
/** Right-hand side value (same for both bounds). */
|
|
65
191
|
rhs: number;
|
|
192
|
+
/** Shared relaxation variable (null if hard constraint). */
|
|
66
193
|
relaxation: Variable | null;
|
|
194
|
+
/** Type discriminator for distinguishing Equality from Constraint. */
|
|
67
195
|
isEquality: true;
|
|
68
196
|
constructor(constraintUpper: Constraint, constraintLower: Constraint);
|
|
197
|
+
/**
|
|
198
|
+
* Add a term to both sides of the equality.
|
|
199
|
+
* @param coefficient - Scalar multiplier.
|
|
200
|
+
* @param variable - The decision variable.
|
|
201
|
+
* @returns This equality for method chaining.
|
|
202
|
+
*/
|
|
69
203
|
addTerm(coefficient: number, variable: Variable): this;
|
|
204
|
+
/** @deprecated Not yet implemented. Placeholder for future term removal. */
|
|
70
205
|
removeTerm(_term: Term): this;
|
|
206
|
+
/**
|
|
207
|
+
* Update the RHS value for both inequality constraints.
|
|
208
|
+
* @param rhs - New right-hand side value.
|
|
209
|
+
*/
|
|
71
210
|
setRightHandSide(rhs: number): void;
|
|
211
|
+
/**
|
|
212
|
+
* Make this equality constraint soft by adding a shared relaxation variable to both bounds.
|
|
213
|
+
* @param weight - Cost per unit of violation.
|
|
214
|
+
* @param priority - Priority level for hierarchical optimization.
|
|
215
|
+
*/
|
|
72
216
|
relax(weight?: number, priority?: Priority): void;
|
|
73
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* A wrapped numeric constant, used for expression building APIs.
|
|
220
|
+
*/
|
|
74
221
|
export declare class Numeral {
|
|
222
|
+
/** The numeric value. */
|
|
75
223
|
value: number;
|
|
76
224
|
constructor(value: number);
|
|
77
225
|
}
|
|
@@ -1,7 +1,2 @@
|
|
|
1
|
-
export const reformat: (model:
|
|
2
|
-
|
|
3
|
-
optimize: string;
|
|
4
|
-
constraints: {};
|
|
5
|
-
variables: {};
|
|
6
|
-
};
|
|
7
|
-
export function solve(model: any): Promise<any>;
|
|
1
|
+
export const reformat: (model: object | string | string[]) => object | string;
|
|
2
|
+
export function solve(model: object): Promise<Record<string, string>>;
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import type { Model as ModelDefinition } from "../types/solver";
|
|
2
|
+
/**
|
|
3
|
+
* Interface for an external solver integration module.
|
|
4
|
+
* Each module provides at least a `solve` method for delegating to an external process.
|
|
5
|
+
*/
|
|
2
6
|
export interface ExternalSolverModule {
|
|
7
|
+
/** Convert a JSON model to the external solver's input format. */
|
|
3
8
|
reformat?: (model: ModelDefinition) => unknown;
|
|
9
|
+
/** Solve the model using the external solver process. */
|
|
4
10
|
solve: (model: ModelDefinition) => Promise<unknown>;
|
|
5
11
|
}
|
|
12
|
+
/** Registry of available external solver modules keyed by solver name. */
|
|
6
13
|
export type ExternalSolvers = Record<string, ExternalSolverModule>;
|
|
7
14
|
declare const External: ExternalSolvers;
|
|
8
15
|
export default External;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/implied-integers.ts
|
|
3
|
+
* @description Detect continuous variables that can be treated as integer
|
|
4
|
+
*
|
|
5
|
+
* A continuous variable x is implied integer when every constraint it
|
|
6
|
+
* appears in, divided by x's coefficient, has integer coefficients on
|
|
7
|
+
* integer variables only and integer bounds. Fix the integer variables at
|
|
8
|
+
* any feasible value: each such row then bounds x by an integer, so x's
|
|
9
|
+
* feasible set is an interval with integer endpoints, and a linear objective
|
|
10
|
+
* attains its optimum at an integral point of it. Some optimal solution
|
|
11
|
+
* therefore has x integral, so branching and cuts may treat it as integer.
|
|
12
|
+
*
|
|
13
|
+
* Typical case: a "max-min" or makespan variable, e.g. maximize floor with
|
|
14
|
+
* sum_i skill_i x_iq - floor >= 0 for each period q. Treating floor as
|
|
15
|
+
* integer lets the LP bound round down to the best integral floor.
|
|
16
|
+
*/
|
|
17
|
+
import type { Model as JsonModel } from "./types/solver";
|
|
18
|
+
/**
|
|
19
|
+
* Ids marked integer in `ints` or `binaries`. A flag set to false or 0
|
|
20
|
+
* (`ints: { x: false }`) leaves the variable continuous, as in loadJson.
|
|
21
|
+
*/
|
|
22
|
+
export declare function integerIds(model: JsonModel): Set<string>;
|
|
23
|
+
/** Ids of continuous variables that can be treated as integer. */
|
|
24
|
+
export declare function findImpliedIntegers(model: JsonModel): Set<string>;
|
|
25
|
+
/**
|
|
26
|
+
* Ids of continuous variables that take integral values at some optimal
|
|
27
|
+
* solution once the integer variables are fixed, by total unimodularity.
|
|
28
|
+
*
|
|
29
|
+
* Fixpoint over a candidate set S (continuous, not free): a row holding two
|
|
30
|
+
* or more S variables must give them all the same coefficient magnitude k,
|
|
31
|
+
* with integer variables' coefficients and the bounds integral multiples of
|
|
32
|
+
* k, and each S variable may appear in at most one such row. A row holding
|
|
33
|
+
* exactly one S variable must satisfy the single-variable rule above (it is
|
|
34
|
+
* then an integral bound once the integers are fixed). Any row that also
|
|
35
|
+
* holds a continuous variable outside S disqualifies its S variables.
|
|
36
|
+
*
|
|
37
|
+
* With the integers fixed, the rows shared between S variables form a matrix
|
|
38
|
+
* with at most one +-1 per column (after scaling by k), which is totally
|
|
39
|
+
* unimodular, and every other row is an integral bound, so the polyhedron of
|
|
40
|
+
* S is integral and a linear objective has an integral optimal vertex.
|
|
41
|
+
* Facility location is the typical case: sum_j y_ij = 1 and y_ij <= z_j.
|
|
42
|
+
*
|
|
43
|
+
* Unlike findImpliedIntegers, these are not meant for branching (there can
|
|
44
|
+
* be many, and they follow the integers); they let branch-and-cut round the
|
|
45
|
+
* objective bound when all objective coefficients are integral.
|
|
46
|
+
*/
|
|
47
|
+
export declare function findIntegralContinuous(model: JsonModel): Set<string>;
|
package/dist/types/main.d.ts
CHANGED
|
@@ -15,23 +15,39 @@ import * as expressions from "./expressions";
|
|
|
15
15
|
import type { Model as ModelDefinition, SolveResult } from "./types/solver";
|
|
16
16
|
/**
|
|
17
17
|
* Main solver class providing the public API for solving optimization problems.
|
|
18
|
+
*
|
|
19
|
+
* Typically used as a singleton (exported as `solver` from this module).
|
|
20
|
+
* Orchestrates model parsing, validation, simplex/B&C solving, and
|
|
21
|
+
* multi-objective optimization.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* import solver from "javascript-lp-solver";
|
|
26
|
+
* const result = solver.Solve({ optimize: "profit", opType: "max", ... });
|
|
27
|
+
* ```
|
|
18
28
|
*/
|
|
19
29
|
declare class Solver {
|
|
30
|
+
/** Model class constructor for programmatic model building. */
|
|
20
31
|
Model: typeof Model;
|
|
32
|
+
/** Tableau class constructor (advanced: for direct tableau manipulation). */
|
|
21
33
|
Tableau: typeof Tableau;
|
|
34
|
+
/** Constraint class constructor. */
|
|
22
35
|
Constraint: typeof expressions.Constraint;
|
|
36
|
+
/** Variable class constructor. */
|
|
23
37
|
Variable: typeof expressions.Variable;
|
|
38
|
+
/** Numeral class constructor. */
|
|
24
39
|
Numeral: typeof expressions.Numeral;
|
|
40
|
+
/** Term class constructor. */
|
|
25
41
|
Term: typeof expressions.Term;
|
|
42
|
+
/** Registry of external solver integrations (e.g., lp_solve). */
|
|
26
43
|
External: import("./external/main").ExternalSolvers;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
constraints: {};
|
|
31
|
-
variables: {};
|
|
32
|
-
};
|
|
44
|
+
/** LP format conversion utility for external solvers. */
|
|
45
|
+
ReformatLP: (model: object | string | string[]) => object | string;
|
|
46
|
+
/** Default branch-and-cut service instance. */
|
|
33
47
|
branchAndCutService: import("./tableau/branch-and-cut").BranchAndCutService;
|
|
48
|
+
/** Convenience method to run branch-and-cut on a tableau. */
|
|
34
49
|
branchAndCut: (tableau: Tableau) => void;
|
|
50
|
+
/** Reference to the most recently solved Model instance (useful for post-solve inspection). */
|
|
35
51
|
lastSolvedModel: Model | null;
|
|
36
52
|
/**
|
|
37
53
|
* Select the appropriate branch-and-cut service based on model options.
|
|
@@ -51,7 +67,7 @@ declare class Solver {
|
|
|
51
67
|
* @param validate - If true, run model through validation functions
|
|
52
68
|
* @returns Solution object or simplified result with variable values
|
|
53
69
|
*/
|
|
54
|
-
Solve<
|
|
70
|
+
Solve<_TVariable extends string = string>(model: ModelDefinition | Model, precision?: number, full?: boolean, validate?: boolean): SolveResult | unknown;
|
|
55
71
|
/**
|
|
56
72
|
* Delegate solving to an external solver (e.g., lp_solve).
|
|
57
73
|
*/
|
package/dist/types/model.d.ts
CHANGED
|
@@ -18,57 +18,228 @@ import type { BranchAndCutService } from "./tableau/branch-and-cut";
|
|
|
18
18
|
import type { Model as JsonModel } from "./types/solver";
|
|
19
19
|
import type { TableauSolution, TableauSolutionSet } from "./tableau";
|
|
20
20
|
import { type PresolveResult } from "./tableau/presolve";
|
|
21
|
+
/**
|
|
22
|
+
* High-level representation of an optimization problem.
|
|
23
|
+
*
|
|
24
|
+
* Provides a programmatic API for building LP/MIP problems:
|
|
25
|
+
* - Add variables with costs, integer constraints, and priorities
|
|
26
|
+
* - Define constraints (<=, >=, =) with coefficients
|
|
27
|
+
* - Load problems from JSON model definitions
|
|
28
|
+
* - Dynamically modify models after initialization
|
|
29
|
+
*
|
|
30
|
+
* The Model converts high-level definitions into the internal Tableau
|
|
31
|
+
* representation used by the simplex algorithm.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const model = new Model();
|
|
36
|
+
* const x = model.addVariable(3, "x");
|
|
37
|
+
* const y = model.addVariable(5, "y");
|
|
38
|
+
* model.smallerThan(10).addTerm(1, x).addTerm(2, y);
|
|
39
|
+
* const solution = model.solve();
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
21
42
|
declare class Model {
|
|
43
|
+
/** The underlying simplex tableau. */
|
|
22
44
|
tableau: Tableau;
|
|
45
|
+
/** Optional model name for identification. */
|
|
23
46
|
name?: string;
|
|
47
|
+
/** All decision variables in insertion order. */
|
|
24
48
|
variables: Variable[];
|
|
49
|
+
/** Subset of variables with integrality constraints. */
|
|
25
50
|
integerVariables: IntegerVariable[];
|
|
51
|
+
/** Set of variable indices that may take negative values. */
|
|
26
52
|
unrestrictedVariables: Record<number, boolean>;
|
|
53
|
+
/** Column bounds (from single-variable JSON constraints and binaries), applied by the tableau. */
|
|
54
|
+
columnBounds: Map<Variable, {
|
|
55
|
+
lb: number;
|
|
56
|
+
ub: number;
|
|
57
|
+
}>;
|
|
58
|
+
/**
|
|
59
|
+
* Continuous variables integral at some optimum once the integers are
|
|
60
|
+
* fixed (implied-integers.ts). Used only to recognize an integral
|
|
61
|
+
* objective, never for branching.
|
|
62
|
+
*/
|
|
63
|
+
integralContinuous: Set<string>;
|
|
64
|
+
/**
|
|
65
|
+
* Continuous variables treated as integer because they are implied
|
|
66
|
+
* integer (implied-integers.ts). Cut separators may treat them as
|
|
67
|
+
* continuous again (relaxing integrality keeps cuts valid).
|
|
68
|
+
*/
|
|
69
|
+
impliedIntegers: Set<string>;
|
|
70
|
+
/** Bounds collected by id while loading JSON, before variables exist. */
|
|
71
|
+
private pendingColumnBounds;
|
|
72
|
+
/** Whether binaries get a column bound instead of an upper-bound row. */
|
|
73
|
+
private binariesAsColumnBounds;
|
|
74
|
+
/** All constraints in insertion order. */
|
|
27
75
|
constraints: Constraint[];
|
|
76
|
+
/** Current number of constraints. */
|
|
28
77
|
nConstraints: number;
|
|
78
|
+
/** Current number of variables. */
|
|
29
79
|
nVariables: number;
|
|
80
|
+
/** True for minimization, false for maximization. */
|
|
30
81
|
isMinimization: boolean;
|
|
82
|
+
/** Whether the tableau has been initialized from this model. */
|
|
31
83
|
tableauInitialized: boolean;
|
|
84
|
+
/** Counter for generating unique relaxation variable IDs. */
|
|
32
85
|
relaxationIndex: number;
|
|
86
|
+
/** Whether MIR cuts are enabled for branch-and-cut. */
|
|
33
87
|
useMIRCuts: boolean;
|
|
88
|
+
/** Whether to detect and abort on simplex pivot cycles. */
|
|
34
89
|
checkForCycles: boolean;
|
|
90
|
+
/** Diagnostic messages collected during solving. */
|
|
35
91
|
messages: unknown[];
|
|
92
|
+
/** Optimality tolerance for early termination in B&C. */
|
|
36
93
|
tolerance?: number;
|
|
94
|
+
/** Maximum solve time in milliseconds. */
|
|
37
95
|
timeout?: number;
|
|
96
|
+
/** Whether to store all integer-feasible solutions found during B&C. */
|
|
38
97
|
keep_solutions?: boolean;
|
|
98
|
+
/** All integer-feasible solutions found (if keep_solutions is true). */
|
|
39
99
|
solutions?: TableauSolutionSet[];
|
|
100
|
+
/** Pool of recycled variable/constraint indices. */
|
|
40
101
|
availableIndexes: number[];
|
|
102
|
+
/** Next fresh index to allocate. */
|
|
41
103
|
lastElementIndex: number;
|
|
104
|
+
/** Whether to apply preprocessing reductions before solving. */
|
|
42
105
|
usePresolve: boolean;
|
|
106
|
+
/** Whether the user explicitly configured presolve behavior. */
|
|
107
|
+
presolveExplicitlyConfigured: boolean;
|
|
108
|
+
/** Cached presolve results (null if not yet applied). */
|
|
43
109
|
presolveResult: PresolveResult | null;
|
|
110
|
+
/**
|
|
111
|
+
* @param precision - Numerical tolerance for the tableau (default: 1e-8).
|
|
112
|
+
* @param name - Optional model name.
|
|
113
|
+
* @param branchAndCutService - Custom B&C strategy for MIP solving.
|
|
114
|
+
*/
|
|
44
115
|
constructor(precision?: number, name?: string, branchAndCutService?: BranchAndCutService);
|
|
116
|
+
/** Set the optimization direction to minimization. */
|
|
45
117
|
minimize(): this;
|
|
118
|
+
/** Set the optimization direction to maximization. */
|
|
46
119
|
maximize(): this;
|
|
120
|
+
/** Allocate a new element index, reusing from pool if available. */
|
|
47
121
|
_getNewElementIndex(): number;
|
|
122
|
+
/** Register a constraint with the model and propagate to tableau if initialized. */
|
|
48
123
|
_addConstraint(constraint: Constraint): void;
|
|
124
|
+
/**
|
|
125
|
+
* Create a <= (upper bound) constraint.
|
|
126
|
+
* @param rhs - The upper bound value.
|
|
127
|
+
* @returns The new constraint (add terms via .addTerm()).
|
|
128
|
+
*/
|
|
49
129
|
smallerThan(rhs: number): Constraint;
|
|
130
|
+
/**
|
|
131
|
+
* Create a >= (lower bound) constraint.
|
|
132
|
+
* @param rhs - The lower bound value.
|
|
133
|
+
* @returns The new constraint (add terms via .addTerm()).
|
|
134
|
+
*/
|
|
50
135
|
greaterThan(rhs: number): Constraint;
|
|
136
|
+
/**
|
|
137
|
+
* Create an equality constraint (implemented as paired upper + lower bounds).
|
|
138
|
+
* @param rhs - The equality value.
|
|
139
|
+
* @returns An Equality wrapping both bounds (add terms via .addTerm()).
|
|
140
|
+
*/
|
|
51
141
|
equal(rhs: number): Equality;
|
|
142
|
+
/**
|
|
143
|
+
* Add a decision variable to the model.
|
|
144
|
+
*
|
|
145
|
+
* @param cost - Objective function coefficient (default: 0).
|
|
146
|
+
* @param id - Unique identifier (default: auto-generated).
|
|
147
|
+
* @param isInteger - If true, the variable is restricted to integer values.
|
|
148
|
+
* @param isUnrestricted - If true, the variable may be negative.
|
|
149
|
+
* @param priority - Priority level for hierarchical optimization.
|
|
150
|
+
* @returns The newly created Variable instance.
|
|
151
|
+
*/
|
|
52
152
|
addVariable(cost?: number | null, id?: string | null, isInteger?: boolean, isUnrestricted?: boolean, priority?: Priority | null): Variable;
|
|
153
|
+
/** Remove a single constraint from the model and propagate to tableau. */
|
|
53
154
|
_removeConstraint(constraint: Constraint): void;
|
|
155
|
+
/**
|
|
156
|
+
* Remove a constraint or equality from the model.
|
|
157
|
+
* For equalities, removes both the upper and lower bound constraints.
|
|
158
|
+
*
|
|
159
|
+
* @param constraint - The Constraint or Equality to remove.
|
|
160
|
+
* @returns This model for chaining.
|
|
161
|
+
*/
|
|
54
162
|
removeConstraint(constraint: Constraint | Equality): this;
|
|
163
|
+
/**
|
|
164
|
+
* Remove a variable from the model.
|
|
165
|
+
* @param variable - The variable to remove.
|
|
166
|
+
* @returns This model for chaining, or void if variable not found.
|
|
167
|
+
*/
|
|
55
168
|
removeVariable(variable: Variable): this | void;
|
|
169
|
+
/**
|
|
170
|
+
* Update a constraint's RHS in the live tableau.
|
|
171
|
+
* @param constraint - The constraint to update.
|
|
172
|
+
* @param difference - Amount to subtract from the current RHS.
|
|
173
|
+
*/
|
|
56
174
|
updateRightHandSide(constraint: Constraint, difference: number): this;
|
|
175
|
+
/**
|
|
176
|
+
* Update a variable's coefficient in a constraint in the live tableau.
|
|
177
|
+
* @param constraint - The constraint being modified.
|
|
178
|
+
* @param variable - The variable whose coefficient is changing.
|
|
179
|
+
* @param difference - Amount to add to the current coefficient.
|
|
180
|
+
*/
|
|
57
181
|
updateConstraintCoefficient(constraint: Constraint, variable: Variable, difference: number): this;
|
|
182
|
+
/**
|
|
183
|
+
* Change a variable's objective coefficient.
|
|
184
|
+
* @param cost - New objective coefficient value.
|
|
185
|
+
* @param variable - The variable to update.
|
|
186
|
+
*/
|
|
58
187
|
setCost(cost: number, variable: Variable): this;
|
|
188
|
+
/**
|
|
189
|
+
* Load a problem from the JSON model definition format.
|
|
190
|
+
*
|
|
191
|
+
* Parses constraints, variables, integer/binary flags, and solver options
|
|
192
|
+
* from the JSON structure. Supports equality constraints (via paired bounds),
|
|
193
|
+
* soft constraints (weight/priority), and binary variable upper bounds.
|
|
194
|
+
*
|
|
195
|
+
* @param jsonModel - The JSON model definition.
|
|
196
|
+
* @returns This model for chaining.
|
|
197
|
+
*/
|
|
59
198
|
loadJson(jsonModel: JsonModel): this;
|
|
199
|
+
/**
|
|
200
|
+
* Find JSON constraints that touch a single variable and can become
|
|
201
|
+
* column bounds instead of tableau rows (HiGHS and other solvers do this
|
|
202
|
+
* in presolve; here it shrinks the dense tableau). Records the bounds in
|
|
203
|
+
* pendingColumnBounds (by variable id) and returns the constraint ids to
|
|
204
|
+
* skip. Disabled whenever something still reads raw tableau rows: MIR
|
|
205
|
+
* cuts, the enhanced/incremental branch-and-cut services, soft
|
|
206
|
+
* constraints, or unrestricted integer variables.
|
|
207
|
+
*/
|
|
208
|
+
private collectColumnBounds;
|
|
209
|
+
/** @returns The number of integer-constrained variables in this model. */
|
|
60
210
|
getNumberOfIntegerVariables(): number;
|
|
211
|
+
/**
|
|
212
|
+
* Solve the optimization problem.
|
|
213
|
+
*
|
|
214
|
+
* Applies presolve reductions (if enabled), initializes the tableau,
|
|
215
|
+
* and runs simplex (for LP) or branch-and-cut (for MIP).
|
|
216
|
+
*
|
|
217
|
+
* @returns The solution with objective value and variable assignments.
|
|
218
|
+
*/
|
|
61
219
|
solve(): TableauSolution;
|
|
62
220
|
/**
|
|
63
221
|
* Apply presolve reductions to the model.
|
|
64
222
|
* Sets fixed variable values and removes redundant constraints.
|
|
65
223
|
*/
|
|
66
224
|
private applyPresolveReductions;
|
|
225
|
+
private shouldRunPresolve;
|
|
226
|
+
/** @returns Whether the last solve found a feasible solution. */
|
|
67
227
|
isFeasible(): boolean;
|
|
228
|
+
/** Snapshot the current tableau state for later restoration. */
|
|
68
229
|
save(): void;
|
|
230
|
+
/** Restore the tableau to the previously saved state. */
|
|
69
231
|
restore(): void;
|
|
232
|
+
/**
|
|
233
|
+
* Enable or disable Mixed Integer Rounding cuts.
|
|
234
|
+
* @param useMIRCuts - Whether to apply MIR cuts during branch-and-cut.
|
|
235
|
+
*/
|
|
70
236
|
activateMIRCuts(useMIRCuts: boolean): void;
|
|
237
|
+
/**
|
|
238
|
+
* Enable or disable cycle detection in the simplex algorithm.
|
|
239
|
+
* @param debugCheckForCycles - Whether to check for pivot cycles.
|
|
240
|
+
*/
|
|
71
241
|
debug(debugCheckForCycles: boolean): void;
|
|
242
|
+
/** Print the tableau to console for debugging. */
|
|
72
243
|
log(message: unknown): Tableau;
|
|
73
244
|
}
|
|
74
245
|
export default Model;
|
package/dist/types/polyopt.d.ts
CHANGED
|
@@ -11,23 +11,40 @@
|
|
|
11
11
|
* the min/max ranges for each objective.
|
|
12
12
|
*/
|
|
13
13
|
import type { Model as ModelDefinition, SolveResult } from "./types/solver";
|
|
14
|
+
/** Minimal solver interface required by Polyopt (allows decoupling from Solver class). */
|
|
14
15
|
interface SolverLike {
|
|
15
16
|
Solve(model: ModelDefinition, precision?: number, full?: boolean, validate?: boolean): unknown;
|
|
16
17
|
}
|
|
18
|
+
/** Extended solve result with numeric attributes for all objectives. */
|
|
17
19
|
type PolyoptSolution = SolveResult & Record<string, number>;
|
|
20
|
+
/** A Pareto-optimal point: variable values achieving an extreme objective combination. */
|
|
18
21
|
type Vertex = Record<string, number>;
|
|
19
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Result of multi-objective optimization.
|
|
24
|
+
*/
|
|
25
|
+
interface PolyoptResult {
|
|
26
|
+
/** The compromise solution at the midpoint of Pareto-optimal vertices. */
|
|
20
27
|
midpoint: PolyoptSolution;
|
|
28
|
+
/** Individual Pareto-optimal solutions (one per objective optimized independently). */
|
|
21
29
|
vertices: Vertex[];
|
|
30
|
+
/** Min/max ranges for each objective attribute across all vertices. */
|
|
22
31
|
ranges: Record<string, {
|
|
23
32
|
min: number;
|
|
24
33
|
max: number;
|
|
25
34
|
}>;
|
|
26
35
|
}
|
|
27
36
|
/**
|
|
28
|
-
* Solve a model with multiple objective functions
|
|
29
|
-
*
|
|
30
|
-
*
|
|
37
|
+
* Solve a model with multiple objective functions using compromise programming.
|
|
38
|
+
*
|
|
39
|
+
* Algorithm:
|
|
40
|
+
* 1. Optimize each objective independently to find Pareto-optimal vertices.
|
|
41
|
+
* 2. Average the vertex values to compute a target midpoint.
|
|
42
|
+
* 3. Solve a derived model with equality constraints at the midpoint.
|
|
43
|
+
*
|
|
44
|
+
* @param solver - A solver instance (or anything with a compatible `Solve` method).
|
|
45
|
+
* @param model - The model with `optimize` as a `Record<string, ObjectiveDirection>`.
|
|
46
|
+
* @returns Midpoint solution, all Pareto vertices, and min/max ranges per objective.
|
|
47
|
+
* @throws If `optimize` has no keys or solver returns a non-object result.
|
|
31
48
|
*/
|
|
32
49
|
export default function Polyopt(solver: SolverLike, model: ModelDefinition): PolyoptResult;
|
|
33
50
|
export {};
|
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* In browser environments, external solvers (which require Node.js
|
|
6
|
-
* child_process and fs) are not available. This shim provides an
|
|
7
|
-
* empty export to satisfy imports without runtime errors.
|
|
2
|
+
* Browser shim for external solvers.
|
|
3
|
+
* External solvers require Node.js (child_process) and are unavailable in browsers.
|
|
8
4
|
*/
|
|
9
5
|
import type { ExternalSolvers } from "../external/main";
|
|
10
6
|
declare const External: ExternalSolvers;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|