testing for definedness

9 views
Skip to first unread message

Ketil Danielsen

unread,
Sep 21, 2017, 9:18:59 AM9/21/17
to AMPL Modeling Language
My model defines:
   var x {i in B, j in N, v in V: A[i,j]>0.5} binary;

The result seems to be a sparse matrix (see below), most x[i,j,v] are just "dots" (undefined?).

I get "invalid subscript" if I for instance want to print all 1's:

   display { i in B, j in N, v in V: x[i,j,v] = 1 };

Any help is appreciated.  

I have searched the AMPL book, hoping to find something like (not succeeding):

   display { i in B, j in N, v in V: defined( x[i,j,v] ) and x[i,j,v] = 1 };

(snip of x after running solve)
:      M6 M60 M62 M64 M65 M66 M72 M73 M74 M76 M77 M79 M80
M11     0   .   .   .   .   .   .   .   .   .   .   .   .
M13     .   .   .   .   .   .   .   .   .   .   0   .   .
M17     .   .   .   .   .   .   .   .   .   .   .   0   .
M18     .   .   .   .   .   .   0   0   1   .   .   .   0
M21     .   .   .   .   .   .   .   .   .   .   0   .   .
M30     .   .   .   .   .   .   0   0   0   .   .   .   1
M33     .   .   .   .   .   .   0   0   0   .   .   .   0
M34     .   .   .   .   .   .   .   .   .   .   0   .   .
M37     .   .   .   .   .   .   .   .   .   .   0   .   .
M39     .   .   .   .   .   .   0   0   0   .   .   .   0
M43     .   .   .   .   .   .   .   .   .   .   .   0   .
M46     .   .   .   .   0   .   .   .   .   .   .   .   .
M52     .   .   .   .   .   0   .   .   .   .   .   .   .
M53     .   .   .   .   .   .   .   .   .   .   0   .   .

Robert Fourer

unread,
Sep 22, 2017, 11:38:17 AM9/22/17
to am...@googlegroups.com
You are right, if you define x to be indexed over {i in B, j in N, v in V: A[i,j]>0.5} then x[i,j,v] only exists for (i,j,v) in that set. When you try to display the set {i in B, j in N, v in V: x[i,j,v] = 1} you index x by (i,j,v) not in that set -- hence the "invalid subscripts" error. You can avoid this error by including the condition on A[i,j]:

display {i in B, j in N, v in V: A[i,j] > 0.5 and x[i,j,v] = 1};

(AMPL will only check the second condition when the first is true.) But it is tedious to write this every time, so instead you may want to give a name to the set of valid (i,j) pairs:

set BN = {i in B, j in N: A[i,j] > 0.5};

Then you can write

var x {BN,V} binary;
...
display {(i,j) in BN, v in V: x[i,j,v] = 1};

Bob Fourer
am...@googlegroups.com

=======
Reply all
Reply to author
Forward
0 new messages