简体   繁体   English

Python-使用取决于输入值的数组索引

[英]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). 我正在尝试在名为pyomo(Python库)的程序中编写数学程序。 Below, model is an object I declared already, BRANCH is a set; 下面,模型是我已经声明的对象, BRANCH是集合; 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). model.branch_scptmodel.Mmodel.branch_tbusmodel.branch_fbus都是在代码执行时作为输入加载的参数列表(它们的维数均为BRANCH)。 Additionally, model.bus_angle , model.line_flow , and model.z_line are lists of decision variables (all also of dimension BRANCH). 此外, model.bus_anglemodel.line_flowmodel.z_line是决策变量的列表(所有维度均为BRANCH)。 This is the definition of one of my constraint types, where j is in BRANCH : 这是我的一种约束类型的定义,其中jBRANCH

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). 注意,元件model.bus_angle[j]在约束中引用Line_FlowA[j]依赖于元件上model.branch_tbus[j]返回(类似地,元素model.branch_fbus[j]返回)。 However, model.branch_tbus[j] is a data input value and I believe this is what is causing the following error: 但是, model.branch_tbus[j]是数据输入值,我相信这是导致以下错误的原因:

"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: 最后,我尝试将值t和f转换为不可变的类型:

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. 我是python的新手,所以它可能是基本的东西。 Thanks a lot 非常感谢

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: 弗斯特一次看到这一点,但inptecting源,我们可以看到, class _ParamValue定义在这里被扩展class NumericConstant ,定义在这里 ,从代码中我们可以得知有属性value ,所以我可以建议的唯一的事情就是改变代码类似那:

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. 但是,如果那些参数保存着其他参数的索引,那当然可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM