manbet体育手机客户端


feasopt_c ++。CPP


/ *版权所有2018,Gurobi优狗万app足彩化,LLC * // *此示例从文件中读取MIP模型,将人工变量添加到每个约束,然后最大限度地减少人工变量的总和。具有目标零的解决方案对应于输入模型的可行解决方案。我们还可以使用Feasrelax功能来进行。在此示例中,我们使用Minrelax = 1,即优化返回的模型找到最小化原始目标的解决方案,但只能从最小化人工变量的总和中的那些解决方案中最小化。* / #include“gurobi_c ++。h”使用命名空间std;int main(int argc,char * argv []){if(argc <2){cout <<“用法:feasopt_c ++ filename”<<端口;返回1;grbenv * env = 0;grbconstr * c = 0;尝试{env = new grbenv(); GRBModel feasmodel = GRBModel(*env, argv[1]); // Create a copy to use FeasRelax feature later */ GRBModel feasmodel1 = GRBModel(feasmodel); // clear objective feasmodel.setObjective(GRBLinExpr(0.0)); // add slack variables c = feasmodel.getConstrs(); for (int i = 0; i < feasmodel.get(GRB_IntAttr_NumConstrs); ++i) { char sense = c[i].get(GRB_CharAttr_Sense); if (sense != '>') { double coef = -1.0; feasmodel.addVar(0.0, GRB_INFINITY, 1.0, GRB_CONTINUOUS, 1, &c[i], &coef, "ArtN_" + c[i].get(GRB_StringAttr_ConstrName)); } if (sense != '<') { double coef = 1.0; feasmodel.addVar(0.0, GRB_INFINITY, 1.0, GRB_CONTINUOUS, 1, &c[i], &coef, "ArtP_" + c[i].get(GRB_StringAttr_ConstrName)); } } // optimize modified model feasmodel.optimize(); feasmodel.write("feasopt.lp"); // use FeasRelax feature */ feasmodel1.feasRelax(GRB_FEASRELAX_LINEAR, true, false, true); feasmodel1.write("feasopt1.lp"); feasmodel1.optimize(); } catch (GRBException e) { cout << "Error code = " << e.getErrorCode() << endl; cout << e.getMessage() << endl; } catch (...) { cout << "Error during optimization" << endl; } delete[] c; delete env; return 0; }