Skip to content

Commit

Permalink
CLI for convetors
Browse files Browse the repository at this point in the history
  • Loading branch information
cheshmi committed Jun 13, 2020
1 parent f4c3ed6 commit 7011f56
Show file tree
Hide file tree
Showing 5 changed files with 284 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@ set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES smp_convertor.cpp)
add_executable(smp-convertor ${SOURCE_FILES} smp_convertor.cpp)

project(bounded2smp)
add_executable(bounded2smp bounded2smp.cpp)


project(ie2smp)
add_executable(ie2smp ie2smp.cpp)

add_subdirectory(old)
44 changes: 44 additions & 0 deletions bounded2smp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// Created by kazem on 6/12/20.
//

#include "utils.h"
#include "mp_format_converter.h"


using namespace format;

int main(int argc, char *argv[]){

std::map<std::string,std::string> qp_args;
parse_args_bounded(argc, argv, qp_args);

std::string p_name, q_name, l_name, a_name, u_name;
std::string output = "noname.yml";

if(qp_args.find("quadratic") != qp_args.end())
p_name = qp_args["quadratic"];
if(qp_args.find("linear") != qp_args.end())
q_name = qp_args["linear"];
if(qp_args.find("l-bounds") != qp_args.end())
l_name = qp_args["l-bounds"];
if(qp_args.find("constraints") != qp_args.end())
a_name = qp_args["constraints"];
if(qp_args.find("u-bounds") != qp_args.end())
u_name = qp_args["u-bounds"];
if(qp_args.find("output") != qp_args.end())
output = qp_args["output"];

auto *bf = load_bounded(p_name, q_name, l_name, a_name, u_name);
Description test;
std::string dtes=test.get_desc();
auto *qfc = new QPFormatConverter(bf);
//qfc->smp_->write("tmp2.yml");
qfc->bounded_to_smp();
qfc->smp_->write(output);

delete bf;
delete qfc;

return 0;
}
47 changes: 47 additions & 0 deletions ie2smp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// Created by kazem on 6/12/20.
//


#include "utils.h"
#include "mp_format_converter.h"


using namespace format;

int main(int argc, char *argv[]){

std::map<std::string,std::string> qp_args;
parse_args_ie(argc, argv, qp_args);

std::string p_name, q_name, a_name, b_name, c_name, d_name;
std::string output = "noname.yml";

if(qp_args.find("quadratic") != qp_args.end())
p_name = qp_args["quadratic"];
if(qp_args.find("linear") != qp_args.end())
q_name = qp_args["linear"];
if(qp_args.find("equality bounds") != qp_args.end())
b_name = qp_args["equality bounds"];
if(qp_args.find("equalities") != qp_args.end())
a_name = qp_args["equalities"];
if(qp_args.find("inequalities") != qp_args.end())
c_name = qp_args["inequalities"];
if(qp_args.find("inequality bounds") != qp_args.end())
d_name = qp_args["inequality bounds"];
if(qp_args.find("output") != qp_args.end())
output = qp_args["output"];

auto *ie = load_ie(p_name, q_name, a_name,b_name, c_name, d_name);
Description test;
std::string dtes=test.get_desc();
auto *qfc = new QPFormatConverter(ie);
//qfc->smp_->write("tmp2.yml");
qfc->ie_to_smp();
qfc->smp_->write(output);

delete ie;
delete qfc;

return 0;
}
84 changes: 84 additions & 0 deletions mp_format_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ 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;}
Expand All @@ -107,6 +108,52 @@ namespace format {

};


IEForm *load_ie(std::string quad_name, std::string linear_name,
std::string eq_name, std::string eql_name,
std::string ineq_name, std::string ineql_name){
auto *ie = new IEForm;
std::ifstream hin(quad_name);
if(hin.is_open()){
read_mtx_csc_real(hin, ie->H);
}
hin.close();

std::ifstream lin(linear_name);
if(lin.is_open()){
read_mtx_array_real(lin, ie->q);
}
lin.close();

std::ifstream blin(eql_name);
if(blin.is_open()){
read_mtx_array_real(blin, ie->b);
}
blin.close();

std::ifstream Ain(eq_name);
if(Ain.is_open()){
read_mtx_csc_real(Ain, ie->A);
}
Ain.close();

std::ifstream Cin(ineq_name);
if(Cin.is_open()){
read_mtx_csc_real(Cin, ie->C);
}
Cin.close();

std::ifstream ulin(ineql_name);
if(ulin.is_open()){
read_mtx_array_real(ulin, ie->d);
}
ulin.close();

return ie;
}



