From ef0d707a4359999c5841437854cf3af3554da476 Mon Sep 17 00:00:00 2001 From: Kazem Date: Sat, 6 Jun 2020 20:53:48 -0400 Subject: [PATCH] integration is fixed --- def.h | 1 + mp_format_converter.h | 53 +++++++++++++++++++++++++++++++++---------- smp_format.h | 45 +++++++++++++++++++++++++++++++++--- utils.h | 10 ++++++++ 4 files changed, 94 insertions(+), 15 deletions(-) diff --git a/def.h b/def.h index 90b0a1b..08db282 100644 --- a/def.h +++ b/def.h @@ -201,6 +201,7 @@ return true; } + }; struct CSR { diff --git a/mp_format_converter.h b/mp_format_converter.h index a37bc20..52bd484 100644 --- a/mp_format_converter.h +++ b/mp_format_converter.h @@ -28,6 +28,7 @@ namespace format { }; struct IEForm{ + Description desc; CSC *A, *C; CSC *AT, *CT; Dense *b, *d, *q; @@ -92,9 +93,22 @@ namespace format { b_c && b_c && pr_c && du_c && ob_c; } + int get_num_var(){ return H ? H->m : 0;} + int get_num_eqc(){ return A ? A->m : 0;} + int get_num_ineqc(){ return C ? C->m : 0;} + + void print(){ + print_csc(H->m, H->n, H->p, H->i, H->x); + print_csc(A->m, A->n, A->p, A->i, A->x); + + print_csc(C->m, C->n, C->p, C->i, C->x); + print_dense(d->row,d->col,d->lda,d->a); + } + }; struct BoundedForm{ + Description desc; Dense *l; Dense *u; Dense *q; @@ -157,7 +171,7 @@ namespace format { }; - +// for converting smp to ie bool find_inequalities_by_bounds(Dense *ld, Dense *ud, CSC *A, CSC *AT, IEForm* ie_out){ if(!A){ @@ -187,7 +201,7 @@ namespace format { std::fill_n(l, n_const, min_dbl); if(!ud) std::fill_n(l, n_const, max_dbl); - ie_out->b = new Dense(2*n_const,1,1);; a_eq = ie_out->b->a; + //ie_out->b = new Dense(2*n_const,1,1);; a_eq = ie_out->b->a; ie_out->d = new Dense(2*n_const,1,1);; a_ineq = ie_out->d->a; ie_out->AT = AT_eq; ie_out->CT = AT_ineq; @@ -249,9 +263,9 @@ namespace format { } } assert(num_ineq <= 2 * A->m); - assert(num_eq <= 2 * A->m); + assert(num_eq == 0); // all equalities assumed to be sperated /// building the equality constraint matrix - if(num_eq > 0) { + /*if(num_eq > 0) { AT_eq->m = h_dim; AT_eq->n = num_eq; AT_eq->p = new int[num_eq + 1]; @@ -276,7 +290,7 @@ namespace format { ie_out->b = NULLPNTR; delete AT_eq; ie_out->A = ie_out->AT = NULLPNTR; - } + }*/ //building the ineq constraint matrix if(num_ineq>0){ @@ -478,6 +492,7 @@ namespace format { struct QPFormatConverter { std::string problem_name; std::string desc_; + // Bounded format l <= A <= u bool bounded_converted; BoundedForm *bf_; @@ -521,8 +536,8 @@ namespace format { a_eq(NULLPNTR), a_ineq(NULLPNTR), H(NULLPNTR), AB_d(NULLPNTR), ab_eqineq(NULLPNTR), H_d(NULLPNTR), A_d(NULLPNTR), B_d(NULLPNTR), q(NULLPNTR){ mode = 0; - max_dbl = 1e+20;//std::numeric_limits::max(); - min_dbl = -1e+20;//std::numeric_limits::min(); + max_dbl = max_dbl;//std::numeric_limits::max(); + min_dbl = -max_dbl;//std::numeric_limits::min(); num_eq = 0; num_ineq = 0; nnz_eq = 0; @@ -536,8 +551,8 @@ namespace format { QPFormatConverter(CSC *H_full_in, double *q_in, CSC *A_in, double *l_in, double *u_in) : H_full(H_full_in), q(q_in), A(A_in), l(l_in), u(u_in) { mode = 1; - max_dbl = 1e+20;//std::numeric_limits::max(); - min_dbl = -1e+20;//std::numeric_limits::min(); + max_dbl = max_dbl;//std::numeric_limits::max(); + min_dbl = -max_dbl;//std::numeric_limits::min(); num_eq = 0; num_ineq = 0; nnz_eq = 0; @@ -581,6 +596,8 @@ namespace format { if(!smp_converted) return false; ief_ = new IEForm; + ief_->desc = smp_->desc_struct_; + problem_name = ief_->desc.name_; ief_->H = sym_lib::copy_sparse(smp_->H_); ief_->q = sym_lib::copy_dense(smp_->q_); ief_->fixed = smp_->r_; @@ -612,6 +629,9 @@ namespace format { return false; int num_vars = ief_->H->n; smp_ = new SMP(""); + smp_->desc_struct_=ief_->desc; + smp_->desc_=smp_->desc_struct_.get_desc(); + problem_name = smp_->desc_struct_.name_; smp_->H_ = sym_lib::copy_sparse(ief_->H); smp_->q_ = sym_lib::copy_dense(ief_->q); smp_->r_ = ief_->fixed; @@ -630,6 +650,9 @@ namespace format { if(!bounded_converted) //bounded is neither loaded nor converted return false; smp_ = new SMP(""); + smp_->desc_struct_=bf_->desc; + smp_->desc_=smp_->desc_struct_.get_desc(); + problem_name = smp_->desc_struct_.name_; smp_->H_ = sym_lib::copy_sparse(bf_->H); smp_->q_ = sym_lib::copy_dense(bf_->q); smp_->r_ = bf_->fixed; @@ -646,6 +669,7 @@ namespace format { int n_eq = smp_->A_ ? smp_->A_->m : 0; int n_ineq = smp_->C_ ? smp_->C_->m : 0; bf_ = new BoundedForm; + bf_->desc = smp_->desc_struct_; bf_->H = sym_lib::copy_sparse(smp_->H_); bf_->q = sym_lib::copy_dense(smp_->q_); bf_->fixed = smp_->r_; @@ -694,11 +718,16 @@ namespace format { return 0; } - void print_log(){ - + void print_log() { + std::setprecision(20); + if (ief_) { + std::cout << ief_->desc.name_ << "," << ief_->get_num_var() << "," << ief_->H->nnz << ","; + std::cout << ief_->get_num_eqc() << "," << (ief_->A ? ief_->A->nnz : 0) << + "," << ief_->get_num_ineqc() << ","; + std::cout << (ief_->C ? ief_->C->nnz : 0) << ","; + } } - /*int read_IE_format(std::string hessian_file, std::string linear_file, std::string eq_file, diff --git a/smp_format.h b/smp_format.h index c731bb0..624cc5d 100644 --- a/smp_format.h +++ b/smp_format.h @@ -34,6 +34,38 @@ namespace format{ return out; } + void set_desc_from_string(std::string d){ + for (unsigned i=0; inum_vars_; in_path_ = smp->in_path_; desc_ = smp->desc_; + desc_struct_.set_desc_from_string(desc_); H_ = sym_lib::copy_sparse(smp->H_); A_ = sym_lib::copy_sparse(smp->A_); C_ = sym_lib::copy_sparse(smp->C_); @@ -174,6 +209,7 @@ namespace format{ read_real_constant(fin, optimal_obj_); }else if(line == "\"description\": |" || line == "\"description\":") { read_string(fin, desc_); + desc_struct_.set_desc_from_string(desc_); }else if(line.find(':') != std::string::npos){ std::cout<<"Key in "<m, H_->n, H_->p, H_->i, H_->x, fout.rdbuf()); } - } else{ std::cout<<"The path: "<< out_path <<" is not available for writing.\n"; return false; @@ -288,7 +325,9 @@ namespace format{ b_c && b_c && p_c && d_c && o_c; } - void set_description(std::string d){desc_ = d;} + void set_description(std::string d){ + desc_ = d; + desc_struct_.set_desc_from_string(desc_);} }; } diff --git a/utils.h b/utils.h index 9249adc..c53882d 100644 --- a/utils.h +++ b/utils.h @@ -105,6 +105,16 @@ namespace format{ rtrim(s); } + /// find the name of a file from a path + std::string strip_name(std::string name){ + auto p1 = name.rfind("/"); + auto p2 = name.rfind("."); + std::string slide = name.substr( p1 != std::string::npos ? p1+1 : 0, + p2 != std::string::npos ? p2-p1-1 : std::string::npos); + return slide; + } + + }