Skip to content

Commit

Permalink
Merge branch 'master' of github.com:sympiler/smp-format
Browse files Browse the repository at this point in the history
  • Loading branch information
cheshmi committed Jun 8, 2020
2 parents 3ef7589 + ef0d707 commit c7dd4c7
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 15 deletions.
1 change: 1 addition & 0 deletions def.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
return true;
}


};

struct CSR {
Expand Down
53 changes: 41 additions & 12 deletions mp_format_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace format {
};

struct IEForm{
Description desc;
CSC *A, *C;
CSC *AT, *CT;
Dense *b, *d, *q;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand All @@ -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){
Expand Down Expand Up @@ -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_;
Expand Down Expand Up @@ -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<double >::max();
min_dbl = -1e+20;//std::numeric_limits<double >::min();
max_dbl = max_dbl;//std::numeric_limits<double >::max();
min_dbl = -max_dbl;//std::numeric_limits<double >::min();
num_eq = 0;
num_ineq = 0;
nnz_eq = 0;
Expand All @@ -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<double >::max();
min_dbl = -1e+20;//std::numeric_limits<double >::min();
max_dbl = max_dbl;//std::numeric_limits<double >::max();
min_dbl = -max_dbl;//std::numeric_limits<double >::min();
num_eq = 0;
num_ineq = 0;
nnz_eq = 0;
Expand Down Expand Up @@ -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_;
Expand Down Expand Up @@ -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_->H_->stype = LOWER;
smp_->q_ = sym_lib::copy_dense(ief_->q);
Expand All @@ -633,6 +653,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_->H_->stype = LOWER;
smp_->q_ = sym_lib::copy_dense(bf_->q);
Expand All @@ -652,6 +675,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_->H->stype = LOWER;
bf_->q = sym_lib::copy_dense(smp_->q_);
Expand Down Expand Up @@ -702,11 +726,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,
Expand Down
45 changes: 42 additions & 3 deletions smp_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,38 @@ namespace format{
return out;
}

void set_desc_from_string(std::string d){
for (unsigned i=0; i<d.length(); d[i]=tolower(d[i]),i++);
std::stringstream ss(d);
std::string line;
while (std::getline(ss, line, '\n')){
auto p1 = line.find('=');
if(p1 != std::string::npos){
std::string key = line.substr(0, p1);
std::string value = line.substr(p1+1);
trim(key);
trim(value);
if(key == "id")
ID_ = value;
else if(key == "category")
category_ = value;
else if(key == "application")
application_ = value;
else if(key =="name")
name_ = value;
else if(key == "group")
group_= value;
else if(key == "source")
group_ = value;
else if(key == "author")
author_ = value;
else if(key == "date")
date_ = value;
}
}
}


};

/*
Expand All @@ -46,6 +78,7 @@ namespace format{
int num_vars_;
std::string in_path_;
std::string desc_;
Description desc_struct_;
// Input attributes
CSC *H_, *A_, *C_;
CSC *AT_, *CT_;
Expand Down Expand Up @@ -76,6 +109,7 @@ namespace format{
primals_(NULLPNTR),duals_(NULLPNTR), optimal_obj_(0),desc_(std::move(desc)),
in_path_(""),num_vars_(0){
set_num_vars();
desc_struct_.set_desc_from_string(desc_);
};


Expand All @@ -84,6 +118,7 @@ namespace format{
num_vars_ = smp->num_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_);
Expand Down Expand Up @@ -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 "<<line <<" is unknown\n"; fin.close();
return false;
Expand All @@ -187,7 +223,9 @@ namespace format{
return false;
}
fin.close();

if(desc_struct_.name_ == ""){//SMP with empty descriptor
desc_struct_.name_ = strip_name(in_path_);
}
return true;
}

Expand Down Expand Up @@ -253,7 +291,6 @@ namespace format{
print_csc(H_->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;
Expand Down Expand Up @@ -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_);}

};
}
Expand Down
10 changes: 10 additions & 0 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}




}
Expand Down

0 comments on commit c7dd4c7

Please sign in to comment.