CVXOPT: Convex Optimization Package

1.3.3 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This example demonstrates how to solve a simple Linear Program (LP) using `cvxopt.solvers.lp`. It sets up the objective function vector `c`, the inequality constraint matrix `G`, and the right-hand side vector `h`, then invokes the solver to find the optimal solution.

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"]}')

view raw JSON →