struct BoundedForm{
Description desc;
Dense *l;
Expand Down Expand Up @@ -171,6 +218,43 @@ namespace format {

};

BoundedForm *load_bounded(std::string quad_name, std::string linear_name,
std::string l_name, std::string constraint_name, std::string u_name){
auto *bf = new BoundedForm;
std::ifstream hin(quad_name);
if(hin.is_open()){
read_mtx_csc_real(hin, bf->H);
}
hin.close();

std::ifstream lin(linear_name);
if(lin.is_open()){
read_mtx_array_real(lin, bf->q);
}
lin.close();

std::ifstream blin(l_name);
if(blin.is_open()){
read_mtx_array_real(blin, bf->l);
}
blin.close();

std::ifstream Ain(constraint_name);
if(Ain.is_open()){
read_mtx_csc_real(Ain, bf->A);
}
Ain.close();

std::ifstream ulin(u_name);
if(ulin.is_open()){
read_mtx_array_real(ulin, bf->u);
}
ulin.close();

return bf;
}


// for converting smp to ie
bool find_inequalities_by_bounds(Dense *ld, Dense *ud, CSC *A, CSC *AT,
IEForm* ie_out){
Expand Down
103 changes: 103 additions & 0 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,112 @@
#include <iomanip>
#include <fstream>
#include <map>
#include <iostream>

namespace format{

void parse_args_bounded(int argc, char **argv, std::map<std::string, std::string> &qp_args){
const char* const short_opts = "p:q:a:l:u:o:h";
const option long_opts[] = {
{"quadratic", required_argument, nullptr, 'p'},
{"linear", required_argument, nullptr, 'q'},
{"constraints", required_argument, nullptr, 'a'},
{"lbounds", required_argument, nullptr, 'l'},
{"ubounds", required_argument, nullptr, 'u'},
{"output", required_argument, nullptr, 'o'},
{"help", no_argument, nullptr, 'h'},
{nullptr, no_argument, nullptr, 0}
};

while (true)
{
const auto opt = getopt_long(argc, argv, short_opts, long_opts, nullptr);

if (-1 == opt)
break;

switch (opt)
{
case 'p':
qp_args.insert(std::pair<std::string,std::string>("quadratic", optarg));
break;
case 'q':
qp_args.insert(std::pair<std::string,std::string>("linear", optarg));
break;
case 'l':
qp_args.insert(std::pair<std::string,std::string>("l-bounds", optarg));
break;
case 'a':
qp_args.insert(std::pair<std::string,std::string>("constraints", optarg));
break;
case 'u':
qp_args.insert(std::pair<std::string,std::string>("u-bounds", optarg));
break;
case 'o':
qp_args.insert(std::pair<std::string,std::string>("output", optarg));
break;
case 'h': // -h or --help
case '?': // Unrecognized option
default:
std::cout<<"Help !\n";
break;
}
}
}

void parse_args_ie(int argc, char **argv, std::map<std::string, std::string> &qp_args){
const char* const short_opts = "p:q:a:b:c:d:o:h";
const option long_opts[] = {
{"quadratic", required_argument, nullptr, 'p'},
{"linear", required_argument, nullptr, 'q'},
{"eq", required_argument, nullptr, 'a'},
{"eqbounds", required_argument, nullptr, 'b'},
{"ineq", required_argument, nullptr, 'c'},
{"ineqbounds", required_argument, nullptr, 'd'},
{"output", required_argument, nullptr, 'o'},
{"help", no_argument, nullptr, 'h'},
{nullptr, no_argument, nullptr, 0}
};

while (true)
{
const auto opt = getopt_long(argc, argv, short_opts, long_opts, nullptr);

if (-1 == opt)
break;

switch (opt)
{
case 'p':
qp_args.insert(std::pair<std::string,std::string>("quadratic", optarg));
break;
case 'q':
qp_args.insert(std::pair<std::string,std::string>("linear", optarg));
break;
case 'a':
qp_args.insert(std::pair<std::string,std::string>("equalities", optarg));
break;
case 'b':
qp_args.insert(std::pair<std::string,std::string>("equality bounds", optarg));
break;
case 'c':
qp_args.insert(std::pair<std::string,std::string>("inequalities", optarg));
break;
case 'd':
qp_args.insert(std::pair<std::string,std::string>("inequality bounds", optarg));
break;
case 'o':
qp_args.insert(std::pair<std::string,std::string>("output", optarg));
break;
case 'h': // -h or --help
case '?': // Unrecognized option
default:
std::cout<<"Help !\n";
break;
}
}
}

void parse_args(int argc, char **argv, std::map<std::string, std::string> &qp_args){
const char* const short_opts = "m:o:l:a:b:c:d:n:p:r:e:t:h";
const option long_opts[] = {
Expand Down

0 comments on commit 7011f56

Please sign in to comment.