Search code examples
juliajulia-jump

JuMP constraint specification using a generator


I am working through some of the JuMP example and in particular the multi commodity flow model. I am able to generate a constraint using the below:

for r in eachrow(supply)
  @constraint(model,sum(x[r.origin,:, r.product]) <= r.supply)

This works fine, however this does not:

@constraint(model,sum(x[r.origin,:, r.product]) <= r.supply for r in eachrow(supply))

The error thrown is below:

Unsupported constraint expression: we don't know how to parse constraints containing expressions of type :generator.

This seems odd because if I use a generator in an objective definition it works just fine:

@objective(model, Max, sum(r.cost*x[r.origin, r.destination, r.product] for r in eachrow(cost)))

So is the takeaway here that objectives take generators, but constraints do not, or is this something else?


Solution

  • So is the takeaway here that objectives take generators, but constraints do not, or is this something else?

    JuMP not support the syntax @constraint(model, lhs <= rhs for arg in iterator).

    Do instead:

    @constraint(
        model,
        [r in eachrow(supply)],
        sum(x[r.origin, :, r.product]) <= r.supply
    )
    

    Your objective function is something different because it is a summation. JuMP does support the sum(term for arg in iterator) syntax.