【问题标题】:maximizing with two functions in R [closed]在R中使用两个函数最大化[关闭]
【发布时间】:2017-10-13 02:27:17
【问题描述】:

我想找到一个受此情景马氏距离限制限制的最大经济压力情景。为此,我必须在优化中考虑两个功能。

为了更容易,我们可以处理一个简化问题:我们有一个简单的线性模型:y=a+bx。为此,我想最小化:sum(a+bx-y)^2。而且,我有例如限制:(ab*5)/2<30

用excel求解器计算这个问题不是问题。但是,我如何在 r 中得到这个?

【问题讨论】:

  • 到目前为止您尝试过什么? SO 不是“请为我编写代码”网站。你应该看看制作一个好的MRE
  • 也许发布 Excel Solver 的步骤以及您到目前为止尝试了什么,然后人们才可能开始贡献
  • StackOverflow 不是家庭作业网站,请阅读stackoverflow.com/help/how-to-ask
  • 在像 maxLik 这样的 r 中的所有优化函数中,都可以将参数限制为上限或下限。例如,像 x1+x2+x3>=2。但不幸的是,我没有找到一个可以像我的问题中的简单示例那样以更复杂的方式限制参数的函数。够了,如果有人见过这样的功能并能给我提示。

标签: r optimization


【解决方案1】:

您可以尝试将约束合并到目标函数中,像这样

# example data whose exact solution lies outside the constraint
x <- runif(100, 1, 10)
y <- 3 + 5*x + rnorm(100, mean=0, sd=.5) 

# big but not too big
bigConst <- sum(y^2) * 100

# if the variables lie outside the feasible region, add bigConst
f <- function(par, x, y) 
    sum((par["a"]+par["b"]*x-y)^2) + 
    if(par["a"]*par["b"]>12) bigConst else 0

# simulated annealing can deal with non-continous objective functions
sol <- optim(par=c(a=1, b=1), fn=f, method="SANN", x=x, y=y)

# this is how it looks like
plot(x,y)
abline(a=sol$par["a"], b=sol$par["b"])

【讨论】:

    猜你喜欢
    • 2019-06-12
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多