# Constrained optimization with Optuna and TPESampler

**URL:** <https://discuss.ray.io/t/constrained-optimization-with-optuna-and-tpesampler/22010>\
**Category:** Ray Tune\
**Created:** [March 10, 2025, 5:01pm UTC](https://discuss.ray.io/t/constrained-optimization-with-optuna-and-tpesampler/22010 "2025-03-10T17:01:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ggalan87](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@ggalan87](https://discuss.ray.io/u/ggalan87)\
**Post date:** [March 10, 2025, 5:01pm UTC](https://discuss.ray.io/t/constrained-optimization-with-optuna-and-tpesampler/22010/1 "2025-03-10T17:01:57Z")

</div>

**1. Severity of the issue: (select one)**  
 None: I’m just curious or want clarification.  
 Low: Annoying but doesn’t hinder my work.  
 Medium: Significantly affects my productivity but can find a workaround.  
 High: Completely blocks me.

**2. Environment:**

- Ray version: 2.42.1
- Python version: 3.10
- OS: Ubuntu/Linux
- Cloud/Infrastructure: -
- Other libs/tools (if relevant): -

**3. What happened vs. what you expected:**

- Expected: -
- Actual: -

Hello, I am interested in experimenting with objective constraints. Up to now I have followed the sugestions from [another topic](https://discuss.ray.io/t/air-tune-custom-hyperparameter-constraints/9456) for penalizing erroneous configurations. From optuna docs, TPESampler is able to handle constraints directly with `constraints_func` argument. I was wondering if such functionality is available to ray, since it depends on setting the optuna trial’s attributes, e.g. as shown in a [relevant medium article](https://medium.com/optuna/announcing-optuna-4-2-98148689e626).

Can we access search algorithm internals (i.e. optuna trials) from within ray objective function or from a callback passed as RunConfig? In the later case callback is called with ray Trial param, however I couldn’t find a relation to the corresponding Optuna trial, as they are probably maintained separately.

Thanks  
George
