lp_c.c


lp_c.c


/* Copyright 2021, 狗万app足彩Gurobi Optimization, LLC */ /*这个例子从一个文件中读取一个LP模型并解决它。如果模型是不可行的或无界的,则实例停止预解,重新求解模型。如果模型不可行,这个例子计算一个不可约不一致子系统(IIS),并将其写入一个文件*/ #include  #include  #include  #include "gurobi_c.h" int main(int argc, char *argv[]) {GRBenv *env = NULL;GRBmodel *model = NULL;Int error = 0;int optimstatus;双objval;if (argc < 2) {fprintf(stderr, "Usage: lp_c filename\n");退出(1);} /*创建环境*/ 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; }