# \[AMPL 24432\] Sum previous elements

**URL:** <https://discuss.ampl.com/t/ampl-24432-sum-previous-elements/289>\
**Category:** Google Group Mirror\
**Created:** [December 26, 2022, 9:56pm UTC](https://discuss.ampl.com/t/ampl-24432-sum-previous-elements/289 "2022-12-26T21:56:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oink\_oink\_B](https://avatars.discourse-cdn.com/v4/letter/o/4491bb/32.png) [@Oink\_oink\_B](https://discuss.ampl.com/u/Oink_oink_B)\
**Post date:** [December 26, 2022, 9:56pm UTC](https://discuss.ampl.com/t/ampl-24432-sum-previous-elements/289/1 "2022-12-26T21:56:27Z")

</div>

Hi,

Hope this will make sense.

Imagine I have a list of numbers for Up[t,i] and Down[t,i] and I want to sum it up until a specific t even tho the entire range is 1…H. This should give me a list for i with t elements. This is my latest attempt:

**subject**  **to** CapacityUpperbound {t **in** 1…H, i **in** 1…n}:

**sum** {k **in** 1…t} (Up[k, i]-Down[k, i]) \<= MaxCap[i] + RampUp[i];

It doesnt work the way I want it to work.

The idea is Up[t, i]-Down[t, i] updates for every time we go up by one t. At every t, the current count cannot exceed MaxCap[i] + RampUp[i],

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [December 27, 2022, 7:19pm UTC](https://discuss.ampl.com/t/ampl-24432-sum-previous-elements/289/2 "2022-12-27T19:19:58Z")

</div>

This looks like it might be right, but it is hard for me to tell, because you do not say how Up, Down, MaxCap, and RampUp are defined. I am not sure which ones are supposed to be AMPL parameters (with known values given in your data) and which are AMPL decision variables (with unknown values that will be set by a solver to optimize some objective function). Also for the variables I do not know whether they have lower and upper bounds, whether they must have integer values, etc.

If you have AMPL param and var statements for Up, Down, MaxCap, and RampUp, you can just paste them into your reply.

---

<div class="post-metadata">

**Author:** ![Oink\_oink\_B](https://avatars.discourse-cdn.com/v4/letter/o/4491bb/32.png) [@Oink\_oink\_B](https://discuss.ampl.com/u/Oink_oink_B)\
**Post date:** [December 27, 2022, 8:21pm UTC](https://discuss.ampl.com/t/ampl-24432-sum-previous-elements/289/3 "2022-12-27T20:21:53Z")

</div>

I ended up changing the constraint and added a new one:

**subject**  **to** Level{t **in** 1…H, i **in** 1…n}:

Level[t,i] = Up[t, i]-Down[t, i] + Level[t-1,i];

Now it works!

I have another issue, but I don’t know if I need to create a new conversation for that? Is there a way to make if-statements? Sometimes Level can be equal to 0, but if Level \> 0 then it should be restricted to Mini[i]

My attempt:

**subject**  **to** CapacityLower {t **in** 1…H, i **in** 1…n}:

Level[t,i] \<= Mini[i] **or** Level[t,i] \<= 0;

I have also tried this, but I think I need to formulate it completely different.

**if** Level[t,i]\>=Mini[i] **then** Level[t,i] **else** Level[t,i]=0;

---

<div class="post-metadata">

**Author:** ![Oink\_oink\_B](https://avatars.discourse-cdn.com/v4/letter/o/4491bb/32.png) [@Oink\_oink\_B](https://discuss.ampl.com/u/Oink_oink_B)\
**Post date:** [December 27, 2022, 9:32pm UTC](https://discuss.ampl.com/t/ampl-24432-sum-previous-elements/289/4 "2022-12-27T21:32:18Z")

</div>

I think I need a binary multiplied with Mini[i], so if Level has a value greater than 0, then the binary, called X, should be 1 and if Level = 0 then the binary should be 0 too, but how do I program that? My current code looks like this:

 ![Screenshot 2022-12-27 at 22.31.04.png](https://us1.discourse-cdn.com/flex019/uploads/ampl/original/1X/e34de802c66a51d3cd6f966c31eb65d4ca2b1862.png)

I have tried to multiply X with MiniCap[i], but it did not quite work. Also tried adding a constraint that said Level \* X = Level to get the right 0 and 1, but it did not work either. I am intuitively trying to program when the washing machine is on or off with the binary, as the water supply needs to be met, but if the machine is off, then Level = 0. MiniCap only applies if the machine is on

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [December 29, 2022, 3:34pm UTC](https://discuss.ampl.com/t/ampl-24432-sum-previous-elements/289/5 "2022-12-29T15:34:37Z")

</div>

If I understand you correctly, you are trying to state that, for each t in 1…H and i in 1…n, either Level[t,i] = 0 or MiniCap[i] \<= Level[t,i] \<= MaxiCap[i]. Section [20.2 Zero-one variables and logical conditions](https://ampl.com/wp-content/uploads/Chapter-20-Integer-Linear-Programs-AMPL-Book.pdf#page=3) in the [AMPL book](https://ampl.com/learn/ampl-book/) uses this kind of condition as one of its examples. In your case, if the binary variables are X[t,i], then the constraints can be Level[t,i] \>= MiniCap[i] \* X[t,i] and Level[t,i] \<= MaxiCap[i] \* X[t,i].

Many MIP solvers now also let you write this constraint as “X[t,i] = 0 ==\> Level[t,i] = 0 else MiniCap[i] \<= Level[t,i] \<= MaxiCap[i]” (where the ==\> operator means “implies”). And for solvers that use our new [MP interface](https://amplmp.readthedocs.io/en/latest/rst/model-guide.html), you can write simply “Level[t,i] = 0 or MiniCap[i] \<= Level[t,i] \<= MaxiCap[i]”.
