-
Notifications
You must be signed in to change notification settings - Fork 577
Initialization Module #3912
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
Open
michaelbynum
wants to merge
62
commits into
Pyomo:main
Choose a base branch
from
michaelbynum:initialization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Initialization Module #3912
Changes from all commits
Commits
Show all changes
62 commits
Select commit
Hold shift + click to select a range
00b3c27
merge observer_fix into intialization
michaelbynum d2aae9f
Merge branch 'pwl_approx' into initialization
michaelbynum b2fa0a5
Merge branch 'pwl_factorable_aggressive_substitution' into initializa…
michaelbynum fd06964
Merge branch 'observer_gurobi_refactor' into initialization
michaelbynum d29a2cf
starting a module for initialization
michaelbynum 8914a63
starting a module for initialization
michaelbynum 5ebfdd4
initialization
michaelbynum 488a539
pwl initialization is almost working
michaelbynum e993623
debugging
michaelbynum aa450e1
initialization
michaelbynum 4945388
Merge branch 'scip_port' into initialization
michaelbynum ab9a46c
Merge branch 'pwl_approx' into initialization
michaelbynum 6f30fac
Merge branch 'pwl_factorable' into initialization
michaelbynum 043f8c8
some updates to initialization module
michaelbynum 83c5e72
Merge branch 'scip_port' into initialization
michaelbynum 8bf25d1
Merge branch 'pwl_factorable' into initialization
michaelbynum 4e3d913
Merge branch 'scip_port' into initialization
michaelbynum c8a0730
initialize with global solvers
michaelbynum e019bff
initialization work
michaelbynum 9568bb0
lp init should bound all variables
michaelbynum 387a646
move initialization module from contrib to devel
michaelbynum 24dbd64
update imports
michaelbynum 2616502
initialization example
michaelbynum 54d354e
initialization: starting tests
michaelbynum 056cc99
initialization: add copyright statements
michaelbynum d70e677
docstring
michaelbynum 823641d
initialization: docs
michaelbynum 6a29fba
Merge branch 'pwl_approx' into initialization
michaelbynum 5eb638f
Merge branch 'scip_port' into initialization
michaelbynum 3ddc25d
Merge branch 'pwl_factorable' into initialization
michaelbynum 0b02132
working on docs for initialization
michaelbynum f96a421
Merge remote-tracking branch 'michaelbynum/initialization' into initi…
michaelbynum b185c8b
Merge branch 'scip_port' into initialization
michaelbynum 25d50e6
Merge branch 'pwl_factorable' into initialization
michaelbynum 2355870
bug
michaelbynum e40f487
try nlp before initialization
michaelbynum 42d7fff
fix some docstrings
michaelbynum fe98b7c
initialization: working on tests
michaelbynum 5d9660b
initialization: fixing bugs
michaelbynum c08a92c
fix nudge
michaelbynum ec9f9b9
initialization: working on tests
michaelbynum e52e123
debugging pwl initialization
michaelbynum 5b4fe02
comment out print statements
michaelbynum 135b152
initialization: testing
michaelbynum fc60afe
initialization: update docstring; remove debugging code
michaelbynum 48bae52
run black
michaelbynum eb0e0ea
Merge branch 'scip_port' into initialization
michaelbynum d0dab9a
Merge branch 'pwl_factorable' into initialization
michaelbynum 89ccca8
Merge branch 'scip_port' into initialization
michaelbynum 4c32139
Merge branch 'pwl_factorable' into initialization
michaelbynum 0d1175f
fix typos
michaelbynum 2030f4e
Merge branch 'scip_port' into initialization
michaelbynum 5d81def
Merge branch 'scip_port' into initialization
michaelbynum 8f2a0da
Merge branch 'pwl_factorable' into initialization
michaelbynum 0534849
merge main into initialization
michaelbynum f5fe237
Fix ScipPersistent import
blnicho 061e46f
update tests
michaelbynum cba1223
fix doctests
michaelbynum c85a864
use attempt_import
michaelbynum a33c665
update tests
michaelbynum a800114
guard tests
michaelbynum 2d4594a
try highs instead of scip
michaelbynum 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
131 changes: 131 additions & 0 deletions
131
doc/OnlineDocs/explanation/analysis/nlp_initialization/nlp_initialization.rst
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,131 @@ | ||
| .. _analysis_nlp_initialization: | ||
|
|
||
| NLP Initialization | ||
| ****************** | ||
|
|
||
| .. warning:: | ||
|
|
||
| This package lives in :mod:`pyomo.devel`. APIs, options, and behavior may | ||
| change without notice. | ||
|
|
||
| The initialization module within ``pyomo.devel.initialization`` is intended to | ||
| provide methods to help initialize nonconvex nonlinear programs (NLPs). The | ||
| goal is to increase the chance of finding a local minimizer (i.e., decrease the | ||
| chance of getting stuck a point that locally minimizes infeasibility). If | ||
| you are already able to solve your problem with a local NLP solver, these | ||
| tools will not help you. Example usage is shown below. | ||
|
|
||
| .. literalinclude:: /../../pyomo/devel/initialization/examples/init_polynomial_ex.py | ||
| :start-after: # === Required imports === | ||
|
|
||
| The :func:`initialize_nlp <pyomo.devel.initialization.initialize.initialize_nlp>` | ||
| function uses the specified method to try to find a good starting point for the | ||
| NLP solver and then attempts to solve the problem with the given NLP solver. | ||
|
|
||
| .. note:: | ||
|
|
||
| Currently, this module only works with solvers from :mod:`pyomo.contrib.solver`. | ||
|
|
||
|
|
||
| Initialization Methods | ||
| ====================== | ||
|
|
||
| The initialization method is selected using the | ||
| :class:`InitializationMethod <pyomo.devel.initialization.initialize.InitializationMethod>` enum. | ||
|
|
||
| .. note:: | ||
|
|
||
| Not all of the methods described below require all nonlinear variables to be | ||
| bounded. However, all of the methods will perform better if all nonlinear | ||
| variables are bounded (the tighter the bounds, the better). | ||
|
|
||
|
|
||
| Method ``global_opt`` | ||
| --------------------- | ||
|
|
||
| This method uses an MINLP solver to try to find a feasible solution. We | ||
| adjust the solver parameters so that the solver will stop as soon as any | ||
| feasible solution is found. We then initialize the NLP solver at that | ||
| feasible solution. Many MINLP solvers will default to a very large | ||
| time limit, so it can be useful to specify a time limit before | ||
| calling :func:`initialize_nlp <pyomo.devel.initialization.initialize.initialize_nlp>`: | ||
|
|
||
| .. testcode:: | ||
| :skipif: not scip_available | ||
|
|
||
| import pyomo.environ as pyo | ||
| from pyomo.contrib.solver.common.factory import SolverFactory | ||
|
|
||
| global_solver = SolverFactory('scip_direct') | ||
| global_solver.config.time_limit = 600 # 10 minutes | ||
| # now call initialize_nlp | ||
|
|
||
| This method currently works with the following solver interfaces for MINLP solvers: | ||
|
|
||
| * SCIP (:class:`direct <pyomo.contrib.solver.solvers.scip.scip_direct.ScipDirect>` and | ||
| :class:`persistent <pyomo.contrib.solver.solvers.scip.scip_direct.ScipPersistent>`) | ||
| * :class:`Gurobi MINLP <pyomo.contrib.solver.solvers.gurobi.gurobi_direct_minlp.GurobiDirectMINLP>` | ||
|
|
||
| Advantages | ||
| ^^^^^^^^^^ | ||
|
|
||
| * Currently, this is the method that is most likely to succeed in finding a | ||
| feasible solution. | ||
| * Does not strictly require variable bounds | ||
|
|
||
| Disadvantages | ||
| ^^^^^^^^^^^^^ | ||
|
|
||
| * This method will only work if the model is completely algebraic. It will not | ||
| work with external functions. | ||
|
|
||
|
|
||
| Method ``pwl_approximation`` | ||
| ---------------------------- | ||
|
|
||
| This method builds a piecewise linear (PWL) approximation of the model, solves | ||
| it, and initializes the NLP solver at the solution. If the NLP solver does not | ||
| converge, then the PWL approximation will be refined by adding additional | ||
| "segments". This is repeated until either a feasible solution is found or | ||
| the iteration limit is reached. | ||
|
|
||
| This method does not currently work as well as ``global_opt``, but it does | ||
| have a great deal of potential. We expect future versions of this method | ||
| to perform significantly better. | ||
|
|
||
| Advantages | ||
| ^^^^^^^^^^ | ||
|
|
||
| * Does not require an MINLP solver | ||
| * Future versions will work with external functions | ||
|
|
||
| Disadvantages | ||
| ^^^^^^^^^^^^^ | ||
|
|
||
| * Current implementation can be slow | ||
| * Requires all nonlinear variables to be bounded | ||
|
|
||
|
|
||
| Method ``lp_approximation`` | ||
| --------------------------- | ||
|
|
||
| This method is similar to the PWL approximation method, but it builds | ||
| an LP approximation instead and does not do any refinement. Another | ||
| distinction is that the LP approximation uses a linear least-squares | ||
| fit, so the approximation may not equal the original function at the | ||
| variable bounds. This also means that variable bounds are not strictly | ||
| necessary, though they do help improve the approximation. | ||
|
|
||
| Advantages | ||
| ^^^^^^^^^^ | ||
|
|
||
| * Fast | ||
| * Future versions will work with external functions | ||
| * Does not strictly require variable bounds | ||
| * Does not require an MINLP or even an MILP solver | ||
|
|
||
| Disadvantages | ||
| ^^^^^^^^^^^^^ | ||
|
|
||
| * This method only attempts to initialize the problem once. If it does | ||
| not succeed, it is done. |
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
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 @@ | ||
| The purpose of this module is to provide methods for initializing nonlinear programming models. |
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,10 @@ | ||
| # ____________________________________________________________________________________ | ||
| # | ||
| # Pyomo: Python Optimization Modeling Objects | ||
| # Copyright (c) 2008-2026 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. | ||
| # ____________________________________________________________________________________ | ||
|
|
||
| from pyomo.devel.initialization.initialize import initialize_nlp, InitializationMethod |
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,8 @@ | ||
| # ____________________________________________________________________________________ | ||
| # | ||
| # Pyomo: Python Optimization Modeling Objects | ||
| # Copyright (c) 2008-2026 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. | ||
| # ____________________________________________________________________________________ |
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,36 @@ | ||
| # ____________________________________________________________________________________ | ||
| # | ||
| # Pyomo: Python Optimization Modeling Objects | ||
| # Copyright (c) 2008-2026 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. | ||
| # ____________________________________________________________________________________ | ||
|
|
||
| from pyomo.core.base.block import BlockData | ||
| from pyomo.contrib.fbbt.fbbt import fbbt | ||
| from pyomo.devel.initialization.utils import get_vars | ||
| import logging | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def bound_all_nonlinear_variables(m: BlockData, default_bound: float = 1.0e8): | ||
| """ | ||
| Attempt to obtain valid bounds on all nonlinear variables based on the | ||
| constraints in the model, m. If variable bounds cannot be obtained, | ||
| we use default_bound. | ||
| """ | ||
| fbbt(m) | ||
| for v in get_vars(m): | ||
| if v.lb is None or v.lb < -default_bound: | ||
| logger.debug( | ||
| f'Could not obtain a lower bound for {str(v)} better than {-default_bound}; setting the lower bound to {-default_bound}' | ||
| ) | ||
| v.setlb(-default_bound) | ||
| if v.ub is None or v.ub > default_bound: | ||
| logger.debug( | ||
| f'Could not obtain an upper bound for {str(v)} better than {default_bound}; setting the upper bound to {default_bound}' | ||
| ) | ||
| v.setub(default_bound) | ||
| fbbt(m) |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing copyright |
Empty file.
Oops, something went wrong.
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.
Can this maybe be named
pyscip_availableorpyscipopt_available? I thinkscip_availableimplies the long-time-existing command line version, but it's actually the Python interface that we are looking for.