-
Notifications
You must be signed in to change notification settings - Fork 559
contrib.solver: Gurobi warm start option #3774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+165
−33
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
38d8455
Adding mip start to Gurobi direct
emma58 b86c491
Adding test for warmstarts in Gurobi direct v2
emma58 8d2843d
Black
emma58 c4167b7
Switching option to 'warm_start'
emma58 dc5989b
Removing an unused import and fixing the docstring
emma58 7b03357
Adding warmstart support for Gurobi MINLP too
emma58 fd9549b
Renmaing the Gurobi persistent option to warm_start too, though this …
emma58 802aa0c
Gurobi MINLP actually obeys tee arguments now
emma58 6cd8b63
Testing Gurobi MINLP warmstarts
emma58 30103af
black
emma58 4749161
Testing warmstart in Gurobi persistent
emma58 3966da5
Merge branch 'main' into warmstarting-in-gurobi-direct-v2
emma58 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
pyomo/contrib/solver/tests/solvers/test_gurobi_warm_start.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| # ___________________________________________________________________________ | ||
| # | ||
| # Pyomo: Python Optimization Modeling Objects | ||
| # Copyright (c) 2008-2025 | ||
| # National Technology and Engineering Solutions of Sandia, LLC | ||
| # Under the terms of Contract DE-NA0003525 with National Technology and | ||
| # Engineering Solutions of Sandia, LLC, the U.S. Government retains certain | ||
| # rights in this software. | ||
| # This software is distributed under the 3-clause BSD License. | ||
| # ___________________________________________________________________________ | ||
|
|
||
| import logging | ||
| from pyomo.common.log import LoggingIntercept | ||
| import pyomo.common.unittest as unittest | ||
| from pyomo.contrib.solver.common.factory import SolverFactory | ||
|
|
||
| from pyomo.environ import ( | ||
| ConcreteModel, | ||
| Var, | ||
| Constraint, | ||
| value, | ||
| Binary, | ||
| NonNegativeReals, | ||
| Objective, | ||
| Set, | ||
| ) | ||
|
|
||
| gurobi_direct = SolverFactory('gurobi_direct') | ||
| gurobi_direct_minlp = SolverFactory('gurobi_direct_minlp') | ||
| gurobi_persistent = SolverFactory('gurobi_persistent') | ||
|
|
||
|
|
||
| class TestGurobiWarmStart(unittest.TestCase): | ||
| def make_model(self): | ||
| m = ConcreteModel() | ||
| m.S = Set(initialize=[1, 2, 3, 4, 5]) | ||
| m.y = Var(m.S, domain=Binary) | ||
| m.x = Var(m.S, domain=NonNegativeReals) | ||
| m.obj = Objective(expr=sum(m.x[i] for i in m.S)) | ||
|
|
||
| @m.Constraint(m.S) | ||
| def cons(m, i): | ||
| if i % 2 == 0: | ||
| return m.x[i] + i * m.y[i] >= 3 * i | ||
| else: | ||
| return m.x[i] - i * m.y[i] >= 3 * i | ||
|
|
||
| # define a suboptimal MIP start | ||
| for i in m.S: | ||
| m.y[i] = 1 | ||
| # objective will be 4 + 4 + 12 + 8 + 20 = 48 | ||
|
|
||
| return m | ||
|
|
||
| def check_optimal_soln(self, m): | ||
| # check that we got the optimal solution: | ||
| # y[1] = 0, x[1] = 3 | ||
| # y[2] = 1, x[2] = 4 | ||
| # y[3] = 0, x[3] = 9 | ||
| # y[4] = 1, x[4] = 8 | ||
| # y[5] = 0, x[5] = 15 | ||
| x = {1: 3, 2: 4, 3: 9, 4: 8, 5: 15} | ||
| self.assertEqual(value(m.obj), 39) | ||
| for i in m.S: | ||
| if i % 2 == 0: | ||
| self.assertEqual(value(m.y[i]), 1) | ||
| else: | ||
| self.assertEqual(value(m.y[i]), 0) | ||
| self.assertEqual(value(m.x[i]), x[i]) | ||
|
|
||
| @unittest.skipUnless(gurobi_direct.available(), "needs Gurobi Direct interface") | ||
| def test_gurobi_direct_warm_start(self): | ||
| m = self.make_model() | ||
|
|
||
| gurobi_direct.config.warm_start = True | ||
| logger = logging.getLogger('tee') | ||
| with LoggingIntercept(module='tee', level=logging.INFO) as LOG: | ||
| gurobi_direct.solve(m, tee=logger) | ||
| self.assertIn( | ||
| "User MIP start produced solution with objective 48", LOG.getvalue() | ||
| ) | ||
| self.check_optimal_soln(m) | ||
|
|
||
| @unittest.skipUnless( | ||
| gurobi_direct_minlp.available(), "needs Gurobi Direct MINLP interface" | ||
| ) | ||
| def test_gurobi_minlp_warmstart(self): | ||
| m = self.make_model() | ||
|
|
||
| gurobi_direct_minlp.config.warm_start = True | ||
| logger = logging.getLogger('tee') | ||
| with LoggingIntercept(module='tee', level=logging.INFO) as LOG: | ||
| gurobi_direct_minlp.solve(m, tee=logger) | ||
| self.assertIn( | ||
| "User MIP start produced solution with objective 48", LOG.getvalue() | ||
| ) | ||
| self.check_optimal_soln(m) | ||
|
|
||
| @unittest.skipUnless( | ||
| gurobi_persistent.available(), "needs Gurobi persistent interface" | ||
| ) | ||
| def test_gurobi_persistent_warmstart(self): | ||
| m = self.make_model() | ||
|
|
||
| gurobi_persistent.config.warm_start = True | ||
| gurobi_persistent.set_instance(m) | ||
| logger = logging.getLogger('tee') | ||
| with LoggingIntercept(module='tee', level=logging.INFO) as LOG: | ||
| gurobi_persistent.solve(m, tee=logger) | ||
| self.assertIn( | ||
| "User MIP start produced solution with objective 48", LOG.getvalue() | ||
| ) | ||
| self.check_optimal_soln(m) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking we could do this in all the interfaces if we want a separate option for just warm starting integer vars, but dunno if we need to add the complexity?