MPC problem with variable sparsity pattern of matrice A

132 views
Skip to first unread message

yegane...@gmail.com

unread,
Aug 13, 2018, 4:21:28 PM8/13/18
to OSQP
hi
first i want to thank all for this solver
i'm using this solver for a MPC problem that it's matrices and vectors (A,P,q,l,u) change in every iteration but it's A matrix not only changes in values also changes it's sparsity pattern in cyclic way. i mean it's sparsity size changes to 476, 480, 484, 488, 492 and repeat again. so when i use update for matrices in every iteration, everything goes wrong. i use new setup and warm start in every iteration and it seems OK but i prefer to use update instead of new setup. how can i solve this problem?
thank you

mohammad

Paul Goulart

unread,
Aug 13, 2018, 4:33:11 PM8/13/18
to yegane...@gmail.com, OSQP
You can just allocate enough memory for the union of all of the nonzeros in your problem.

I would suggest initialising the solver with an A matrix that is 1 in those places where a zero might appear, and zero elsewhere. You can then call the update function to populate it with the correct values at each time step.

Note that there is no problem with updating an entry of A to zero (and then back again) - the only prohibition is trying to put a nonzero value in a place where OSQP is not expecting one from the outset.
> --
> You received this message because you are subscribed to the Google Groups "OSQP" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to osqp+uns...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/osqp/91a56bc5-4ac7-431b-bd9c-723eb30364c3%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Message has been deleted

yegane...@gmail.com

unread,
Aug 14, 2018, 11:38:58 AM8/14/18
to OSQP

Thank you for the quick reply.
i did this but it didn't work. i initialized the problem in setup step with an sparse A matrix that had fixed size (532) and at each step time i updated it with desired nonzero values that i wanted, but it still doesn't work

Goran Banjac

unread,
Aug 14, 2018, 11:45:36 AM8/14/18
to OSQP
Are you sure you are updating the values of A in a correct way? Please see the following thread for a similar issue:

In which programming language is your code written?

yegane...@gmail.com

unread,
Aug 14, 2018, 12:37:34 PM8/14/18
to OSQP
i wrote my code in python and i think i do update in correct way.
i have five A matrices that they are different in sparsity patterns and some values. their sparse size are 476,480,484,488,492.
first i create an sparse A matrix with sparse.csc_matrix function, that it's size is 532(i put 1 in places of A matrix that their values changes between zero and nonzero) .then according to my desired A matrix i change some of A's nonzero values to zero and some zero to nonzero values by indicing (e.g. A[10,10]=0). and after that i use update function (m.update(A=A))
for P matrix i use sparse.triu with csc format

Goran Banjac

unread,
Aug 14, 2018, 2:46:47 PM8/14/18
to OSQP
This is not how the update function should be used to update the matrix A. You should use instead:
m.update('Ax',Ax,'Ax_idx',Ax_idx);

If you have N nonzero elements in the matrix A, then Ax_idx should be a vector of indices (taking integer values from 0 to N-1) of the nonzero elements (ordered column-wise) that you want to update, and Ax a vector of values of these elements.

See the Matlab code in this topic to get a better idea on the usage of the update function:

Hope it helps.

Best,
Goran

Goran Banjac

unread,
Aug 14, 2018, 4:00:04 PM8/14/18
to OSQP
The syntax above should be used in Matlab. In Python, you should use:
m.update(Ax=Ax, Ax_idx=Ax_idx);

Goran

yegane...@gmail.com

unread,
Aug 16, 2018, 7:13:19 AM8/16/18
to OSQP
thank you for your helps
it's work
Reply all
Reply to author
Forward
0 new messages