简体   繁体   English

如何修复 Pyomo 中的属性错误 model python

[英]How to fix attribute Error in Pyomo model python

I am trying to write a pyomo model in python3, but I am facing this error that I can't seem to figure out - 'list' object has no attribute 'is_expression_type'.我正在尝试在 python3 中编写一个 pyomo model,但我正面临这个我似乎无法弄清楚的错误 - 'list' object 没有属性 'is_expression_type'。 Below is my pyomo model, any help would be appreciated.以下是我的 pyomo model,如有任何帮助,我们将不胜感激。

    R_avg_tolist = [[0.00043159478649482775,
  0.00045388639592182584,
  0.0006735271301199177,
  0.00044026758948786,
  0.0037176592984565836]]

    Cov_list = [[5.884677519869241e-05,
  5.756542207262417e-05,
  6.017027849080026e-05,
  6.180151597797322e-05,
  -0.0005074353586106837],
 [5.756542207262417e-05,
  6.0380562653096757e-05,
  6.613608499966434e-05,
  6.737370769879904e-05,
  -0.0005362752804115953],
 [6.017027849080026e-05,
  6.613608499966434e-05,
  8.206495000024503e-05,
  8.01694525889321e-05,
  -0.0005958716888916681],
 [6.180151597797322e-05,
  6.737370769879904e-05,
  8.01694525889321e-05,
  0.00010129901491226823,
  -0.000608829853150321],
 [-0.0005074353586106837,
  -0.0005362752804115953,
  -0.0005958716888916681,
  -0.000608829853150321,
  0.007373689071617548]]

    import pyomo.environ as pyo
    
    # Optimization Problem
    def create_model(rho,R_avg,Cov):
        
        m = pyo.ConcreteModel()
        init_x = {}
        m.idx = pyo.Set(initialize=[0,1,2,3,4])
        for i in m.idx:
            init_x[i] = 0
        m.x = pyo.Var(m.idx,initialize=init_x,bounds=(0,None))
        
        def Obj_func(m):
            b = []
            mult_result = 0
            for i in m.idx:
                a = 0
                for j in m.idx:
                    a+= m.x[j]*Cov[j][i]
                b.append(a)
            for i in m.idx:
                mult_result += b[i]*m.x[i]
            
            return mult_result
        m.OBJ = pyo.Objective(rule=Obj_func)
        
        def constraint1(m):
            sum=0
            for i in m.idx:
              sum+=m.x[i]
              return sum ==100  
    
        m.C1 = pyo.Constraint(rule=constraint1(m))
        
        def constraint2(m):
            
            sum=0
            for i in m.idx:
              sum += R_avg_tolist[i]*m.x[i]
    
            return sum >=0.08
    
        m.C2 = pyo.Constraint(rule=constraint2(m))
        
        return m

When I run model using below code, I face the attribute error - 'list' object has no attribute 'is_expression_type'.当我使用以下代码运行 model 时,我遇到了属性错误 - 'list' object 没有属性 'is_expression_type'。

rho = 0.0008
model1 = create_model(rho,R_avg_tolist,Cov_list)

solver = SolverFactory('ipopt')
results = solver.solve(model1, tee = True)

Probably not what you want to hear, but your model has many syntax probs.可能不是您想听到的,但是您的 model 有很多语法问题。 It's obviously a course assignment... Do you have somebody (instructor/TA) to go over this with who can advise a bit?这显然是一个课程作业......你有没有人(讲师/助教)与 go 联系,谁可以提供一些建议?

You didn't include enough info about which line of code caused the issue, but there are several problem areas.您没有包含足够的信息来说明是哪一行代码导致了问题,但是有几个问题区域。 I've posted many simple pyomo examples if you scan through some of them, you'll get some ideas, along with the documentation and whatever you have from your course notes....我已经发布了许多简单的pyomo示例,如果你浏览其中的一些示例,你会得到一些想法,以及文档以及课程笔记中的任何内容......

A few pointers may help:一些建议可能会有所帮助:

Do NOT overwrite keywords/functions by using them as variables.不要将关键字/函数用作变量来覆盖它们。 When you write:当你写:

sum = 0
sum ....

you are nuking the python function sum by making that name a variable and assigning it the value of 0. You should be using sum in several of your functions with verbiage like:你通过使该名称成为一个变量并将其赋值为 0 来核对 python function sum 。你应该在你的几个函数中使用 sum 并使用如下措辞:

sum(m.X[i] for i in m.idx)    # or similar

You seem to be confused on making valid pyomo expressions.你似乎对制作有效的 pyomo 表达式感到困惑。 That is the core job of pyomo... to make expressions and fill the model. For example, in your constraint1 , you can just make an expression (without a function) and add it to your model. You can do a 1-liner there because the constraint is not a "for each".这是 pyomo 的核心工作...制作表达式并填充 model。例如,在您的constraint1中,您可以制作一个表达式(没有函数)并将其添加到您的 model。您可以做一个 1-liner那里是因为约束不是“for each”。 You could:你可以:

m.C1 = pyo.Constraint(expr=sum(m.x[i] for x in m.idx) == 100)

In general, when you are starting:一般来说,当你开始时:

Add 1 thing to your model, and then print the model:添加 1 东西到你的 model,然后打印 model:

model.pprint()

See if it looks right, if not, fix it.看看它是否看起来正确,如果不正确,请修复它。 Then repeat!然后重复!

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

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