I am trying to minimize the function:
f(x) = -x[1]*x[2]*x[3]
subject to the constraints:
0 <= x[1] + 2*x[2] + 2*x[3] <= 72.
What I did so far is that I wrote the constraint as two separate constraints:
constraint_1: -x[1] - 2*x[2] - 2*x[3] <=0
constraint_2: x[1] + 2*x[2] + 2*x[3] <= 72
Then I used the following codes, however I cannot figure out what I should write for the objective.in:
library(lpSolve)
lp(direction = "min", objective.in, const.mat, const.dir, const.rhs)
const.mat = matrix(c(-1,-2,-2,1,2,2), nrow = 2, ncol = 3, byrow=TRUE)
constraint_1 = 0
constraint_2 = 72
const.rhs = c(constraint_1, constraint_2)
const.dir = c("<=", "<=")
EDIT:
I used nloptr package, but I am having this error: "Error in .checkfunargs(eval_f, arglist, "eval_f") :
eval_f requires argument 'x_2' but this has not been passed to the 'nloptr' function."
When I apply these codes:
objective function
eval_f0 <-function( x_1, x_2, x_3 ){
return(-x_1*x_2*x_3)
}
eval_grad_f0 <-function( x_1, x_2, x_3 ){
return(c(-x_2*x_3, -x_1*x_3, -x_1*x_2))
}
constraint function
eval_g0 <- function(x_1, x_2, x_3) {
return((-x_1 - 2*x_2 - 2*x_3),
(x_1 + 2*x_2 + 2*x_3 - 72))
}
eval_jac_g0 <- function(x_1, x_2, x_3) {
return(rbind(c(-1,-2,-2),
c(1,2,2)))
}
res0 <-nloptr(x0=c(0,0,0),
eval_f=eval_f0,
eval_grad_f=eval_grad_f0,
lb =c(-Inf,-Inf,-Inf),
ub =c(Inf,Inf,Inf),
eval_g_ineq =eval_g0,
eval_jac_g_ineq =eval_jac_g0,
opts =list("algorithm"="NLOPT_LD_MMA",
"xtol_rel"=1.0e-8,
"print_level"=3,
"check_derivatives"=TRUE,
"check_derivatives_print"="all"))