Filter löschen
Filter löschen

Optimize function with Bayesian Optimization

11 Ansichten (letzte 30 Tage)
Pontus Vikstål
Pontus Vikstål am 10 Apr. 2019
Bearbeitet: Alan Weiss am 5 Okt. 2020
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'.".

Akzeptierte Antwort

Alan Weiss
Alan Weiss am 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 Kommentare
Subhodip Biswas
Subhodip Biswas am 4 Okt. 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
Alan Weiss am 5 Okt. 2020
Bearbeitet: Alan Weiss am 5 Okt. 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by