1

I'm new to optimization in R and to R at all, Trying to implement the following set of equations - to find the Maximum, Minimum and intersections and maximum and minimum values for intersect points. a specific form of the function is:

P = 1.8X+2.55Y+3.2Z

where the constraints are:

X + Y + Z = 1 
X, Y, Z =>0
1.8X = 2.55Y = 3.2Z

I'm using nlcoptim trying to implement it the following way: library(NlcOptim)

objective function

obj = function(x){
  return(1.8 * x[1] + 2.55 * x[2] + 3.2 * x[3])
}

constraint function:

con = function(x){
  f = NULL
  f = rbind(f, x[1] + x[2] + x[3] - 1)
  f = rbind(f, x[1] >= 0)
  f = rbind(f, x[1] <= 1)
  f = rbind(f, x[2] >= 0)
  f = rbind(f, x[2] <= 1)
  f = rbind(f, x[3] >= 0)
  f = rbind(f, x[3] <= 1)
  return(list(ceq = NULL, c = f))
}

setting initial value for x:

x0 = c(0, 0, 0)

solving using solnl:

solnl(x0, objfun = obj, confun = con)

I keep getting an error. What am I doing wrong?

Amidavid
  • 177
  • 7
  • 1
    Nice post. But it's missing something important: The actual error message. – Holli Sep 06 '19 at 10:18
  • This is a linear model, so it is better to use an LP solver (specialized for linear models). Note that non-negativity restrictions are directly supported by LP solvers. – Erwin Kalvelagen Sep 06 '19 at 23:45
  • Erwin is right, a linear solver will be more appropriate. Coming back to the question "what went wrong?": expressions like `x[1] >= 0` etc. are obviously not allowed in the constraint function. And BTW, use lower and upper bounds `lb` and `ub`. You didn't mention `X,Y,Z<=1`. Doing this I get `(1,0,0)` and `(0,0,1)` as minimum and maximum. As necessary for a linear objective function, the optima lie on the boundary. I don't know what you mean with "intersect points". – Hans W. Sep 08 '19 at 12:52

0 Answers0