首页 > 解决方案 > 在 R 中使用 solnp 进行优化的问题

问题描述

我遇到以下问题:

我想使用 solnp() 相对于约束 B 最大化函数 U

我将 U 函数定义为:

U = function(x){ 
  #P is a vector of parameters
  #X is a vector of variables
  -P[1]*(x[1]^P[2])*(x[2]^P[3])
}

函数B如下:

B = function(x){ 
  #M is a vector of parameters
  #X is a vector of variables
  #The constant M[3] is an argument of CCM
  -M[1]*x[1]-M[2]*x[2]
}

然后我以这种方式将优化器函数定义为 solnp() 的覆盖:

CCM = function(P,M){
  
  solnp(c(0,0), #starting values (random - obviously need to be positive and sum to 15)
        U, #function to optimise
        eqfun=B, #equality function 
        eqB=-M[3],   #the equality constraint
        LB=c(0,0), #lower bound for parameters i.e. greater than zero
        UB=c(10000,10000)) #upper bound for parameters (I just chose 10000 randomly)
  
}

我编写了一个优化函数来为这个问题选择不同的参数。

这里的问题是,当我运行该CCM函数时,它似乎没有最大化(给我 (0,0) 作为解决方案),solnp()默认情况下最小化,所以我改变了UB因此的符号。

顺便说一句,我正在尝试编写一个消费者选择模型。


编辑:

使用OP 注释中的值进行测试。

P <- c(2,0.1,0.9)
M <- c(1,2,15)
CCM(P, M) 
#
#solnp-->The linearized problem has no feasible
#solnp-->solution.  The problem may not be feasible.
#
#Iter: 1 fn: 0       Pars:  0       0      
#solnp--> Solution not reliable....Problem Inverting Hessian.
#$pars
#[1] 0 0
#
#$convergence
#[1] 2
#
#$values
#[1] 0 0
#
#$lagrange
#[1] 0
#
#$hessian
#     [,1] [,2]
#[1,]    1    0
#[2,]    0    1
#
#$ineqx0
#NULL
#
#$nfuneval
#[1] 7
#
#$outer.iter
#[1] 1
#
#$elapsed
#Time difference of 0.6487024 secs
#
#$vscale
#[1] 1.0e-08 1.5e+01 1.0e+00 1.0e+00
#

CCM(P, M)$convergence
#
#solnp-->The linearized problem has no feasible
#solnp-->solution.  The problem may not be feasible.
#
#Iter: 1 fn: 0       Pars:  0       0      
#solnp--> Solution not reliable....Problem Inverting Hessian.
#[1] 2

标签: roptimization

解决方案


推荐阅读