Skip to content

Commit

Permalink
#382 To continue
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan08 committed Jun 6, 2019
1 parent 17da37d commit 22a63ce
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/function/ibex_Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ class Function : public Fnc {
* hexadecimal representation, whence a safe serialization.
*/
std::string minibex(bool human=true) const;
std::string toAmpl() const;

/**
* \brief Serialize the function
Expand Down
5 changes: 5 additions & 0 deletions src/function/ibex_FunctionBuild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,4 +611,9 @@ std::string Function::minibex(bool human) const {
return s.str();
}


std::string Function::toAmpl() const {
return "TODO";
}

} // namespace ibex
113 changes: 113 additions & 0 deletions src/system/ibex_System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,119 @@ std::string System::minibex(bool human) const {
return s.str();
}


std::string System::toAmpl() const {
stringstream s;
s << "# Build with IBEX " << _IBEX_RELEASE_ << "\n";

Array<Domain> domains(args.size());
for (int i=0; i<args.size(); i++) {
domains.set_ref(i,*new Domain(args[i].dim));
}

ibex::load(domains, box);

for (int i=0; i<args.size(); i++) {
const ExprSymbol& x=args[i];
switch(domains[i].dim.type()) {
case Dim::SCALAR:{
s << "var " << x.name << " ";
Interval tmp = domains[i].i();
if (tmp.lb() > NEG_INFINITY) {
s << " >= " << tmp.lb();
if (tmp.ub() < POS_INFINITY) s << " , ";
}
if (tmp.ub() < POS_INFINITY) {
s << " <= " << tmp.ub();
}
s << "; \n";
break;
}

case Dim::COL_VECTOR:
case Dim::ROW_VECTOR:{
int n = (x.dim.nb_rows()>1) ? x.dim.nb_rows() : x.dim.nb_cols();
s << "set S"<<i<< " = {1.." << n << "}; \n";
s << "param lower_bounds"<< i << " {S"<<i<<"}; \n";
s << "param upper_bounds"<< i << " {S"<<i<<"}; \n";
s << "param: lower_bounds" <<i << " upper_bounds"<< i <<" := \n";
for (int j =0; j<n; j++) {
s << j << " " << domains[i].v()[j].lb()<< " " << domains[i].v()[j].ub() << " \n";
}
s <<";\n";
s << "var " << x.name << "{p in S"<<i<<"} >= lower_bounds"<<i<<"[p] , <= upper_bounds"<<i <<"[p] ;\n\n";
break;
}
case Dim::MATRIX: {
s << "set S1"<<i<< " = {1.." << x.dim.nb_rows() << "}; \n";
s << "set S2"<<i<< " = {1.." << x.dim.nb_cols() << "}; \n";
s << "param lower_bounds"<< i << " {S1"<<i<<", S2"<<i<< " }; \n";
s << "param upper_bounds"<< i << " {S1"<<i<<", S2"<<i<< " }; \n";
s << "param lower_bounds"<< i<< " : ";
for (int ii=0 ; ii < x.dim.nb_cols(); ii++) {
s << ii << " ";
}
s << " : = \n ";
for (int ii=0 ; ii < x.dim.nb_rows(); ii++) {
s << ii << " ";
for (int jj=0 ; jj < x.dim.nb_cols(); jj++) {
s << domains[i].m()[ii][jj].lb() << " ";
}
s <<"\n";
}
s << ";\n";
s << "param upper_bounds"<< i<< " : ";
for (int ii=0 ; ii < x.dim.nb_cols(); ii++) {
s << ii << " ";
}
s << " : = \n ";
for (int ii=0 ; ii < x.dim.nb_rows(); ii++) {
s << ii << " ";
for (int jj=0 ; jj < x.dim.nb_cols(); jj++) {
s << domains[i].m()[ii][jj].ub() << " ";
}
s <<"\n";
}
s << ";\n";
s << "var " << x.name << "{p in S1"<<i<<", k in S2"<< i <<"} >= lower_bounds"<<i<<"[p,k] , <= upper_bounds"<<i <<"[p,k] ;\n\n";
break;
}

}
}

s << '\n';

if (goal) {
s << goal->toAmpl() << ";\n\n";
}

if (nb_ctr>0) {
s << f_ctrs.toAmpl() << ";\n\n";
}

if (goal) {
s << "minimize GOAL:" << goal->name << ";\n\n";
}

if (nb_ctr>0) {
for (int i=0; i<nb_ctr; i++) {
s << "subject to ctrs_" << f_ctrs.name << "[" << i<< "] : ";
s << f_ctrs.name << "["<< i <<"] " << ops[i] << "0 ;\n\n";
}
}
s << "option ibexopt_auxfiles rc; \n";
s << "option solver ibexopt; \n";
s << "solve; \n\n ";
s.flush();

for (int i=0; i<args.size(); i++) {
delete &domains[i];
}

return s.str();
}

std::ostream& operator<<(std::ostream& os, const System& sys) {

os << "variables: " << endl << " ";
Expand Down
1 change: 1 addition & 0 deletions src/system/ibex_System.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ class System {
* hexadecimal representation, whence a safe serialization.
*/
std::string minibex(bool human=true) const;
std::string toAmpl () const;

/**
* \brief Identifying number.
Expand Down

0 comments on commit 22a63ce

Please sign in to comment.