params_c + + . cpp


/*版权所有2023,Gurobi O狗万app足彩ptimization, LLC */ /*使用与模型相关的参数。一个MIP可以用不同的参数集求解几秒钟。选择MIP间隙最小的,继续优化,直到找到最优解为止。*/ #include " gu罗比_c++.h"使用命名空间std;int main(int argc, char *argv[]) {if (argc < 2) {cout << "用法:params_c++文件名" << 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; }