Facility_C.C.
/*版权2019,Gurobi Opt狗万app足彩imization,LLC*//*设施位置:一家公司目前将其产品从5个工厂运送到4个仓库。狗万滚球球它正在考虑关闭一些工厂以降低成本。为了将运输和固定成本降至最低,公司应关闭哪些工厂?根据前线系统的一个示例:http://www.solver.com/disfacility.htm 经许可使用。*/#include#include#include#include“gurobi_c.h”#定义opencol(p)p#定义transportcol(w,p)nPlants*(w+1)+p#定义MAXSTR 128 int main(int argc,char*argv[]{G手机万博登录RBenv*env=NULL;GRBmodel*model=NULL;int error=0;int p,w,col;int*cbeg=NULL;int*cind=NULL;int idx,rowct;double*cval=NULL;double*rhs=NULL;char*sense=NULL;char vname[MAXSTR];int cnamect=0;char**cname=NULL;double maxFixed=-GRB_∞,sol,obj;/*工厂和仓库数量*/const int nPlants=5;const int nWarehouses=4;/*以千台为单位的仓库需求*/double demand[]={15,18,14,20};/*以千台为单位的工厂产能*/double产能[]={20,22,17,19,18}59;/*创建环境*/error=GRBloadenv(&env,“facility.log”);if(error)退出;/*创建初始模型*/error=GRBnewmodel(env,&model,“facility”,nPlants*(nWarehouses+1),NULL,NULL,NULL,NULL,NULL;if(error)退出;/*为工厂开放变量初始化决策变量*/for(p=0;pnWarehouses)?nPlants:nWarehouses;cbeg=malloc(sizeof(int)*rowct;如果(!cbeg)要退出;cind=malloc(sizeof(int)*(nPlants*(nWarehouses+1));如果(!cind)要退出;cval=malloc(sizeof(double)*(nPlants*(nWarehouses+1));如果(!cval)要退出;rhs=malloc(sizeof(double)*rowct);如果(!rhs)要退出;sense=malloc(sizeof(char)*rowct);如果(!sense)要退出;cname=oc(rowct,sizeof(char*);如果(!cname)退出;/*生产约束注意,如果工厂关闭,该限制将生产设置为零*/idx=0;对于(p=0;pmaxFixed){maxFixed=FixedCosts[p];}}}for(p=0;p 0.99) { printf("Plant %i open:\n", p); for (w = 0; w < nWarehouses; ++w) { error = GRBgetdblattrelement(model, "X", transportcol(w, p), &sol); if (error) goto QUIT; if (sol > 0.0001) { printf(" Transport %f units to warehouse %i\n", sol, w); } } } else { printf("Plant %i closed!\n", p); } } QUIT: /* Error reporting */ if (error) { printf("ERROR: %s\n", GRBgeterrormsg(env)); exit(1); } /* Free data */ free(cbeg); free(cind); free(cval); free(rhs); free(sense); for (p = 0; p < cnamect; ++p) { free(cname[p]); } free(cname); /* Free model */ GRBfreemodel(model); /* Free environment */ GRBfreeenv(env); return 0; }