@kinaxis/optimization-model-builder
v1.0.0
Published
A library to help create optimization models in Maestro using Embedded Algorithms
Readme
optimization-model-builder
A helper library for building optimization models for use in Embedded Algorithms.
Table of Contents
Quick Start
- Create a new
Model - Add variables and constraints using the instance methods
- Serialize the model using
serializeModelToMPS()
Classes
Model
Represents an optimization model. It can have Variables,
Constraints added, and an optional objective function offset value. It can then be serialized.
Constructor
Model(objectiveType[, name])
| Argument | Type | Description |
| ------------- | --------------------------------- | -------------------------------------- |
| objectiveType | ObjectiveType | How to optimize the objective function |
| modelName | string \| undefined | The name of the model (optional) |
Example Usage:
import { Model, ObjectiveType } from "@kinaxis/optimization-model-builder";
const myModel = new Model(ObjectiveType.Maximize, "MyModel");Instance Properties
The properties are read-only and must be populated using the constructor and instance methods.
| Property | Type | Description |
| --------------| ------------------------------------ | ------------------------------------------- |
| objectiveType | ObjectiveType | How to optimize the objective function |
| modelName | string \| undefined | The name of the model |
| variables | Map<string, Variable> | The model's variables keyed by name |
| constraints | Constraint[] | The model's constraints |
Instance Methods
Model.addVariable(name, params)
Adds a Variable to the model using the provided name and parameters.
Variable names must be unique strings between 1 and 255 characters with no whitespace. The name "OBJ" is prohibited. Names beginning with an underscore are prohibited.
A variable's objective coefficient and bounds (if specified) must be finite numbers. Bounds for binary variables can only be 0 or 1, and bounds for integer variables must be integers.
| Argument | Type | Description |
| -------- | ----------------------------------- | ---------------------------------------- |
| name | string | The name of the new variable |
| params | VariableParams | Paramaters which define the new variable |
Return Value: The newly created and added Variable.
Example Usage:
import { Model, ObjectiveType, VariableType } from "@kinaxis/optimization-model-builder";
const myModel = new Model(ObjectiveType.Maximize, "MyModel");
myModel.addVariable("Var1", {
variableType: VariableType.Integer,
objectiveCoefficient: 6.12,
upperBound: 95
});Model.addConstraint(lhs, constraintType, rhs[, rhsVariables])
Adds a Constraint to the model.
Constraints must have at least one variable on the left-hand side with no duplicates. Variable coefficients as well as the right-hand side must be finite numbers.
The addVarCoefficient() function
can be used to build the left-hand side more easily.
| Argument | Type | Description |
| -------------- | -------------------------------------------------------------| ---------------------------------------------------------------------------------------- |
| lhs | VariableCoefficient[] | The variables that make up the left-hand side of the constraint, with their coefficients |
| constraintType | ConstraintType | The relationship between the left-hand and right-hand sides |
| rhs | number | The right-hand side of the constraint |
| rhsVariables | VariableCoefficient[] \| undefined | The variables that make up the right-hand side of the constraint, with their coefficients (optional) |
Return Value: The newly created and added Constraint.
Example Usage:
import { Model, ObjectiveType, VariableType, addVarCoefficient } from "@kinaxis/optimization-model-builder";
const myModel = new Model(ObjectiveType.Maximize, "MyModel");
const var1 = myModel.addVariable("Var1", {
variableType: VariableType.Integer,
objectiveCoefficient: 6.12,
upperBound: 95
});
const var2 = myModel.addVariable("Var2", {
variableType: VariableType.Integer,
objectiveCoefficient: 10,
upperBound: 60
});
myModel.addConstraint([
addVarCoefficient(5, var1)
], ConstraintType.LessEqual, 31);
myModel.addConstraint([
addVarCoefficient(3, var1)
], ConstraintType.LessEqual, 31, [
addVarCoefficient(2, var2)
]);
Model.setObjectiveOffset(offset)
Adds a constant offset to the objective function. Must be a finite number.
| Argument | Type | Description |
| -------------- | ----------------------------------------------- | ----------------------------------------------------- |
| offset | number | The number to set the objective function's offset to |
Example Usage:
import { Model } from "@kinaxis/optimization-model-builder";
const myModel = new Model(ObjectiveType.Maximize, "MyModel");
myModel.setObjectiveOffset(10);Model.getObjectiveOffset()
Returns the objective function's offset.
Return Value: If the offset has been set, number, else returns undefined.
Example Usage:
import { Model } from "@kinaxis/optimization-model-builder";
const myModel = new Model(ObjectiveType.Maximize, "MyModel");
myModel.setObjectiveOffset(10);
const offsetValue = myModel.getObjectiveOffset();Free Functions
addVarCoefficient(coefficient, variable)
Helper function for creating VariableCoefficients
to define Constraints.
| Argument | Type | Description |
| ----------- | ----------------------- | ---------------------------------------------------------- |
| coefficient | number | The coefficient for the variable when used in a constraint |
| variable | Variable | The variable |
Return Value: Variable and coefficient as a VariableCoefficient.
Example Usage:
import { Model, ObjectiveType, VariableType, addVarCoefficient } from "@kinaxis/optimization-model-builder";
const myModel = new Model(ObjectiveType.Minimize, "MyModel");
const var1 = myModel.addVariable("Var1", {
variableType: VariableType.Integer,
objectiveCoefficient: 4.07,
lowerBound: 65
});
const var2 = myModel.addVariable("Var2", {
variableType: VariableType.Binary,
objectiveCoefficient: 5.15
});
const lhs = [
addVarCoefficient(6, var1),
addVarCoefficient(1, var2),
];
myModel.addConstraint(lhs, ConstraintType.LessEqual, 1000);serializeModelToMPS(model)
Serializes a Model to the MPS format.
| Argument | Type | Description |
| -------- | ---------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| model | Model | The optimization model to serialize |
| compress | boolean \| undefined | Whether to compress the model (optional; default true). You should only need to set this to false for troubleshooting/debugging purposes. |
Return Value: A string containing the serialized MPS model.
Example Usage:
import { Model, ObjectiveType, serializeModelToMPS } from "@kinaxis/optimization-model-builder";
const myModel = new Model(ObjectiveType.Minimize, "MyModel");
// Populate model...
const mpsString = serializeModelToMPS(myModel);Enums
ConstraintType
The relationship between a constraint's left-hand side (lhs) and right-hand side (rhs).
| Value | Description |
| ------------ | ---------------------- |
| GreaterEqual | Indicates lhs >= rhs |
| LessEqual | Indicates lhs <= rhs |
| Equal | Indicates lhs == rhs |
ObjectiveType
How a Model's objective function should be optimized.
| Value | Description | | -------- | ----------------------------------- | | Maximize | Indicates to maximize the objective | | Minimize | Indicates to minimize the objective |
VariableType
The type of a Variable's value.
| Value | Description | | ---------- | --------------------------------------------------- | | Integer | Indicates an integer variable value | | Binary | Indicates a binary variable value (0 or 1) | | Continuous | Indicates a continuous variable value (real number) |
Types
Variable
A Model variable.
| Property | Type | Description |
| -------------------- | ------------------------------- |---------------------------------------------------------------------------------------------------------------------------- |
| name | string | The name of the variable |
| variableType | VariableType | The type of the variable's value |
| objectiveCoefficient | number | The coefficient for the variable when used in a model's objective function |
| lowerBound | number \| undefined | The lower bound on the variable's value (optional). Defaults to 0. |
| upperBound | number \| undefined | The upper bound on the variable's value (optional). Defaults to positive infinity for continuous variables and 1 otherwise. |
VariableParams
Parameters used when adding a Variable to a Model.
See Model instance methods section.
| Property | Type | Description |
| -------------------- | ------------------------------- |---------------------------------------------------------------------------------------------------------------------------- |
| variableType | VariableType | The type of the variable's value |
| objectiveCoefficient | number | The coefficient for the variable when used in a model's objective function |
| lowerBound | number \| undefined | The lower bound on the variable's value (optional). Defaults to 0. |
| upperBound | number \| undefined | The upper bound on the variable's value (optional). Defaults to positive infinity for continuous variables and 1 otherwise. |
Constraint
A Model constraint.
For example, with constraintType equal to ConstraintType.LessEqual, the
constraint is effectively interpreted as lhs[0] + lhs[1] ... lhs[n] <= rhs.
| Property | Type | Description |
| -------------- | ----------------------------------------------- | ------------------------------------------------------------------------------------- |
| lhs | VariableCoefficient[] | Each variable making up the left-hand side of the constraint, with their coefficients |
| constraintType | ConstraintType | The relationship between the left-hand and right-hand sides |
| rhs | number | The right-hand side of the constraint |
| rhsVariables | VariableCoefficient[] | Each variable making up the right-hand side of the constraint, with their coefficients|
VariableCoefficient
A Variable and its Constraint coefficient.
See Model instance methods section.
| Property | Type | Description |
| ------------------- | ----------------------- | ---------------------------------------------------------- |
| variable | Variable | The variable |
| variableCoefficient | number | The coefficient for the variable when used in a constraint |
Please contact [email protected] if you experience any issues.
Copyright © 2025 Kinaxis. All Rights Reserved.
