无法在R中使用genalg解决运输优化
我试图使用R中的无法在R中使用genalg解决运输优化,r,R,我试图使用R中的lpsolve和genalg解决以下运输问题。我使用genalg面临一些问题 Boston New York Supply --------------------------------------- Detroit 30 20 200 Pittsburgh 40 10 100 --------------------------------------- Demand
lpsolve
和genalg
解决以下运输问题。我使用genalg
面临一些问题
Boston New York Supply
---------------------------------------
Detroit 30 20 200
Pittsburgh 40 10 100
---------------------------------------
Demand 150 150
如果上述问题不清楚,这里是问题陈述的链接。
使用genalg
编码:
library(genalg)
dataset <- data.frame(arc = c("det_bos", "det_new", "pit_bos",
"pit_new"), cost = c(30, 20, 40, 10), supply_1 = c(1,1,0,0),
supply_2 = c(0,0,1,1),demand_1 = c(1,0,1,0),demand_2 = c(0,1,0,1))
supply_1_limit <- 200
supply_2_limit <- 100
demand_1_limit <- 150
demand_2_limit <- 150
evalFunc <- function(arc=c()) {
current_solution_cost <- arc%*%dataset$cost
current_solution_supply_1 <- arc%*%dataset$supply_1
current_solution_supply_2 <- arc%*%dataset$supply_2
current_solution_demand_1 <- arc%*%dataset$demand_1
current_solution_demand_2 <- arc%*%dataset$demand_2
if (current_solution_supply_1 < supply_1_limit |
current_solution_supply_2 < supply_2_limit |
current_solution_demand_1 > demand_1_limit |
current_solution_demand_2 > demand_2_limit)
return(0) else return(current_solution_cost)}
GAmodel <- rbga(stringMin=c(0,0,0,0),
stringMax=c(150,150,150,150),
suggestions=NULL,popSize=200,
iters=100, mutationChance=0.10,elitism=T,
monitorFunc=NULL,evalFunc=evalFunc,showSettings=FALSE, verbose=FALSE)
cat(summary.rbga(GAmodel))
答案应该是
Total Cost = 6500 , Units Shipped = 300 as ( 150 , 50, 0 , 100)
请帮忙 您提到您正在使用lpSolve。你知道lpSolve有一个专门的接口来解决交通问题吗?加载lpSolve并查看
?lp.运输
下面是您的示例问题的实现:
library(lpSolve)
cost.mat <- matrix(c(30,20,40,10), nrow=2,byrow=TRUE)
row.signs <- c("=","=")
col.signs <- c("=","=")
col.rhs <- c(150,150)
row.rhs <- c(200,100)
sol<-lp.transport (cost.mat, direction="min", row.signs, row.rhs,
col.signs, col.rhs, presolve=0, compute.sens=0, integers = 1:4 )
与SAS结果相符。感谢您的回复。我们已经用lpsolve解决了这个问题。现在,我们正在尝试使用元启发式来解决它,从而尝试genalg的方法。如果可以使用genalg解决此问题,则会很有帮助。
library(lpSolve)
cost.mat <- matrix(c(30,20,40,10), nrow=2,byrow=TRUE)
row.signs <- c("=","=")
col.signs <- c("=","=")
col.rhs <- c(150,150)
row.rhs <- c(200,100)
sol<-lp.transport (cost.mat, direction="min", row.signs, row.rhs,
col.signs, col.rhs, presolve=0, compute.sens=0, integers = 1:4 )
> sol$solution
[,1] [,2]
[1,] 150 50
[2,] 0 100