This is more of a programming problem than an AMPL issue. Rather than write
for {i in 1..n}
...
for {i in 1..n-1}
...
for {i in 1..n-2}
...
for {i in 1..n-3}
...
you should think in terms of nested loops:
for {k in 0..3}
for {i in 1..n-k}
...
Similarly, rather than write
let A[i,n+i+6]:=1;
let A[i+1,n+i+6]:=1;
let A[i+2,n+i+6]:=1;
let A[i+3,n+i+6]:=1;
you should think in terms of an indexed assignment statement:
let {j in 0..3} A[i+j,n+i+6]:=1;
Once you have written the entire computation of A in terms of indexed forms like this, you can consider generalizing to n > 4 by replacing the remaining explicit numbers by something more general. For instance, "{k in 0..3}" might be replaced by "{k in 0..n-1}".
Bob Fourer
am...@googlegroups.com
=======
From:
am...@googlegroups.com [mailto:
am...@googlegroups.com] On Behalf Of khan
Sent: Saturday, January 21, 2017 6:33 AM
To: AMPL Modeling Language
Subject: [AMPL 13416] Want to Make a Matrix A for any input number n Elements for set N
am trying to make a mtrix to represent adjacent elements. In the following example and AMPL code I have done it for n=4. But I want to do it for any n elements. My code doesnt work with n elements. You will be appreciated if you suggest me how to do it for any number n in.
I have a set N={1 2 3 4} with n=4 elements and the possible adjacent combinations for this are:
{empty set} {1} {2} {3} {4} {1,2} {2,3} {3,4} {1,2,3} {2,3,4} and {1,2,3,4}.
So the total possible combinations are c=11 which can be calculated with the formula:
c=(n^2/2)+(n/2)+1 (n squared divided by 2 + n divided by 2 + 1)
I can model this using a matrix A=n X c as below whose elements can be represented as a(n,c) are: