/* 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"
int
main(int argc,
char *argv[])
{
GRBenv *env = NULL;
GRBmodel *model = NULL;
int error = 0;
int optimstatus;
double objval;
if (argc < 2) {
fprintf(stderr, "Usage: lp_c filename\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;
}