CVXOPT: Convex Optimization Package
CVXOPT is a Python package for convex optimization. It provides a dense and sparse matrix object type and an extensive library of solvers for linear programs, quadratic programs, semidefinite programs, and general convex optimization problems. The current version is 1.3.3, and it receives active maintenance, primarily through bug fixes and Python version compatibility updates (e.g., Python 3.8+ is generally recommended).
Warnings
- gotcha CVXOPT's solvers primarily operate on its own `cvxopt.matrix` objects, which are distinct from NumPy arrays. While `cvxopt.matrix` can be initialized from NumPy arrays, direct use of NumPy arrays in solver inputs will raise type errors.
- gotcha Installation of CVXOPT, especially on Windows or when seeking high performance, can sometimes be challenging due to its dependency on external BLAS and LAPACK libraries. While `pip install` often works with pre-compiled wheels, custom builds may require careful configuration.
- gotcha For very large-scale convex optimization problems or specific problem types, CVXOPT might not offer the same performance or specialized algorithms as commercial solvers (e.g., Gurobi, MOSEK) or other open-source alternatives like CVXPY (which can interface with various backend solvers including CVXOPT).
Install
-
pip install cvxopt
Imports
- matrix
from cvxopt import matrix
- solvers
from cvxopt import solvers
Quickstart
from cvxopt import matrix, solvers
# Minimize -4x_1 - 5x_2
# Subject to:
# x_1 + 2x_2 <= 10
# 3x_1 + 2x_2 <= 12
# x_1 >= 0, x_2 >= 0
# Objective function c (coefficients of x_1, x_2)
c = matrix([-4.0, -5.0])
# Inequality constraints Gx <= h
# G for x_1 + 2x_2 <= 10 (row 1)
# G for 3x_1 + 2x_2 <= 12 (row 2)
# G for x_1 >= 0 (i.e., -x_1 <= 0, row 3)
# G for x_2 >= 0 (i.e., -x_2 <= 0, row 4)
G = matrix([[1.0, 3.0, -1.0, 0.0],
[2.0, 2.0, 0.0, -1.0]]) # Note: CVXOPT matrix is column-major
h = matrix([10.0, 12.0, 0.0, 0.0])
# Solve the linear program
sol = solvers.lp(c, G, h)
# Print optimal values
print('Optimal solution:')
print(f'x_1 = {sol["x"][0]}')
print(f'x_2 = {sol["x"][1]}')
print(f'Optimal objective value = {sol["primal objective"]}')