/* Copyright 2025, Gurobi Optimization, LLC *//* This example reads an LP model from a file and solves it. If the model is infeasible or unbounded, the example turns off presolve and solves the model again. If the model is infeasible, the example computes an Irreducible Inconsistent Subsystem (IIS), and writes it to a file */#include<stdlib.h>#include<stdio.h>#include<math.h>#include"gurobi_c.h"intmain(intargc,char*argv[]){GRBenv*env=NULL;GRBmodel*model=NULL;interror=0;intoptimstatus;doubleobjval;if(argc<2){fprintf(stderr,"Usage: lp_c filename\n");exit(1);}/* Create environment */error=GRBloadenv(&env,"lp.log");if(error)gotoQUIT;/* Read model from file */error=GRBreadmodel(env,argv[1],&model);if(error)gotoQUIT;/* Solve model */error=GRBoptimize(model);if(error)gotoQUIT;/* Capture solution information */error=GRBgetintattr(model,GRB_INT_ATTR_STATUS,&optimstatus);if(error)gotoQUIT;/* 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)gotoQUIT;error=GRBoptimize(model);if(error)gotoQUIT;error=GRBgetintattr(model,GRB_INT_ATTR_STATUS,&optimstatus);if(error)gotoQUIT;}if(optimstatus==GRB_OPTIMAL){error=GRBgetdblattr(model,GRB_DBL_ATTR_OBJVAL,&objval);if(error)gotoQUIT;printf("Optimal objective: %.4e\n\n",objval);}elseif(optimstatus==GRB_INFEASIBLE){printf("Model is infeasible\n\n");error=GRBcomputeIIS(model);if(error)gotoQUIT;error=GRBwrite(model,"model.ilp");if(error)gotoQUIT;}elseif(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);return0;}
Help and Feedback