Optimize function with Bayesian Optimization

조회 수: 5 (최근 30일)
Pontus Vikstål
Pontus Vikstål 2019년 4월 10일
편집: Alan Weiss 2020년 10월 5일
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개)

Community Treasure Hunt

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

Start Hunting!

Translated by