Skip to main content

Optimization using Gurobi

Answered

Comments

6 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    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.
  • Eli Towle
    • Gurobi Staff

    The problem occurs in this call to Model.addConstrs():

    for g in n_generators:
        for t in time_periods:
            if t >= UT:
               uptime[g,t] = m.addConstrs(sum(v[g,p] for p in range (t-UT+1, t+1) ) <= u[g,t] 
                                        for g in n_generators for t in time_periods)

    You loop over \( \texttt{g in n_generators} \) and \( \texttt{t in time_periods} \) in the outer \( \texttt{for} \) loops. Then, you iterate over \( \texttt{g in n_generators} \) and \( \texttt{t in time_periods} \) again in the call to Model.addConstrs(). You should only iterate over these sets in one place. I imagine you mean to write something like this:

    uptime = m.addConstrs(
        (
            sum(v[g, p] for p in range(t - UT + 1, t + 1)) <= u[g, t]
            for g in n_generators
            for t in time_periods
            if t >= UT
        ),
      name="uptime"
    )

    With this code, you receive a different KeyError for the key \( \texttt{(0, 24)} \). Consider the case when \( \texttt{g=0} \) and \( \texttt{t=24} \). We have \( \texttt{t-UT+1=21} \) and \( \texttt{t+1=25} \). The summation iterates over all \( \texttt{p in range(21, 25)} \), which includes \( \texttt{p = 24} \). The error occurs trying to access \( \texttt{v[g, p] = v[0, 24]} \). The key \( \texttt{(0, 24)} \) does not exist in \( \texttt{v} \), which you can verify with \(\texttt{print(v.keys())}\). This is because you define your \( \texttt{v} \) variables as

    v = m.addVars(ntypes, nperiods, lb=-GRB.INFINITY, vtype=GRB.BINARY, name="v_output")

    where \( \texttt{ntypes = 10} \) and \( \texttt{nperiods = 24} \). As a result, the keys of \( \texttt{v} \) are \( \{0, \ldots, 9\} \times \{0, \ldots, 23\} \):

    (0, 0), (0, 1), ..., (0, 23),
    (1, 0), (1, 1), ..., (1, 23),
    (2, 0), (2, 1), ..., (2, 23),
    . . . .
    . . . .
    . . . .
    (9, 0), (9, 1), ..., (9, 23)

    The keys you are generating for \( \texttt{v} \) in your constraints need to to be included in the keys defined for \( \texttt{v} \). Right now, these sets of keys are very different, because \( \texttt{nperiods} \) is \( 24 \) but your \( \texttt{time_periods} \) dictionary includes \( 30 \) numbers.

    Note also that \( \texttt{lb=-GRB.INFINITY} \) does not do anything if the variable type is binary.

    0
  • Hussain Askar
    • Gurobi-versary
    • Conversationalist
    • First Question

    Thank You Eli. 

    My code finally runs without errors, but for a reason it doesn't seem It cares about the constraint because it is not satisfying it!

     

    I used your formulation and it worked but the constraint didn't change anything like I expected. Here why is the if statement at the end? shouldn't it be at the beginning of the loop?

    uptime = m.addConstrs(
        (
            sum(v[g, p] for p in range(t - UT + 1, t + 1)) <= u[g, t]
            for g in n_generators
            for t in time_periods
            if t >= UT
        ),
      name="uptime"
    )
    0
  • Hussain Askar
    • Gurobi-versary
    • Conversationalist
    • First Question

    I also noticed that you removed This part [g,t] in uptime constraint:

    uptime[g,t]

    So my question is: Is the code you gave me (shown below) will count for all [g,t] values for uptime or just a single value?

    uptime = m.addConstrs(
        (
            sum(v[g, p] for p in range(t - UT + 1, t + 1)) <= u[g, t]
            for g in n_generators
            for t in time_periods
            if t >= UT
        ),
      name="uptime"
    )

    Based on the output i'm getting, V is always 0 so the constraint is useless. and only u changes!

    0
  • Eli Towle
    • Gurobi Staff

    It's valid syntax for the \( \texttt{if} \) statement to be at the end of the generator expression. It is not valid syntax to place the \( \texttt{if} \) statement before the \( \texttt{for} \) loops.

    The syntax I used to define the Python variable \( \texttt{uptime} \) creates a dictionary mapping \( \texttt{(g, t)} \) pairs to the corresponding constraint objects. You can print the dictionary to see what it looks like. The call to Model.addConstrs() adds a constraint to the model for every \( \texttt{g} \) in \( \texttt{n_generators} \) and \( \texttt{t} \) in \(\texttt{time_periods} \) satisfying \( \texttt{t} \geq \texttt{UT} \). You can confirm this by inspecting the \( \texttt{junk.lp} \) file you write at the end of your code.

    The \( \texttt{v} \) variables do not appear in any constraints other than these \( \texttt{uptime} \) constraints, nor do they appear in the objective function. As such, there is no reason for the solver to set the \( \texttt{v} \) variables to anything other than \( 0 \). If you expect the \( \texttt{v} \) variables to be nonzero, there is a problem with the model's logic or implementation.

    0
  • Hussain Askar
    • Gurobi-versary
    • Conversationalist
    • First Question

    thank you sir!

    I added another constraint to define v and now it works as expected!

    0

Post is closed for comments.