2
$\begingroup$

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.

$\endgroup$
3
  • 1
    $\begingroup$ 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? $\endgroup$
    – Sune
    May 20, 2022 at 10:19
  • $\begingroup$ Oh, Sorry and thanks, i have posted it badly, in my real code it was correct the brackets, thats not the issue, but thanks! $\endgroup$
    – ADMGYP
    May 20, 2022 at 10:24
  • $\begingroup$ 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. $\endgroup$
    – ttnick
    May 20, 2022 at 12:08

1 Answer 1

2
$\begingroup$

Try

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.

$\endgroup$
2
  • $\begingroup$ Hi thanks very much but i have the error: Key error: 0 with your proposed code. $\endgroup$
    – ADMGYP
    May 20, 2022 at 12:31
  • 1
    $\begingroup$ 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? $\endgroup$
    – ttnick
    May 20, 2022 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.