Skip to content

Internal/mindtpy short circuit base#3907

Merged
jsiirola merged 13 commits intoPyomo:mainfrom
bernalde:internal/mindtpy-short-circuit-base
May 8, 2026
Merged

Internal/mindtpy short circuit base#3907
jsiirola merged 13 commits intoPyomo:mainfrom
bernalde:internal/mindtpy-short-circuit-base

Conversation

@bernalde
Copy link
Copy Markdown
Contributor

@bernalde bernalde commented Apr 7, 2026

Fixes #3906 .

Summary/Motivation:

Based on a comment from @jsiirola in #3861 I went ahead and revisited how we are dealing with a problem that has no discrete variables. Now we check whether the MIP solver (eventually we need to change that nomenclature, but I will leave that to a future PR) can address the problem directly. This would be the case with quadratic programs (QPs) or quadratically constrained programs (QCPs). Although Gurobi can deal with NLPs now, usually it would try to solve them to global optimality, defeating the whole purpose of MindtPy, so we are still passing that down to the NLP (again, I need to change the naming here) solver.

This PR depends on #3861, so it is worth looking into it only after merging that

Changes proposed in this PR:

  • Modification in the 'shortcircuit' part of MindtPy and added tests

Legal Acknowledgement

By contributing to this software project, I have read the contribution guide and agree to the following terms and conditions for my contribution:

  1. I agree my contributions are submitted under the BSD license.
  2. I represent I am authorized to make the contributions and grant the license. If my employer has rights to intellectual property that includes these contributions, I represent that I have received permission to make contributions and grant the required license on behalf of that employer.

bernalde and others added 6 commits April 6, 2026 18:35
- Fix stale Returns docstring in model_is_valid: LP, QP, QCP, or NLP
- Defer working_obj fetch into the obj_degree is None fallback branch
- Add Parameters section to _classify_short_circuit_problem docstring
- Add comment in _mip_solver_supports_capability explaining that unknown
  APPSI solvers fall through conservatively to return False
- Add test_short_circuit_mixed_degree_model_routes_to_nlp to cover the
  case where both has_quadratic_constraints and has_nonquadratic_constraints
  are True (model with quadratic and cubic constraints must route to NLP)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…lowup

[codex] Refine MindtPy no-discrete short-circuit routing
@jsiirola
Copy link
Copy Markdown
Member

@bernalde, @Toflamus: Note that this PR is currently blocked by merge conflicts introduced by #3861.

@bernalde
Copy link
Copy Markdown
Contributor Author

@jsiirola Addressed in bernalde@888bf23.

I merged current main into internal/mindtpy-short-circuit-base, which clears the branch-blocking state noted in your comment. GitHub is now reporting this PR as mergeable again.

I also reran python -m pytest pyomo/contrib/mindtpy/tests/test_mindtpy_no_discrete.py on the updated branch.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 10, 2026

Codecov Report

❌ Patch coverage is 99.09091% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.08%. Comparing base (ae432ba) to head (aea900a).

Files with missing lines Patch % Lines
pyomo/contrib/mindtpy/algorithm_base_class.py 99.09% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3907      +/-   ##
==========================================
+ Coverage   90.07%   90.08%   +0.01%     
==========================================
  Files         904      904              
  Lines      107003   107067      +64     
==========================================
+ Hits        96384    96454      +70     
+ Misses      10619    10613       -6     
Flag Coverage Δ
builders 29.14% <4.54%> (-0.02%) ⬇️
default 86.41% <99.09%> (?)
expensive 35.57% <4.54%> (?)
linux 87.56% <99.09%> (-2.01%) ⬇️
linux_other 87.56% <99.09%> (+0.01%) ⬆️
oldsolvers 28.09% <4.54%> (-0.01%) ⬇️
osx 82.94% <99.09%> (+0.01%) ⬆️
win 86.00% <99.09%> (+0.01%) ⬆️
win_other 86.00% <99.09%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bernalde
Copy link
Copy Markdown
Contributor Author

As far as I could see, none of the failed tests had to do with code introduced in this PR. We can restart the tests later to make sure everything is working

Copy link
Copy Markdown
Member

@jsiirola jsiirola left a comment

Choose a reason for hiding this comment

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

Overall, this looks pretty good. There is one case where woul will end up with an unexpected exception because of the order in which things are being processed (you are doing the short circuit handling before you have checked / normalized the objective).

