Bart Ch.
Bart Ch.

Reputation: 43

Gurobi - declare constrains in python

I'm trying to declare the constraints below in Python but it does not work.

This is my code:

m.addConstrs((tsk*Xkt[i]).sum(k, '*') + (tbk*Qkt[i]).sum(k, "*") <= bt[i]+Ot[i] for i in range(0, t))

Upvotes: 0

Views: 367

Answers (1)

Greg Glockner
Greg Glockner

Reputation: 5653

The tupledict.sum() method applies to a tupledict object, not a LinExpr such as tsk*Xkt[i]. You have two alternatives:

  1. Use the sum() or quicksum() function, creating an expression like:

    m.addConstrs(quicksum(ts[k]*X[k,t] + tb[k]*Q[k,t] for k in Krange) <= b[t]+O[t] for t in Trange)
    
  2. Use the tupledict.prod() method:

    m.addConstrs(X.prod(ts, '*', t) + Q.prod(tb, '*', t) <= b[t]+O[t] for t in Trange)
    

A few comments:

  1. I changed your subscript notation to better match the mathematical expressions. I am not clear whether the expressions are ts and tb or t ˙ s and t ˙ b; adjust your code accordingly.
  2. There is currently a documentation bug in the syntax for tupledict.prod(); my code above reflects the correct use of this method.

Upvotes: 2

Related Questions