forked from coin-or/qpOASES
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding Python script to generate sparse QP
- Loading branch information
Showing
12 changed files
with
15,422 additions
and
94 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import numpy as np | ||
import scipy as sp | ||
from scipy.sparse import csc_matrix, coo_matrix, csr_matrix, lil_matrix, random | ||
from jinja2 import Environment | ||
from jinja2.loaders import FileSystemLoader | ||
import os | ||
|
||
seed = 42 | ||
density = 0.1 | ||
gamma = 0.01 | ||
|
||
out_file_name = 'qp_data.hpp' | ||
in_file_name = 'qp_data.in.hpp' | ||
|
||
NV = 100 | ||
NC = 10 | ||
|
||
H = csc_matrix((NV, NV)) | ||
A = csc_matrix((NV, NV)) | ||
|
||
myinf = 1e10 | ||
|
||
for i in range(NV): | ||
H[i,i] = 1.0 | ||
|
||
# H = H + gamma*random(NV, NV, density=density, format='csc', random_state=seed) | ||
# H = H.T*H | ||
|
||
for i in range(NC): | ||
A[i,i] = 1.0 | ||
|
||
H_ri = H.indices | ||
H_cp = H.indptr | ||
H_val = H.data | ||
H_nnz = H.nnz | ||
|
||
A_ri = A.indices | ||
A_cp = A.indptr | ||
A_val = A.data | ||
A_nnz = A.nnz | ||
|
||
g = np.ones((NV,1)) | ||
|
||
lb = -myinf*np.ones((NV, 1)) | ||
ub = myinf*np.ones((NV, 1)) | ||
|
||
lbA = -np.ones((NC, 1)) | ||
ubA = np.ones((NC, 1)) | ||
|
||
print('rendering templated C++ code...') | ||
env = Environment(loader=FileSystemLoader(os.path.dirname(os.path.abspath(__file__)))) | ||
tmpl = env.get_template(in_file_name) | ||
|
||
code = tmpl.render(NV = NV, NC = NC, H_cp = H_cp, H_ri = H_ri, H_val = H_val, H_nnz = H_nnz, \ | ||
A_cp = A_cp, A_ri = A_ri, A_val = A_val, A_nnz = A_nnz, g = g, lb = lb, ub = ub, lbA = lbA, ubA = ubA) | ||
|
||
with open(out_file_name, "w+") as f: | ||
f.write(code.replace('inf', 'Inf')) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* This file is part of qpOASES. | ||
* | ||
* qpOASES -- An Implementation of the Online Active Set Strategy. | ||
* Copyright (C) 2007-2017 by Hans Joachim Ferreau, Andreas Potschka, | ||
* Christian Kirches et al. All rights reserved. | ||
* | ||
* qpOASES is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* qpOASES is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with qpOASES; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
*/ | ||
|
||
|
||
/** | ||
* \author Andrea Zanelli | ||
* \version 3.2 | ||
* \date 2022 | ||
* | ||
* QP data generated by a Python script for testing purposes. | ||
*/ | ||
|
||
|
||
USING_NAMESPACE_QPOASES | ||
|
||
#define NV {{NV}} | ||
#define NC {{NC}} | ||
|
||
const real_t Inf = INFTY; | ||
|
||
sparse_int_t H_ri[] = { | ||
{% for d in H_ri %} | ||
{{ d }}, | ||
{%- endfor %} | ||
}; | ||
|
||
sparse_int_t H_cp[] = { | ||
{% for d in H_cp %} | ||
{{ d }}, | ||
{%- endfor %} | ||
}; | ||
|
||
real_t H_val[] = { | ||
{% for d in H_val %} | ||
{{ d }}, | ||
{%- endfor %} | ||
}; | ||
|
||
sparse_int_t A_ri[] = { | ||
{% for d in A_ri %} | ||
{{ d }}, | ||
{%- endfor %} | ||
}; | ||
|
||
sparse_int_t A_cp[] = { | ||
{% for d in A_cp %} | ||
{{ d }}, | ||
{%- endfor %} | ||
}; | ||
|
||
real_t A_val[] = { | ||
{% for d in A_val %} | ||
{{ d }}, | ||
{%- endfor %} | ||
}; | ||
|
||
real_t g[] = { | ||
{% for d in g %} | ||
{{ d[0] }}, | ||
{%- endfor %} | ||
}; | ||
|
||
real_t lb[] = { | ||
{% for d in lb %} | ||
{{ d[0] }}, | ||
{%- endfor %} | ||
}; | ||
|
||
real_t ub[] = { | ||
{% for d in ub %} | ||
{{ d[0] }}, | ||
{%- endfor %} | ||
}; | ||
|
||
real_t lbA[] = { | ||
{% for d in lbA %} | ||
{{ d[0] }}, | ||
{%- endfor %} | ||
}; | ||
|
||
real_t ubA[] = { | ||
{% for d in ubA %} | ||
{{ d[0] }}, | ||
{%- endfor %} | ||
}; | ||
|
||
long H_nnz = {{ H_nnz }}; | ||
long A_nnz = {{ A_nnz }}; |
Oops, something went wrong.