# [AMPL 24432] Sum previous elements

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],

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.

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;

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:

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

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 in the 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, you can write simply “Level[t,i] = 0 or MiniCap[i] <= Level[t,i] <= MaxiCap[i]”.