Accessing elements of a set

502 views
Skip to first unread message

lawrenc...@comcast.net

unread,
Dec 4, 2007, 8:03:48 AM12/4/07
to AMPL Modeling Language
I am new to AMPL and new to this group. I have what I hope is a
simple problem:

Can the members of a set, { 5 19 -23 45 6 2 1 }, be accessed by
position in the set and assigned to a parameter? For example:

set A := 3 2 54 7 20 1;

param Value_of_A{card(A)};

for {i in A} { let Value_of_A[i] := A[i] };

display Value_of_A;



The "let" command above is an illegal syntax but I cannot find another
example that accesses the members of the set. The set is not an
ordered set so "next", "prev", etc. so AMPL says they cannot be used
on my set.

Thanks - Larry.

Paul

unread,
Dec 4, 2007, 6:14:31 PM12/4/07
to AMPL Modeling Language
There are a variety of issues with your code snippet, including the
fact that A[i] is invalid syntax and in the for statement you are
using elements of A to index Value_of_A, which is not defined with A
as its index set. Try the following:

set A := {3, 2, 54, 7, 20, 1} ordered;
param Value_of_A {1 .. card(A)};
for {i in 1 .. card(A)} { let Value_of_A[i] := member(i, A) };
display Value_of_A;

Note that you can (and will need to) declare A to be ordered in order
to access members by position. In the declaration, "ordered" means
"in the order written", not "in the canonical order of the integers".

HTH,
Paul
Reply all
Reply to author
Forward
0 new messages