0
mdl = Model('CVRP')

x = mdl.binary_var_dict(A, name='x')
u = mdl.continuous_var_dict(N, name='u')

mdl.minimize(mdl.sum(distanceList[i][j]*x[i, j] for i, j in A))
mdl.add_constraints(mdl.sum(x[i, j] for j in V if j != i) == 1 for i in N)
mdl.add_constraints(mdl.sum(x[i, j] for i in V if i != j) == 1 for j in N)
mdl.add_constraints(mdl.add(u[i] - u[j] + n*(x[i,j]) <=  n - 1  for i in N for j in N if i!=j))

When I ran this code, I got this warning:

Warning: constraint has already been posted: 25x_25_24-u_24+u_25 <= 24, index is: 649

I think my model duplicates same constraints many times, but I cannot figure that why this is happening. It is related to the last constraint that provides subtour elimination.

I really appreciate that somebody helps me to get rid of duplication or a new subtour elimination constraint. Thanks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mustafa
  • 1
  • 3

1 Answers1

0

Model.add(..) builds a constraint , add it to the model and returns it. Thus when you call Model.add_constraints on the results of the various Model.add calls, you end adding constraints *which have already been added.

Removing the call to Model.add should do the trick:

mdl.add_constraints((u[i] - u[j] + n*(x[i,j]) <=  n - 1) for i in N for j in N if i!=j)
Philippe Couronne
  • 826
  • 1
  • 5
  • 6