mip2_c.c
/* Copyright 2009, Gurobi Optimization, Inc. */
/* This example reads a MIP model from a file,
solves it, creates the fixed model from it and
solves it */
#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;
GRBmodel *fixed = NULL;
int error = 0;
int optimstatus, foptimstatus;
double objval, fobjval;
/* To change settings for a loaded models, it needs first to get
the model environment, which will be freed when the model
is freed. */
GRBenv *menv, *fenv;
if (argc < 2) {
fprintf(stderr, "Usage: mip2_c filename\n");
exit(1);
}
/* Create environment */
error = GRBloadenv(&env, "mip2.log");
if (error || env == NULL) {
fprintf(stderr, "Error: could not create environment\n");
exit(1);
}
/* Read model from file */
error = GRBreadmodel(env, argv[1], &model);
if (error) goto QUIT;
/* get model environment */
menv = GRBgetenv(model);
if (!menv) {
fprintf(stderr, "Error: could not get model environment\n");
goto QUIT;
}
/* set MIPGAP tolerance to zero */
error = GRBsetdblparam(menv, "MIPGAP", 0.0);
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;
printf("\nOptimization complete\n");
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_INF_OR_UNBD) {
printf("Model is infeasible or unbounded\n\n");
goto QUIT;
} else if (optimstatus == GRB_INFEASIBLE) {
printf("Model is infeasible\n\n");
goto QUIT;
} else if (optimstatus == GRB_UNBOUNDED) {
printf("Model is unbounded\n\n");
goto QUIT;
} else {
printf("Optimization was stopped with status = %d\n\n", optimstatus);
goto QUIT;
}
/* Create a fixed model, turn off presolve and solve */
fixed = GRBfixedmodel(model);
if (!fixed) {
fprintf(stderr, "Error: could not create fixed model\n");
goto QUIT;
}
fenv = GRBgetenv(fixed);
if (!fenv) {
fprintf(stderr, "Error: could not get fixed model environment\n");
goto QUIT;
}
error = GRBsetintparam(fenv, "PRESOLVE", 0);
if (error) goto QUIT;
error = GRBoptimize(fixed);
if (error) goto QUIT;
error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &foptimstatus);
if (error) goto QUIT;
if (foptimstatus != GRB_OPTIMAL) {
fprintf(stderr, "Error: fixed model isn't optimal\n");
goto QUIT;
}
error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &fobjval);
if (error) goto QUIT;
if (fabs(fobjval - objval) > 1.0e-6 * (1.0 + fabs(objval))) {
fprintf(stderr, "Error: objective values are different\n");
}
QUIT:
/* Error reporting */
if (error) {
printf("ERROR: %s\n", GRBgeterrormsg(env));
exit(1);
}
/* Free models */
GRBfreemodel(model);
GRBfreemodel(fixed);
/* Free environment */
GRBfreeenv(env);
return 0;
}
