Optimize function with Bayesian Optimization

11 次查看(过去 30 天)
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')
untitled.jpg
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'.".

采纳的回答

Alan Weiss
Alan Weiss 2019-4-10
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 个评论
Subhodip Biswas
Subhodip Biswas 2020-10-4
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
Alan Weiss 2020-10-5
编辑:Alan Weiss 2020-10-5
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by