Reputation: 185
I am using the pyomo library to solve a Traveling Salesman Problem (TSP), but there is an error. 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:
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. 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.
Error thrown for Constraint constraint_1
Constraint 1 has only index i, and the sum is over it. 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.
Upvotes: 1
Views: 975
Reputation: 2634
What is the value of lendist
?
It looks like the model.set_I
is empty (which will happen if lendist>=1
). When Python sums over an empty iterable it returns the integer constant 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). As False
is not a valid Pyomo expression (and is trivially infeasible), Pyomo raises the error.
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
.
Upvotes: 2