【问题标题】:Maximizing/optimizing a function with constraints in r在 r 中最大化/优化具有约束的函数
【发布时间】:2019-06-26 20:42:25
【问题描述】:

假设我有这个数据框。

library(dplyr)
library(lpSolve)
a <- seq(from=-0.5, to=0.5, by=.01)
success_rate <- sample(a, size=945, replace=TRUE)
attempts <- sample(1:800, 945, replace = TRUE)
df <- data.frame(success_rate,attempts)

我想找到最大化以下函数的 X1 和 X2:

{X1*df$attempts[1] + X2*df$success_rate[1]}/{X1*max(df$attempts) + X2*max(df$success_rate)}

然而,最大化应该在这些约束下完成:

对于数据帧内的所有观察,函数的结果必须为

我使用 lpsolve 来解决它,但它总是给我 1 和 0 作为解决方案,即使我指定 Xs 不能等于 0。这是我所拥有的:

df$success_rate <- (df$success_rate-min(df$success_rate))/(max(df$success_rate)-min(df$success_rate))
df$attempts <- (df$attempts-min(df$attempts))/(max(df$attempts)-min(df$attempts))

aux <- cbind(inputs, outputs)
f.obj <- c(df2$pass_snapsscale[2], df2$pass_perscale[2])
f.con <- rbind(aux, c(1,1), c(1,1), c(0,1), c(1,0))
f.dir <- c(rep("<=",946),">=",">", ">" )
f.rhs <- c(rep(1,946),0,0,0)
model <- lp ("max", f.obj, f.con, f.dir, f.rhs, compute.sens = TRUE)

我得到的是:

Success: the objective function is 0.753689 

df$attempts[1] 的值是多少。有没有其他方法可以最大化这个功能,或者你能指出我的代码有什么问题吗?

【问题讨论】:

    标签: r function dataframe optimization


    【解决方案1】:

    您似乎正在尝试使用线性编程库来解决非线性问题。尝试重新表述文档中提出的问题。 http://lpsolve.sourceforge.net/5.5/ 希望能帮助到你。

    【讨论】:

    • 谢谢。你有什么推荐的包来为非线性函数做这个吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 2018-05-03
    • 2012-12-08
    • 2018-10-20
    相关资源
    最近更新 更多