-
Notifications
You must be signed in to change notification settings - Fork 557
Add update_model utility to utils.py for updating suffix values in Pyomo.DoE #3650
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
Merged
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
222b396
Add utility to utils.py
sscini 81a595f
Ran black to update formatting
sscini a3504f8
Merge branch 'Pyomo:main' into add-update-model-utility
sscini 748c732
Added example in doe to show use of util
sscini 3e3a5b8
Modified utils based on 7/1 meeting
sscini 403eae7
Updated example and added tests, still debugging
sscini 1304112
Merge branch 'Pyomo:main' into add-update-model-utility
sscini 5ea8ba5
Merge branch 'Pyomo:main' into add-update-model-utility
sscini df4de56
Moved utility from doe to parmest
sscini 2b6e304
Updated test_utils, confirming they work tomorrow.
sscini 02460d5
Ran black
sscini 67dccc5
Updated doe utils to match new in main
sscini 63e3a7d
Merge branch 'main' into add-update-model-utility
sscini 692fa9e
Ran black on doe
sscini cb394e7
Merge branch 'add-update-model-utility' of https://github.com/sscini/…
sscini a4f3d1c
Ran black again after merging changes
sscini d2786d7
Added parmest example, fixed and added tests, ran black.
sscini aa3b6f1
Remove changes from doe tests
sscini 7b390b1
Addressed comments from Dan that do not need clarification
sscini 21db44b
Merge branch 'Pyomo:main' into add-update-model-utility
sscini d43721a
Updated files and added test to address comments
sscini dee6c38
Ran black on doe and parmest
sscini c013ac4
Merge branch 'Pyomo:main' into add-update-model-utility
sscini a25875c
Made edits to address comments, ran black
sscini cf37a8b
Merge branch 'Pyomo:main' into add-update-model-utility
sscini 92f772e
Merge branch 'main' into add-update-model-utility
mrmundt 4d1dfec
Addressed comments and ran black
sscini 793f34a
Update updatesuffix_example.py
sscini a7babec
Delete scenarios.csv
sscini 9ea7e31
Update test_utils.py
sscini 14f7eb6
Addressed file name changes
sscini a47c54f
Addressed consistency changes
sscini 0a4c9a2
Merge branch 'main' into add-update-model-utility
sscini 6efaa0e
Merge branch 'main' into add-update-model-utility
mrmundt 2767263
Apply suggestions from code review
blnicho 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# ___________________________________________________________________________ | ||
# | ||
# 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. | ||
# ___________________________________________________________________________ | ||
from pyomo.common.dependencies import numpy as np | ||
|
||
from pyomo.contrib.doe.examples.reactor_experiment import ReactorExperiment | ||
from pyomo.contrib.doe import DesignOfExperiments | ||
from pyomo.contrib.doe import utils | ||
|
||
from pyomo.contrib.parmest.utils.model_utils import update_model_from_suffix | ||
from os.path import join, abspath, dirname | ||
|
||
import pyomo.environ as pyo | ||
|
||
import json | ||
|
||
|
||
# Example to run a DoE on the reactor | ||
def main(): | ||
# Read in file | ||
file_dirname = dirname(abspath(str(__file__))) | ||
file_path = abspath(join(file_dirname, "result.json")) | ||
|
||
# Read in data | ||
with open(file_path) as f: | ||
data_ex = json.load(f) | ||
|
||
# Put temperature control time points into correct format for reactor experiment | ||
data_ex["control_points"] = { | ||
float(k): v for k, v in data_ex["control_points"].items() | ||
} | ||
|
||
# Create a ReactorExperiment object; data and discretization information are part | ||
# of the constructor of this object | ||
experiment = ReactorExperiment(data=data_ex, nfe=10, ncp=3) | ||
|
||
# Call the experiment's model using get_labeled_model | ||
reactor_model = experiment.get_labeled_model() | ||
|
||
# Show the model | ||
reactor_model.pprint() | ||
# The suffix object 'measurement_error' stores measurement error values for each component. | ||
# Here, we retrieve the original values from the suffix for inspection. | ||
suffix_obj = reactor_model.measurement_error | ||
me_vars = list(suffix_obj.keys()) # components | ||
orig_vals = np.array(list(suffix_obj.values())) | ||
|
||
# Original values | ||
print("Original sigma values") | ||
print("-----------------------") | ||
suffix_obj.display() | ||
|
||
# Update the suffix with new values | ||
new_vals = orig_vals + 1 | ||
# Here we are updating the values of the measurement error | ||
# You must know the length of the list and order of the suffix items to update them correctly | ||
update_model_from_suffix(suffix_obj, new_vals) | ||
|
||
# Updated values | ||
print("Updated sigma values :") | ||
print("-----------------------") | ||
suffix_obj.display() | ||
return suffix_obj, orig_vals, new_vals | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
67 changes: 67 additions & 0 deletions
67
pyomo/contrib/parmest/examples/reactor_design/update_suffix_example.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,67 @@ | ||
# ___________________________________________________________________________ | ||
# | ||
# 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. | ||
# ___________________________________________________________________________ | ||
|
||
from pyomo.common.dependencies import numpy as np, pandas as pd | ||
from os.path import join, abspath, dirname | ||
import pyomo.contrib.parmest.parmest as parmest | ||
from pyomo.contrib.parmest.examples.reactor_design.reactor_design import ( | ||
ReactorDesignExperiment, | ||
) | ||
|
||
import pyomo.environ as pyo | ||
from pyomo.contrib.parmest.utils.model_utils import update_model_from_suffix | ||
|
||
|
||
def main(): | ||
# Read in file | ||
# Read in data | ||
file_dirname = dirname(abspath(str(__file__))) | ||
file_name = abspath(join(file_dirname, "reactor_data.csv")) | ||
data = pd.read_csv(file_name) | ||
|
||
experiment = ReactorDesignExperiment(data, 0) | ||
|
||
# Call the experiment's model using get_labeled_model | ||
reactor_model = experiment.get_labeled_model() | ||
|
||
example_suffix = "unknown_parameters" | ||
suffix_obj = reactor_model.unknown_parameters | ||
var_list = list(suffix_obj.keys()) # components | ||
orig_var_vals = np.array([pyo.value(v) for v in var_list]) # numeric var values | ||
|
||
# Print original values | ||
print("Original sigma values") | ||
print("----------------------") | ||
print(orig_var_vals) | ||
|
||
# Update the suffix with new values | ||
new_vals = orig_var_vals + 0.5 | ||
|
||
print("New sigma values") | ||
print("----------------") | ||
print(new_vals) | ||
|
||
# Here we are updating the values of the unknown parameters | ||
# You must know the length of the list and order of the suffix items to update them correctly | ||
update_model_from_suffix(suffix_obj, new_vals) | ||
|
||
# Updated values | ||
print("Updated sigma values :") | ||
print("-----------------------") | ||
new_var_vals = np.array([pyo.value(v) for v in var_list]) | ||
print(new_var_vals) | ||
|
||
# Return the suffix obj, original and new values for further use if needed | ||
return suffix_obj, new_vals, new_var_vals | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
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.
Uh oh!
There was an error while loading. Please reload this page.