# Optimization in R Non linear programming

Hello

I am trying to solve a simple non linear programming problem using R.

``````#Maximize profit p
x1=14
x2<=20
x3>=5000
p=x2*x3-x1*x3
``````

Below is the R code I have tried, let me know where I am going wrong.

``````library(ROI)
library(ROI.plugin.nloptr)
prob<-OP(objective=F_objective(F=function(x){x*x-x*x},n=3),
constraints=F_constraint(F=function(x){c(x,x,x)}, dir=c("<=","<=",">="),rhs=c(14,20,5000),J=function(x)c(1,0,0,0,1,0,0,0,1)),maximum=TRUE)

ROI_solve(prob,solver="nloptr",start=c(10,10,4000))
``````

Did you really mean the first constraint to read 'x1=14'? Because, if that is the case, it's not necessary to include x1 in the objective function; you could simply replace it with 14 and do the resulting contrained optimization in R^2 (over x2 and x3).

Also, I do not see any of the contraints you've outlined in the original problem appearing anywhere in the constraint specification in your code. Mind you, I've never used the package ROI.

Okay. So I have changed the objective function as below.

``````x1<=20
x2>=5000
p=x1*x2-14*x2
``````

Any function or package which you can think of, using which I can optimize non linear objective function with linear constrains.

Yes, well there's another problem which didn't occur to me earlier. The objective function you've given has neither a maximum nor a minimum on the region described by the constraints.

To wit: Let x1=20 and let x2 go to positive infinity. Then p likewise grows without bound. On the other hand, with y=5000, as x1 goes to negative infinity p also goes to negative infinity. So it would be pointless to employ any method to try to optimize. In general you need a closed and bounded (compact) region and an objective function continuous on that region to be sure that both absolute extrema exist.

As to your other question, I am not familiar with any R pachages for numerical optimization but doubtless there are many.

1 Like

Oh Yes, thank you.

There are multiple packages I came across now.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.