As we all know, Optuna is a well-regarded hyperparameter optimization framework that is independent of any machine learning framework and is very easy to use in Python. However, my cost object-function is in MATLAB, which typically belongs to black-box optimization, and I am unsure how to use the Optuna library. I also know that the Statistics and Machine Learning Toolbox has techniques like random search, grid search, and Bayesian hyperparameter optimization, but they haven't performed very well. The Global Optimization Toolbox can use heuristic search algorithms such as GA and PSO, but I am limited by the high computational cost of the cost function. I also tried surrogateopt. Although the iteration speed is relatively fast, it tends to get stuck in local optima, causing subsequent iterations to nearly stop. Overall, it doesn't perform as well as the PSO algorithm! Therefore, I would like to explore the performance of the Optuna library.
-----------------2025.12.11 update------------------------
Simple Example:
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
Local minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the value of the optimality tolerance,
and constraints are satisfied to within the value of the constraint tolerance.
install python optuna library
websave("get-pip.py","https://bootstrap.pypa.io/get-pip.py");
!python get-pip.py
Defaulting to user installation because normal site-packages is not writeable
Collecting pip
Using cached pip-25.3-py3-none-any.whl.metadata (4.7 kB)
Using cached pip-25.3-py3-none-any.whl (1.8 MB)
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 25.3
Uninstalling pip-25.3:
Successfully uninstalled pip-25.3
□[33m WARNING: The scripts pip, pip3 and pip3.10 are installed in '/home/matlab/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.□[0m□[33m
□[0mSuccessfully installed pip-25.3
!python -m pip --version
pip 25.3 from /home/matlab/.local/lib/python3.10/site-packages/pip (python 3.10)
!python -m pip install numpy optuna
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: numpy in /home/matlab/.local/lib/python3.10/site-packages (2.2.6)
Requirement already satisfied: optuna in /home/matlab/.local/lib/python3.10/site-packages (4.6.0)
Requirement already satisfied: alembic>=1.5.0 in /home/matlab/.local/lib/python3.10/site-packages (from optuna) (1.17.2)
Requirement already satisfied: colorlog in /home/matlab/.local/lib/python3.10/site-packages (from optuna) (6.10.1)
Requirement already satisfied: packaging>=20.0 in /home/matlab/.local/lib/python3.10/site-packages (from optuna) (25.0)
Requirement already satisfied: sqlalchemy>=1.4.2 in /home/matlab/.local/lib/python3.10/site-packages (from optuna) (2.0.45)
Requirement already satisfied: tqdm in /home/matlab/.local/lib/python3.10/site-packages (from optuna) (4.67.1)
Requirement already satisfied: PyYAML in /usr/lib/python3/dist-packages (from optuna) (6.0.1)
Requirement already satisfied: Mako in /home/matlab/.local/lib/python3.10/site-packages (from alembic>=1.5.0->optuna) (1.3.10)
Requirement already satisfied: typing-extensions>=4.12 in /home/matlab/.local/lib/python3.10/site-packages (from alembic>=1.5.0->optuna) (4.15.0)
Requirement already satisfied: tomli in /home/matlab/.local/lib/python3.10/site-packages (from alembic>=1.5.0->optuna) (2.3.0)
Requirement already satisfied: greenlet>=1 in /home/matlab/.local/lib/python3.10/site-packages (from sqlalchemy>=1.4.2->optuna) (3.3.0)
Requirement already satisfied: MarkupSafe>=0.9.2 in /home/matlab/.local/lib/python3.10/site-packages (from Mako->alembic>=1.5.0->optuna) (3.0.3)
Below is the my call optuna for optimization. How can I best pass the MATLAB cost function fun and its constraints to optuna? I want call optuna from MATLAB.
study = py.optuna.create_study()
[I 2025-12-11 07:34:52,821] A new study created in memory with name: no-name-bec3ab28-bca9-4d4c-94d9-abc7e7e51676
study =
Python Study with properties:
best_trials: [1×0 py.list]
direction: [1×1 py.optuna.study._study_direction.StudyDirection]
directions: [1×1 py.list]
metric_names: [1×1 py.NoneType]
system_attrs: [1×1 py.dict]
trials: [1×0 py.list]
user_attrs: [1×1 py.dict]
study_name: [1×44 py.str]
sampler: [1×1 py.optuna.samplers._tpe.sampler.TPESampler]
pruner: [1×1 py.optuna.pruners._median.MedianPruner]
<optuna.study.study.Study object at 0x76ec4ac4a260>
study.optimize(fun, n_trials=100)
Error using py.optuna.study.study.Study/optimize
Handle to MATLAB function '@(x)(x-2).^2' is not supported. Use a handle to a Python function.
original native python example:
x = trial.suggest_float('x', -10, 10)
study = optuna.create_study()
study.optimize(objective, n_trials=100)
study.best_params # E.g. {'x': 2.002108042}