r - 在 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()
默认情况下最小化,所以我改变了U
和B
因此的符号。
顺便说一句,我正在尝试编写一个消费者选择模型。
编辑:
使用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
解决方案
推荐阅读
- wordpress - Wordpress API - 访问页面字段
- c++ - 自动转换为无效未使用的变量 C++
- android - 将 CSV 文件从 Android 设备传输到 PC
- android - 当我更新 ListView 上的列表时,Flutter 页面不会重新加载
- java - Intellij java.lang.NoSuchFieldError 更新后启动
- google-cloud-platform - 将 Google Cloud Bucket 下载到本地计算机上的特定文件夹
- git - `ls-remote --refs` 与克隆然后 `describe --tags` 有什么关系?
- plugins - Terraform 与插件不兼容的 API 版本。插件版本:5,核心版本:4
- powershell - 从另一个脚本运行 PowerShell 脚本,传递字符串变量
- azure-databricks - 我可以在不使用管道的情况下从 Azure Synapse 查询 SQL Server 数据库吗?