我试图在 R 中使用lpsolve
和解决以下运输问题。我在使用.genalg
genalg
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)
请帮忙。