简体   繁体   English

在 R 中编写线性程序在印刷上最安全的方法是什么?

[英]What is the most typographically safe way to write a linear program in R?

In R, linear programming using the lpsolve library requires an extensive amount of manually typed values for the needed matrices and vectors for the objective function coefficients, left-hand side coefficients, constraints, etc. Messing up even one value or one comma will make the script error or worse, the program will find a solution but it will be the wrong one due to incorrect setup.在 R 中,使用lpsolve库的线性规划需要为目标 function 系数、左侧系数、约束等所需的矩阵和向量手动键入大量值。即使弄乱一个值或一个逗号也会使脚本错误或更糟的是,程序会找到解决方案,但由于设置不正确,这将是错误的解决方案。 For large, real-world problems like.network flow, simply typing the program itself is time prohibitive.对于像网络流量这样的大型现实问题,简单地输入程序本身是非常耗时的。 What am I missing about R's capabilities in this space?关于 R 在这个领域的功能,我缺少什么? Or, is there an alternate tool better fit for the job?或者,是否有更适合这项工作的替代工具? Open source preferred due to budget.由于预算,首选开源。

Here is an example of the type of code needed in R for a relatively simple optimization problem:下面是 R 中一个相对简单的优化问题所需代码类型的示例:

# fleet size optimization, with an added computation of total miles driven
library(lpSolve)

# Objective function coefficients
ObjCoeff<-c(1300, 690, 421.5, 531, 690, 427.50, 277.50, 421.5, 427.50, 303, 531, 277.50, 303,
            460, 281,   354, 460, 285,    185,    281,   285,    202, 354, 185,    202, 0)

# Constraint matrix            
Amatrix<-matrix(c(0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  
                  0, 1, 1, 1, -1, 0, 0, -1, 0, 0, -1, 0, 0, 1, 1, 1, -1, 0, 0, -1,  0, 0, -1, 0, 0,0,
                  0, -1,0, 0,  1, 1, 1,  0, -1, 0, 0,-1, 0,-1, 0, 0,  1, 1, 1,  0, -1, 0, 0, -1, 0,0,
                  0, 0,-1, 0,  0,-1, 0,  1,  1, 1, 0, 0,-1, 0,-1, 0, 0, -1, 0,  1,  1, 1, 0,  0,-1,0,
                  0, 0, 0, -1, 0, 0,-1,  0,  0,-1, 1, 1, 1, 0, 0,-1, 0,  0,-1,  0,  0,-1, 1,  1, 1,0,
                  -1300, 460, 281,354,460,285,185,281,285,202,354,185,202,460,281,354, 460, 285,185, 281, 285,202, 354, 185, 202, 0,
                  0, 460, 281,354,460,285,185,281,285,202,354,185,202,460,281,354, 460, 285,185, 281, 285,202, 354, 185, 202,-1), nrow=18, byrow=TRUE)

# Right hand side constraint vector                  
Bvector<-c(10, 10, 10, 20, 10, 10, 10, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0,0 )

# Constraint inequality direction vector
constrainttype<- c(">=", ">=",">=", ">=",">=", ">=",">=", ">=",">=", ">=",">=", ">=","=", "=" , "=" , "=" ,"<=", "=" )

# Solve the specified integer program by setting all.int=TRUE
optimum<-lp(direction="min", objective.in=ObjCoeff, const.mat = Amatrix, const.dir = constrainttype, const.rhs = Bvector, all.int =TRUE)

# Print constraint matrix to verify it was specified correctly
print(optimum$constraints)

# Check to see if the solver reached optimality (0 means yes)
print(optimum$status)

# Print values of each variable in the optimal solution
# note that they are all integer valued
print(optimum$solution)

# Print the optimal objective function value
print(optimum$objval)

R has its strengths for sure, but formulation of non-trivial LP's is not one of them. R 肯定有其优势,但非平凡 LP 的制定不是其中之一。

There are several other open source frameworks that are much more expressive.还有其他几个更具表现力的开源框架。 If you are a python user, you can pick from many including:如果您是python用户,您可以从许多选项中进行选择,包括:

pyomo, gekko, cvxpy, pulp, or-tools and others I'm sure. pyomo, gekko, cvxpy, pulp, or-tools和其他我确定。

I'm a fan of pyomo for model building, but it requires installation of a separate solver, which isn't too difficult.我是 model 建筑的pyomo的粉丝,但它需要安装一个单独的求解器,这并不难。 There are several open-source free solvers that are excellent and in common use with different capabilities such as cbc, glpk, ipopt and others, and of course many excellent licensed solvers.有几个开源的免费解算器,它们非常出色并且具有不同的功能,例如cbc, glpk, ipopt等,当然还有许多优秀的许可解算器。

If you want to start with an "all in one" the pulp framework includes a solver with the build--I forget which one.如果你想从“一体式”开始, pulp框架包括一个带有构建的求解器——我忘了是哪个。

There are many examples on this site for all the pkgs above if you search by tag.如果您按标签搜索,此站点上有许多上述所有 pkg 的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 R 中粘贴字符串的最有效方法是什么? - What is the most efficient way to paste strings in R? 时间序列:为子集编写代码的最有效方法是什么? - Time series: What's the most efficient way to write code for subsets? 在R中分区和访问数据帧行的最有效方法是什么? - What's the most efficient way to partition and access dataframe rows in R? 用 R 计算季节性平均值的最优雅方法是什么? - What is the most elegant way to calculate seasonal means with R? 这是否真的是最实用的方法来返回R中线性模型(lm)对象的p值? - Is this really the most practical way to return the p-value of a linear model (lm) object in R? 有什么更好的方法在R中编写此嵌套的for循环? - What is a better way to write this nested for loop in R? 在 R 中删除数据表中的空列的最有效方法是什么 - What is the most efficient way to remove empty columns in a datatable in R 检查 R 中缺失数据模式的最优雅方法是什么? - what is the most elegant way to check for patterns of missing data in R? 在 R 中从 netcdf 创建栅格的最准确方法是什么? - What is the most accurate way of creating a raster from netcdf in R? 将多个函数映射到 R 中的字符串向量的最有效方法是什么? - What is the most efficient way of mapping multiple functions to a vector of strings in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM