feasopt_c.c


/*版权2023,Gurobi Opt狗万app足彩imization, LLC */ /*本例从文件中读取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错误= 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; }