Handy way to index set of tupple in AMPL

68 Views Asked by At

I am dealing with a discrete math optimization problem on a complete graph. My variables are the arcs but I want to delete the arcs that "cost too much". I have n nodes which means I have n² arcs. I define the following set on AMPL

ARCS:={i in 1..n, j in 1..n : i!=j && d[i,j]<= R}

where d[i,j] is the cost on the arc (i,j) and R the limit I am putting.

My problem is that I don't know how to index the variables now. I know I can write

sum{ i in 1..n, j in 1..n : (i,j) in ARCS} blablabla[i,j]

But I think this is quit a tedious way to do. I thought I could write something like this:

sum{e in ARCS} blablabla[e[0],e[1]]
1

There are 1 best solutions below

0
On

I'm not sure if AMPL supports referencing triple elements by position number the way you've written above. (I suspect not, since I don't recall AMPL ever taking a stance on zero- vs. one-indexing, which it would have to do in order to reference them this way.)

Instead, you can do this:

sum{(i,j) in ARCS} blablabla[i,j]

As well as being more succinct, this may also be more efficient for cases where the membership of ARCS is only a small percentage of possible (i,j) combinations, since it avoids the need to generate and test tuples that aren't in ARCS.