Skip to main content

How to correctly wirte a conditional constraint without having corrrepsonding indices FOR loop?

Answered

Comments

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

    Hi Yaojian,

    The mathematical formulation of this constraint family isn't well-defined. \( h \) and \( t \) are indices in a summation for a fixed value of \( n \) and \( k \), so it does not make sense to reference them in the forall statement.

    What exactly is this constraint trying to do? Is the goal to add a constraint for all \( n \in N \) and \( k \in K \), but in each individual constraint we only sum over the elements of \( h \in H \) and \( t \in T \) that satisfy \( t - h = SHL_k + 1 \)? If so, you can just move the conditional statement into the \( \texttt{quicksum()} \) function:

    gp.quicksum(inv[n,k,h,t] for h in H for t in T if t - h == SHL[k] + 1)

    Thanks,

    Eli

    0
  • YAOJIAN ZHOU
    • Gurobi-versary
    • First Question
    • First Comment

    Hi Eli,

    Thank you for the reply!

    I am solving a storing MILP problem.

    n stand for room number, k for storing type, h for harvest time, t for time period.

    So there will be a hidden condition that h<=t

    This constriant is the definition of loss that:

    loss of room n with type k is the sum of inventory quantity of all time(t) that harvested(h)    that storing time(t-h) is longer than shelf life of type K(SHL[k]),

    which leads to the condition

    if t - h == SHL[k] + 1)

    I tried your adviced modification and the result shows no loss but my set data was designed to make sure that it supposed to have some.

    I'm not sure if I explained clear. Are there any other way that can realized the constriant with this condition?

    0
  • Eli Towle
    • Gurobi Staff

    Hi Yaojian,

    The constraints are still not written correctly: \( h \) and \( t \) are not defined in the forall statement, so we can't include \( h \) and \( t \) here. Is there a textbook or paper formulation that you are trying to follow? It would be a lot easier to pinpoint the issue if you could post an existing formulation.

    From what you describe, it sounds like the conditional statement with \( h \) and \( t \) belongs in the summation:

    $$\begin{align*} qloss_{nk} = \sum_{h \in H} \mkern-18mu \sum_{\substack{t \in T \colon \\ t - h = SHL_k + 1}} \mkern-18mu inv_{nkht} \qquad \forall n \in N,\ k \in K\end{align*}$$

    If you expect to have a solution with loss but this formulation produces a solution with no loss, I suspect something is incorrect with some other part of the model formulation. I would expect the harvest time to be represented as a decision variable. Right now, the inventory variable has two different "time" indices (harvest time and time period), which is strange to me. Could this be the problem?

    Eli

     

    0
  • YAOJIAN ZHOU
    • Gurobi-versary
    • First Question
    • First Comment

    Hi Eli,

    Thank you for the reply !

    I think the constriant you write make more sence to me !

    And yes I am refering a paper which also used Gurobi: https://doi.org/10.1016/j.compag.2019.06.003

    I can post all details below, maybe how they wrote the constriants are informal.

    And this Loss definition is only of the issues that confused me. A7,A10,A12 also have the simular problem.

    About the two different time indices, I was confused too.

    I suppose they have to use (t-h) to calculate the sotring time, although H is the subset of T. And I haven't think of finding another way to present it.

    0
  • Eli Towle
    • Gurobi Staff

    Hi Yaojian,

    I see, the "harvest time" index of the inventory tracks when the product was harvested. It makes sense to track this in order to determine when the product expires (shelf life is up). And yes, some of this notation is a bit informal, but I believe the authors' intent is indeed to sum over \( h \in H \) and \( t \in T \) that satisfy \( t - h  = SHL_k +1 \).

    At any rate, if there is a problem with the solution, it lies with the data or another part of the model. To narrow down the issue, you could try the following. It would likely help to use a simplified instance of the problem (a small number of time periods, only one product, etc.).

    • Write and visually inspect an LP model file to ensure the formulation is correct. E.g., \( \texttt{m.write('harvest.lp')} \).
    • Inspect the solution to see if it makes sense. In this case, I would look at where the inventory is going. It there is no loss and nonzero inventory, inventory must be transferred to the \( qout \) variables. These are constrained by the maximum line capacity and certain expressions involving the \( qline \) variables.

    Thanks,

    Eli

    0
  • YAOJIAN ZHOU
    • Gurobi-versary
    • First Question
    • First Comment

    Hi Eli,

     

    Thank you for your kindly support for all the time!

    I think now my model is working by adjusting certain constraints that could avoid these confusing constriant conding.

    Now I am dong the parameter adjustment.

     

    Thank you again!

    Yaojian Zhou

    0

Post is closed for comments.