Want to Make a Matrix A for any input number n Elements for set N

10 Aufrufe
Direkt zur ersten ungelesenen Nachricht

khan

ungelesen,
21.01.2017, 07:32:3121.01.17
an AMPL Modeling Language
Dear All, 

I 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:

AMPL CODE:

param n:=4;
param c:=(n^2/2)+(n/2)+1;
param A {1..n,1..c} binary, default 0;

for {i in 1..n} 
{let A[i,i+1] := 1;}
for {i in 1..n-1} 
{let A[i,n+i+1] := 1;
       let A[i+1,n+i+1] := 1;}
for {i in 1..n-2}
{let A[i,n+i+4]:=1;
let A[i+1,n+i+4]:=1;
let A[i+2,n+i+4]:=1;}
for {i in 1..n-3}
{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;}

Robert Fourer

ungelesen,
23.01.2017, 11:00:4423.01.17
an am...@googlegroups.com
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:


Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten