Skip to main content

modeling question

Answered

Comments

3 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff 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 why not try our AI Gurobot?.
  • Eli Towle
    • Gurobi Staff Gurobi Staff

    It looks like the "only one" constraints are the problem:

    for k in demand:
    m.addConstr(quicksum(u[i,j,k] + u1[i,k] for i in facility2 for j in facility1) == 1, name="only one")

    The mathematical formulation of these constraints is

    $$\begin{align*}\sum_{i \in F_2} \sum_{j \in F_1} (u_{ijk} + u^1_{ik}) = 1 \quad \forall k \in D.\end{align*}$$

    The \( u^1 \) variables are enclosed in a summation over \( F_1 \). As a result, each \( u^1_{ik} \) variable shows up \( |F_1| = 20 \) times on the left-hand side of the constraint:

    $$\begin{align*}\sum_{i \in F_2} \sum_{j \in F_1} u_{ijk} + |F_1| \sum_{i \in F_2} u^1_{ik} = 1 \quad \forall k \in D.\end{align*}$$

    As a result, setting any of the binary \( u^1 \) variables to \( 1 \) results in an infeasible model. To fix this, move the \( u^1_{ik} \) terms outside of the summation over \( F_1 \):

    $$\begin{align*}\sum_{i \in F_2} \bigg( u^1_{ik} + \sum_{j \in F_1} u_{ijk} \bigg) = \sum_{i \in F_2} \sum_{j \in F_1} u_{ijk} + \sum_{i \in F_2} u^1_{ik} = 1 \quad \forall k \in D.\end{align*}$$

    You can write these constraints concisely using tupledict.sum():

    for k in demand:
        m.addConstr(u.sum('*', '*', k) + u1.sum('*', k) == 1, name='only_one')

    A few other comments:

    0
  • chaung yun chi
    • Gurobi-versary
    • Curious
    • Conversationalist

    Thanks for your help, I actually try the same thing before, using different form

    for k in demand:
    m.addConstr(quicksum(u[i,j,k] for i in facility2 for j in facility1)+quicksum(u1[i,k] for i in facility2)==1, name="only one")

    but somehow it didn't work , so I thought this kind of constraint is not correct, and after I try your answer, I decide to try my origin one again, because I think the concept is the same as your answer, and it works. I have no idea why it wasn't working then, but anyway thanks for your help! 





    0

Post is closed for comments.