lpmod_c.c


/ * 2023年版权,Gurobi优狗万app足彩化,LLC * / / *这个例子从文件读取一个LP模型并解决它。如果模型可以解决,那么它找到最小的积极的变量,其上限设置为零,并解决了模型两个方面:首先是一个先进的开始,那么没有一个先进的(即开始。“从头开始”)。* / # include < stdlib。h > # include < stdio。h > # include <数学。h > # include <字符串。h > # include " gurobi_c。h“int主要(int命令行参数个数,char * argv []) {GRBenv * env =零;GRBmodel *模型=零;int错误= 0;int j; int numvars, isMIP, status, minVar = 0; double minVal = GRB_INFINITY, sol, lb; char *varname; double warmCount, warmTime, coldCount, coldTime; if (argc < 2) { fprintf(stderr, "Usage: lpmod_c filename\n"); exit(1); } error = GRBloadenv(&env, "lpmod.log"); if (error) goto QUIT; /* Read model and determine whether it is an LP */ error = GRBreadmodel(env, argv[1], &model); if (error) goto QUIT; error = GRBgetintattr(model, "IsMIP", &isMIP); if (error) goto QUIT; if (isMIP) { printf("The model is not a linear program\n"); goto QUIT; } error = GRBoptimize(model); if (error) goto QUIT; error = GRBgetintattr(model, "Status", &status); if (error) goto QUIT; if ((status == GRB_INF_OR_UNBD) || (status == GRB_INFEASIBLE) || (status == GRB_UNBOUNDED)) { printf("The model cannot be solved because it is "); printf("infeasible or unbounded\n"); goto QUIT; } if (status != GRB_OPTIMAL) { printf("Optimization was stopped with status %i\n", status); goto QUIT; } /* Find the smallest variable value */ error = GRBgetintattr(model, "NumVars", &numvars); if (error) goto QUIT; for (j = 0; j < numvars; ++j) { error = GRBgetdblattrelement(model, "X", j, &sol); if (error) goto QUIT; error = GRBgetdblattrelement(model, "LB", j, &lb); if (error) goto QUIT; if ((sol > 0.0001) && (sol < minVal) && (lb == 0.0)) { minVal = sol; minVar = j; } } error = GRBgetstrattrelement(model, "VarName", minVar, &varname); if (error) goto QUIT; printf("\n*** Setting %s from %f to zero ***\n\n", varname, minVal); error = GRBsetdblattrelement(model, "UB", minVar, 0.0); if (error) goto QUIT; /* Solve from this starting point */ error = GRBoptimize(model); if (error) goto QUIT; /* Save iteration & time info */ error = GRBgetdblattr(model, "IterCount", &warmCount); if (error) goto QUIT; error = GRBgetdblattr(model, "Runtime", &warmTime); if (error) goto QUIT; /* Reset the model and resolve */ printf("\n*** Resetting and solving "); printf("without an advanced start ***\n\n"); error = GRBreset(model, 0); if (error) goto QUIT; error = GRBoptimize(model); if (error) goto QUIT; /* Save iteration & time info */ error = GRBgetdblattr(model, "IterCount", &coldCount); if (error) goto QUIT; error = GRBgetdblattr(model, "Runtime", &coldTime); if (error) goto QUIT; printf("\n*** Warm start: %f iterations, %f seconds\n", warmCount, warmTime); printf("*** Cold start: %f iterations, %f seconds\n", coldCount, coldTime); QUIT: /* Error reporting */ if (error) { printf("ERROR: %s\n", GRBgeterrormsg(env)); exit(1); } /* Free model */ GRBfreemodel(model); /* Free environment */ GRBfreeenv(env); return 0; }