Internal/mindtpy short circuit base#3907
Conversation
- 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 Addressed in bernalde@888bf23. I merged current I also reran |
Codecov Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
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 |
jsiirola
left a comment
There was a problem hiding this comment.
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).
| 'Using NLP solver %s to solve.' % config.nlp_solver | ||
| original_obj = next( | ||
| self.original_model.component_data_objects(ctype=Objective, active=True) | ||
| ) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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().
There was a problem hiding this comment.
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.
|
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: |
| appsi_capabilities = { | ||
| 'appsi_cplex': {'quadratic_objective': True, 'quadratic_constraint': True}, | ||
| 'appsi_gurobi': {'quadratic_objective': True, 'quadratic_constraint': True}, | ||
| 'appsi_highs': { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
Nitpick - can you please break this comment onto multiple lines? It's really long.
There was a problem hiding this comment.
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.
emma58
left a comment
There was a problem hiding this comment.
Several comments below, but they're all picky or optional--nothing that needs to block the PR.
| if ( | ||
| self.original_model.component(self._temporary_original_objective_name) | ||
| is not None | ||
| ): | ||
| self.original_model.del_component(self._temporary_original_objective_name) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| setattr(model, dummy_name, Objective(expr=1)) | ||
| return getattr(model, dummy_name), objective_count, dummy_name |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
| """Classify a no-discrete model for short-circuit direct solves. | |
| """Classify a continuous model for short-circuit direct solves. |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
| ``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?)
There was a problem hiding this comment.
Addressed in ca776cdee. The paragraph now uses continuous-model short-circuit path: https://github.com/bernalde/pyomo/blob/ca776cdee086aac3419576ad3860d54a28bf7fbb/pyomo/contrib/mindtpy/algorithm_base_class.py#L445.
| # TODO: revisit if/when the APPSI HiGHS wrapper adds QP support. | ||
| 'quadratic_objective': False, | ||
| 'quadratic_constraint': False, | ||
| }, |
There was a problem hiding this comment.
Also not a this-PR problem, but there is quadratic support in the contrib.solver HiGHS interface now.
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
It looks like you always call this with create_dummy_objective=True. Do you need the argument?
There was a problem hiding this comment.
Addressed in ca776cdee. I removed the unused create_dummy_objective argument from _get_main_objective() and updated both callers to use the simplified signature: https://github.com/bernalde/pyomo/blob/ca776cdee086aac3419576ad3860d54a28bf7fbb/pyomo/contrib/mindtpy/algorithm_base_class.py#L252, https://github.com/bernalde/pyomo/blob/ca776cdee086aac3419576ad3860d54a28bf7fbb/pyomo/contrib/mindtpy/algorithm_base_class.py#L874, https://github.com/bernalde/pyomo/blob/ca776cdee086aac3419576ad3860d54a28bf7fbb/pyomo/contrib/mindtpy/algorithm_base_class.py#L988.
| """ | ||
| config = self.config | ||
| self.original_model = model | ||
| self._temporary_original_objective_name = None |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
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: |
|
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) |
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:
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: