I am trying to write a mathematical program in a program called pyomo (a python library). Below, model is an object I declared already, BRANCH is a set; model.branch_scpt, model.M, model.branch_tbus, and model.branch_fbus are all parameter lists that are loaded as input when the code executes (They all have dimension = BRANCH). Additionally, model.bus_angle, model.line_flow, and model.z_line are lists of decision variables (all also of dimension BRANCH). This is the definition of one of my constraint types, where j is in BRANCH:
def Line_FlowA_rule(model,j):
return ( model.branch_scpt[j]*( model.bus_angle[model.branch_tbus[j]]
- model.bus_angle[model.branch_fbus[j]] )
- model.line_flow[j] + (1 - model.z_line[j]) * model.M[j] >= 0 )
model.Line_FlowA = Constraint(model.BRANCH, rule=LineFlowA_rule)
Notice that the element model.bus_angle[j] referenced in constraint Line_FlowA[j] depends on the element that model.branch_tbus[j] returns (similarly, the element that model.branch_fbus[j] returns). However, model.branch_tbus[j] is a data input value and I believe this is what is causing the following error:
"Unexpected exception while running model arpatest_nbp_constraint.py
Unable to index variable bus_angle using supplied index with unhashable type: '_ParamValue'"
In order to make the function neater, I tried to redefine the function as follows:
def Line_FlowA_rule(model,j):
t = model.branch_tbus[j]
f = model.branch_fbus[j]
return ( model.branch_scpt[j]*( model.bus_angle[f]
- model.bus_angle[t] )
- model.line_flow[j] + (1 - model.z_line[j]) * model.M[j] >= 0 )
model.Line_FlowA = Constraint(model.BRANCH, rule=Line_FlowA_rule)
but I obtained the same error.
Lastly, to convert the values t and f into immutable types I tried:
def Line_FlowA_rule(model,j):
t = tuple(model.branch_tbus[j])
f = tuple(model.branch_fbus[j])
return ( model.branch_scpt[j]*( model.bus_angle[f]
- model.bus_angle[t] )
- model.line_flow[j] + (1 - model.z_line[j]) * model.M[j] >= 0 )
model.Line_FlowA = Constraint(model.BRANCH, rule=Line_FlowA_rule)
This led to the error:
ERROR: Unexpected exception while running model arpatest_nbp_constraint.py
'_ParamValue' object is not iterable
Can anyone please tell me what I am doing wrong here? I am new to python so it is probably something basic. Thanks a lot
model.branch_tbusormodel.branch_tbus[j](or the equivalent on "fbus") is not an iterable type (list, tuple, etc.).