- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
library(lpSolveAPI)
lprec1 <- make.lp(0,nrow(df)
add.constraint(lprec1, c(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,0,0,0,0,0,0), "<=", as.numeric(ads1))
add.constraint(lprec1, 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,0,0,0,0,0), "<=", as.numeric(ads2))
add.constraint(lprec1, c(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,0,0,0,0), "<=", as.numeric(ads3))
add.constraint(lprec1, c(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,0,0,0), "<=", as.numeric(ads4))
add.constraint(lprec1, c(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,0,0), "<=", as.numeric(ads5))
add.constraint(lprec1, c(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,0), "<=", as.numeric(ads6))
add.constraint(lprec1, c(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), "<=", as.numeric(ads7))
add.constraint(lprec1, c(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), "<=", as.numeric(ads8))
add.constraint(lprec1, c(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), "<=", as.numeric(ads9))
add.constraint(lprec1, c(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), "<=", as.numeric(ads10))
add.constraint(lprec1, c(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), "<=", as.numeric(ads11))
add.constraint(lprec1, c(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), "<=", as.numeric(ads12))
add.constraint(lprec1, c(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), "<=", as.numeric(ads13))
add.constraint(lprec1, c(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), "<=", as.numeric(ads14))
add.constraint(lprec1, c(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), "<=", as.numeric(ads15))
add.constraint(lprec1, c(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), "<=", as.numeric(ads16))
add.constraint(lprec1, c(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), "<=", as.numeric(ads17))
add.constraint(lprec1, c(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), "<=", as.numeric(ads18))
add.constraint(lprec1, c(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), "<=", as.numeric(ads19))
add.constraint(lprec1, c(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), "<=", as.numeric(ads20))
add.constraint(lprec1, c(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), "<=", as.numeric(ads21))
add.constraint(lprec1, c(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), "<=", as.numeric(ads22))
add.constraint(lprec1, c(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), "<=", as.numeric(ads23))
add.constraint(lprec1, c(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), "<=", as.numeric(ads24))
add.constraint(lprec1, c(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), "<=", as.numeric(ads25))
add.constraint(lprec1, c(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), "<=", as.numeric(ads26))
add.constraint(lprec1, c(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), "<=", as.numeric(ads27))
add.constraint(lprec1, c(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,0,1,0,0,0,0,0), "<=", as.numeric(ads28))
add.constraint(lprec1, c(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,0,0,1,0,0,0,0), "<=", as.numeric(ads29))
add.constraint(lprec1, c(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,0,0,0,1,0,0,0), "<=", as.numeric(ads30))
add.constraint(lprec1, c(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,0,0,0,0,1,0,0), "<=", as.numeric(ads31))
add.constraint(lprec1, c(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,0,0,0,0,0,1,0), "<=", as.numeric(ads32))
add.constraint(lprec1, c(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,0,0,0,0,0,0,1), "<=", as.numeric(ads33))
solve(lprec1)
print(get.variables(lprec1))
print(get.objective(lprec1))
我将我所有的ads 向量都变成了一个单一的data.frame df$ads,有什么方法可以转换这个矩阵吗?我尝试使用
add.constraint(lprec1, diag(nrow (df))), "<=", as.vector(df$ads))
solve(lprec1)
但 lpSolveAPI 认识到长度不同:add.constraint(lprec1, diag(nrow(df), "<=", df$ads) 中的错误: ‘xt’的长度不等于模型中决策变量的数量但是有33个决策变量,nrow(df)是33...有什么方法可以二值化而不必制作这个矩阵吗?
length(diag(nrow(df))) = 361
长度大小不一,有没有办法把这些向量变成一个长度为33的data.frame?
最佳答案
这是一个选项,我们在其中创建向量的 list
,遍历 list
的序列并分配约束
a <- as.vector(diag(5))
lst1 <- asplit(matrix(a, ncol = 5, byrow = TRUE), 1)
library(lpSolveAPI)
lprec1 <- make.lp(0, length(lst1))
ads <- c(0, 5, 1, -1, 0)
for(i in seq_along(lst1)) add.constraint(lprec1, lst1[[i]], "<=", ads[i])
solve(lprec1)
#[1] 2
关于r - 如何将二进制矩阵转换为 R 中的 data.frame? lpSolveAPI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59491515/
我在 RStudio 中使用 lpsolveAPI。当我输入一个决策变量很少的模型名称时,我可以读取模型中当前约束的打印输出。例如 > lprec Model name: COL
我正在尝试在 R 中构建逻辑回归模型,并且我也在使用“safeBinaryRegression”包。当我输入我的型号代码并按回车键时,我收到以下消息: 分隔符错误(X,Y,目的=“查找”): lpSo
我正在尝试深入优化,并且一直在摆弄 lpSolveAPI 包。示例将演示我的简单设置。 数据(每行保存不同的变量): dput(test.df) structure(list(objectiv
library(lpSolveAPI) lprec1 <- make.lp(0,nrow(df) add.constraint(lprec1, c(1,0,0,0,0,0,0,0,0,0,0,0,
可重现示例: 我描述了一个简单的0/1-Knapsack lpSolveAPI 的问题在 R ,它应该返回 2 个解决方案: library(lpSolveAPI) lp_model= make.lp
我是一名优秀的程序员,十分优秀!