matlabfminsearch

How to let fminsearch only search over integers?


I'm using the fminsearch Method of Matlab to minimize a function:

c = cvpartition(200,'KFold',10);
minfn = @(z)kfoldLoss(fitcsvm(cdata,grp,'CVPartition',c,...
    'KernelFunction','rbf','BoxConstraint',exp(z(2)),...
    'KernelScale',exp(z(1))));
opts = optimset('TolX',5e-4,'TolFun',5e-4);
[searchmin fval] = fminsearch(minfn,randn(2,1),opts)

The minimization is over two parameters.

Now I would like to minimize a third parameter, but this parameter can only take positive integer values, i.e. 1,2,3,...

How can I tell fminsearch to only consider positive integers?

Second, if my third parameter gets initialized to 10 but it actual best value is 100, does fminsearch converge fast in such cases?


Solution

  • You can't tell fminsearch to consider only integers. The algorithm it uses is not suitable for discrete optimization, which in general is much harder than continuous optimization.

    If there are only relatively few plausible values for your integer parameter(s), you could just loop over them all, but that might be too expensive. Or you could cook up your own 1-dimensional discrete optimization function and have it call fminsearch for each value of the integer parameter it tries. (E.g., you could imitate some standard 1-dimensional continuous optimization algorithm, and just return once you've found a parameter value that's, say, better than both its neighbours.) You may well be able to adapt this function to the particular problem you're trying to solve.