Help! Dynamic constraint definition for power system MIP optimization in Gurobi
Awaiting user inputTo whom it may concern at Gurobi Support,
I hope you are doing well.
I have below some Python code that I would like to convert to a Gurobi constraint, however, I am running into issues. I am trying to solve an MIP Unit Commitment problem .
The code that I'd like to convert to a Gurobi constraint is:
for t in time_periods:
UT = min(gen['time_up_minimum'],data['time_periods'])
if t >= UT:
m.uptime[g,t] = sum(m.vg[g,t] for t in range(t-UT+1, t+1)) <= m.ug[g,t]
UT is a list that contains data of the uptime of each generator each hour.
My attempt at making this Python loop a Gurobi constraint is below. Note that I use the variable 'p' as the equivalent iterator 't'
min_uptime = model.addConstrs(gp.quicksum(nstart[g, p] for p in range(p-UT+1,p+1)) <= ngen[g, periods]
for g in range(ngens) for periods in range(nperiods))
The error that results when I run my code is
File C:/Users/... test.py:60 in <genexpr>
min_uptime = model.addConstrs(gp.quicksum(nstart[g, p] for p in range(p-UT+1,p+1)) <= ngen[g, periods]
NameError: name 'p' is not defined
I know that the issue stems from the part:
... for p in range(p-UT+1,p+1)) ...
I believe the issue is that this is a dynamic constraint, meaning the constraint is not constant; it changes every time period, 'p'. I cannot think of a way to include a way to iterate through p inside the range like the Python code iterates with 't'.
What do you think?
Thanks in advance!!!
Elliott Fix
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Elliott,
In your code that you would like to convert, it would be best to not misuse \(\texttt{t}\) as iterator and fixed value. I think that the following should also work for you
for T in time_periods:
UT = min(gen['time_up_minimum'],data['time_periods'])
if T >= UT:
m.uptime[g,t] = sum(m.vg[g,t] for t in range(T-UT+1, T+1)) <= m.ug[g,T]You can then convert the above to Gurobi code by looping over time_periods and adjusting \(\texttt{p}\) to \(\texttt{T}\) in the \(\texttt{range}\) function.
Best regards,
Jaromił0
Post is closed for comments.
Comments
2 comments