Call Matlab inbuilt function from Julia

Hi, there is a problem when I try to call the bayesopt function (inbuilt function) from Julia, here is a simple example, and it will give an error like follows.

Unable to use a value of type table as an index.

Error in @(tbl)matlab_jl_2(tbl)

Error in BayesianOptimization/callObjNormally (line 2576)
                        Objective = this.ObjectiveFcn(conditionalizeX(this, X));

Error in BayesianOptimization/callObjFcn (line 481)
                        = callObjNormally(this, X);

Error in BayesianOptimization/runSerial (line 1996)
                    ObjectiveFcnObjectiveEvaluationTime, ObjectiveNargout] = callObjFcn(this, this.XNext);

Error in BayesianOptimization/run (line 1948)
                this = runSerial(this);

Error in BayesianOptimization (line 457)
            this = run(this);

Error in bayesopt (line 323)
Results = BayesianOptimization(Options);

As I know, within matlab, the variables are transfered to mdlfun() in type of table, here I just want to run the objective function in Julia (cause it’s fast), and use matlab bayesopt function (cause it’s convenient), thanks in advance for any suggestions

using MATLAB

function f(x, y)
    return -x - y
end

function mdlfun(tbl)
    x = tbl.v1
    y = tbl.v2
    return f(x, y)
end

mat"""
xrange = optimizableVariable('v1',[-2,2],'Type','real');
yrange = optimizableVariable('v2',[-5,5],'Type','real');
var=[xrange yrange];

bayesObject =bayesopt(@(tbl)$mdlfun(tbl),var,...
    'MaxObjectiveEvaluations',50)

"""

Can’t help you, but I just want to comment that your statement

does not really make sense in this context (if I’m understanding correctly). Everything inside mat""" ... """ will be converted to Matlab code, run in Matlab, and at Matlab speed. It isn’t related to Julia at all at that point.

1 Like

Yeah, you are correct, I also have this worry, but I find that I can import python packages and let the objective run within julia, and use python bayesian optimization function to optimize it, here is another example, I want to change to matlab because I find bayesopt is more efficient to find the optimal result, at least from my experience. So, I just hope to interpolate with matlab~

using PyCall

function black_box_function(; x, y)
    return -x - y
end

py"
pbounds = {'x': (-2, 2), 'y': (-5, 5)}
"
temp = pyimport("bayes_opt")
optimizer = temp.BayesianOptimization(
    f = black_box_function,
    pbounds = py"pbounds",
    random_state = 7,   # random seed
)
optimizer.maximize(
    init_points = 2,
    n_iter = 5
)