Summing over all elements of a set (or param)

51 views
Skip to first unread message

mimi

unread,
Feb 23, 2022, 2:00:39 AM2/23/22
to AMPL Modeling Language
Dear all, 
I would need a param p = the total number of elements in the set WAG;
set WAG[W1] := 1 2; or A B
set WAG[W2] := 3 4 5; or C D  E
set WAG[W3] := 6 7; or E F

param p := 7;
 Thanks!
M

AMPL Google Group

unread,
Feb 23, 2022, 11:06:29 AM2/23/22
to AMPL Modeling Language
WAG is not really one set, but rather an indexed collection of sets. To get the sum of the number of elements in all sets of the indexed collection, you could write, for example,

set W;
set WAG {W};
param p = sum {w in W} card(WAG[w]);


If instead you wanted the total number of different elements in all sets of the indexed collection, then you would need to define p like this:

param p = card (union {w in W} WAG[w]);

(In either case, in your data you will need

set W := W1 W2 W3 ;

followed by your data statements that give the members of WAG[W1], WAG[W2], and WAG[W1].)


--
Robert Fourer
am...@googlegroups.com
{#HS:1796028222-108796#}

mimi

unread,
Feb 24, 2022, 7:20:29 AM2/24/22
to AMPL Modeling Language
Perfect. Thanks!
mimi

Reply all
Reply to author
Forward
0 new messages