fixanddive_c + + . cpp


fixanddive_c + + . cpp


/*版权2021,Gurobi优化,L狗万app足彩LC */ /*实现一个简单的MIP启发。放松模型,根据分数对变量进行排序,并确定最接近整数变量的25%的分数变量。重复,直到松弛是整数可行或线性不可行的。*/ #include "gurobi_c++.h" #include  #include  #include  using namespace std;bool vcomp (GRBVar *、GRBVar *);int main(int argc, char *argv[]) {if (argc < 2) {cout << "Usage: fixanddive_c++ filename" << endl;返回1;} GRBEnv* env = 0;GRBVar* x = 0;try{//读取模型env = new GRBEnv(); GRBModel model = GRBModel(*env, argv[1]); // Collect integer variables and relax them // Note that we use GRBVar* to copy variables deque intvars; x = model.getVars(); for (int j = 0; j < model.get(GRB_IntAttr_NumVars); ++j) { if (x[j].get(GRB_CharAttr_VType) != GRB_CONTINUOUS) { intvars.push_back(&x[j]); x[j].set(GRB_CharAttr_VType, GRB_CONTINUOUS); } } model.set(GRB_IntParam_OutputFlag, 0); model.optimize(); // Perform multiple iterations. In each iteration, identify the first // quartile of integer variables that are closest to an integer value // in the relaxation, fix them to the nearest integer, and repeat. for (int iter = 0; iter < 1000; ++iter) { // create a list of fractional variables, sorted in order of // increasing distance from the relaxation solution to the nearest // integer value deque fractional; for (size_t j = 0; j < intvars.size(); ++j) { double sol = fabs(intvars[j]->get(GRB_DoubleAttr_X)); if (fabs(sol - floor(sol + 0.5)) > 1e-5) { fractional.push_back(intvars[j]); } } cout << "Iteration " << iter << ", obj " << model.get(GRB_DoubleAttr_ObjVal) << ", fractional " << fractional.size() << endl; if (fractional.size() == 0) { cout << "Found feasible solution - objective " << model.get(GRB_DoubleAttr_ObjVal) << endl; break; } // Fix the first quartile to the nearest integer value sort(fractional.begin(), fractional.end(), vcomp); int nfix = (int) fractional.size() / 4; nfix = (nfix > 1) ? nfix : 1; for (int i = 0; i < nfix; ++i) { GRBVar* v = fractional[i]; double fixval = floor(v->get(GRB_DoubleAttr_X) + 0.5); v->set(GRB_DoubleAttr_LB, fixval); v->set(GRB_DoubleAttr_UB, fixval); cout << " Fix " << v->get(GRB_StringAttr_VarName) << " to " << fixval << " ( rel " << v->get(GRB_DoubleAttr_X) << " )" << endl; } model.optimize(); // Check optimization result if (model.get(GRB_IntAttr_Status) != GRB_OPTIMAL) { cout << "Relaxation is infeasible" << endl; break; } } } catch (GRBException e) { cout << "Error code = " << e.getErrorCode() << endl; cout << e.getMessage() << endl; } catch (...) { cout << "Error during optimization" << endl; } delete[] x; delete env; return 0; } bool vcomp(GRBVar* v1, GRBVar* v2) { double sol1 = fabs(v1->get(GRB_DoubleAttr_X)); double sol2 = fabs(v2->get(GRB_DoubleAttr_X)); double frac1 = fabs(sol1 - floor(sol1 + 0.5)); double frac2 = fabs(sol2 - floor(sol2 + 0.5)); return (frac1 < frac2); }