Skip to content

Commit

Permalink
adding Python script to generate sparse QP
Browse files Browse the repository at this point in the history
  • Loading branch information
zanellia committed Aug 25, 2022
1 parent f42bc57 commit ac8305a
Show file tree
Hide file tree
Showing 12 changed files with 15,422 additions and 94 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ all: $(EXTERNAL) src examples

ifeq ($(DEF_SOLVER), SOLVER_MUMPS)
mumps:
@echo $(QPOASESROOT)
@cd external/ThirdParty-Mumps; \
if [ -d "MUMPS" ]; then \
echo "Found MUMPS source code."; \
else get.Mumps && ./configure --prefix=$(PWD)/external/mumps_installation; fi; \
else get.Mumps; ./configure --prefix=$(PWD)/external/mumps_installation; fi; \
make && make install
endif

Expand Down
58 changes: 58 additions & 0 deletions examples/generate_sparse_qp/main.py
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'))
108 changes: 108 additions & 0 deletions examples/generate_sparse_qp/qp_data.in.hpp
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 }};
Loading

0 comments on commit ac8305a

Please sign in to comment.