loopsconstraintsjuliajulia-jumpnetwork-flow

Loop over subset of variables in JuMP constraint


I am trying to implement an arc-flow problem where I have a set of arcs in an array. Each arc is a custom data structure composed of from/to nodes. I want to add a constraint where I only include the arcs that go from a specific node (1), something like:

@constraint(m, sum(x[a] for a in arcs; a.from==1) == 1)

This does not work. What is the best approach to deal with this? Is there a way to do it without precomputing all outgoing arcs from each node first? If so, is there a way to add additional conditions? Thanks in advance

Bernardo


Solution

  • I'm guessing that the syntax you're looking for is

    @constraint(m, sum(x[a] for a in arcs if a.from==1) == 1)
    

    This follows from the standard Julia syntax for generator expressions.

    However, the expression is just as inefficient as it would be in plain Julia because it loops through all arcs. If it this looping becomes a bottleneck, you'll need to reformulate the expression in another way, e.g., by precomputing the outgoing arcs from each node.