2

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

Berni
  • 93
  • 10

2 Answers2

2

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.

mlubin
  • 943
  • 5
  • 10
  • Yes, that solved my issue. I forgot to mention that I'm not worried about looping over all arcs when building the constraint. – Berni Oct 21 '20 at 08:14
1

You need to redefine your x to be an adjacency matrix, that is a square matrix that has 1 (or edge weight) where there is an arc between a pair of nodes and 0 otherwise.

Assuming that the set of vertices you are considering is N (e.g. N = 1:10 for 10 vertices) your constraint can now look like this:

@constraint(m, arcConstraint[n in N], sum(x[n,k] for k ∈ N) == 1 )

Note that arcConstraint is just the constraint name so you can reference it later.

The sum can be also written as sum(x[n,:]) if you actually know that you go through the entire column (depends on your exact scenario).

Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62
  • Wouldn't this involve the creation of NxN variables? In my case, the amount of arcs in the problem is significantly lower than NxN and I want to avoid having so many variables (just one per arc). – Berni Oct 20 '20 at 18:34
  • You cannot have conditional instructions in a JuMP model (since the solvers do not support it). If your goal is to select arcs for a graph having size N, naturally you have NxN variables (or half of that for an undirected graph). If your N is large than depending on some specific characteristics of your problem you could try to re-structure it but than this is a mathematic modelling rather than Julia JuMP issue – Przemyslaw Szufel Oct 20 '20 at 20:49