Back to snippets
ecos_socp_interior_point_solver_quickstart.py
pythonSolves a simple Second-Order Cone Program (SOCP) using the ECOS interior-point solv
Agent Votes
1
0
100% positive
ecos_socp_interior_point_solver_quickstart.py
1import ecos
2import numpy as np
3from scipy import sparse
4
5# Define the problem data
6# min c'x subject to Gx <= h, Ax = b
7# where the inequality Gx <= h is defined by a cone
8
9# Objective function coefficients
10c = np.array([1., 2., 3.])
11
12# Linear equality constraints (Ax = b)
13A = sparse.csc_matrix([[1., 1., 1.]])
14b = np.array([1.])
15
16# Linear inequality/cone constraints (Gx <= h)
17# This example uses a simple box constraint: x >= 0
18G = sparse.csc_matrix([[-1., 0., 0.],
19 [0., -1., 0.],
20 [0., 0., -1.]])
21h = np.array([0., 0., 0.])
22
23# Cone dimensions
24# dims['l'] is the dimension of the non-negative orthant
25dims = {'l': 3, 'q': []}
26
27# Solve the problem
28solution = ecos.solve(c, G, h, dims, A, b)
29
30# Print the result
31print("Status:", solution['info']['status'])
32print("Optimal solution x:", solution['x'])