feasopt_c.c


feasopt_c.c


*/ /*这个例子从一个文件中读取一个狗万app足彩MIP模型,为每个约束添加人工变量,然后最小化人工变量的总和。目标为零的解对应于输入模型的可行解。我们也可以使用FeasRelax功能来实现。在这个例子中,我们使用minrelax=1,即优化返回的模型找到一个使原始目标最小化的解决方案,但只从那些使人工变量之和最小化的解决方案中。*/ #include  #include  #include  #include  #include "gurobi_c.h" int main(int argc, char *argv[]) {GRBenv *env = NULL;GRBmodel *model = NULL;GRBmodel *feasmodel = NULL;double *rhspen = NULL;Int error = 0;int i, j。 int numvars, numconstrs; char sense; int vind[1]; double vval[1]; double feasobj; char *cname, *vname; if (argc < 2) { fprintf(stderr, "Usage: feasopt_c filename\n"); exit(1); } error = GRBloadenv(&env, "feasopt.log"); if (error) goto QUIT; error = GRBreadmodel(env, argv[1], &model); if (error) goto QUIT; /* Create a copy to use FeasRelax feature later */ feasmodel = GRBcopymodel(model); if (error) goto QUIT; /* clear objective */ error = GRBgetintattr(model, "NumVars", &numvars); if (error) goto QUIT; for (j = 0; j < numvars; ++j) { error = GRBsetdblattrelement(model, "Obj", j, 0.0); if (error) goto QUIT; } /* add slack variables */ error = GRBgetintattr(model, "NumConstrs", &numconstrs); if (error) goto QUIT; for (i = 0; i < numconstrs; ++i) { error = GRBgetcharattrelement(model, "Sense", i, &sense); if (error) goto QUIT; if (sense != '>') { error = GRBgetstrattrelement(model, "ConstrName", i, &cname); if (error) goto QUIT; vname = malloc(sizeof(char) * (6 + strlen(cname))); if (!vname) goto QUIT; strcpy(vname, "ArtN_"); strcat(vname, cname); vind[0] = i; vval[0] = -1.0; error = GRBaddvar(model, 1, vind, vval, 1.0, 0.0, GRB_INFINITY, GRB_CONTINUOUS, vname); if (error) goto QUIT; free(vname); } if (sense != '<') { error = GRBgetstrattrelement(model, "ConstrName", i, &cname); if (error) goto QUIT; vname = malloc(sizeof(char) * (6 + strlen(cname))); if (!vname) goto QUIT; strcpy(vname, "ArtP_"); strcat(vname, cname); vind[0] = i; vval[0] = 1.0; error = GRBaddvar(model, 1, vind, vval, 1.0, 0.0, GRB_INFINITY, GRB_CONTINUOUS, vname); if (error) goto QUIT; free(vname); } } /* Optimize modified model */ error = GRBoptimize(model); if (error) goto QUIT; error = GRBwrite(model, "feasopt.lp"); if (error) goto QUIT; /* Use FeasRelax feature */ rhspen = (double *) malloc(numconstrs*sizeof(double)); if (rhspen == NULL) { printf("ERROR: out of memory\n"); goto QUIT; } /* set penalties for artificial variables */ for (i = 0; i < numconstrs; i++) rhspen[i] = 1; /* create a FeasRelax model with the original objective recovered and enforcement on minimum of aretificial variables */ error = GRBfeasrelax(feasmodel, GRB_FEASRELAX_LINEAR, 1, NULL, NULL, rhspen, &feasobj); if (error) goto QUIT; /* optimize FeasRelax model */ error = GRBwrite(feasmodel, "feasopt1.lp"); if (error) goto QUIT; error = GRBoptimize(feasmodel); if (error) goto QUIT; QUIT: /* Error reporting */ if (error) { printf("ERROR: %s\n", GRBgeterrormsg(env)); exit(1); } /* Free models, env and etc. */ if (rhspen) free(rhspen); GRBfreemodel(model); GRBfreemodel(feasmodel); GRBfreeenv(env); return 0; }