-
-
Save olzhas/af43dcdfa5295da1079b12f9cc1e17dd to your computer and use it in GitHub Desktop.
Solve QP using qpOASES and CasADi from Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as NP | |
import casadi as C | |
def qpsolve(H,g,lbx,ubx,A=NP.zeros((0,0)),lba=NP.zeros(0),uba=NP.zeros(0)): | |
# Convert to CasADi types | |
H = C.DMatrix(H) | |
g = C.DMatrix(g) | |
lbx = C.DMatrix(lbx) | |
ubx = C.DMatrix(ubx) | |
A = C.DMatrix(A) | |
A = A.reshape((A.size1(),H.size1())) # Make sure matching dimensions | |
lba = C.DMatrix(lba) | |
uba = C.DMatrix(uba) | |
# QP structure | |
qp = C.qpStruct(h=H.sparsity(),a=A.sparsity()) | |
# Create CasADi solver instance | |
if False: | |
solver = C.QpSolver("qpoases",qp) | |
else: | |
solver = C.QpSolver("nlp",qp) | |
solver.setOption("nlp_solver","ipopt") | |
# Set options | |
#solver.setOption("option_name", ...) | |
# Initialize the solver | |
solver.init() | |
# Pass problem data | |
solver.setInput(H,"h") | |
solver.setInput(g,"g") | |
solver.setInput(A,"a") | |
solver.setInput(lbx,"lbx") | |
solver.setInput(ubx,"ubx") | |
solver.setInput(lba,"lba") | |
solver.setInput(uba,"uba") | |
# Solver the QP | |
solver.evaluate() | |
# Return the solution | |
return NP.array(solver.getOutput("x")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment