Double Summation of Variable with superscripts and subscripts

89 views
Skip to first unread message

Hazel

unread,
Apr 14, 2022, 9:19:52 AM4/14/22
to AMPL Modeling Language
Hello. I have come across this group searching for tutorials on how to program ampl.
I am really confused about how to define variables with superscripts and subscripts such as this one,  Variable Only.png.

I am also confused about how to write this kind of double summation,Double Summation.png.

Thank you so much in advance for your help.

AMPL Google Group

unread,
Apr 15, 2022, 11:24:37 AM4/15/22
to AMPL Modeling Language
AMPL does not make a distinction between subscripts and superscripts. Judging from the way your constraint is written, it appears that there are supposed to be a different D parameter and X variable for each combination of i, m, n, and k. I cannot tell what the ranges for i, m, n, and k are supposed to be, but for illustration I will suppose that they are members of four different indexing sets, which can be defined in AMPL as

set I;
set M;
set N;
set K;

Then the parameters and variables could be defined as, for example,

param D {I, M, N, K};
var X {I, M, N, K} >= 0;
var Y {I} >= 0;

(I have chosen to write I, M, N, K, but any other ordering of the sets could be used.) The constraint could then be written as

subject to constr {i in I}:
sum {m in M, n in N} sum {k in K} X[i,m,n,k] * D[i,m,n,k] <= Y[i];

Note that in AMPL you have to define a summation index (like k) and a corresponding set (like K) that the index runs over. In mathematical notation you often see a simple "sum over k" but that is not specific enough for a computer language.


--
Robert Fourer
am...@googlegroups.com
{#HS:1848187453-109588#}

Hazel

unread,
Apr 16, 2022, 9:11:08 AM4/16/22
to AMPL Modeling Language
Thank you so much, Robert, for your clarification. This is very helpful. 
Reply all
Reply to author
Forward
0 new messages