LPSolve with R - 多个数据集作为输入
LPSolve with R - Multiple datasets as input
我正在使用 R 处理 LPSolve,我的输入数据是多个 CSV 文件的形式,每个文件都有一个 table。下面提到了 table 中的 2 个:
约束说明 -
- 有从每个制作室出发的路线
- 生产厂的总流出量 = 源自它的路线的总和(路线量)
- 生产车间总流出量 <= 生产能力
- Route Volume 本身就是一个决策变量,它依赖于本post
中未提及的其他变量
约束的数学表示如下:
`Production Total Outflow = ∑(Route Volume) where (Production House ID from table_1)==(Originating from Prod House ID from table_2)`
Production Total Outflow <= Production Capacity
实际上,我有几千行。我尝试为上述 2 个约束编写以下代码。将有 2 个约束条件:
#Reading Data from files
routeData = read.csv("Route.csv", header = TRUE)
ProductionData = read.csv("Production.csv", header = TRUE)
#Fetching variable columns
routeID = routeData$RouteID
productionID = ProductionData$ProductionID
productionCapacity = ProductionData$Supply.Capacity
numberOfColumns = length(routeID) + length(productionID) #4+2 decision variables
model <- make.lp(nrow=0, ncol=numberOfColumns, verbose="important")
for(i in 1:length(productionID)){
add.constraint(model, 1, "<=", productionCapacity[i]) #Something wrong here
}
#I haven't attempted to write the other constraint
我无法继续编写约束。请大家帮忙。我没有分享 objective 因为它还有很多其他限制。
这是一个尝试将路线量平均分配给制作公司的示例
library(lpSolveAPI)
prodcap <- setNames(c(50,100), c(1,2))
route <- data.frame(rid=1:4, pid_from=rep(1:2, each=2))
route_volume <- 125 # example
nvars <- nrow(route)+1 # example: evenly distribute production house output relative to capacity
lprec <- make.lp(0, nvars)
set.objfn(lprec, obj=1, indices=nvars)
# capacity constraints
for (i in seq(1, length(prodcap))) {
route_ids <- which(route[,"pid_from"]==i)
add.constraint(lprec, xt=rep(1, length(route_ids)), type="<=", rhs=prodcap[i], indices=route_ids)
}
# total outflow constraint
add.constraint(lprec, xt=rep(1, nrow(route)), type="=", rhs=route_volume, indices=seq(1, nvars-1))
# example: define the last decision variable as maximum flow over each production house
for (i in seq(1, length(prodcap))) {
route_ids <- which(route[,"pid_from"]==i)
add.constraint(lprec, xt=c(rep(1/prodcap[i], length(route_ids)), -1), type="<=", rhs=0, indices=c(route_ids, nvars))
}
# solve
status <- solve(lprec)
if(status!=0) stop("no solution found, error code=", status)
get.variables(lprec)[seq(1, nrow(route))]
#[1] 41.66667 0.00000 83.33333 0.00000
请注意,如果您有数以千计的路线/制作公司,那么在 make.lp
中预分配约束并使用 set.row
而不是 add.constraint
可能会更有效。这是一个例子,并且按照评论中的要求将 route_volume 作为附加决策变量:
library(lpSolveAPI)
prodcap <- setNames(c(50,100), c(1,2))
route <- data.frame(rid=1:4, pid_from=rep(1:2, each=2))
route_volume <- 125 # example
# the first nrow(route) vars are the outflows,
# then 1 variable for maximum flow (relative to capacity) over all production house
# then 1 last variable for the route volume
nvars <- nrow(route)+2
ncons <- 2*length(prodcap)+3
# pre-allocate the constraints
lprec <- make.lp(ncons, nvars)
# set objective: minimize maximum flow relative to capacity (example)
set.objfn(lprec, obj=1, indices=nvars-1)
# capacity constraints
rownum <- 1
for (i in seq(1, length(prodcap))) {
route_ids <- which(route[,"pid_from"]==i)
set.row(lprec, row=rownum, xt=rep(1, length(route_ids)), indices=route_ids)
set.rhs(lprec, prodcap[i], constraints=rownum)
rownum <- rownum + 1
}
# total outflow constraint ("=" resolves to two constraints)
set.row(lprec, row=rownum, xt=c(rep(1, nrow(route)), -1), indices=c(seq(1, nvars-2), nvars))
set.rhs(lprec, 0, constraints=rownum)
rownum <- rownum + 1
set.row(lprec, row=rownum, xt=c(rep(-1, nrow(route)), 1), indices=c(seq(1, nvars-2), nvars))
set.rhs(lprec, 0, constraints=rownum)
rownum <- rownum + 1
# additional constraint for route volume
set.row(lprec, row=rownum, xt=-1, indices=nvars)
set.rhs(lprec, -125, constraints=rownum) #example: route_volume >= 125
rownum <- rownum + 1
# example: define the second last decision variable as maximum flow (relative to capacity) over all production houses
# rhs is 0, which is preset
for (i in seq(1, length(prodcap))) {
route_ids <- which(route[,"pid_from"]==i)
set.row(lprec, row=rownum, xt=c(rep(1/prodcap[i], length(route_ids)), -1), indices=c(route_ids, nvars-1))
set.rhs(lprec, 0, constraints=rownum)
rownum <- rownum + 1
}
# solve
status <- solve(lprec)
if(status!=0) stop("no solution found, error code=", status)
get.variables(lprec)[seq(1, nrow(route))]
我正在使用 R 处理 LPSolve,我的输入数据是多个 CSV 文件的形式,每个文件都有一个 table。下面提到了 table 中的 2 个:
约束说明 -
- 有从每个制作室出发的路线
- 生产厂的总流出量 = 源自它的路线的总和(路线量)
- 生产车间总流出量 <= 生产能力
- Route Volume 本身就是一个决策变量,它依赖于本post 中未提及的其他变量
约束的数学表示如下:
`Production Total Outflow = ∑(Route Volume) where (Production House ID from table_1)==(Originating from Prod House ID from table_2)`
Production Total Outflow <= Production Capacity
实际上,我有几千行。我尝试为上述 2 个约束编写以下代码。将有 2 个约束条件:
#Reading Data from files
routeData = read.csv("Route.csv", header = TRUE)
ProductionData = read.csv("Production.csv", header = TRUE)
#Fetching variable columns
routeID = routeData$RouteID
productionID = ProductionData$ProductionID
productionCapacity = ProductionData$Supply.Capacity
numberOfColumns = length(routeID) + length(productionID) #4+2 decision variables
model <- make.lp(nrow=0, ncol=numberOfColumns, verbose="important")
for(i in 1:length(productionID)){
add.constraint(model, 1, "<=", productionCapacity[i]) #Something wrong here
}
#I haven't attempted to write the other constraint
我无法继续编写约束。请大家帮忙。我没有分享 objective 因为它还有很多其他限制。
这是一个尝试将路线量平均分配给制作公司的示例
library(lpSolveAPI)
prodcap <- setNames(c(50,100), c(1,2))
route <- data.frame(rid=1:4, pid_from=rep(1:2, each=2))
route_volume <- 125 # example
nvars <- nrow(route)+1 # example: evenly distribute production house output relative to capacity
lprec <- make.lp(0, nvars)
set.objfn(lprec, obj=1, indices=nvars)
# capacity constraints
for (i in seq(1, length(prodcap))) {
route_ids <- which(route[,"pid_from"]==i)
add.constraint(lprec, xt=rep(1, length(route_ids)), type="<=", rhs=prodcap[i], indices=route_ids)
}
# total outflow constraint
add.constraint(lprec, xt=rep(1, nrow(route)), type="=", rhs=route_volume, indices=seq(1, nvars-1))
# example: define the last decision variable as maximum flow over each production house
for (i in seq(1, length(prodcap))) {
route_ids <- which(route[,"pid_from"]==i)
add.constraint(lprec, xt=c(rep(1/prodcap[i], length(route_ids)), -1), type="<=", rhs=0, indices=c(route_ids, nvars))
}
# solve
status <- solve(lprec)
if(status!=0) stop("no solution found, error code=", status)
get.variables(lprec)[seq(1, nrow(route))]
#[1] 41.66667 0.00000 83.33333 0.00000
请注意,如果您有数以千计的路线/制作公司,那么在 make.lp
中预分配约束并使用 set.row
而不是 add.constraint
可能会更有效。这是一个例子,并且按照评论中的要求将 route_volume 作为附加决策变量:
library(lpSolveAPI)
prodcap <- setNames(c(50,100), c(1,2))
route <- data.frame(rid=1:4, pid_from=rep(1:2, each=2))
route_volume <- 125 # example
# the first nrow(route) vars are the outflows,
# then 1 variable for maximum flow (relative to capacity) over all production house
# then 1 last variable for the route volume
nvars <- nrow(route)+2
ncons <- 2*length(prodcap)+3
# pre-allocate the constraints
lprec <- make.lp(ncons, nvars)
# set objective: minimize maximum flow relative to capacity (example)
set.objfn(lprec, obj=1, indices=nvars-1)
# capacity constraints
rownum <- 1
for (i in seq(1, length(prodcap))) {
route_ids <- which(route[,"pid_from"]==i)
set.row(lprec, row=rownum, xt=rep(1, length(route_ids)), indices=route_ids)
set.rhs(lprec, prodcap[i], constraints=rownum)
rownum <- rownum + 1
}
# total outflow constraint ("=" resolves to two constraints)
set.row(lprec, row=rownum, xt=c(rep(1, nrow(route)), -1), indices=c(seq(1, nvars-2), nvars))
set.rhs(lprec, 0, constraints=rownum)
rownum <- rownum + 1
set.row(lprec, row=rownum, xt=c(rep(-1, nrow(route)), 1), indices=c(seq(1, nvars-2), nvars))
set.rhs(lprec, 0, constraints=rownum)
rownum <- rownum + 1
# additional constraint for route volume
set.row(lprec, row=rownum, xt=-1, indices=nvars)
set.rhs(lprec, -125, constraints=rownum) #example: route_volume >= 125
rownum <- rownum + 1
# example: define the second last decision variable as maximum flow (relative to capacity) over all production houses
# rhs is 0, which is preset
for (i in seq(1, length(prodcap))) {
route_ids <- which(route[,"pid_from"]==i)
set.row(lprec, row=rownum, xt=c(rep(1/prodcap[i], length(route_ids)), -1), indices=c(route_ids, nvars-1))
set.rhs(lprec, 0, constraints=rownum)
rownum <- rownum + 1
}
# solve
status <- solve(lprec)
if(status!=0) stop("no solution found, error code=", status)
get.variables(lprec)[seq(1, nrow(route))]