Optimization with multiple inequality constraints

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

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
#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"))

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

--

#Objective function

    objfun &lt;- function(x, score) 0.272 * log(x) + my_data$score


# Define the constraints

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

# Set up the optimization problem

    x0 &lt;- rep(1, nrow(my_data))
    lb &lt;- rep(0, nrow(my_data))
    ub &lt;- rep(Inf, nrow(my_data))


    problem &lt;- list(
      objective = objfun,
      lb = lb,
      ub = ub,
      eval_g_ineq = con )

    result &lt;- nloptr(x0 = x0, 
                 eval_f = problem$objective, 
                 lb = problem$lb, 
                 ub = problem$ub, 
                 eval_g_ineq = problem$eval_g_ineq, 
                 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

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

```r
score <- c(12, 14) # 我没有您的数据
MKT   <- c(22, 26) # 我没有您的数据

objfun <- function(x){ # 最大化问题,所以取负值
  -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"))
英文:

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

score &lt;- c(12, 14) # I don&#39;t have your data
MKT   &lt;- c(22, 26) # I don&#39;t have your data

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

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

x0 &lt;- rep(1, 2)
lb &lt;- rep(0, 2)
ub &lt;- rep(Inf, 2)


result &lt;- nloptr(x0 = x0, 
                 eval_f = objfun, 
                 lb = lb, 
                 ub = ub, 
                 eval_g_ineq = con, 
                 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:

确定