params_c + + . cpp


params_c + + . cpp


/* Copyright 2021, 狗万app足彩Gurobi Optimization, LLC */ /*使用与模型相关的参数。用不同的参数集求解一个MIP,只需几秒钟。选择MIP间隙最小的那个,继续优化,直到找到最优解。*/ #include "gurobi_c++.h" using namespace std;int main(int argc, char *argv[]) {if (argc < 2) {cout << "Usage: params_c++ filename" << endl;返回1;} GRBEnv* env = 0;GRBModel *bestModel = 0, *m = 0;try{//读取模型并验证它是一个MIP env = new GRBEnv();m = new GRBModel(*env, argv[1]); if (m->get(GRB_IntAttr_IsMIP) == 0) { cout << "The model is not an integer program" << endl; return 1; } // Set a 2 second time limit m->set(GRB_DoubleParam_TimeLimit, 2); // Now solve the model with different values of MIPFocus bestModel = new GRBModel(*m); bestModel->optimize(); for (int i = 1; i <= 3; ++i) { m->reset(); m->set(GRB_IntParam_MIPFocus, i); m->optimize(); if (bestModel->get(GRB_DoubleAttr_MIPGap) > m->get(GRB_DoubleAttr_MIPGap)) { swap(bestModel, m); } } // Finally, delete the extra model, reset the time limit and // continue to solve the best model to optimality delete m; m = 0; bestModel->set(GRB_DoubleParam_TimeLimit, GRB_INFINITY); bestModel->optimize(); cout << "Solved with MIPFocus: " << bestModel->get(GRB_IntParam_MIPFocus) << endl; } catch (GRBException e) { cout << "Error code = " << e.getErrorCode() << endl; cout << e.getMessage() << endl; } catch (...) { cout << "Error during optimization" << endl; } delete bestModel; delete m; delete env; return 0; }