How to send variable amount of optimizableVariables to bayesopt function

4 views (last 30 days)
The bayesian optimization documentation provides an example of how to execute bayesopt:
The example shows a single optimizableVariable being passed to the objective function "fun" using anonymous function handles like so:
num = optimizableVariable('n',[1,30],'Type','integer');
dst = optimizableVariable('dst',{'chebychev','euclidean','minkowski'},'Type','categorical');
c = cvpartition(351,'Kfold',5);
fun = @(x)kfoldLoss(fitcknn(X,Y,'CVPartition',c,'NumNeighbors',x.n,...
'Distance',char(x.dst),'NSMethod','exhaustive'));
the objective function can receive a set of optimizableVariables using similar code, this allows the objective function to work its algorithm using several optimizableVariable values:
ObjFunct = @(x)MyObjFunct([x.dst1 x.dst2 x.dst3 ...])
When trying to build this set of optimizableVariable before the objective function call, I am unsure what the approriate syntax would be, so far I've tried:
optVarSet = @x[x.dst1 x.dst2 x.dst3 ...]
ObjFunct = @(x)MyObjFunct(OptVarSet)
which returns the following error:
Error using double
Conversion to double from function_handle is not possible.
It appears the fucntion does not receive the optimizableVariable value anymore but a function handle. What would be the correct syntax to build the optimizableVariable values set to be sent to my objective function?

Answers (1)

Varun
Varun on 28 Aug 2023
Hi,
I understand you are trying to build the set of optimizableVariablee” before the objective function call as below:
optVarSet = @(x)[x.dst1 x.dst2 x.dst3 ...]
ObjFunct = @(x)MyObjFunct(optVarSet)
But the syntax is incorrect as “optVarSet” is an anonymous function’s handle and you are passing this function handle as input to" instead of a vector like [x.dst1 x.dst2 x.dst3 ...].
Correct syntax can be as below:
optVarSet = @(x)[x.dst1 x.dst2 x.dst3 ...]
ObjFunct = @(x)MyObjFunct(optVarSet(x))
Or,
optVarSet =[x.dst1 x.dst2 x.dst3 ...]
ObjFunct = @(x)MyObjFunct(optVarSet)

Community Treasure Hunt

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

Start Hunting!