Optimize function with Bayesian Optimization
3 ビュー (過去 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')
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 件のコメント
採用された回答
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
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
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!