From 7011f56e2ac08c776a4d946ce0781fabaa2405a9 Mon Sep 17 00:00:00 2001 From: Kazem Date: Fri, 12 Jun 2020 21:52:03 -0400 Subject: [PATCH] CLI for convetors --- CMakeLists.txt | 6 +++ bounded2smp.cpp | 44 ++++++++++++++++++ ie2smp.cpp | 47 +++++++++++++++++++ mp_format_converter.h | 84 ++++++++++++++++++++++++++++++++++ utils.h | 103 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 284 insertions(+) create mode 100644 bounded2smp.cpp create mode 100644 ie2smp.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 36eb96b..f2a1fb4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/bounded2smp.cpp b/bounded2smp.cpp new file mode 100644 index 0000000..cbac3e6 --- /dev/null +++ b/bounded2smp.cpp @@ -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 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; +} diff --git a/ie2smp.cpp b/ie2smp.cpp new file mode 100644 index 0000000..e739bac --- /dev/null +++ b/ie2smp.cpp @@ -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 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; +} diff --git a/mp_format_converter.h b/mp_format_converter.h index 5cba337..0cadb89 100644 --- a/mp_format_converter.h +++ b/mp_format_converter.h @@ -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;} @@ -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; @@ -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){ diff --git a/utils.h b/utils.h index 3c9aafd..a737bcd 100644 --- a/utils.h +++ b/utils.h @@ -10,9 +10,112 @@ #include #include #include +#include namespace format{ + void parse_args_bounded(int argc, char **argv, std::map &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("quadratic", optarg)); + break; + case 'q': + qp_args.insert(std::pair("linear", optarg)); + break; + case 'l': + qp_args.insert(std::pair("l-bounds", optarg)); + break; + case 'a': + qp_args.insert(std::pair("constraints", optarg)); + break; + case 'u': + qp_args.insert(std::pair("u-bounds", optarg)); + break; + case 'o': + qp_args.insert(std::pair("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 &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("quadratic", optarg)); + break; + case 'q': + qp_args.insert(std::pair("linear", optarg)); + break; + case 'a': + qp_args.insert(std::pair("equalities", optarg)); + break; + case 'b': + qp_args.insert(std::pair("equality bounds", optarg)); + break; + case 'c': + qp_args.insert(std::pair("inequalities", optarg)); + break; + case 'd': + qp_args.insert(std::pair("inequality bounds", optarg)); + break; + case 'o': + qp_args.insert(std::pair("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 &qp_args){ const char* const short_opts = "m:o:l:a:b:c:d:n:p:r:e:t:h"; const option long_opts[] = {