0

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))

2
  • 1
    this is not your code. This is a line of your code. Please provide a minimal reproducible example and the exact error. Jan 8, 2017 at 16:52
  • Sorry for terminology. Error is so long that I did not want to paste it here. and the end it says only "KeyError: 0"
    – Bart Ch.
    Jan 8, 2017 at 17:18

1 Answer 1

2

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.
1
  • Thank you so much for help! I chose quiksum - done the job well!
    – Bart Ch.
    Jan 8, 2017 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.