manbet体育手机客户端


设施。R


# #解决经典的饮食模型,显示如何添狗万app足彩加约束#到现有的模型。库(矩阵)库(gurobi) #定义原始数据nPlants < - 5 nWarehouses < - 4 #仓库需求在成千上万的单位需求< - c(14日,15日,18日20)#工厂生产能力在成千上万的单位容量< - c(18) 20日,22日,17日,19日#固定成本为每个工厂FixedCosts < - c(12000、15000、17000、13000、#每千单位运输成本TransCosts <- c(4000, 2000, 3000, 2500, 4500, 2500, 2600, 3400, 3000, 4000, 1200, 1800, 2600, 4100, 3000, 2200, 2600, 3100, 3700, 3200) flowidx <- function(w, p) {nPlants * (w-1) + p} # Build model model <- list() modelModelname <- 'facility'模型Modelsense <- 'min' #初始化变量模型的数据Lb <- 0模型ub <- c(rep(1, nPlants), rep(Inf, nPlants * nrepositories))模型vtype <- c(rep('B', nPlants), rep(' c ', nPlants * nrepositories))模型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(rep(0, nPlants), Demand)模型感知<- c(rep('<', nPlants), rep('=', nrepositories))模型construcnames <- c(sprintf('Capacity%d',1:nPlants), sprintf('Demand%d',1: nrepositories)) #保存模型gurobi_write(model,'facilityR.lp') #在起始点猜测:关闭固定成本最高的工厂;先打开所有植物模型start <- c(rep(1,nPlants),rep(NA, nPlants * nwarehouse)) #找到最昂贵的植物,并关闭它在mipstart cat('Initial guess:\n') worstidx <-哪个.max(FixedCosts)模型start[worstidx] <- 0 cat('Closing plant',worstidx,'\n') #设置参数params <- list()参数方法<- 2 #优化res <- gurobi(模型,参数)#打印解决方案如果(resstatus == 'OPTIMAL') {cat('\nTotal Costs:',resobjval, ' \ nsolution: \ n”)猫(“设施:”模型varnames [(resx[1:nPlants]>1e-5)], '\n')活动的<- nPlants + which(resx [(nPlants + 1): (nPlants * (nWarehouses + 1))) > 1 e-5)猫的流动:猫(sprintf(“% s = % g”模型varnames[活动],resx[active]), '\n') rm(active)} else {cat('No solution\n')} #清除空间rm(res, model, params, A1, A2, A3, A4)