lp_c.c


/ * 2023年版权,Gurobi优狗万app足彩化,LLC * / / *这个例子从文件读取一个LP模型并解决它。如果模型是不可行或无界,关掉presolve和解决模型的例子。如果模型是不可行的,例子计算一个不可约不一致的子系统(IIS),并将其写入一个文件* / # include < stdlib。h > # include < stdio。h > # include <数学。h > # include " gurobi_c。h“int主要(int命令行参数个数,char * argv []) {GRBenv * env =零;GRBmodel *模型=零;int错误= 0;int optimstatus;双objval;如果(命令行参数个数< 2){流(stderr,“用法:lp_c文件名\ n”); exit(1); } /* Create environment */ error = GRBloadenv(&env, "lp.log"); if (error) goto QUIT; /* Read model from file */ error = GRBreadmodel(env, argv[1], &model); if (error) goto QUIT; /* Solve model */ error = GRBoptimize(model); if (error) goto QUIT; /* Capture solution information */ error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus); if (error) goto QUIT; /* If model is infeasible or unbounded, turn off presolve and resolve */ if (optimstatus == GRB_INF_OR_UNBD) { /* Change parameter on model environment. The model now has a copy of the original environment, so changing the original will no longer affect the model. */ error = GRBsetintparam(GRBgetenv(model), "PRESOLVE", 0); if (error) goto QUIT; error = GRBoptimize(model); if (error) goto QUIT; error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus); if (error) goto QUIT; } if (optimstatus == GRB_OPTIMAL) { error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval); if (error) goto QUIT; printf("Optimal objective: %.4e\n\n", objval); } else if (optimstatus == GRB_INFEASIBLE) { printf("Model is infeasible\n\n"); error = GRBcomputeIIS(model); if (error) goto QUIT; error = GRBwrite(model, "model.ilp"); if (error) goto QUIT; } else if (optimstatus == GRB_UNBOUNDED) { printf("Model is unbounded\n\n"); } else { printf("Optimization was stopped with status = %d\n\n", optimstatus); } QUIT: /* Error reporting */ if (error) { printf("ERROR: %s\n", GRBgeterrormsg(env)); exit(1); } /* Free model */ GRBfreemodel(model); /* Free environment */ GRBfreeenv(env); return 0; }