Skip to content

Commit 33e1bd0

Browse files
sscinismondal13
authored andcommitted
updated with sscini's utils.py.
1 parent 043e7de commit 33e1bd0

1 file changed

Lines changed: 18 additions & 28 deletions

File tree

pyomo/contrib/doe/utils.py

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -126,47 +126,37 @@ def rescale_FIM(FIM, param_vals):
126126

127127

128128
# Adding utility to update parameter values in a model based on the suffix
129-
def update_model_from_suffix(model, suffix_name, values):
129+
def update_model_from_suffix(suffix_obj: pyo.Suffix, values):
130130
"""
131-
Iterate over the components (variables or parameters) referenced by the
132-
given suffix in the model, and assign each a new value from the provided iterable.
131+
Overwrite each variable/parameter referenced by ``suffix_obj`` with the
132+
corresponding value in ``values``.
133133
134134
Parameters
135135
----------
136-
model : pyomo.environ.ConcreteModel
137-
The Pyomo model containing the suffix and components to update.
138-
suffix_name : str
139-
The name of the Suffix attribute on the model whose items will be updated.
140-
Must be one of: 'experiment_outputs', 'experiment_inputs', 'unknown_parameters', or 'measurement_error'.
136+
suffix_obj : pyomo.core.base.suffix.Suffix
137+
The suffix whose *keys* are the components you want to update.
138+
Call like ``update_from_suffix(model.unknown_parameters, vals)``.
141139
values : iterable of numbers
142-
The new values to assign to each component referenced by the suffix. The length of this
143-
iterable must match the number of items in the suffix.
140+
New numerical values for the components referenced by the suffix.
141+
Must be the same length as ``suffix_obj``.
144142
"""
145-
# Allowed suffix names
146-
allowed = {
147-
"experiment_outputs",
148-
"experiment_inputs",
149-
"unknown_parameters",
150-
"measurement_error",
151-
}
152-
# Validate input is an allowed suffix name
153-
if suffix_name not in allowed:
154-
raise ValueError(f"suffix_name must be one of {sorted(allowed)}")
155-
# Check if the model has the specified suffix
156-
suffix_obj = getattr(model, suffix_name, None)
157-
if suffix_obj is None:
158-
raise AttributeError(f"Model has no attribute '{suffix_name}'")
159-
# Check if the suffix is a Suffix object
143+
# Check that the length of values matches the suffix length
160144
items = list(suffix_obj.items())
161145
if len(items) != len(values):
162146
raise ValueError("values length does not match suffix length")
163-
# Set the new values for the suffix items
147+
148+
# Iterate through the items in the suffix and update their values
149+
# Note: items are tuples of (component, suffix_value)
164150
for (comp, _), new_val in zip(items, values):
165-
# Update the variable/parameter itself if it is VarData or ParamData
151+
152+
# update the component value
153+
# Check if the component is a VarData or ParamData
166154
if isinstance(comp, (VarData, ParamData)):
167155
comp.set_value(new_val)
168156
else:
169-
raise TypeError(f"Unsupported component type: {type(comp)}")
157+
raise TypeError(
158+
f"Unsupported component type {type(comp)}; expected VarData or ParamData."
159+
)
170160

171161

172162
def check_FIM(FIM):

0 commit comments

Comments
 (0)