如何修复在Pyomo中出现的“ValueError: 约束没有适当的值”错误?

huangapple go评论74阅读模式
英文:

How can I fix 'ValueError: Constraint does not have a proper value' in Pyomo?

问题

# ValueError: 约束条件没有适当的值。
英文:

ValueError: Constraint does not have a proper value.

ValueError: Constraint 'uvuoncea122222222222[1]' does not have a proper value. Found 'AbstractScalarConstraint'
Expecting a tuple or relational expression. Examples:
   sum(model.costs) == model.income
   (0, model.price[item], 50)

This is the error I encounter for constraint 3. Here is the code and how I defined my sets and decision variables:

## Sets
model.P = pe.Set(initialize = depots, ordered = False) #depots
model.S = pe.Set(initialize = satellites, ordered = False) #satellites
model.I = pe.Set(within=model.P | model.S) #union depots and satellites
model.Z = pe.Set(initialize = customers, ordered = False) #customers
model.Y = pe.Set(within=model.S | model.Z) #union customers and satellites
model.T = pe.Set(initialize=numUV, ordered=False) #FE vehicle
model.F = pe.Set(initialize=numCF, ordered=False) #SE vehicle
model.V = model.P | model.S | model.Z
model.A = model.V*model.V

## Decision variables
model.x = pe.Var(model.T, model.P, model.S, domain = pe.NonNegativeIntegers) #flow FE
model.r = pe.Var(model.T, model.P, model.S, domain = pe.Binary) #binary FE arc used
model.q = pe.Var(model.F, model.S, model.Z, domain = pe.Binary) #binary SE arc used
model.w = pe.Var(model.Z, model.S, domain = pe.Binary) #binary Z gekoppeld aan S
model.u1 = pe.Var(model.T, domain = pe.Binary) #binary FE vehicle used
model.u2 = pe.Var(model.F, domain = pe.Binary) #binary SE vehicle used
model.p1 = pe.Var(model.T, model.S,domain = pe.NonNegativeIntegers) #arrival time at s
model.b1 = pe.Var(model.F, model.Z, domain = pe.NonNegativeIntegers) #arrival time at z
#model.pi = pe.Var(model.F, domain = pe.NonNegativeIntegers) #working duration
model.pprint()

## Objective function
objExpr = (sum(model.r[t,i,j]* c[i,j] * cKm for t in model.T for i in model.I for j in model.I)\
          + sum(model.q[f,i,j]* c[i,j] * cKm for f in model.F for i in model.Y for j in model.Y)\
          + sum(model.u1[t]* ch1 for t in model.T) + sum(model.u2[f] * ch2 for f in model.F))
model.obj = pe.Objective(expr = objExpr, sense = pe.minimize)

## Constraints
#2
model.confcfe = pe.ConstraintList()
for j in model.I:
    for t in model.T:
        expression = sum(model.r[t,l,j] for l in model.I) - sum(model.r[t,j,l] for l in model.I) == 0
        model.confcfe.add(expression)
#3
model.uvuoncea122222222222 = pe.ConstraintList()
for t in model.T:
    expression = sum(model.r[t, i, j] for i in model.I for j in model.P)
    constraint = pe.Constraint(expr=expression <= 1)
    model.uvuoncea122222222222.add(constraint)

The reason for coding constraint 3 in another style then constraint 2 is that I get a trivial/boolean error for constraint3, and it seems like that is fixed while coding the constraint in this manner.

I tried to defining the constraint in different manners (like defining a constraint rule, indirect/direct into the model, defining them as a range (0,sum(...),1). It all didn't work..

答案1

得分: 1

The error is telling you that the type of expression needed when making a constraint is a relational expression, so where you have this:

错误提示您,制定约束时需要的表达式类型是*关系*表达式,所以在这里:

expression = sum(model.r[t, i, j] for i in model.I for j in model.P)
constraint = pe.Constraint(expr=expression <= 1)

表达式 = sum(model.r[t, i, j] for i in model.I for j in model.P) <= 1
约束 = pe.Constraint(expr=expression)

The constraint above should be able to be constructed several different ways, so there is likely something else wrong/not handled appropriately if it is not working, but there isn't enough information in your question to deduce why.

上面的约束应该可以以多种不同的方式构建,所以如果它不起作用,很可能还有其他问题/没有适当处理,但是您的问题中没有足够的信息来推断原因。

On a separate note, I think you are setting yourself up for trouble with the way you are indexing/accessing your variables.

另外,请注意,我认为您在索引/访问变量方面可能会遇到麻烦。

You create model.r indexed by [T, P, S] but then in your constraint 2 you are accessing it by [T, I, I] and in constraint 3 you are accessing it by [T, I, P]. That's not going to work as the domain of the sets in the constraints are outside (or supersets) of the variable indices.

您创建了按[T, P, S]索引的model.r,但是在约束2中,您正在通过[T, I, I]访问它,在约束3中,您正在通过[T, I, P]访问它。这不会起作用,因为约束中集合的域位于变量索引之外(或是变量索引的超集)。

If you are stuck there, post a new question with a small instance of the model data that you are initializing all of your variables from and some additional comments about what the purpose of the objectives are, and you'll likely get some help. The code in the example should be able to execute model.pprint().

如果您在这方面遇到问题,请发布一个新的问题,其中包含您正在初始化所有变量的模型数据的小示例,以及关于目标的目的的一些额外注释,您可能会得到一些帮助。示例中的代码应该能够执行model.pprint()

英文:

The error is telling you that the type of expression needed when making a constraint is a relational expression, so where you have this:

expression = sum(model.r[t, i, j] for i in model.I for j in model.P)
constraint = pe.Constraint(expr=expression <= 1)

bring the RHS (right hand side) up to the expression, like you did in the previous constraint:

expression = sum(model.r[t, i, j] for i in model.I for j in model.P) <= 1
constraint = pe.Constraint(expr=expression)

The constraint above should be able to be constructed several different ways, so there is likely something else wrong/not handled appropriately if it is not working, but there isn't enough information in your question to deduce why.

On a separate note, I think you are setting yourself up for trouble with the way you are indexing/accessing your variables.

You create model.r indexed by [T, P, S] but then in your constraint 2 you are accessing it by [T, I, I] and in constraint 3 you are accessing it by [T, I, P]. That's not going to work as the domain of the sets in the constraints are outside (or supersets) of the variable indices.

If you are stuck there, post a new question with a small instance of the model data that you are initializing all of your variables from and some additional comments about what the purpose of the objectives are and you'll likely get some help. The code in the example should be able to execute model.pprint().

huangapple
  • 本文由 发表于 2023年5月21日 17:22:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299163.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定