Skip to content
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

Pyomo.DoE adds checking sentences to the type of objects #3250

Open
wants to merge 26 commits into
base: main
Choose a base branch
from

Conversation

jialuw96
Copy link
Contributor

Fixes # .

No issue

Summary/Motivation:

In Pyomo.DoE, check if an object is Var or Param before proceeding to fix it or set bounds to it.

Changes proposed in this PR:

  • Check if an object is Var before setting upper and lower bounds to it. If it is not a type of Var, no need to fix it.
  • Check if an object is Var before fixing it to a given value. If it is one type of Param, check if it is mutable before changing it value.

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.

@jialuw96
Copy link
Contributor Author

@adowling2 This PR is ready for your review. Thank you!

pyomo/contrib/doe/doe.py Outdated Show resolved Hide resolved
pyomo/contrib/doe/doe.py Outdated Show resolved Hide resolved
@jialuw96
Copy link
Contributor Author

jialuw96 commented May 8, 2024

The unit test is added in the following file:

  • contrib/doe/examples/reactor_compute_FIM.py

We added the unit test where in the kinetics example, the design variables are defined as Param instead of Var, to test if the checking sentences can identify the type of the objects and fix them to another value.

pyomo/contrib/doe/doe.py Outdated Show resolved Hide resolved
pyomo/contrib/doe/doe.py Outdated Show resolved Hide resolved
pyomo/contrib/doe/doe.py Outdated Show resolved Hide resolved
pyomo/contrib/doe/doe.py Outdated Show resolved Hide resolved
pyomo/contrib/doe/doe.py Outdated Show resolved Hide resolved
pyomo/contrib/doe/doe.py Outdated Show resolved Hide resolved
pyomo/contrib/doe/doe.py Outdated Show resolved Hide resolved
Copy link

codecov bot commented May 8, 2024

Codecov Report

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

Project coverage is 88.47%. Comparing base (a0765b4) to head (3bcebd5).
Report is 199 commits behind head on main.

Files Patch % Lines
pyomo/contrib/doe/examples/reactor_kinetics.py 86.74% 11 Missing ⚠️
pyomo/contrib/doe/doe.py 55.00% 9 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3250   +/-   ##
=======================================
  Coverage   88.47%   88.47%           
=======================================
  Files         852      852           
  Lines       96222    96319   +97     
=======================================
+ Hits        85131    85223   +92     
- Misses      11091    11096    +5     
Flag Coverage Δ
linux 86.43% <81.30%> (+<0.01%) ⬆️
osx ?
other 86.62% <81.30%> (-0.01%) ⬇️
win 83.94% <81.30%> (-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.

jialuw96 and others added 7 commits May 8, 2024 22:22
Co-authored-by: Bethany Nicholson <blnicho@users.noreply.github.com>
Co-authored-by: Bethany Nicholson <blnicho@users.noreply.github.com>
Co-authored-by: Bethany Nicholson <blnicho@users.noreply.github.com>
Co-authored-by: Bethany Nicholson <blnicho@users.noreply.github.com>
Co-authored-by: Bethany Nicholson <blnicho@users.noreply.github.com>
Co-authored-by: Bethany Nicholson <blnicho@users.noreply.github.com>
Copy link
Member

@blnicho blnicho left a comment

Choose a reason for hiding this comment

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

@jialuw96 could you explain the motivation behind this PR? It looks like the Pyomo.DoE API was meant to support design variables specified as either Pyomo mutable Param or Var components but there were a few bugs with the Param case. Is that right?

The docstrings for the DesignOfExperiments and DesignVariables classes should also be updated. Right now they don't mention that both Param and Var components are accepted:
design_vars – A DesignVariables which contains the Pyomo variable names and their corresponding indices and bounds for experiment degrees of freedom

Comment on lines 1194 to 1202
# If fix_opt is True, fix the design variable
var.fix(design_val[name])
else:
# Otherwise check optimize_option
if optimize_option is None:
# If optimize_option is None, unfix all design variables
var.unfix()

if var.ctype is Var:
if fix_opt:
var.fix(design_val[name])
else:
# Otherwise, unfix only the design variables listed in optimize_option with value True
Copy link
Member

Choose a reason for hiding this comment

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

Why did you remove the comments explaining the logic here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is added back

formula="central", # formula for finite difference
)

result2.result_analysis()
Copy link
Member

Choose a reason for hiding this comment

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

Are you expecting the results to be the same as the previous case? If so I think you should have the same assert statements checking the trace and determinant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I modified this test to use both Param components for parameters and variables, change one parameter value, use assertions to check if the generated model uses the updated model parameter value, and check the trace and determinant.

Comment on lines 636 to 638
for par in self.param:
cuid = pyo.ComponentUID(par)
var = cuid.find_component_on(b)
Copy link
Member

Choose a reason for hiding this comment

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

There is another copy of this code starting on line 612. Does it need to be updated also?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, they are updated now

Comment on lines 1214 to 1215
elif var.ctype is Param:
var = design_val[name]
Copy link
Member

@blnicho blnicho May 9, 2024

Choose a reason for hiding this comment

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

What if this is called with fix_opt=False? Are there cases where the user expects var to be a degree of freedom and they would be surprised by this code silently leaving it as fixed? If this is expected behavior then I think it needs to be clear in the documentation what the tradeoff is when using a design variable declared as a Pyomo Param vs a Pyomo Var.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if with fix_opt=False, var will be unfixed, so it is fine if the user wants to leave it as DOF (they can use fix_opt=False to do that). I add a statement in the doc to note that, if a design variable is defined as Param, stochastic_program function will not work because it cannot be optimized.

@jialuw96
Copy link
Contributor Author

@jialuw96 could you explain the motivation behind this PR? It looks like the Pyomo.DoE API was meant to support design variables specified as either Pyomo mutable Param or Var components but there were a few bugs with the Param case. Is that right?

The current Pyomo.DoE API can only support design variables and to-be-estimated parameters as Var components; This PR allows the user to specify them as either Var or Param components.

The docstrings for the DesignOfExperiments and DesignVariables classes should also be updated. Right now they don't mention that both Param and Var components are accepted: design_vars – A DesignVariables which contains the Pyomo variable names and their corresponding indices and bounds for experiment degrees of freedom

This is updated. Thank you!

@adowling2
Copy link
Member

@blnicho Is this ready for review again?

@adowling2
Copy link
Member

Decision: @adowling2 will work on resolving conflicts and extensive testing after #3267 is merged. This current PR is NOT critical for the Pyomo.DoE workshop next week.

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.

None yet

5 participants