Skip to main content

How to define the constraints with accumulation of one index?

Answered

Comments

10 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than two 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.
  • Riley Clement
    • Gurobi Staff

    Hi Yaoyao,

    I think you've got the right idea, which is to introduce an auxiliary variable for the multiplication of s and theta variables.

    \[z^v_{ijm} = s^{v}_{ij}\theta^v_{im}\]
    \[e^v_j = \sum_{i \in S_0} Ez^v_{ijm} + s^{v}_{ij}e^v_i - e^v_iz^v_{ijm} - u_j\]

    The And constraint would only work if s and th are binary variables, which I'll assume they are and also define sth as binary, but I'd suggest an equality constraint linking the sth with the multiplication of s and th is more readable.

    V = range(1,3)
    F = range(1,3)
    S = range(1,4)

    for v in V:
        for m in F:        
            for j in S:
                expr = gp.QuadExpr()
                for i in S:
                    if i != j:
                        sth = model.addVar(vtype="B")
                        model.addConstr(sth == s[v, i, j]*th[v, i, m])
                        expr += E*sth + e[v,i]*s[v,i,j] + e[v,i]*sth - Econsumption[j]
                model.addConstr(expr == e[v,j],"l7")

    You could also structure this slightly differently, so that the code is not as nested, with the help of itertools.product:

    from itertools import product

    V = range(1,3)
    F = range(1,3)
    S = range(1,4)

    model.addConstrs((e[v,0] == E for v in V),"l7a")
    model.addConstrs((th[v,0,m] == 0 for v, m in product(V,F)), name="l7_")

    z_indices = [(v,i,j,m) for v,i,j,m in product(V, S, S, F) if i != j]
    z = model.addVars(z_indices, vtype="B")
    model.addConstrs(z[v,i,j,m] == s[v, i, j]*th[v, i, m] for v,i,j,m in z_indices)

    for v,m,j in product(V,F,S):
        m.addConstr(e[v,j] == gp.quicksum(
            E*z[v,i,j,m] + e[v,i]*s[v,i,j] + e[v,i]*z[v,i,j,m] - Econsumption[j]
            for i in S if i != j
        ))

    - Riley

    0
  • Ariel W
    • Gurobi-versary
    • Curious
    • Conversationalist

    Thanks, Riley. I got it!

    0
  • Ariel W
    • Gurobi-versary
    • Curious
    • Conversationalist

    Hi, Riley:

    I'm sorry to bother you, I met problems again. when i want to introduce another auxiliary variable rth[v,i,j,m]for the multiplication of r[v,j] and (1-th[v,i,m]) variables, i try the structure you proposed, but as the index of the product is same to sth, it outputs "KeyError: 'Duplicate keys in Model.addVars()'".

    The code of defining rth is as follow. How can i deal with the definition of sth and rth?

    rth_indices = [(v,i,j,m) for v,i,j,m in product(range(1,V+1),  range(S), range(1,S+1),range(1,F+1)) if i < j]
    rth = model.addVars(rth_indices, vtype=GRB.BINARY, name='rth')
    model.addConstrs(rth[v,i,j,m] == r[v, j] * (1-th[v, i, m]) for v,i,j,m in rth_indices)
    for m in vrange(1,F+1):
    for v in range(1, V + 1):
    for i in range(S):
    for j in range(i+1,S+1):
    name = 'C16_mvij' + str(m) + str(v) + str(i) + str(j)
    model.addConstr(st[i] + RT[i] + t + th[d,i,m] * q * (E - e[v,i]) + rth[v,i,j,m]*h[v,j] <= st[j] + BigM * (1 - s[v,i,j]), name=name)
    0
  • Ariel W
    • Gurobi-versary
    • Curious
    • Conversationalist

    And how can i continue calling sth as an auxiliary  variable in another constraint, it seems to be wrong as well, which also outputs"KeyError: 'Duplicate keys in Model.addVars()'"?

    0
  • Riley Clement
    • Gurobi Staff

    Hi Ariel,

    No problem, can you show me the values of V, S, F, and paste the full error message including stacktrace?

    - Riley

    0
  • Ariel W
    • Gurobi-versary
    • Curious
    • Conversationalist

    Difinitely! The values of V,S,F are the same as above.

    V = range(1,3)
    F = range(1,3)
    S = range(1,4)

    The full error message is as follow, the definition of sth referred to your answer about 'product' structure:

    Traceback (most recent call last):
      File "D:\Python_installment\Pythoncode\Research\writing2\Driver_bind_VehicleMode\Model1_Driver_bind_VehicleMode.py", line 183, in <module>
        sth = model.addVars(sth_indices, vtype=GRB.BINARY, name='sth')
      File "src\gurobipy\model.pxi", line 2991, in gurobipy.Model.addVars
    KeyError: 'Duplicate keys in Model.addVars()'
    Set parameter IntegralityFocus to value 1

    Thanks, Riley!

    0
  • Riley Clement
    • Gurobi Staff

    Hi Ariel,

    Any chance it's V,F,S = 3,3,4?  The code you pasted is incompatible with the ranges.  I.e. this doesn't work

    V = range(1,3)
    ...
    range(1,V+1)

    Since the stack trace is pointing to the definition of the sth variables we'll need you to paste the code for that, not the rth variables.

    - Riley

    0
  • Ariel W
    • Gurobi-versary
    • Curious
    • Conversationalist

    Sorry, Riley. Waste your time. When I just revisited my code, I found typo. It can work with two similar index. Thank you!

    0
  • Riley Clement
    • Gurobi Staff

    No worries, good luck!

    0

Post is closed for comments.