ERROR: Constructing component 'objective' from data=None failed: TypeError: 'float' object is not subscriptable

不问归期 提交于 2019-12-25 01:45:28

问题


Team Pyomo, I kindly need help with the above-stated error. I have done everything I could, but still can't get my model to work. Below is the formulation of my 'Objective Function', and screenshots of the errors message. Thank you.

Screenshot of the error from the running code at the command prompt:


回答1:


Assuming model.x and model.d are declared correctly with 2-D indices, the problem is that you are using double square brackets. The correct way to access a particular index is model.x[i,j].

Here are the correct ways to declare model.x, model.d, and model.a.

Assuming model.a is two dimensional:

model.a = Set(initialize=[(1,1),(1,2),(2,1),(2,2)])
model.d = Param(model.a, default=0)
model.x = Var(model.a)

def _obj_rule(m):
    return sum(m.d[i,j]*m.x[i,j] for i,j in m.a)
model.obj = Objective(rule=_obj_rule)

Assuming model.a is one dimensional:

model.a = Set(initialize=[1,2,3])
model.d = Param(model.a,model.a,default=0)
model.x = Param(model.a,model.a)

def _obj_rule(m):
    return sum(m.d[i,j]*m.x[i,j] for i in m.a for j in m.a)
model.obj = Objective(rule=_obj_rule)

Notice that either model.a is declared as two dimensional or model.x and model.d are indexed by model.a twice. Also notice the slight differences in how the sum in the objective is written.



来源:https://stackoverflow.com/questions/48198946/error-constructing-component-objective-from-data-none-failed-typeerror-flo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!