Skip to content

Segmentation fault on simple problem #152

@Skiski

Description

@Skiski

Hi,

I'm trying to use OSQP to solve some least-squares problems . I've adapted your example and it works great with one of my problems, but with the second one, I've got a segmentation fault.

Here is my file:

import osqp
import numpy as np
from scipy import sparse

# Define problem data
#                        0  1  2  3  4  5  6
A1 = sparse.csc_matrix([[0, 1, 0, 0, 1, 0, 0], #c12
                        [1, 0, 1, 0, 0, 1, 1], #c13
                        [1, 1, 1, 0, 1, 1, 1], #c23
                        [1, 1, 1, 1, 0, 0, 0], #maths
                        [0, 1, 0, 0, 1, 0, 0], #spc
                        [0, 0, 0, -1, 1, 1, 1] #svt
                       ])

#                        0  1  2  3  4  5  6
A2 = sparse.csc_matrix([[1, 0, 0, 0, 0, 0, 0],
                        [0, 1, 0, 0, 0, 0, 0],
                        [0, 0, 1, 0, 0, 0, 0],
                        [0, 0, 0, 1, 0, 0, 0],
                        [0, 0, 0, 0, 1, 0, 0],
                        [0, 0, 0, 0, 0, 1, 0],
                        [0, 0, 0, 0, 0, 0, 1],
                        [0, 1, 1, 0, 0, 0, 0],
                        [0, 0, 0, 0, 1, 1, 0]
                       ])

vmax = [13, 24, 24, 8, 15, 15, 1,
    24, 15
    ]

objectifs = [21, 12, 33, 27, 19, 3.5]

m = len(objectifs)
n = len(vmax)
vmin = [0] * n

l = np.array(objectifs+vmin)
u = np.array(objectifs+vmax)


P = sparse.block_diag([sparse.csc_matrix((n, n)), sparse.eye(m)], format='csc')
q = np.zeros(n+m)
A = sparse.bmat([
                 [A1, -sparse.eye(m)],
                 [A2,  None],
                 ], format='csc')


# Create an OSQP object
prob = osqp.OSQP()

# Setup workspace and change alpha parameter
prob.setup(P, q, A, l, u, verbose=False)

# Solve problem
res = prob.solve()

It crashes during the setup. If I remove the last two lines of A2, and the last two values of vmax, it works. If I add any of the two lines or any line with multiple 1 ot -1, it crashes.

Here is a similar problem that works:

import osqp
import numpy as np
from scipy import sparse

#                        a  b  c  d  e  f  g  h  i
A1 = sparse.csc_matrix([[0, 0, 0, 0, 0, 1, 0, 1, 0], #c12
                        [1, 1, 1, 1, 0, 0, 1, 0, 1], #c13
                        [-1, -1, -1, -1, 0, -1, -1, -1, -1], #c23
                        [1, 1, 1, 0, 0, 0, 1, 0, 0], #maths
                        [0, 0, 0, 0, 0, 1, 0, 1, 0], #spc
                        [0, 0, 1, 1, -1, 0, 0, 0, 1] #svt
                       ])

m, n = A1.shape

constantes = np.array([40, 5, 64, 16, 1, 9])

effectifs_attendus = np.array([54.5, 27.25, 27.25, 31, 20, 13.5])

objectifs = effectifs_attendus - constantes

vmin = np.array([0] * len(vmax))

#                        a  b  c  d  e  f  g  h  i
A2 = sparse.csc_matrix([[1, 0, 0, 0, 0, 0, 0, 0, 0],
                        [0, 1, 0, 0, 0, 0, 0, 0, 0],
                        [0, 0, 1, 0, 0, 0, 0, 0, 0],
                        [0, 0, 0, 1, 0, 0, 0, 0, 0],
                        [0, 0, 0, 0, 1, 0, 0, 0, 0],
                        [0, 0, 0, 0, 0, 1, 0, 0, 0],
                        [0, 0, 0, 0, 0, 0, 1, 0, 0],
                        [0, 0, 0, 0, 0, 0, 0, 1, 0],
                        [0, 0, 0, 0, 0, 0, 0, 0, 1],
                        [0, 0, 0, 0, 0, 1, 1, 0, 0],
                        [0, 0, 0, 0, 0, 0, 0, 1, 1]
                       ])

l = np.concatenate((objectifs, vmin), axis=None)
u = np.concatenate((objectifs, vmax), axis=None)

P = sparse.block_diag([sparse.csc_matrix((n, n)), sparse.eye(m)], format='csc')

q = np.zeros(n+m)

A = sparse.bmat([
    [A1, -sparse.eye(m)],
    [A2,  None],
    ], format='csc')

prob = osqp.OSQP()
prob.setup(P, q, A, l, u, verbose=0)
res = prob.solve()

I've installed osqp-python with pip.

I've used solve_ls from qpsolvers with the OSQP solver with the same problem, and it worked. Here is the program I used:

from numpy import array
from qpsolvers import solve_ls
from scipy import sparse
import time

#R = array([[1., 2., 0.], [-8., 3., 2.], [0., 1., 1.]])
R = sparse.csc_matrix([[0, 1, 0, 0, 1, 0, 0], #c12
                        [1, 0, 1, 0, 0, 1, 1], #c13
                        [1, 1, 1, 0, 1, 1, 1], #c23
                        [1, 1, 1, 1, 0, 0, 0], #maths
                        [0, 1, 0, 0, 1, 0, 0], #spc
                        [0, 0, 0, -1, 1, 1, 1] #svt
                       ])
#s = array([3., 2., 3.])
objectifs = [21,
             12,
             33,
             27,
             19,
             3.5
             ]
s = array(objectifs)

G = array([[0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0]])
h = array([24, 15])

l = array([0] * 7)
u = array([13, 24, 24, 8, 15, 15, 1])

#h = array([3., 2., -2.]).reshape((3,))

debut = time.time()
x_sol = solve_ls(R, s, G, h, lb=l, ub=u, solver="osqp")
print(time.time()-debut)
print(f"LS solution: {x_sol = }")

I hope it can help finding the bug.

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions