Skip to content

Instantly share code, notes, and snippets.

@jgillis
Forked from jaeandersson/qpsolve.py
Created August 4, 2014 11:29
Show Gist options
  • Save jgillis/d94a644175a446588781 to your computer and use it in GitHub Desktop.
Save jgillis/d94a644175a446588781 to your computer and use it in GitHub Desktop.
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
try:
solver = C.QpSolver("qpoases",qp)
except:
solver = C.QpSolver("nlp",qp)
solver.setOption("nlp_solver","ipopt")
solver.setOption("nlp_solver_options",{"linear_solver": "mumps"})
# 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