【发布时间】:2021-04-16 12:47:58
【问题描述】:
我正在尝试解决以下优化问题, 我想最小化距离 (log(1+exp(x))-ax^2-bx-c)^2,关于 x 周围区域中的一些参数 a,b,c,但也要满足不等式对于所有 x,log(1+exp(x))
【问题讨论】:
标签: r
我正在尝试解决以下优化问题, 我想最小化距离 (log(1+exp(x))-ax^2-bx-c)^2,关于 x 周围区域中的一些参数 a,b,c,但也要满足不等式对于所有 x,log(1+exp(x))
【问题讨论】:
标签: r
1/ 情况 1(a , b , c 是常量):
library(nloptr)
fn <- function(x,a=1,b=1,c=1) {
(log(1+exp(x))-a*x^2-b*x-c)^2
}
fn(1)
eval_g0 <-function( x, a=1,b=1,c=1 ) { return( log(1+exp(x))-a*x^2-b*x-c) }
eval_g0(0)
a=1
b=1
c=1
res1 <-nloptr(x0=c(10),eval_f=fn,lb =c(-Inf),ub =c(Inf),eval_g_ineq =eval_g0,opts =list("algorithm"="NLOPT_LN_COBYLA","xtol_rel"=1.0e-8),a =a,b =b ,c=c)
res1
这会输出以下内容:.
[1] 2.845086
[1] -0.3068528
Call:
nloptr(x0 = c(10), eval_f = fn, lb = c(-Inf), ub = c(Inf), eval_g_ineq = eval_g0,
opts = list(algorithm = "NLOPT_LN_COBYLA", xtol_rel = 1e-08),
a = a, b = b, c = c)
Minimization using NLopt version 2.4.2
NLopt solver status: 4 ( NLOPT_XTOL_REACHED: Optimization stopped because
xtol_rel or xtol_abs (above) was reached. )
Number of Iterations....: 42
Termination conditions: xtol_rel: 1e-08
Number of inequality constraints: 1
Number of equality constraints: 0
Optimal value of objective function: 0.0554408002852988
Optimal value of controls: -0.2854395
要了解事情的运作方式,您可以查看:
https://cran.r-project.org/web/packages/nloptr/vignettes/nloptr.pdf
2/ 案例 2 x, a, b , c 是变量:
如果a,b,c也是变量,只需使用向量x作为入口:
library(nloptr)
fn <- function(x) {
a=x[2]
b=x[3]
c=x[4]
return((log(1+exp(x[1]))-a*(x[1])^2-b*(x[1])-c)^2)
}
eval_g0 <-function(x) {
a=x[2]
b=x[3]
c=x[4]
return((log(1+exp(x[1]))-a*(x[1])^2-b*(x[1])-c)^2) }
res1 <-nloptr(x0=rep(18,4),eval_f=fn,lb =rep(-Inf,4),ub =rep(Inf,4),eval_g_ineq =eval_g0,opts =list("algorithm"="NLOPT_LN_COBYLA","xtol_rel"=1.0e-8))
res1
在这里您可以通知x[1](它是向量x 的第一个元素)只是您的主函数变量x 的f(x,a,b,c) 的4 variables。输出如下:
Number of Iterations....: 100
Termination conditions: xtol_rel: 1e-08
Number of inequality constraints: 1
Number of equality constraints: 0
Current value of objective function: 2.47413071040375e-09
Current value of controls: -1.977346 6.058104 19.29975 14.60534
pb的解决方法是:x=-1.977346, a=6.058104 , b=19.29975 , c=14.60534
让我们验证约束g:
print(c("the constraint value for x is : "))
eval_g0(c(x=-1.977346, a=6.058104 , b=19.29975 , c=14.60534))
[1] "the constraint value for x is : "
x
1.232363e-09
这里通常,我们应该期望约束值的负数,而不是接近0 的非常小的数字。但是,正如nloptr documentation 第 12 页所述:
" 点 x 被认为是可行的 用于判断是否停止优化 if eval_g_ineq(x) "
在我们的例子中,我们有eval_g_ineq(x)= 1.232363e-09 <= tol=1e-08。这意味着事情进展顺利,我们(从数字上讲)在可行性范围内。
3/ 另一个约束优化问题:
我们将解决另一个受限问题。在$ 1-x <= 0 $ 下最小化$ f= x^2 $。约束等效于 $ 1 <= x $ 并且目标函数在 [1, + inf [ 中增加。我们将简单地将x*=1 作为约束问题的最佳值。让我们检查一下:
library(nloptr)
fn <- function(x) {
return(x^2)
}
eval_g0 <-function(x) {
return(1-x)
}
res1 <-nloptr(x0=rep(14),eval_f=fn,eval_g_ineq =eval_g0,opts =list("algorithm"="NLOPT_LN_COBYLA","xtol_rel"=1.0e-8))
res1
print(c("the constraint value for x is : "))
eval_g0(0.999999902845997)
这给出了以下内容:
Call:
nloptr(x0 = rep(14), eval_f = fn, eval_g_ineq = eval_g0, opts = list(algorithm = "NLOPT_LN_COBYLA",
xtol_rel = 1e-08))
Minimization using NLopt version 2.4.2
NLopt solver status: 4 ( NLOPT_XTOL_REACHED: Optimization stopped because
xtol_rel or xtol_abs (above) was reached. )
Number of Iterations....: 18
Termination conditions: xtol_rel: 1e-08
Number of inequality constraints: 1
Number of equality constraints: 0
Optimal value of objective function: 0.999999902845997
Optimal value of controls: 1
[1] "the constraint value for x is : "
[1] 9.7154e-08
【讨论】:
a , b , c 值。这个函数不需要雅可比的计算,因为它需要一些努力!
x (即变量数)。请点击对勾接受答案!
a,b,c也是变量而不是常量的情况!