LP Format Export

Export optimization models to the standard LP file format

Export models to .lp files for use with external solvers like CPLEX, Gurobi, GLPK, and HiGHS.

1 Linear Program

from optyx import Problem, Variable

x = Variable("x", lb=0)
y = Variable("y", lb=0)

prob = Problem("simple_lp")
prob.minimize(2 * x + 3 * y)
prob.subject_to(x + y >= 1)
prob.subject_to(x - y <= 5)

print(prob.to_lp())
\ Model simple_lp

Minimize
  obj: 2 x + 3 y

Subject To
  c0: x + y >= 1
  c1: x - y <= 5

Bounds
  0 <= x
  0 <= y

End

Use prob.write("model.lp") to save directly to a file.

2 Quadratic Program

Quadratic objectives use the standard [ ... ] / 2 notation.

from optyx import VectorVariable, quadratic_form
import numpy as np

w = VectorVariable("w", 3, lb=0, ub=1)
Q = np.array([[2, 1, 0], [1, 3, 1], [0, 1, 2]], dtype=float)
c = np.array([1.0, 2.0, 3.0])

prob = Problem("portfolio")
prob.minimize(quadratic_form(w, Q) + c @ w)
prob.subject_to(w.sum().eq(1))

print(prob.to_lp())
\ Model portfolio

Minimize
  obj: w[0] + 2 w[1] + 3 w[2] [ 4 w[0] ^2 + 4 w[0] * w[1] + 6 w[1] ^2 + 4 w[1] * w[2] + 4 w[2] ^2 ] / 2

Subject To
  c0: w[0] + w[1] + w[2] == 1

Bounds
  0 <= w[0] <= 1
  0 <= w[1] <= 1
  0 <= w[2] <= 1

End

3 Mixed-Integer Program

Integer and binary variables are listed in Generals and Binaries sections.

from optyx import BinaryVariable, IntegerVariable

x = IntegerVariable("x", lb=0, ub=10)
y = BinaryVariable("y")
z = Variable("z", lb=0)

prob = Problem("mip")
prob.maximize(3 * x + 5 * y + z)
prob.subject_to(x + 2 * y + z <= 15)
prob.subject_to(x + y >= 2)

print(prob.to_lp())
\ Model mip

Maximize
  obj: 3 x + 5 y + z

Subject To
  c0: x + 2 y + z <= 15
  c1: x + y >= 2

Bounds
  0 <= x <= 10
  0 <= y <= 1
  0 <= z

Generals
  x

Binaries
  y

End

4 Supported Features

Feature Support
Linear objectives
Quadratic objectives
<=, >=, == constraints
Variable bounds
Free variables
Integer variables (Generals)
Binary variables (Binaries)
Matrix constraints
Sparse matrix constraints
Nonlinear objectives ✗ (raises error)

5 Export Methods

The Problem.to_lp() method returns the LP string, while Problem.write(path) writes it to a file.

# String representation
lp_string = prob.to_lp()

# Write to file
prob.write("model.lp")