Python Wrapper for Embedded Conic Solver (ECOS)

2.0.14 · active · verified Sun Apr 12

ECOS is a lightweight numerical solver for convex second-order cone programs (SOCPs) designed for embedded systems. This library provides its Python interface. It is actively maintained, with version 2.0.14 released in June 2024, and receives regular updates to its Python wrapper.

Warnings

Install

Imports

Quickstart

This example demonstrates how to solve a basic Linear Program (a special case of SOCP) using `ecos.solve`. It minimizes `x[0] + x[1]` subject to non-negativity and a lower bound constraint.

import numpy as np
from scipy import sparse
import ecos

# Define a simple Linear Program (LP) as an SOCP
# Minimize: x[0] + x[1]
# Subject to: x[0] >= 0, x[1] >= 0, x[0] + x[1] >= 1

# Objective vector c: Minimize x[0] + x[1]
c = np.array([1., 1.])

# Inequality constraints Gx <= h (linear cone part)
# -x[0] <= 0
# -x[1] <= 0
# -x[0] - x[1] <= -1  (equivalent to x[0] + x[1] >= 1)
G = sparse.csr_matrix([
    [-1., 0.],
    [0., -1.],
    [-1., -1.]
])
h = np.array([0., 0., -1.])

# Dimensions of the cones
dims = {
    'l': 3,  # Number of linear inequality constraints
    'q': [], # List of second-order cone dimensions
    'e': 0   # Number of exponential cone constraints
}

# Solve the problem
solution = ecos.solve(c, G, h, dims)

# Print the optimal solution
print(f"Optimal x: {solution['x']}")
print(f"Optimal value: {solution['info']['pcost']}")

view raw JSON →