26 #include "sqic_interface.hpp"
27 #include "casadi/core/conic.hpp"
28 #include "casadi/core/mx_function.hpp"
29 #include "casadi/core/casadi_misc.hpp"
32 #include "casadi/interfaces/sqic/resource_sqic.hpp"
37 int CASADI_CONIC_SQIC_EXPORT
40 plugin->name =
"sqic";
42 plugin->version = CASADI_VERSION;
67 std::fill(
x_.begin()+n_,
x_.end(), 0);
70 rc_.begin(), negate<double>());
71 std::fill(
rc_.begin()+n_,
rc_.end(), 0);
79 for (casadi_int i=0;i<n_+nc_+1;++i) {
80 if (
bl_[i]==-std::numeric_limits<double>::infinity())
bl_[i]=-
inf_;
81 if (
bu_[i]==std::numeric_limits<double>::infinity())
bu_[i]=
inf_;
90 std::copy(
x_.begin(),
x_.begin()+n_, output(
CONIC_X)->begin());
108 bl_.resize(n_+nc_+1, 0);
109 bu_.resize(n_+nc_+1, 0);
110 x_.resize(n_+nc_+1, 0);
111 hs_.resize(n_+nc_+1, 0);
113 pi_.resize(nc_+1, 0);
114 rc_.resize(n_+nc_+1, 0);
116 locH_ = st_[QP_STRUCT_H].colind();
117 indH_ = st_[QP_STRUCT_H].row();
120 for (casadi_int i=0;i<
indH_.size();++i)
indH_[i]+=1;
121 for (casadi_int i=0;i<
locH_.size();++i)
locH_[i]+=1;
129 for (casadi_int i=0;i<
indA_.size();++i)
indA_[i]+=1;
130 for (casadi_int i=0;i<
locA_.size();++i)
locA_[i]+=1;
147 casadi_int m = nc_+1;
150 casadi_int nnzH=input(
CONIC_H).size();
156 &input(
CONIC_H).nonzeros()[0]);
161 std::map<casadi_int, string> f;
170 std::map<casadi_int, string>::const_iterator it =
flagmap.find(flag);
172 std::stringstream ss;
174 ss <<
"Unknown error (" << flag <<
") from module \"" << module <<
"\".";
176 ss <<
"Module \"" << module <<
"\" returned flag \"" << it->second <<
"\".";
178 ss <<
" Consult SQIC documentation.";
179 casadi_error(ss.str());
184 std::string resource_sqic_input(resource_sqic);
185 std::istringstream stream(resource_sqic_input);
187 while (std::getline(stream, line)) {
188 size_t b_i = line.find(
"bind ( C, ");
189 if (b_i!=std::string::npos) {
190 file << line.substr(0, b_i) << std::endl;
192 file << line << std::endl;
196 file.precision(std::numeric_limits<double>::digits10+2);
197 file << std::scientific;
200 file <<
"program exported" << std::endl;
201 file <<
" use SQICModule" << std::endl;
202 file <<
" implicit none" << std::endl;
203 file <<
" integer(ip) :: m, n, n_inf, nnH, nnzH, nnzA, nS" << std::endl;
206 file <<
" real(rp) :: Obj" << std::endl;
208 file <<
" real(rp), allocatable:: bl(:), bu(:), x(:), valA(:), valH(:) , pi(:), rc(:)"
210 file <<
" integer(ip), allocatable:: indA(:), locA(:), indH(:), locH(:), hEtype(:), hs(:)"
214 casadi_int m = nc_+1;
216 casadi_int nnzH=input(
CONIC_H).size();
218 file <<
" n = " << n << std::endl;
219 file <<
" m = " << m << std::endl;
220 file <<
" nnzA = " << nnzA << std::endl;
221 file <<
" nnzH = " << nnzH << std::endl;
223 file <<
" allocate ( bl(n+m), bu(n+m) )" << std::endl;
224 file <<
" allocate ( hEtype(n+m) )" << std::endl;
225 file <<
" allocate ( locA(n+1), valA(nnzA), indA(nnzA) )" << std::endl;
226 file <<
" allocate ( pi(m), rc(n+m), x(n+m) )" << std::endl;
227 file <<
" allocate ( hs(n+m) )" << std::endl;
228 file <<
" allocate ( valH(nnzH), locH(n+1), indH(nnzH) )" << std::endl;
230 for (casadi_int i=0;i<
indA_.size();++i) {
231 file <<
" indA(" << i +1 <<
") = " <<
indA_[i] << std::endl;
233 for (casadi_int i=0;i<
locA_.size();++i) {
234 file <<
" locA(" << i +1 <<
") = " <<
locA_[i] << std::endl;
237 file <<
" valA(" << i +1 <<
") = " <<
formatA_.output().at(i) << std::endl;
239 for (casadi_int i=0;i<
bl_.size();++i) {
240 file <<
" bl(" << i +1 <<
") = " <<
bl_[i] << std::endl;
241 file <<
" bu(" << i +1 <<
") = " <<
bu_[i] << std::endl;
243 for (casadi_int i=0;i<
hEtype_.size();++i) {
244 file <<
" hEtype(" << i +1 <<
") = " <<
hEtype_[i] << std::endl;
246 for (casadi_int i=0;i<
hs_.size();++i) {
247 file <<
" hs(" << i +1 <<
") = " <<
hs_[i] << std::endl;
249 for (casadi_int i=0;i<
indH_.size();++i) {
250 file <<
" indH(" << i +1 <<
") = " <<
indH_[i] << std::endl;
252 for (casadi_int i=0;i<
locH_.size();++i) {
253 file <<
" locH(" << i +1 <<
") = " <<
locH_[i] << std::endl;
255 for (casadi_int i=0;i<input(
CONIC_H).size();++i) {
256 file <<
" valH(" << i +1 <<
") = " << input(
CONIC_H).at(i) << std::endl;
258 for (casadi_int i=0;i<input(
CONIC_X0).size();++i) {
259 file <<
" x(" << i +1 <<
") = " << input(
CONIC_X0).at(i) << std::endl;
261 for (casadi_int i=0;i<
pi_.size();++i) {
262 file <<
" pi(" << i +1 <<
") = " << 0 << std::endl;
265 for (casadi_int i=0;i<
rc_.size();++i) {
266 file <<
" rc(" << i +1 <<
") = "
271 file <<
" call wsqic (m, n, nnzA, indA, locA, valA, bl, bu, hEtype, "
272 <<
"hs, x, pi, rc, nnzH, indH, locH, valH)" << std::endl;
300 file <<
" call sqicSolve(Obj)" << std::endl;
301 file <<
" deallocate ( bl, bu )" << std::endl;
302 file <<
" deallocate ( hEtype )" << std::endl;
303 file <<
" deallocate ( locA, valA, indA )" << std::endl;
304 file <<
" deallocate ( pi, rc, x )" << std::endl;
305 file <<
" deallocate ( valH, locH, indH )" << std::endl;
306 file <<
" call sqicDestroy()" << std::endl;
307 file <<
"end program exported" << std::endl;
static const Options options_
Options.
virtual void check_inputs(const double *lbx, const double *ubx, const double *lba, const double *uba) const
Check if the numerical values of the supplied bounds make sense.
void init(const Dict &opts) override
Initialize.
bool inputs_check_
Errors are thrown if numerical values of inputs look bad.
std::pair< casadi_int, casadi_int > size_out(casadi_int ind) const
Get output dimension.
static MX sym(const std::string &name, casadi_int nrow=1, casadi_int ncol=1)
Create an nrow-by-ncol symbolic primitive.
MX T() const
Transpose the matrix.
static void registerPlugin(const Plugin &plugin, bool needs_lock=true)
Register an integrator in the factory.
static Sparsity dense(casadi_int nrow, casadi_int ncol=1)
Create a dense rectangular sparsity pattern *.
const casadi_int * row() const
Get a reference to row-vector,.
const casadi_int * colind() const
Get a reference to the colindex of all column element (see class description)
bool is_init_
Flag: is already initialized.
static Conic * creator(const std::map< std::string, Sparsity > &st)
Create a new QP Solver.
std::vector< casadi_int > hEtype_
Storage space for sqic hEtype variable.
std::vector< double > pi_
Storage space for sqic rc variable.
std::vector< double > rc_
Storage space for sqic rc variable.
static std::map< casadi_int, std::string > calc_flagmap()
Calculate the error message map.
~SqicInterface() override
Destructor.
static void sqic_error(const std::string &module, casadi_int flag)
Throw error.
SqicInterface()
Constructor.
static const std::string meta_doc
A documentation string.
std::vector< casadi_int > locH_
Storage space for sqic locH variable.
virtual void generateNativeCode(std::ostream &file) const
Generate native code for debugging.
std::vector< casadi_int > locA_
Storage space for sqic locA variable.
std::vector< casadi_int > indA_
Storage space for sqic indA variable.
static std::map< casadi_int, std::string > flagmap
Error message map.
std::vector< casadi_int > indH_
Storage space for sqic indH variable.
std::vector< double > bu_
Storage space for sqic bu variable.
std::vector< casadi_int > hs_
Storage space for sqic hs variable.
Function formatA_
Helper function to bring A into correct format.
virtual void init()
Initialize.
std::vector< double > x_
Storage space for sqic x variable.
std::vector< double > bl_
Storage space for sqic bl variable.
@ CONIC_UBA
dense, (nc x 1)
@ CONIC_A
The matrix A: sparse, (nc x n) - product with x must be dense.
@ CONIC_G
The vector g: dense, (n x 1)
@ CONIC_LBA
dense, (nc x 1)
@ CONIC_UBX
dense, (n x 1)
@ CONIC_LBX
dense, (n x 1)
int CASADI_CONIC_SQIC_EXPORT casadi_register_conic_sqic(Conic::Plugin *plugin)
void CASADI_CONIC_SQIC_EXPORT casadi_load_conic_sqic()
@ CONIC_X
The primal solution.
@ CONIC_LAM_A
The dual solution corresponding to linear bounds.
@ CONIC_COST
The optimal cost.
@ CONIC_LAM_X
The dual solution corresponding to simple bounds.