Skip to content

Add projection function for tree ensembles formulations - #551

Merged
pobonomo merged 2 commits into
Gurobi:mainfrom
hal0taso:project-input
Nov 21, 2026
Merged

pobonomo merged 2 commits into
Gurobi:mainfrom
hal0taso:project-input

Conversation

@hal0taso

Copy link
Copy Markdown
Contributor

Summary

Add project_input() to tree constraint classes. It returns the closest input consistent with the selected leaves, using the precision of the original regressor, or NaN if no feasible input can reach all selected leaves. This is an advanced technique used in some notebooks of #541.

This addresses cases where solver tolerances or float32 rounding cause predict(x) to differ from the formulation’s output.

Changes

  • Share projection logic across supported frameworks.
  • Keep LightGBM thresholds in float64.
  • Simplify sklearn ensemble formulation construction.
  • Update the docstrings for _compute_leafs_bounds
  • Update the user guide and notebooks to use project_input() instead of repetition

hal0taso and others added 2 commits November 14, 2026 20:09
Solver tolerances and float32 rounding can leave the input of a
solution outside the leaves it selects, so predict(x) need not
match the output variable.

- Add project_input() to the tree constraint classes. It returns
  the closest input inside the selected leaves, in the precision
  the regressor evaluates, or NaN rows if the leaves share no
  input.
- Share the projection across frameworks; each wrapper stores its
  trees and declares its strict side and input precision.
- Keep LightGBM thresholds in float64, as LightGBM compares them.
- Call add_tree_ensemble_formulation directly from the sklearn
  ensembles and drop the helper that converted the trees again.
- Document the parameters of _compute_leafs_bounds.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
- Describe project_input() in Split Thresholds and Exactness of
  the user guide, with a short example.
- Replace the hand-written leaf box helpers of the three tree
  ensemble notebooks with project_input().
- Show the leaves selected in each tree in the Tree Ensemble
  Formulations notebook.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@hal0taso

hal0taso commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @pobonomo, continuing from the thread #541, this is the PR for projecting the result x to the closest input x' to the box of selected leaves. Hope it looks good to you too :-)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the tree-input projection change.

No open issues 🎉

What changed in this PR

Adds project_input() to tree constraints so users can check solutions when solver tolerances or input rounding cause predictions to differ from formulation outputs. This builds on the experimental tree-ensemble formulations introduced in #541.

Changes:

  • Share precision-aware projection logic, returning NaN rows when selected leaves have no common input.
  • Integrate projection across scikit-learn, XGBoost, and LightGBM; preserve LightGBM thresholds in float64 and simplify scikit-learn ensemble construction.
  • Add cross-framework tests and replace duplicated notebook projection code with the public API.
File Description
tests/​tree_ensemble.py Add shared projection tests across formulations.
tests/​test_xgboost/​test_project_input.py Apply projection tests to XGBoost.
tests/​test_sklearn/​test_project_input.py Test scikit-learn projection and incompatible leaves.
tests/​test_lightgbm/​test_project_input.py Apply projection tests to LightGBM.
src/​gurobi_ml/​xgboost/​xgboost_regressor.py Retain tree data and specify strict-left routing.
src/​gurobi_ml/​sklearn/​random_forest_regressor.py Retain tree data and use shared ensemble construction.
src/​gurobi_ml/​sklearn/​gradient_boosting_regressor.py Retain tree data and use shared ensemble construction.
src/​gurobi_ml/​sklearn/​decision_tree_regressor.py Remove the redundant ensemble-construction wrapper.
src/​gurobi_ml/​modeling/​decision_tree/​decision_tree_model.py Expose project_input() through the shared tree accessor.
src/​gurobi_ml/​modeling/​decision_tree/​_common.py Implement projection and rounding helpers; document leaf bounds.
src/​gurobi_ml/​lightgbm/​lgbm_regressor.py Preserve float64 thresholds and retain projection tree data.
notebooks/​tree-ensembles/​Tree Ensemble Formulations.ipynb Use the projection API and display selected leaves.
notebooks/​tree-ensembles/​Concrete and Wine Forest Optimization.ipynb Replace local projection helpers with project_input().
notebooks/​tree-ensembles-merck/​Drug Design with Tree Ensembles.ipynb Replace local projection helpers with project_input().
docs/​source/​user/​mip-models.rst Explain projection usage, NaN results, and constraint caveats.

Review effort: Balanced

@pobonomo
pobonomo merged commit 87c12dc into Gurobi:main Nov 21, 2026
13 checks passed
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.

3 participants