简体   繁体   English

Pyomo 中的无效约束表达式错误

[英]Invalid constraint expression error in Pyomo

I am using the pyomo library to solve a Traveling Salesman Problem (TSP), but there is an error.我正在使用 pyomo 库来解决旅行商问题 (TSP),但出现错误。 The main part of the model is as follows:该模型的主要部分如下:

model.set_I = range(lendist, 1) # The set related to the distance matrix
model.set_J = range(nStops, 1) # nStop is the number of cities and set_J is the related set.
model.trips = Var(model.set_I, domain=NonNegativeReals, bounds=(0, 1)) # trips is the decision variable

def obj_expression(model): # Objective function
    return sum(model.dist[i]*model.trips[i] for i in model.set_I)
model.OBJ = Objective(rule=obj_expression)

# Constraint 1:
def constraint_1(model):
    return sum(model.trips[i] for i in model.set_I) == nStops
model.constraint_1 = Constraint(rule=constraint_1)

# Constraint 2:
def constraint_2(model, j):
    return sum(model.trips[whichIdxs[i, j]] for i in model.set_I) == 2
model.constraint_2 = Constraint(model.set_J, rule=constraint_2)

The resulting error is as follows:结果错误如下:

ERROR: Constructing component 'constraint_1' from data=None failed:错误:从 data=None 构建组件“constraint_1”失败:

ValueError: Invalid constraint expression. The constraint expression resolved to a trivial Boolean (False) instead of a Pyomo object. Please modify your rule to return Constraint. Infeasible instead of False.

ValueError: Invalid constraint expression. ValueError: 无效的约束表达式。 The constraint expression resolved to a trivial Boolean (False) instead of a Pyomo object.约束表达式解析为一个简单的布尔值 (False) 而不是 Pyomo 对象。 Please modify your rule to return Constraint.请修改您的规则以返回约束。 Infeasible instead of False.不可行而不是 False。

Error thrown for Constraint constraint_1 Constraintconstraint_1 引发的错误

Constraint 1 has only index i, and the sum is over it.约束 1 只有索引 i,并且总和在它上面。 Therefore, there is no index after the rule definition.因此,规则定义后没有索引。 Is the problem here?问题出在这里了吗? In fact, a similar situation is valid for the objective function, but there is no error about this.事实上,类似的情况对目标函数也是有效的,但没有错误。

What is the value of lendist ? lendist的价值是lendist

It looks like the model.set_I is empty (which will happen if lendist>=1 ).看起来model.set_I是空的(如果lendist>=1就会发生这种情况)。 When Python sums over an empty iterable it returns the integer constant 0 .当 Python 对空的可迭代对象求和时,它返回整数常量0 Assuming that you have nStops != 0 , then the expression is evaluated by Python in the constraint_1 function, causing it to return False (and not a Pyomo expression object).假设您有nStops != 0 ,那么表达式由 Python 在constraint_1函数中计算,导致它返回False (而不是 Pyomo 表达式对象)。 As False is not a valid Pyomo expression (and is trivially infeasible), Pyomo raises the error.由于False不是有效的 Pyomo 表达式(并且几乎不可行),因此 Pyomo 会引发错误。

Remember that when you pass two arguments to the Python range() function, the arguments specify the starting and ending values: range(start, end) only returns the integers starting at start up to, but and not including end .请记住,当您将两个参数传递给 Python range()函数时,这些参数指定了起始值和结束值: range(start, end)仅返回从start到的整数,但不包括end

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

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