Hi, with the help of PyCall.jl, I can easily run a script of python, but if I want to replace the function defined in python with the function defined in julia, it reminds me cannot find the sub-function, can anybody help me to have a look, because in the future, I want to use quite complex sub-function written in julia, here is a simple example
function black_box_function(x, y) # with this function, doesn't work
return -x^2 - (y - 1)^2 + 1
end
using PyCall
py"""
from bayes_opt import BayesianOptimization
#def black_box_function(x, y): # with this function, it works well
#return -x ** 2 - (y - 1) ** 2 + 1
pbounds = {'x': (2, 4), 'y': (-3, 3)}
optimizer = BayesianOptimization(
f=black_box_function,
pbounds=pbounds,
random_state=1,
)
optimizer.maximize(
init_points=2,
n_iter=3,
)
print(optimizer.max)
"""
You need to “interpolate” the variable to send it to the Python side, something like f=$black_box_function, assuming you defined black_box_function in Julia as a function.
Fwiw I believe there are Bayesian optimization packages for Julia as well that might be interesting to check out.
Hi, I find a way that works, hope that can give some helps for other people, here is my code
function black_box_function(;x, y) # here the ';' is because of the difference of parameters transfer, see this please[https://discourse.julialang.org/t/call-julia-function-from-python/75202](https://discourse.julialang.org/t/call-julia-function-from-python/75202)
return -x^2 - (y - 1)^2 + 1
end
using PyCall
py"""
pbounds = {'x': (2, 4), 'y': (-3, 3)}
"""
temp=pyimport("bayes_opt")
optimizer = temp.BayesianOptimization(
f=black_box_function,
pbounds=py"pbounds",
random_state=1,
)
optimizer.maximize(
init_points=2,
n_iter=3,
)
print(optimizer.max)