I have a function that I need to maximize that contains 3 parameters, one of which is an integer.
How do I let the optim function know to maximize (instead of minimize which is the default). And how do I let it know that one of the parameters in an integer?
Will it work if one of the parameters is a binary or categorical?
Max vs min is easy (set
fnscale=-1
in thecontrol
parameter).Integer parameters are not easy. Such problems are called mixed integer programming problems and most available methods only handle mixed linear or quadratic problems. From the Optimization Task View:
(I'm not very familiar with these methods.)
Most of the methods implemented in
optim
assume continuous parameter spaces. (method="SANN"
will work since you can give it explicit rules for updating - see the examples - but it's tricky to get it to work efficiently.) Most of the optimizers listed in the Optimization Task View are for continuous optimization - the section on global/stochastic gives the most options for mixed discrete/continuous problems.If the range of plausible integers is reasonably small you can use brute force (i.e., optimize over the two continuous parameters for each of a range of fixed integer values); you could also use bisection search over the integers.