1

我试图在 R 中使用lpsolve和解决以下运输问题。我在使用.genalggenalg

            Boston  New York    Supply
---------------------------------------  
Detroit     30      20          200  
Pittsburgh  40      10          100
---------------------------------------  
Demand      150     150 

这是问题陈述的链接,以防上述问题不清楚。 http://support.sas.com/documentation/cdl/en/ormpug/66107/HTML/default/viewer.htm#ormpug_optmodel_sect005.htm

代码使用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))

当我从上面的模型中得出最佳解决方案时,它并没有提供正确的数字。我曾尝试更改迭代次数,但没有帮助。

solution =c(37.3445626697503, 108.823138475418, 124.833289347589, 1.39844713266939)  
sum(solution)  
total_cost = solution%*%dataset$cost  
print(total_cost)

答案应该是

Total Cost = 6500 , Units Shipped = 300  as ( 150 , 50, 0 , 100)

请帮忙。

4

1 回答 1

0

你提到你正在使用 lpSolve。您知道 lpSolve 有专门的交通问题接口吗?加载 lpSolve 并查看? lp.transport.

这是您的示例问题的实现:

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

与 SAS 结果匹配。

于 2013-10-23T16:25:38.557 回答