Comment thread pyomo/contrib/mindtpy/algorithm_base_class.py
Comment thread pyomo/contrib/mindtpy/algorithm_base_class.py Outdated
Comment thread pyomo/contrib/mindtpy/algorithm_base_class.py
Comment thread pyomo/contrib/mindtpy/algorithm_base_class.py
'Using NLP solver %s to solve.' % config.nlp_solver
original_obj = next(
self.original_model.component_data_objects(ctype=Objective, active=True)
)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Short-circuit routing now depends on objective metadata before MindtPy has normalized the objective state. This path, together with set_up_solve_data(), still assumes there is exactly one active objective, so models with no objective will still raise unexpectedly and multiobjective models can take the first objective silently. Please validate or normalize the objective once before the short-circuit classification path and then use the cached degree directly here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in ee55f3921. Objective normalization now happens once in set_up_solve_data(), the short-circuit path consumes the cached degree directly, multiobjective models raise early, and no-objective models use a temporary dummy objective that is cleaned up before returning.

)


class TestMindtPyShortCircuitRouting(unittest.TestCase):
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These routing tests cover the LP, QP, QCP, and NLP branches well, but they do not exercise the objective-normalization edge cases that this short-circuit path now depends on. Please add regressions for both a no-objective model and a multiobjective model so the behavior stays aligned with process_objective() and set_up_solve_data().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in ee55f3921. I added test_short_circuit_model_with_no_objective_uses_temporary_dummy_objective and test_short_circuit_multiobjective_model_raises in pyomo/contrib/mindtpy/tests/test_mindtpy_no_discrete.py.

@bernalde
Copy link
Copy Markdown
Contributor Author

Addressed the requested review changes in ee55f3921. The fix normalizes the objective before short-circuit routing, removes the redundant cached-degree re-check, adds regressions for missing-objective and multiobjective models, and keeps the current capability-based QP and QCP routing behavior in place for this PR. Tests run: python -m pytest pyomo/contrib/mindtpy/tests/test_mindtpy_no_discrete.py -q and python -m pytest pyomo/contrib/mindtpy/tests -q (52 passed, 32 skipped). For the broader QuadraticRepnVisitor follow-up, I opened #3920.

Copy link
Copy Markdown
Member

@jsiirola jsiirola left a comment

Choose a reason for hiding this comment

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

(pending passing tests)

