lpmethod_c.c


lpmethod_c.c


/* Copyright 2021, 狗万app足彩Gurobi Optimization, LLC */ /*用Method参数的不同值求解模型;显示哪个值给出了最短的求解时间。*/ #include  #include  #include "gurobi_c.h" int main(int argc, char *argv[]) {GRBenv *env = NULL, *menv;GRBmodel *m = NULL;Int error = 0;int我;int optimstatus;int最佳方法= -1;双bestTime;if (argc < 2) {fprintf(stderr, "Usage: lpmethod_c filename\n"); exit(1); } error = GRBloadenv(&env, "lpmethod.log"); if (error) goto QUIT; /* Read model */ error = GRBreadmodel(env, argv[1], &m); if (error) goto QUIT; menv = GRBgetenv(m); error = GRBgetdblparam(menv, "TimeLimit", &bestTime); if (error) goto QUIT; /* Solve the model with different values of Method */ for (i = 0; i <= 2; ++i) { error = GRBreset(m, 0); if (error) goto QUIT; error = GRBsetintparam(menv, "Method", i); if (error) goto QUIT; error = GRBoptimize(m); if (error) goto QUIT; error = GRBgetintattr(m, "Status", &optimstatus); if (error) goto QUIT; if (optimstatus == GRB_OPTIMAL) { error = GRBgetdblattr(m, "Runtime", &bestTime); if (error) goto QUIT; bestMethod = i; /* Reduce the TimeLimit parameter to save time with other methods */ error = GRBsetdblparam(menv, "TimeLimit", bestTime); if (error) goto QUIT; } } /* Report which method was fastest */ if (bestMethod == -1) { printf("Unable to solve this model\n"); } else { printf("Solved in %f seconds with Method: %i\n", bestTime, bestMethod); } QUIT: /* Error reporting */ if (error) { printf("ERROR: %s\n", GRBgeterrormsg(env)); exit(1); } /* Free model */ GRBfreemodel(m); /* Free environment */ GRBfreeenv(env); return 0; }