facility.R


# 2023年版权,Gurobi优化狗万app足彩,LLC # #设施地点:公司目前船舶产品从5植物# 4仓库。狗万滚球球它正在考虑关闭一些工厂降低#成本。什么植物(s)公司应关闭,为了最小化#运输和固定成狗万滚球球本?# #注意到,这个示例使用列表而不是字典。自#它不处理稀疏数据,列表是一个合理的选择。# #从前线系统基于一个例子:http://www.solver.com/disfacility.htm # #使用许可。手机万博登录库(矩阵)库(gurobi) #定义原始数据nPlants < - 5 nWarehouses < - 4 #仓库需求在成千上万的单位需求< - c(14日,15日,18日20)#工厂生产能力在成千上万的单位容量< - c(18) 20日,22日,17日,19日#固定成本为每个工厂FixedCosts < - c(12000、15000、17000、13000、16000) #运输成本每千单位TransCosts < - c (4000, 2000, 3000, 2500, 4500, 2500, 2600, 3400, 3000, 4000, 1200, 1800, 2600, 4100, 3000, 2200, 2600, 3100, 3700, 3200) flowidx < -函数(w、p) {nPlants * (w1) + p} #构建模型模型< -列表()模型美元modelname < -“设施”模型美元modelsense < -“min”#变量初始化数据模型美元磅< - 0模型美元乌兰巴托< - c(代表(1,nPlants),代表(Inf, nPlants * nWarehouses))模型美元vtype < - c(代表(“B”, nPlants),代表(“c”, nPlants * nWarehouses))模型美元obj < - c (FixedCosts TransCosts)模型美元varnames < - c (paste0(代表(“开放”,nPlants), 1: nPlants), sprintf(反式% d % d, c (mapp(代表,1:nWarehouses, nPlants)), 1: nPlants)) #构建生产约束矩阵A1 < - spMatrix (nPlants nPlants,我= c (1: nPlants), j = (1: nPlants), x =能力)A2 < - spMatrix (nPlants nPlants * nWarehouses,我= c (mapp(代表,1:nPlants, nWarehouses)), j = mapp (flowidx, 1: nWarehouses, c (mapp(代表,1:nPlants, nWarehouses))), x =代表(1,nWarehouses * nPlants) A3 < - spMatrix (nWarehouses nPlants) A4 < - spMatrix (nWarehouses nPlants * nWarehouses,我= c (mapp(代表,1:nWarehouses, nPlants)), j = mapp (flowidx c (mapp(代表,1:nWarehouses, nPlants)), 1: nPlants), x =代表(1,nPlants * nWarehouses))模型美元< - rbind (cbind (A1, A2) cbind (A3、A4))模型美元rhs < - c(代表(0,nPlants),需求)模型美元< - c(代表(“<”感觉,nPlants),代表(“=”,nWarehouses))模型美元constrnames < - c (sprintf(的能力% d ', 1: nPlants), sprintf(的需求% d ', 1: nWarehouses)) # gurobi_write保存模型(模型、facilityR.lp) #猜测的起点:关闭工厂固定#成本最高的;打开所有其他人第一次打开所有的植物模型美元开始< - c(代表(1,nPlants),代表(NA, nPlants * nWarehouses) #找到最昂贵的植物,并关闭它在mipstart猫(“初始猜测:\ n”) worstidx < - which.max (FixedCosts)模型美元(worstidx) < - 0开始猫(关闭工厂,worstidx, ' \ n ') #设置参数参数< -()参数列表美元< - 2 #方法优化res < - gurobi(模型、参数)#如果(res打印解决方案美元状态= = '最佳'){猫(‘\ nTotal成本:’,res美元objval, ' \ nsolution: \ n”)猫(“设施:”模型美元varnames [(res美元x (1: nPlants) > 1 e-5)], ' \ n ') <——nPlants + (res活跃美元x [(nPlants + 1): (nPlants * (nWarehouses + 1))) > 1 e-5)猫的流动:猫(sprintf (“% s = % g”模型美元varnames[活动],res美元x[活动]),“\ n”)其他rm(活动)}{猫(无解\ n)} #腾出rm (res,模型、参数、A1, A2, A3、A4)