Comment on lines +507 to +510
appsi_capabilities = {
'appsi_cplex': {'quadratic_objective': True, 'quadratic_constraint': True},
'appsi_gurobi': {'quadratic_objective': True, 'quadratic_constraint': True},
'appsi_highs': {
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.

Not a this-PR problem most likely, but it's probably a good idea for MindtPy to migrate away from supporting / relying on APPSI sometime in the future and instead move towards the contrib.solver interfaces.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not addressed in this PR. This would change the solver-interface strategy beyond the continuous short-circuit fix, so I opened #3936 to track evaluating a MindtPy migration from APPSI-specific routing to contrib.solver interfaces.

self.best_solution_found = None
self.best_solution_found_time = None
self.initialize_mip_problem()
# TODO: if the MindtPy solver is defined once and called several times to solve models. The following two lines are necessary. It seems that the solver class will not be init every time call.
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.

Nitpick - can you please break this comment onto multiple lines? It's really long.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in ca776cdee. I split the long TODO into multiple comment lines and kept the existing behavior unchanged: https://github.com/bernalde/pyomo/blob/ca776cdee086aac3419576ad3860d54a28bf7fbb/pyomo/contrib/mindtpy/algorithm_base_class.py#L3075.

Copy link
Copy Markdown
Contributor

@emma58 emma58 left a comment

Choose a reason for hiding this comment

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

Several comments below, but they're all picky or optional--nothing that needs to block the PR.

Comment on lines +300 to +304
if (
self.original_model.component(self._temporary_original_objective_name)
is not None
):
self.original_model.del_component(self._temporary_original_objective_name)
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.

del_component doesn't actually raise an error if you're deleting by name and the name doesn't exist. I only know because I just tried it out of curiosity... But for better or for worse, you don't need the if clause. In fact, m.del_component(None) is silent too, so you could just call it as long as you have the model, even if the name is None.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in ca776cdee. _cleanup_temporary_original_objective() now calls del_component() whenever original_model is available and then clears the stored temporary objective name: https://github.com/bernalde/pyomo/blob/ca776cdee086aac3419576ad3860d54a28bf7fbb/pyomo/contrib/mindtpy/algorithm_base_class.py#L290.

Comment on lines +288 to +289
setattr(model, dummy_name, Objective(expr=1))
return getattr(model, dummy_name), objective_count, dummy_name
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.

What you have works fine, but just a style thing, you might do:

obj = Objective(expr=1)
model.add_component(dummy_name, obj)
return obj, objective_count, dummy_name

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in ca776cdee. The dummy objective is now constructed first, added with model.add_component(...), and returned directly: https://github.com/bernalde/pyomo/blob/ca776cdee086aac3419576ad3860d54a28bf7fbb/pyomo/contrib/mindtpy/algorithm_base_class.py#L282.

return True

def _classify_short_circuit_problem(self, MindtPy, obj_degree):
"""Classify a no-discrete model for short-circuit direct solves.
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.

Suggested change
"""Classify a no-discrete model for short-circuit direct solves.
"""Classify a continuous model for short-circuit direct solves.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in ca776cdee. The docstring now says continuous model for the short-circuit direct-solve classifier: https://github.com/bernalde/pyomo/blob/ca776cdee086aac3419576ad3860d54a28bf7fbb/pyomo/contrib/mindtpy/algorithm_base_class.py#L443.

"""Classify a no-discrete model for short-circuit direct solves.

This classification is intentionally independent of
``quadratic_strategy``. In the no-discrete short-circuit path we are
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.

Suggested change
``quadratic_strategy``. In the no-discrete short-circuit path we are
``quadratic_strategy``. In the continuous-model short-circuit path we are

(Or something like that?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines +511 to +514
# TODO: revisit if/when the APPSI HiGHS wrapper adds QP support.
'quadratic_objective': False,
'quadratic_constraint': False,
},
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.

Also not a this-PR problem, but there is quadratic support in the contrib.solver HiGHS interface now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not addressed in this PR. This belongs with the solver-interface migration rather than this behavior-preserving short-circuit change, so I opened #3936 to track evaluating contrib.solver routing and HiGHS quadratic support for MindtPy.

self.build_ordered_component_lists(model)
self.add_cuts_components(model)

def _get_main_objective(self, model, create_dummy_objective=False, logger=None):
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.

It looks like you always call this with create_dummy_objective=True. Do you need the argument?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

"""
config = self.config
self.original_model = model
self._temporary_original_objective_name = None
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.

This line looks unnecessary? I think it has to already be None, and even if not, _get_main_objective doesn't use it and you are setting it in line 1006 below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in ca776cdee. I removed the redundant reset before _get_main_objective(); set_up_solve_data() now assigns _temporary_original_objective_name only from the returned dummy objective name: https://github.com/bernalde/pyomo/blob/ca776cdee086aac3419576ad3860d54a28bf7fbb/pyomo/contrib/mindtpy/algorithm_base_class.py#L987.

@bernalde
Copy link
Copy Markdown
Contributor Author

bernalde commented May 6, 2026

For the top-level Codecov report, no separate coverage-only change was made. The report shows 98.24561% patch coverage, and the author revision in ca776cdee only addresses review cleanup without adding new behavior branches.

Local checks for this revision:
python -m pytest pyomo/contrib/mindtpy/tests/test_mindtpy_no_discrete.py -q passed with 28 tests.
python -m pytest pyomo/contrib/mindtpy/tests -q passed with 52 tests and 32 skipped.
python -m black --check --diff --fast pyomo/contrib/mindtpy/algorithm_base_class.py passed.
git diff --check passed.

@bernalde
Copy link
Copy Markdown
Contributor Author

bernalde commented May 8, 2026

Could you please share the failure reason in Jenkins so I can fix this PR? Thanks!

@jsiirola
Copy link
Copy Markdown
Member

jsiirola commented May 8, 2026

Could you please share the failure reason in Jenkins so I can fix this PR? Thanks!

It's unrelated (it has to do with cuOpt and #3916)

@jsiirola jsiirola merged commit 3b18254 into Pyomo:main May 8, 2026
64 of 65 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.

MindtPy short-circuit follow-up: avoid duplicate structure checks and refine continuous QP/QCP routing

6 participants