LP files
Knitro can import linear and quadratic optimization problems for the standard LP (Linear Programming) file format. LP files should be ASCII-encoded text files.
Basic Structure
An LP file is structured into several sections, each identified by a case-insensitive keyword.
MINIMIZE or MAXIMIZE: Defines the objective function.
SUBJECT TO (or ST): Lists the constraints.
BOUNDS: Sets variable bounds (optional).
BINARY: Declares binary variables (optional).
INTEGER: Declares integer variables (optional).
RANGES: Specifies range constraints (optional).
END: Marks the end of the file.
Example
Here is a simple example illustrating the structure of an LP file:
MINIMIZE
cost: x + 4 y + 9 z
SUBJECT TO
constraint1: x + y <= 5
constraint2: x + z >= 10
constraint3: y - z = 7
BOUNDS
0 <= x <= 4
-1 <= y <= 1
z free
END
Objective Function Section
To include an objective function, the LP file must contain this section, which starts with a keyword such as MINIMIZE, MAXIMIZE, MIN, or MAX. This is followed by an optional label and the objective expression itself.
MINIMIZE
[label:] expression
The objective expression can be a combination of constant terms and linear terms (e.g., coeff * var). If a variable appears without a coefficient, the coefficient is assumed to be 1.
MINIMIZE
cost: 2 x + 3.5 y - z + 10
MAXIMIZE
profit: revenue - expenses
Constraints Section
The constraints section follows the objective and begins with a keyword like SUBJECT TO, ST, SUCH THAT, or S.T.. Each constraint is defined on a new line with the following format:
[label:] expression relation rhs
Where:
label: An optional name for the constraint.
expression: A linear combination of variables.
relation: One of<=,>=, or=.
rhs: The numerical right-hand side value.
SUBJECT TO
capacity: 2 x + 3 y <= 100
demand: x + y >= 50
balance: x - y = 0
c4: 0.5 a + 1.2 b + 0.8 c <= 75.5
Variable Bounds Section
The optional BOUNDS (or BOUND) section allows you to define lower and upper limits for variables. If a variable’s bounds are not specified, it is treated as unbounded (free), ranging from negative to positive infinity.
Bounds can be specified in several ways:
lower <= variable <= upper
variable >= lower
variable <= upper
variable = value
variable free
The keyword free explicitly declares a variable as unbounded.
BOUNDS
0 <= x <= 100
y >= -50
z <= 25
w = 10
v free
Declaring Variable Types
Binary Variables
To declare binary variables, list them in the BINARY section (also BIN or BINARIES).
BINARY
x1 x2 x3
Integer Variables
To declare general integer variables, list them under the INTEGER section. Synonyms like INT, INTEGERS, GENERAL, GENERALS, or GEN are also accepted.
INTEGER
y1 y2 y3
Complete Example
This example combines multiple sections, including variable type declarations:
MINIMIZE
objective: 3 x + 2 y + z
SUBJECT TO
resource1: 2 x + y <= 100
resource2: x + 3 y + z <= 150
demand: x + y >= 25
BOUNDS
x >= 0
0 <= y <= 50
z free
BINARY
x
INTEGER
y
END
Range Constraints Section
The optional RANGES (or RANGE) section modifies existing constraints to create double-sided inequalities, effectively placing the constraint’s expression within an interval.
Each line in the RANGES section references a constraint by its label and provides a new lower and upper bound for its expression.
lhs <= constraint_label <= rhs
For example, consider these initial constraints:
SUBJECT TO
con1: x + y <= 10
con2: 2 x + 3 y >= 15
You can apply ranges to them as follows:
RANGES
5 <= con1 <= 10
15 <= con2 <= 25
This effectively transforms the original constraints into:
For
con1:5 <= x + y <= 10For
con2:15 <= 2 x + 3 y <= 25
Note that the bounds specified in the RANGES section can override the original bounds of the referenced constraints. The lower bound will replace the original lower bound if it is more restrictive, and similarly for the upper bound.
Quadratic Extensions
Knitro’s LP parser extends the standard format to support quadratic terms in both
the objective function and constraints. Quadratic expressions must be enclosed in
square brackets [ ]. A quadratic term is represented by either a squared
variable (e.g., x^2) or a product of two different variables (e.g., x*y)
with an associated coefficient (if the coefficient is 1, it can be omitted).
Optionally, a division operator can be added at the end of the square bracket
expression to indicate a potential division of the coefficients by a non-zero
constant, which can be useful if the quadratic matrix is given within a
particular convention.
Quadratic Objective
MINIMIZE
obj: x + 2 y + [ x^2 + 2 x*y + 3 y^2 ] / 2
Quadratic Constraints
SUBJECT TO
qc1: x + y + [ x^2 + 2 y^2 - x*y ] <= 10
Unsupported Features
The LP format can describe features that Knitro’s parser does not support. Including any of the following will result in a parsing error:
Special Ordered Sets (SOS)
Semi-continuous variables
Piecewise linear functions
Indicator or logical constraints
Non-linear functions beyond quadratic terms.
Parser Rules
Variable, Constraint, and Objective Names: Names are case-sensitive and must start with a letter. They can contain letters, digits, underscores, and the symbols
!#$%&()|~. Spaces and operators (-,+,*,/,^,=,>,<,;) are not allowed as they may interfere with parsing. When exporting, only the linear and quadratic components of the problem are written to the file; any general non-linear structures are omitted. Furthermore, if any variable, constraint, or objective names contain special characters (+-*/^=><;) or whitespace, these characters will be replaced with (@). If a variable does not have any name or its name is empty, Knitro will assign it a default name likex1,x2, etc. Similarly, unnamed constraints will be labeled asc1,c2, etc. The indexing of these default names is the same as the indexing used internally by Knitro.Numerical Values: Standard integer and floating-point numbers are supported, including scientific notation (e.g.,
1.5e-3). Infinity can be represented as+inf,-inf,+infinity, or-infinity.Comments: Use
\for single-line comments. For block comments, enclose the text between/*and*/.
\ This is a single-line comment
MINIMIZE
obj: x + y \ Minimize total cost
/* This is a block comment
spanning multiple lines */
Using LP Files in Knitro
Loading an LP File (C API)
Use the KN_read_problem function to load an LP file into a Knitro context.
/*---- LOAD LP FILE ----*/
if (KN_read_problem (kc, "problem.lp", NULL) != 0) {
printf("Error loading LP file\n");
exit(-1);
}
Writing an LP File (C API)
Use the KN_write_problem function to export the current problem in a Knitro context to the LP format.
/*---- WRITE LP FILE ----*/
if (KN_write_problem (kc, "output.lp", NULL) != 0) {
printf("Error writing LP file\n");
exit(-1);
}