user1246364
user1246364

Reputation: 1

Python - Using an array index that depends on an input value

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

Upvotes: 0

Views: 692

Answers (1)

Odomontois
Odomontois

Reputation: 16308

Furst time seeing this, but inptecting the source, we can see, that class _ParamValue defined here is extending class NumericConstant, defined here, from code we can learn there is attribute value, so the only thing i can suggest is to change code something like that:

def Line_FlowA_rule(model,j):

    t = model.branch_tbus[j].value
    f = model.branch_fbus[j].value

    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 that can work of course if those parameters are holding indexes for other parameters.

Upvotes: 1

Related Questions