# Restriction on gurobipy problem

The main problem is the optimization of the filling of a tank, filling it in cheap periods and extracting in expensive periods to sell. I am using Python and gurobipy.

I am not able to model this constraint: the daily injection should be less than an expression depending on the previous day's filling.

\begin{align}X_i&\le C_i*(\sum_{j=0}^{j=i-1} X_j )\end{align}

I have tried with this expression:

m.addConstrs((x[i]<= C[i]*v1) for i in index)
v1=sum(x[j] for j in range(1,i-1))


But it does not work. Note that $$C_i$$ is a constant.

• I don't know gurobipy, but tt seems there is 1 opening bracket and two closing brackets in your first line of code. Might be the problem?
– Sune
May 20, 2022 at 10:19
• Oh, Sorry and thanks, i have posted it badly, in my real code it was correct the brackets, thats not the issue, but thanks! May 20, 2022 at 10:24
• As a general rule, I think it is helpful to include error messages in your question which these two lines of code certainly would provoke. May 20, 2022 at 12:08

m.addConstrs(x[i] <= C[i] * sum(x[j] for j in range(i)) for i in index)

Python's range(n) yields an iterator from 0 to n-1. I also think that defining v1 after the constraint won't work (moreover, i ist not known to v1) and even if, I think the code becomes more legible if you avoid this.
• Most likely, x or C are defined different than your formula would suggest. Can you update your question and provide a minimal example, where the error occurs? May 20, 2022 at 13:11