Optimize function with Bayesian Optimization
3 views (last 30 days)
Show older comments
Hi,
I'm working with a simple example in trying to understand Bayesian Optimization.
Suppose I want to find the minimum of the 2-Dimensional Rastrigin function (this has a global minimum at coordinates (0,0)).
% Rastrigin function
fun = @(x,y)reshape(rastriginsfcn([x(:)/10,y(:)/10]),size(x));
fsurf(fun,[-30 30],'ShowContours','on')
title('rastriginsfcn([x/10,y/10])')
xlabel('x')
ylabel('y')
Now, I want to use Bayesian Optimization in order to minimize this function, but I can't seem to get it to work.w
Here is the code that I use.
% Variables for a Bayesian Optimization
X1 = optimizableVariable('x',[-30 30]);
X2 = optimizableVariable('y',[-30 30]);
vars = [X1,X2];
% Function to Optimize
fun = @(x)rastriginsfcn(x./10);
results = bayesopt(fun,vars,'AcquisitionFunctionName','expected-improvement-plus');
This code just gives me the error "Undefined function 'rdivide' for input arguments of type 'table'.".
0 Comments
Accepted Answer
Alan Weiss
on 10 Apr 2019
As clearly stated in the documentation for bayesopt, the function passes a TABLE of values. However, rastriginsfcn expects a 2-D double array. You need to write your own objective function for bayesopt, and cannot rely on those provided by Global Optimization Toolbox.
function fval = myrastrig(in)
x(1) = in.x;
x(2) = in.y;
fval = rastriginsfcn(x/10);
end
Then call the function as follows:
results = bayesopt(@myrastrig,vars)
Alan Weiss
MATLAB mathematical toolbox documentation
2 Comments
Subhodip Biswas
on 4 Oct 2020
This example works well for a 2D functin. How can this code be automated if I have functions in 10D, 30D, and so on?
Alan Weiss
on 5 Oct 2020
Edited: Alan Weiss
on 5 Oct 2020
I think that you would be best served by surrogateopt rather than bayesopt for this case. The solvers have similar underlying mechanisms, but surrogateopt allows you to use higher-dimensional variables easily. bayesopt requires you to create variables one dimension at a time.
That said, is your problem very computationally demanding, with a slow-to-compute objective function? If not, then fmincon or patternsearch are usually better solvers for smooth and nonsmooth problems respecitvely.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!