在 R 中使用 lpsolve 最小化目标函数问题



我正在解决一个简单的线性优化问题。我有一堆作物,每吨土地需求和每吨水足迹都有相关的土地需求。我想知道每种作物需要多少吨,以尽量减少水足迹。对于第一次运行,我将创建一个仅具有土地约束的线性优化。土地必须等于 2958700。但是,当我运行代码时,我的解决方案对所有作物都说"0"。有人可以帮忙吗?

land<-c(0.03448276,0.09090909,0.06346154,0.25099602,0.26731171)
water_footprint<-c(2990.3, 5980.1, 31679.5,8802.3,16404.3)
crop_name<-c("banana","guava","mango","maize","rice")
crop_dataframe<-data.frame(crop_name,land,water_footprint)
const.mat<-land
const.dir<-c("=")
const.rhs<-2958700
library(lpSolve)
min_waterfootpint<-lp(direction = "min", water_footprint, const.mat, const.dir, const.rhs)$solution
paste(min_waterfootpint)

我认为你应该处理矩阵land作为lp的输入,即

const.mat<-t(matrix(land))

然后你会得到

> paste(min_waterfootpint)
[1] "0"                "0"                "0"                "11787836.3170858"
[5] "0"  

最新更新