Optimization with multiple inequality constraints

huangapple go评论93阅读模式
英文:

Optimization with multiple inequality constraints

问题

I am working on R, I need to set up a non linear optimization problem, my dataset has the following columns:

  • Phs Code, that define univocally each pharmacy
  • Score, that define the score associated to each pharmacy
  • MKT, that define the market value generated by each pharmacy

The non-linear maximization problem should contain:

  • decisional variable X(i) = the value to invest in pharmacy i
  • Objective function: MAX sum Y(i)
    where ``Y(i) = (0.272*log( x(i) ) + score (i) ) sales in face of the investment X(i)
  • Constraints 1: exp(0.0272*log( x(i) ) + score (i) ) < 0.8*MKT (i)
  • Constraints 2: exp(0.0272*log( x(i) ) + score (i) ) - exp( 0.0272*log( x(i) + 1 ) + score (i) ) >= 1
  • Constraints 3: sum of X(i) < B
    with B = budget
  • Constraints 4: X(i) > 0 for each i
  1. #Objective function
  2. objfun <- function(x, score) 0.272 * log(x) + my_data$score
  3. # Define the constraints
  4. con <- function(x, Score, MKT) {
  5. con1 <- exp(0.0272 * log(x) + Score) - 0.8 * MKT
  6. con2 <- exp(0.0272 * log(x) + Score) - exp(0.0272 * log(x + 1) + Score) - 1
  7. con3 <- sum(x) - 18000000
  8. }
  9. # Set up the optimization problem
  10. x0 <- rep(1, nrow(my_data))
  11. lb <- rep(0, nrow(my_data))
  12. ub <- rep(Inf, nrow(my_data))
  13. problem <- list(
  14. objective = objfun,
  15. lb = lb,
  16. ub = ub,
  17. eval_g_ineq = con )
  18. result <- nloptr(x0 = x0,
  19. eval_f = problem$objective,
  20. lb = problem$lb,
  21. ub = problem$ub,
  22. eval_g_ineq = problem$eval_g_ineq,
  23. opts = list("algorithm" = "NLOPT_LN_COBYLA"))

Trying to change different things but keep bumping into errors. Not sure about where is the problem with my code. Really need some help
Thanks

英文:

I am working on R, I need to set up a non linear optimization problem, my dataset has the following columns:

  • Phs Code, that define univocally each pharmacy
  • Score, that define the score associated to each pharmacy
  • MKT, that define the market value generated by each pharmacy

The non linear maximization problem should contain:

  • decisional variable X(i) = the value to invest in pharmacy i
  • Objective function: MAX sum Y(i)
    where ``Y(i) = (0.272*log( x(i) ) + score (i) ) sales in face of the investment X(i)
  • Constraints 1: exp(0.0272*log( x(i) ) + score (i) ) &lt; 0.8*MKT (i)
  • Constraints 2: exp(0.0272*log( x(i) ) + score (i) ) - exp( 0.0272*log( x(i) + 1 ) + score (i) ) &gt;= 1
  • Constraints 3: sum of X(i) &lt; B
    with B = budget
  • Constraints 4: X(i) > 0 for each i

--

  1. #Objective function
  2. objfun &lt;- function(x, score) 0.272 * log(x) + my_data$score
  3. # Define the constraints
  4. con &lt;- function(x, Score, MKT) {
  5. con1 &lt;- exp(0.0272 * log(x) + Score) - 0.8 * MKT
  6. con2 &lt;- exp(0.0272 * log(x) + Score) - exp(0.0272 * log(x + 1) + Score) - 1
  7. con3 &lt;- sum(x) - 18000000
  8. }
  9. # Set up the optimization problem
  10. x0 &lt;- rep(1, nrow(my_data))
  11. lb &lt;- rep(0, nrow(my_data))
  12. ub &lt;- rep(Inf, nrow(my_data))
  13. problem &lt;- list(
  14. objective = objfun,
  15. lb = lb,
  16. ub = ub,
  17. eval_g_ineq = con )
  18. result &lt;- nloptr(x0 = x0,
  19. eval_f = problem$objective,
  20. lb = problem$lb,
  21. ub = problem$ub,
  22. eval_g_ineq = problem$eval_g_ineq,
  23. opts = list(&quot;algorithm&quot; = &quot;NLOPT_LN_COBYLA&quot;))

Trying to change different things but keep bumping into errors. Not sure about where is the problem with my code. Really need some help
Thanks

答案1

得分: 1

  1. 假设您的数据有两个观察值,您应该这样做:
  2. ```r
  3. score <- c(12, 14) # 我没有您的数据
  4. MKT <- c(22, 26) # 我没有您的数据
  5. objfun <- function(x){ # 最大化问题,所以取负值
  6. -0.272 * sum(log(x) + score)
  7. }
  8. con <- function(x) {
  9. con1 <- exp(0.0272 * log(x) + score) - 0.8 * MKT
  10. con2 <- exp(0.0272 * log(x) + score) - exp(0.0272 * log(x + 1) + score) - 1
  11. con3 <- sum(x) - 18000000
  12. c(con1, con2, con3)
  13. }
  14. x0 <- rep(1, 2)
  15. lb <- rep(0, 2)
  16. ub <- rep(Inf, 2)
  17. result <- nloptr(x0 = x0,
  18. eval_f = objfun,
  19. lb = lb,
  20. ub = ub,
  21. eval_g_ineq = con,
  22. opts = list("algorithm" = "NLOPT_LN_COBYLA"))
英文:

Assuming your data has two observations, you should do something like that:

  1. score &lt;- c(12, 14) # I don&#39;t have your data
  2. MKT &lt;- c(22, 26) # I don&#39;t have your data
  3. objfun &lt;- function(x){ # minus for maximization
  4. -0.272 * sum(log(x) + score)
  5. }
  6. con &lt;- function(x) {
  7. con1 &lt;- exp(0.0272 * log(x) + score) - 0.8 * MKT
  8. con2 &lt;- exp(0.0272 * log(x) + score) - exp(0.0272 * log(x + 1) + score) - 1
  9. con3 &lt;- sum(x) - 18000000
  10. c(con1, con2, con3)
  11. }
  12. x0 &lt;- rep(1, 2)
  13. lb &lt;- rep(0, 2)
  14. ub &lt;- rep(Inf, 2)
  15. result &lt;- nloptr(x0 = x0,
  16. eval_f = objfun,
  17. lb = lb,
  18. ub = ub,
  19. eval_g_ineq = con,
  20. opts = list(&quot;algorithm&quot; = &quot;NLOPT_LN_COBYLA&quot;))

huangapple
  • 本文由 发表于 2023年5月6日 16:46:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76187962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定