R语言 多不等式约束优化

o7jaxewo  于 2023-05-11  发布在  其他
关注(0)|答案(1)|浏览(164)

我在R上工作,我需要设置一个非线性优化问题,我的数据集有以下列:

  • Phs代码,明确定义每个药房
  • 分数,定义与每个药房关联的分数
  • MKT,定义了每家药店产生的市场价值

非线性最大化问题应包含:

  • 决策变量X(i)=投资于药房i的价值
  • 目标函数:MAX sum Y(i)其中``Y(i)=(0.272*log(x(i))+ score(i))面对投资X(i)的销售额
  • 制约因素1:exp(0.0272*log( x(i) ) + score (i) ) < 0.8*MKT (i)
  • 制约因素二:exp(0.0272*log( x(i) ) + score (i) ) - exp( 0.0272*log( x(i) + 1 ) + score (i) ) >= 1
  • 制约因素三:X(i) < B与B的总和=预算
  • 制约因素4:对于每个i,X(i)> 0

--

#Objective function

    objfun <- function(x, score) 0.272 * log(x) + my_data$score

# Define the constraints

    con <- function(x, Score, MKT) {
      con1 <- exp(0.0272 * log(x) + Score) - 0.8 * MKT
      con2 <- exp(0.0272 * log(x) + Score) - exp(0.0272 * log(x + 1) + Score) - 1
      con3 <- sum(x) - 18000000
    }

# Set up the optimization problem

    x0 <- rep(1, nrow(my_data))
    lb <- rep(0, nrow(my_data))
    ub <- rep(Inf, nrow(my_data))

    problem <- list(
      objective = objfun,
      lb = lb,
      ub = ub,
      eval_g_ineq = con )

    result <- nloptr(x0 = x0, 
                 eval_f = problem$objective, 
                 lb = problem$lb, 
                 ub = problem$ub, 
                 eval_g_ineq = problem$eval_g_ineq, 
                 opts = list("algorithm" = "NLOPT_LN_COBYLA"))

试图改变不同的东西,但不断遇到错误。我不知道我的代码出了什么问题。真的需要帮助谢谢

sy5wg1nm

sy5wg1nm1#

假设你的数据有两个观测值,你应该这样做:

score <- c(12, 14) # I don't have your data
MKT   <- c(22, 26) # I don't have your data

objfun <- function(x){ # minus for maximization
  -0.272 * sum(log(x) + score)
}

con <- function(x) {
  con1 <- exp(0.0272 * log(x) + score) - 0.8 * MKT
  con2 <- exp(0.0272 * log(x) + score) - exp(0.0272 * log(x + 1) + score) - 1
  con3 <- sum(x) - 18000000
  c(con1, con2, con3)
}

x0 <- rep(1, 2)
lb <- rep(0, 2)
ub <- rep(Inf, 2)

result <- nloptr(x0 = x0, 
                 eval_f = objfun, 
                 lb = lb, 
                 ub = ub, 
                 eval_g_ineq = con, 
                 opts = list("algorithm" = "NLOPT_LN_COBYLA"))

相关问题