1

I have the following function which i maximize using optim().

Budget = 2000 

X = 4
Y = 5


min_values = c(0.3,0)
start_values = c(0.3,0.5)
max_values = c(1,1)



sample_function <- function(z,Spend){
  Output = (z[1]*X*Spend) + (z[2]*Y*Spend) 
  return(Output)
}


MaxFunction <- optim(par=start_values ,fn= sample_function, method = "L-BFGS-B", lower = min_values , upper= max_values  ,control=list(maxit=100000 ,fnscale=-1), Spend= Budget)

However i would like to add some constraints when maximizing such as:

 z[1] => 1/3

and

 z[1] + z[2] = 1 

Any help will much be appreciated since this is linked to a more complicated problem that i'm tackling. Or if there's a different method for solving the problem without using otpim() please let me know.

CS.py
  • 43
  • 6
  • Can't really answer your question since I've never used the `optim` function. But just wanted to say you can simplify the code for your sample function by not defining output like this `sample_function <- function(z,Spend){ (z[1]*X*Spend) + (z[2]*Y*Spend) }` The function will return anything printed. – Daniel O Apr 30 '20 at 12:15
  • This looks like a Linear Programming problem to me. You could try using the [lpSolve](https://cran.r-project.org/web/packages/lpSolve/lpSolve.pdf) package. Find a tutorial [here](https://firsttimeprogrammer.blogspot.com/2018/08/linear-programming-in-r.html). – Bas Apr 30 '20 at 13:13
  • Hi Bas, its actually not a linear programming problem since the output variable its not predefined – CS.py Apr 30 '20 at 13:19

1 Answers1

1

optim is not a good option for constrained optimization, but it is still possible for your case as long as you formulate your objective function sample_function in a different way.

Below is an example

min_values = 1/3
start_values = 0.5
max_values = 1

sample_function <- function(z,Spend){
    z*X*Spend + (1-z)*Y*Spend
}

MaxFunction <- optim(par=start_values ,
                     fn= sample_function, 
                     method = "L-BFGS-B", 
                     lower = min_values , 
                     upper= max_values,
                     control=list(maxit=100000 ,fnscale=-1), 
                     Spend= Budget)

If you want to see the distribution of elements of z and 1-z, you can use

z1 <- MaxFunction$par
z2 <- 1- z1
Zopt <- c(z1,z2)

such that

> Zopt
[1] 0.3333333 0.6666667
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • Hi Thomas, thank you for replying. Unfortunately this does not work for me since my end goal is to get the value of the parameters of the z vector (so i can see how the Spend variable is distributed). This is really important since the z vector will include > 2 parameters when i figure this thing out. – CS.py Apr 30 '20 at 14:50
  • Thanks again but if you add another parameter X=4 Y =5 Z = 8 min_values = 1/3 start_values = 0.5 max_values = 1 sample_function <- function(z,Spend){ z*X*Spend + (1-z)*Y*Spend + (1-((1-z)+z))*Z*Spend } MaxFunction <- optim(par=start_values , fn= sample_function, method = "L-BFGS-B", lower = min_values , upper= max_values, control=list(maxit=100000 ,fnscale=-1), Spend= 2000) z1 <- MaxFunction$par z2 <- 1- z1 z3 <- 1-z1-z2 Zopt <- c(z1,z2,z3) – CS.py Apr 30 '20 at 15:23
  • @ChrisSolomou As I said at the beginning of my answer, `optim` is not suitable for constrained optimization. Your case with only two parameters `X` and `Y` is a special one, since it is enough to use single `z` to represent `z` and `1-z`. This method cannot be extended when the number of parameters are greater than 2. You need other tools. I suggest you use `lpSolve` or `fmincon` or `CVX` from other packages, rather than `optim` with base R – ThomasIsCoding Apr 30 '20 at 15:32