AMPL syntax error - constraint over indexed set

775 Views Asked by At

first, here is a simple example that demonstrates my problem:

# .mod file
param N;
set TIME := 1 .. N;
set C ordered;
param tbar {C};
var X {C,TIME} >= 0;

minimize cost: sum {c in C} X[c,N];

subject to C1 {c in C: ord(c)>1, t in 1 .. N-1}:
    X[c,t+1] = X[c,t] + 3;

Here is the .dat file:

param N := 10;
set C := C1 C2 C3;
param tbar :=
    C1 2
    C2 3
    C3 3 ;

I get the syntax error

test.mod, line 9 (offset 142):
    syntax error
context:  subject to C1 {c in C:  >>> ord(c)>1, <<<  t in 1 .. N-1}:

If I change the position of the indexing to

{t in 1 .. N-1, c in C: ord(c)>1},

it works fine. The problem is, that I want to have

{c in C: ord(c)>1, t in tbar[c] .. N-1},

so I cannot change the position. Does anybody know why this error happens and if there is a way around it?

1

There are 1 best solutions below

0
On

As you've found out already, the condition should be at the end of the indexing expression:

subject to C1 {c in C, t in 1 .. N-1: ord(c)>1}:
    X[c,t+1] = X[c,t] + 3;

If you want to use c as a subscript in a later expression such as tbar[c] and it is not defined for c = first(C) then you can do

{c in (C diff {first(C)}), t in tbar[c] .. N-1}

as suggested by Geoffrey Brent.