Skip to content

Commit

Permalink
Merge pull request #104 from coin-or/grb_wrks
Browse files Browse the repository at this point in the history
Latest updates including memory allocation fix
  • Loading branch information
hhijazi authored Sep 28, 2023
2 parents a293467 + 794fedc commit 943e710
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 32 deletions.
6 changes: 5 additions & 1 deletion examples/Optimization/MISDP/CBF/misdp_cbf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ using namespace std;
int main(int argc, char * argv[]){

//string fname=string(prj_dir)+"/data_sets/MISDP/2x7_3bars.cbf";
string fname=string(prj_dir)+"/data_sets/MISDP/band50_3.cbf";
string fname=string(prj_dir)+"/data_sets/MISDP/2x3_3bars.cbf";
bool root_refine = false, add_soc=false, add_threed=false, add_bag=false, hierarc=false;
string root_refine_s = "false", add_soc_s="false", add_threed_s="false", add_bag_s="false", hierarc_s="false";
if(argc>=2){
Expand Down Expand Up @@ -94,6 +94,10 @@ int main(int argc, char * argv[]){
m->add_hierarc=hierarc;

auto g=CBF_read(fname.c_str(), m);
if(g.nodes.size()==0){
cerr << "graph with size 0\n";
return -1;
}
m->print();
DebugOn("Instance "<<fname<<endl);

Expand Down
3 changes: 2 additions & 1 deletion examples/Optimization/MISDP/CBF/read_misdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ Net CBF_read(const char *file, shared_ptr<Model<double>>& m, bool add_3d) {

pFile = FOPEN(file, "rt");
if (!pFile) {
throw invalid_argument("cannot open misdp data file");
cerr <<"Cannot open misdp data file " << file << endl;
return g;
}
CBFdata data = { 0, };
// Keyword OBJ should exist!
Expand Down
81 changes: 51 additions & 30 deletions src/GurobiProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,42 @@ class cuts: public GRBCallback
vector<GRBLinExpr> vec_expi;
int soc_viol=0, soc_found=0,soc_added=0,det_viol=0, det_found=0, det_added=0;
int soc_viol_user=0, soc_found_user=0,soc_added_user=0,det_viol_user=0, det_found_user=0, det_added_user=0;


void update_rel_solution(){
size_t vid, vid_inst;
GRBVar gvar;
param_* v;
for(auto& v_p: m->_vars)
{
v = v_p.second.get();
auto idx = v->get_id();
auto dim = v->_dim[0];
for (auto i = 0; i < dim; i++) {
auto vid = idx + v->get_id_inst(i);
gvar = vars.at(vid);
v->set_double_val(i,getNodeRel(gvar));
}
}
}

void update_solution(){
size_t vid, vid_inst;
GRBVar gvar;
param_* v;
for(auto& v_p: m->_vars)
{
v = v_p.second.get();
auto idx = v->get_id();
auto dim = v->_dim[0];
for (auto i = 0; i < dim; i++) {
auto vid = idx + v->get_id_inst(i);
gvar = vars.at(vid);
v->set_double_val(i,getSolution(gvar));
}
}
}

//cuts(vector<GRBVar> _grb_vars, int xn, Model<>* mn, Model<> interiorn, vector<GRBLinExpr>& vec_exp) {
cuts(vector<GRBVar> _grb_vars, int xn, Model<>* mn, Model<> interiorn) {
vars = _grb_vars;
Expand Down Expand Up @@ -49,15 +85,16 @@ class cuts: public GRBCallback
add_bag_iteration=add_bag;
add_full_iteration=add_full;
/* Found an integer feasible solution */
double *x = new double[n];
vector<double> vec_x;
int i;
x=getSolution(vars.data(),n);
for(i=0;i<n;i++){
vec_x.push_back(x[i]);
}
// double *x = new double[n];
// vector<double> vec_x;
// int i;
// x=getSolution(vars.data(),n);
// for(i=0;i<n;i++){
// vec_x.push_back(x[i]);
// }
update_solution();
// m->compute_funcs();
if(add_soc){
m->set_solution(vec_x);
auto res= m->cutting_planes_soc(1e-9, soc_found, soc_added);
if(res.size()>=1){
for(auto i=0;i<res.size();i++){
Expand All @@ -74,13 +111,8 @@ class cuts: public GRBCallback
m->num_cuts[0]++;
}
}
// if(hierarc && res.size()>=1){
// add_bag_iteration=false;
// add_full_iteration=false;
// }
}
if(add_threed){
m->set_solution(vec_x);
auto res= m->cutting_planes_threed(1e-9, soc_found, soc_added);
if(res.size()>=1){
for(auto i=0;i<res.size();i++){
Expand All @@ -103,7 +135,6 @@ class cuts: public GRBCallback
}
}
if(add_bag_iteration){
m->set_solution(vec_x);
auto res1=m->cuts_eigen_bags(1e-9);
if(res1.size()>=1){
for(auto i=0;i<res1.size();i++){
Expand All @@ -124,7 +155,6 @@ class cuts: public GRBCallback
add_full_iteration=false;
}
if(add_full_iteration){
m->set_solution(vec_x);
auto res2=m->cuts_eigen_full(1e-9);
if(res2.size()>=1){
for(auto i=0;i<res2.size();i++){
Expand All @@ -151,16 +181,9 @@ class cuts: public GRBCallback
if(nc%1==0){
add_bag_iteration=add_bag;
add_full_iteration=add_full;
double *x = new double[n];
vector<double> vec_x;
int i;
x=getNodeRel(vars.data(),n);
for(i=0;i<n;i++){
vec_x.push_back(x[i]);
}
m->set_solution(vec_x);
update_rel_solution();
// m->compute_funcs();
if(add_soc){
m->set_solution(vec_x);
auto res= m->cutting_planes_soc(1e-9, soc_found, soc_added);
if(res.size()>=1){
for(auto i=0;i<res.size();i++){
Expand All @@ -183,7 +206,6 @@ class cuts: public GRBCallback
// }
}
if(add_threed){
m->set_solution(vec_x);
auto res= m->cutting_planes_threed(1e-9, soc_found, soc_added);
if(res.size()>=1){
for(auto i=0;i<res.size();i++){
Expand All @@ -206,7 +228,6 @@ class cuts: public GRBCallback
}
}
if(add_bag_iteration){
m->set_solution(vec_x);
auto res1=m->cuts_eigen_bags(1e-9);
if(res1.size()>=1){
DebugOff("Added " << res1.size() << " bag cuts\n");
Expand All @@ -228,7 +249,6 @@ class cuts: public GRBCallback
add_full_iteration=false;
}
if(add_full_iteration){
m->set_solution(vec_x);
auto res2=m->cuts_eigen_full(1e-9);
if(res2.size()>=1){
DebugOff("Added " << res2.size() << " full cuts\n");
Expand Down Expand Up @@ -467,14 +487,15 @@ void GurobiProgram::reset_model(){
bool GurobiProgram::solve(bool relax, double mipgap, double time_limit){
//cout << "\n Presolve = " << grb_env->get(GRB_IntParam_Presolve) << endl;
// print_constraints();
grb_mod->update();
if (relax) relax_model();
else
unrelax_model();
// relax_model();

grb_mod->set(GRB_DoubleParam_MIPGap, 1e-6);
grb_mod->set(GRB_DoubleParam_FeasibilityTol, 1e-9);
grb_mod->set(GRB_DoubleParam_OptimalityTol, 1e-9);
// grb_mod->set(GRB_DoubleParam_OptimalityTol, 1e-9);
// grb_mod->set(GRB_IntParam_StartNodeLimit, -3);
// grb_mod->getEnv().set(GRB_IntParam_DualReductions, 0);
// grb_mod->getEnv().set(GRB_IntParam_PreCrush, 1);
Expand All @@ -492,7 +513,7 @@ bool GurobiProgram::solve(bool relax, double mipgap, double time_limit){
// grb_mod->set(GRB_IntParam_RINS,1000);
// grb_mod->set(GRB_IntParam_Cuts,0);

grb_mod->set(GRB_DoubleParam_TimeLimit,25200);
grb_mod->set(GRB_DoubleParam_TimeLimit,10800);
grb_mod->set(GRB_IntParam_OutputFlag,1);
//grb_mod->set(GRB_DoubleParam_Cutoff,5.33);
// grb_mod->set(GRB_IntParam_MinRelNodes,0);
Expand Down Expand Up @@ -793,7 +814,7 @@ void GurobiProgram::prepare_model(){
fill_in_grb_vmap();
create_grb_constraints();
set_grb_objective();
grb_mod->write("gurobiprint.lp");
// grb_mod->write("gurobiprint.lp");
// print_constraints();
}
void GurobiProgram::update_model(){
Expand Down

0 comments on commit 943e710

Please sign in to comment.