Try our new documentation site (beta).


sum ( pattern )

Returns the sum of the values associated with keys that match the specified pattern. If the values are Gurobi Var objects, the result is a LinExpr. The pattern should provide one value for each field in the key tuple. A '*' value indicates that any value is accepted in that field.

Without arguments, this method returns the sum of all values in the tupledict.


pattern: Pattern to match for a key tuple.

Example usage:

  x = m.addVars([(1,2), (1,3), (2,3)])
  expr = x.sum()       # LinExpr: x[1,2] + x[1,3] + x[2,3]
  expr = x.sum(1, '*') # LinExpr: x[1,2] + x[1,3]
  expr = x.sum('*', 3) # LinExpr: x[1,3] + x[2,3]
  expr = x.sum(1, 3)   # LinExpr: x[1,3]

Try Gurobi for Free

Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Evaluation License
Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Academic License
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.