Skip to content

feat: Constraints on Tuner#155

Merged
toby-coleman merged 13 commits intomainfrom
feat/tuner-constraints
Jul 26, 2025
Merged

feat: Constraints on Tuner#155
toby-coleman merged 13 commits intomainfrom
feat/tuner-constraints

Conversation

@toby-coleman
Copy link
Contributor

Summary

Closes #149 and allows optimisation runs to apply constraints during a run.

Changes

  • Adds ConstraintError, which can be raised by a Component when a constraint is breached.
  • Handle constraint breaches in optimisation.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @toby-coleman, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the Tuner's capabilities by introducing a robust mechanism for handling constraints during optimization runs. It defines a dedicated exception for constraint violations and integrates its handling directly into the optimization process, ensuring that trials breaching specified constraints are appropriately penalized and logged. This allows for more controlled and effective optimization by guiding the Tuner away from invalid or undesirable parameter configurations.

Highlights

  • New Exception Type: Introduced a new ConstraintError exception class in plugboard/exceptions/__init__.py. This exception is intended to be raised by Component instances when a defined constraint is violated during an optimization run.
  • Tuner Constraint Handling: Modified the Tuner's internal objective function (_build_objective in plugboard/tune/tune.py) to catch ConstraintError exceptions. When a constraint is violated, a warning is logged, and the objective value for that trial is set to math.inf (for minimization) or -math.inf (for maximization) to effectively penalize the trial and guide the optimization away from invalid parameter spaces.
  • Ray Tune Integration for Early Stopping: Configured the underlying ray.tune.Tuner instance to stop trials early when a constraint_hit signal is received. This integrates the new ConstraintError handling with Ray Tune's stopping mechanisms, allowing for more efficient optimization by terminating unpromising trials.
  • Integration Tests for Constraints: Added a new integration test (test_tune_with_constraint in tests/integration/test_tuner.py) to validate the Tuner's behavior with constraints. This test uses a custom ConstrainedB component that raises ConstraintError under specific conditions, ensuring that the Tuner correctly handles these violations, penalizes the objective, and maintains valid results.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a mechanism to handle constraints in the Tuner. It adds a ConstraintError and updates the optimization objective function to catch this error, assigning a very poor score to trials that violate constraints. This allows the optimizer to avoid invalid regions of the parameter space. I've identified a bug in the multi-objective optimization logic and a disconnected early-stopping configuration. The provided suggestions aim to fix these issues to make the constraint handling more robust and effective.

@codecov
Copy link

codecov bot commented Jul 23, 2025

Codecov Report

❌ Patch coverage is 57.14286% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
plugboard/tune/tune.py 50.00% 3 Missing and 3 partials ⚠️

📢 Thoughts on this report? Let us know!

@toby-coleman toby-coleman requested a review from chrisk314 July 23, 2025 19:42
Copy link
Contributor

@chrisk314 chrisk314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@toby-coleman toby-coleman merged commit 2ed4482 into main Jul 26, 2025
14 checks passed
@toby-coleman toby-coleman deleted the feat/tuner-constraints branch July 26, 2025 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

chore: Handle exceptions during tuner runs

2 participants