Pyomo / pyomo

An object-oriented algebraic modeling language in Python for structured optimization problems.
https://www.pyomo.org
Other
1.9k stars 490 forks source link

Add a model viewer option to specify the model by variable name in __main__ #3271

Closed eslickj closed 2 weeks ago

eslickj commented 1 month ago

Summary/Motivation:

When you are working with the model viewer in Jupyter (Lab, qtconsole...) usually your Pyomo model is stored in main. This isn't necessarily the case, but it is more than likely, and if we assume that is the case we can do a few things to make the viewer easier to work with. This adds an option to specify the model by its variable name in main rather than directly specifying the model by pointer. This is just a new option and doesn't change the default behavior. In jupyter there is a callback that causes the model view to refresh every time you run a command. When you use the new option, the refresh will also check the model id to see if it has changed and load the new model if it has.

This also changes what happens if you open the UI without specifying a model. It looks in the main namespace and pulls out the first pyomo model it finds based on the order of dir(main), which I think is alphabetical. In the qtconsole-based model-viewer, it no longer creates an empty model if it can't find one, it just shows the viewer empty, and you'll have to point it at a model after you create one.

The main motivation for this is that it saves me (and hopefully anyone else that uses the viewer) a lot of time selecting the model in the UI. A lot of times I create a model by running a script. This lets me run the script and open the UI without explicitly selecting the model. Then I often find something need changed in the model which is easier done by updating the script. This lets me rerun the script overwriting the model and having the UI update without having to go back and select the model again. This also avoids confusion where the user may not realize they are looking at an older version of the model.

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:

  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.
codecov[bot] commented 1 month ago

Codecov Report

Attention: Patch coverage is 41.66667% with 21 lines in your changes are missing coverage. Please review.

Project coverage is 88.55%. Comparing base (6fb0c5d) to head (19df8be). Report is 82 commits behind head on main.

Files Patch % Lines
pyomo/contrib/viewer/ui.py 42.30% 15 Missing :warning:
pyomo/contrib/viewer/model_select.py 25.00% 6 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #3271 +/- ## ========================================== + Coverage 88.51% 88.55% +0.03% ========================================== Files 865 865 Lines 97585 97610 +25 ========================================== + Hits 86381 86440 +59 + Misses 11204 11170 -34 ``` | [Flag](https://app.codecov.io/gh/Pyomo/pyomo/pull/3271/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Pyomo) | Coverage Δ | | |---|---|---| | [linux](https://app.codecov.io/gh/Pyomo/pyomo/pull/3271/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Pyomo) | `86.48% <41.66%> (-0.02%)` | :arrow_down: | | [osx](https://app.codecov.io/gh/Pyomo/pyomo/pull/3271/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Pyomo) | `?` | | | [other](https://app.codecov.io/gh/Pyomo/pyomo/pull/3271/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Pyomo) | `86.68% <41.66%> (-0.01%)` | :arrow_down: | | [win](https://app.codecov.io/gh/Pyomo/pyomo/pull/3271/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Pyomo) | `83.96% <41.66%> (-0.01%)` | :arrow_down: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Pyomo#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.