-
Notifications
You must be signed in to change notification settings - Fork 548
Parmest update of util convert_params_to_vars. #3339
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
a5d949e
e534e32
0ae57d4
7b64490
efa6465
7ea02db
394a6d6
0545ecf
d432500
577aea3
92c23f0
1401893
c249edb
51d1ba7
b07b547
5b947fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,16 +22,16 @@ | |
logger = logging.getLogger(__name__) | ||
|
||
|
||
def convert_params_to_vars(model, param_names=None, fix_vars=False): | ||
def convert_params_to_vars(model, param_CUIDs=None, fix_vars=False): | ||
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. TODO: make sure the param domain is also being transferred to the new var |
||
""" | ||
Convert select Params to Vars | ||
|
||
Parameters | ||
---------- | ||
model : Pyomo concrete model | ||
Original model | ||
param_names : list of strings | ||
List of parameter names to convert, if None then all Params are converted | ||
param_CUIDs : list of strings | ||
List of parameter CUIDs to convert, if None then all Params are converted | ||
fix_vars : bool | ||
Fix the new variables, default is False | ||
|
||
|
@@ -43,21 +43,31 @@ def convert_params_to_vars(model, param_names=None, fix_vars=False): | |
|
||
model = model.clone() | ||
|
||
if param_names is None: | ||
param_names = [param.name for param in model.component_data_objects(pyo.Param)] | ||
if param_CUIDs is None: | ||
param_CUIDs = [ | ||
ComponentUID(param) for param in model.component_data_objects(pyo.Param) | ||
] | ||
|
||
indexed_param_names = [] | ||
# if param_CUIDs is None: | ||
# param_CUIDs = [ | ||
# ComponentUID(param.name) | ||
# for param in model.component_data_objects(pyo.Param) | ||
# ] | ||
mrmundt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# keep a list of the parameter CUIDs in the case of indexing | ||
indexed_param_CUIDs = [] | ||
|
||
# Convert Params to Vars, unfix Vars, and create a substitution map | ||
substitution_map = {} | ||
comp_map = ComponentMap() | ||
for i, param_name in enumerate(param_names): | ||
for param_CUID in param_CUIDs: | ||
|
||
# Leverage the parser in ComponentUID to locate the component. | ||
theta_cuid = ComponentUID(param_name) | ||
theta_object = theta_cuid.find_component_on(model) | ||
theta_object = param_CUID.find_component_on(model) | ||
|
||
# Param | ||
if theta_object.is_parameter_type(): | ||
|
||
# Delete Param, add Var | ||
vals = theta_object.extract_values() | ||
model.del_component(theta_object) | ||
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. I don't think this will work on hierarchical models. I think we need to add more tests for edge cases. 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. I think I fixed this and added a test. |
||
|
@@ -69,20 +79,24 @@ def convert_params_to_vars(model, param_names=None, fix_vars=False): | |
substitution_map[id(theta_object)] = theta_var_object | ||
comp_map[theta_object] = theta_var_object | ||
|
||
# Indexed Param | ||
# Indexed Param -- Delete Param, add Var | ||
elif isinstance(theta_object, IndexedParam): | ||
# Delete Param, add Var | ||
# Before deleting the Param, create a list of the indexed param names | ||
|
||
# save Param values | ||
vals = theta_object.extract_values() | ||
param_theta_objects = [] | ||
for theta_obj in theta_object: | ||
indexed_param_name = theta_object.name + '[' + str(theta_obj) + ']' | ||
theta_cuid = ComponentUID(indexed_param_name) | ||
param_theta_objects.append(theta_cuid.find_component_on(model)) | ||
indexed_param_names.append(indexed_param_name) | ||
|
||
# get indexed Params | ||
param_theta_objects = [theta_obj for _, theta_obj in theta_object.items()] | ||
blnicho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# get indexed Param names | ||
blnicho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
indexed_param_CUIDs += [ | ||
ComponentUID(theta_obj) for _, theta_obj in theta_object.items() | ||
] | ||
blnicho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# delete Param | ||
model.del_component(theta_object) | ||
|
||
# add Var w/ previous Param values | ||
index_name = theta_object.index_set().name | ||
index_cuid = ComponentUID(index_name) | ||
index_object = index_cuid.find_component_on(model) | ||
|
@@ -94,13 +108,9 @@ def convert_params_to_vars(model, param_names=None, fix_vars=False): | |
theta_var_cuid = ComponentUID(theta_object.name) | ||
theta_var_object = theta_var_cuid.find_component_on(model) | ||
comp_map[theta_object] = theta_var_object | ||
var_theta_objects = [] | ||
for theta_obj in theta_var_object: | ||
theta_cuid = ComponentUID( | ||
theta_var_object.name + '[' + str(theta_obj) + ']' | ||
) | ||
var_theta_objects.append(theta_cuid.find_component_on(model)) | ||
|
||
var_theta_objects = [ | ||
var_theta_obj for _, var_theta_obj in theta_var_object.items() | ||
] | ||
blnicho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for param_theta_obj, var_theta_obj in zip( | ||
param_theta_objects, var_theta_objects | ||
): | ||
|
@@ -124,14 +134,14 @@ def convert_params_to_vars(model, param_names=None, fix_vars=False): | |
if len(substitution_map) == 0: | ||
return model | ||
|
||
# Update the list of param_names if the parameters were indexed | ||
if len(indexed_param_names) > 0: | ||
param_names = indexed_param_names | ||
# Update the list of param_CUIDs if the parameters were indexed | ||
if len(indexed_param_CUIDs) > 0: | ||
param_CUIDs = indexed_param_CUIDs | ||
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. Why do we replace 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. I think we have to replace it because it might not have the indexed variables, only the main variable. For example "theta" (param_CUIDs) vs. "theta[asymptote]" and "theta[rate_constant]" (indexed_param_CUIDs). |
||
|
||
# Convert Params to Vars in Expressions | ||
for expr in model.component_data_objects(pyo.Expression): | ||
if expr.active and any( | ||
blnicho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
v.name in param_names for v in identify_mutable_parameters(expr) | ||
ComponentUID(v) in param_CUIDs for v in identify_mutable_parameters(expr) | ||
blnicho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
): | ||
new_expr = replace_expressions(expr=expr, substitution_map=substitution_map) | ||
model.del_component(expr) | ||
|
@@ -143,7 +153,8 @@ def convert_params_to_vars(model, param_names=None, fix_vars=False): | |
model.constraints = pyo.ConstraintList() | ||
for c in model.component_data_objects(pyo.Constraint): | ||
if c.active and any( | ||
v.name in param_names for v in identify_mutable_parameters(c.expr) | ||
ComponentUID(v) in param_CUIDs | ||
for v in identify_mutable_parameters(c.expr) | ||
): | ||
if c.equality: | ||
model.constraints.add( | ||
|
@@ -181,7 +192,7 @@ def convert_params_to_vars(model, param_names=None, fix_vars=False): | |
# Convert Params to Vars in Objective expressions | ||
for obj in model.component_data_objects(pyo.Objective): | ||
if obj.active and any( | ||
v.name in param_names for v in identify_mutable_parameters(obj) | ||
ComponentUID(v) in param_CUIDs for v in identify_mutable_parameters(obj) | ||
): | ||
expr = replace_expressions(expr=obj.expr, substitution_map=substitution_map) | ||
model.del_component(obj) | ||
|
Uh oh!
There was an error while loading. Please reload this page.