cumsum in AMPL

139 views
Skip to first unread message

Hans

unread,
Aug 16, 2015, 2:15:05 PM8/16/15
to AMPL Modeling Language
I need to formulate a constraint involving a `cumsum` calculation:

    sum_{k=1}^j (x_k - p_k) <= d  for all j in 1..n

The following expression in AMPL does not work as it returns an error message
"k is not defined":

    subject to some_rule {j in 1..n}: sum {k in 1..j} x[k] - p[k] <= d

How can I write such a constraint in AMPL correctly?
Many thanks in advance.

Robert Fourer

unread,
Aug 18, 2015, 3:33:39 PM8/18/15
to am...@googlegroups.com
As in the mathematical expression, the scope of the "sum" in the AMPL expression ends at the first + or - sign, unless you parenthesize it like this:

subject to some_rule {j in 1..n}: sum {k in 1..j} (x[k] - p[k]) <= d;

Without the parentheses, the sum only extends to x[k], and you get an error message that k is undefined in p[k].

Bob Fourer
am...@googlegroups.com

=======
Reply all
Reply to author
Forward
0 new messages