-
-
Save jgillis/d94a644175a446588781 to your computer and use it in GitHub Desktop.
This file contains 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 | |
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