0

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

1
  • Can you tell us what line the error points to? It seems likely that either model.branch_tbus or model.branch_tbus[j] (or the equivalent on "fbus") is not an iterable type (list, tuple, etc.). Commented Mar 3, 2012 at 5:03

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! This fixed everything. I wish I could vote but I don't have a high enough reputation. You are great!
@user1246364 Clearly you can accept answer if it's satisfied your needs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.