I have the following optimization problem:
We are minimizing with respect to y
. A is a known matrix, b
is a known vector, and c
is a known constant.
Two important things here: while we are trying to minimize the function, it can not be less than 0. Also, it would be ideal to use an R method that doesn't require initial values, since it is quite complex to eyeball a feasible starting value of y
.
Is there any package/function in R that would allow me to implement this problem? Any help would be greatly appreciated! Thank you.
I second @Stu's suggestion of 'optim'. Here is an quick example of using it for your case:
set.seed(1001)
c = 1
m = 10
b = runif(m)
loss_fun = function(y_i){
if (all(y_i>=0)){ # <- Here is where we enforce all y's >=0
return(c - sum(b*log(y_i)))
}else{
return(9e9) # If any y's are negative, return a very large number
}
}
y_initial = runif(m)
print(optim(y_initial, loss_fun))
Hope that helps.
Update : Sorry confused your 'Ay' for 'For All y'. Try something like this:
set.seed(1001)
c = 1
m = 10
b = runif(m)
A = matrix(runif(m*m), nrow=m)
loss_fun = function(y_i){
if (all(A%*%y_i>0)){
return_val = (c - sum(b*log(y_i)))
}else{
return_val = 9e9
}
if ((c - sum(b*log(y_i)))<0.000001){
return_val = 9e9
}
else{
return_val = (c - sum(b*log(y_i)))
}
return(return_val)
}
y_initial = runif(m)
print(optim(y_initial, loss_fun, method = "L-BFGS-B", lower = 0))
You might have to play around with the different methods and bounds on y to get it to act properly.
The overall idea here is to enforce constraints ('if' conditions in your loss function) to by returning large values if they are broken.
Update 2: If the function returns the bad value '9e9', it will say it converged because it satisfied the loss function. E.g. every other feasible solution gives a value higher than 9e9. This means there is no solution for the method/constraints you have.