I have written a function below that I would like to optimize
my_function = function(param, q, m){
out = sum(-param*q + param*m)
return(-out)
}
I am able to run the function and obtain an optimized result
> init = c(0,0,0)
> q = c(0.6, 0.14, 0.18)
> m = c(0, 2.5 , 4.2)
>
> nlminb(init, my_function, q=q, m=m, lower=c(0,0,0), upper=c(3,3,3))
$par
[1] 0 3 3
$objective
[1] -19.14
$convergence
[1] 0
$iterations
[1] 3
$evaluations
function gradient
4 9
$message
[1] "both X-convergence and relative convergence (5)"
I would like to introduce the following constraints but I'm not sure how to do this
- The output parameters should be non-negative integers
- The parameters should sum up to some value k
Can someone inform me on how I can achieve this please?
1) Define a function
projsuch that for any input vector x the output vector y satisfies sum(y) = k. Then we have the following.Note that this is a relaxation of the original problem where we have not applied the integer constraint; however, if the relaxed problem satisfies the constraint then it must be the solution to the original problem as well.
2) Another approach is to use integer programming. This one does explicitly impose the integer constraint.
giving the following (res$solution is the solution).