Chiara Abergo
Chiara Abergo

Reputation: 1

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:

The non linear maximization problem should contain:

--

#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

Upvotes: 0

Views: 77

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84649

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

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

Upvotes: 1

Related Questions