manbet体育手机客户端


lpmod_c.c


/* Copyright 2019, 狗万app足彩Gurobi Optimization, LLC */ /*这个例子从一个文件中读取LP模型并解决它。如果模型可以求解,则找到最小的正变量,并将其上界设为0,然后采用两种方式求解模型:先提前开始,后不提前开始(即。“从头开始”)。*/ #include  #include  #include  #include  #include "gurobi_c.h" int main(int argc, char *argv[]) {GRBenv *env = NULL;GRBmodel *model = NULL;Int error = 0;int j;int numvars, isMIP,状态,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; }