メインコンテンツへスキップ

GurobiError: Constraint has no bool value (are you trying "lb <= expr <= ub"?)

回答済み

コメント

5件のコメント

  • 正式なコメント
    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?.
  • Jaromił Najman
    • Gurobi Staff

    Hi,

    Could you elaborate more on your code snippet, i.e., what are the optimization variables, what are constant scalars?

    Best regards,
    Jaromił

    0
  • Sheng Hui Tan
    • Gurobi-versary
    • First Comment
    • First Question

    Hi Jaromil,

    I'm currently working on an energy home scheduling optimization problem over a time horizon. The constant scalars are battery_input_voltage and initial_level. The optimization variables are the charging c[t] and discharging d[t] of the energy storage system. I modelled the battery resistance as an increasing linear function as shown in the function of linear_batt_resistance(time_step).

    The losses of the battery only occur during the charging or discharging process of the energy storage, therefore I wish to keep track of the length of time the energy storage charge or discharge using the discharge_tracker(t) function. The battery resistance increases linearly over the time period when it charges or discharges continuously and revert back to the initial resistance when it stops. 


    if d[t] * d[t-1] == 0:

    I believe the error comes from the discharge_track function if statement. However, this line is required to check whether the period before t was discharging. Given that both c[t] and d[t] for all t in the time horizon are positive when charging and discharging, if d[t] * d[t-1] > 0 it means that there is a discharge value for both the time period at t and t-1 and therefore the value of the battery resistance needs to be higher on period t as compared to period t-1. 

    However, since this equality function was placed within the constraint, it ended up with both equality and inequality in the same constraint line and therefore causing the error message. May I know if there is another alternative way to tackle this issue?

    Thank you,

    Tan Sheng Hui

     

    0
  • Jaromił Najman
    • Gurobi Staff

    Hi Tan Scheng Hui,

    Indeed, the error occurs because you are trying to evaluate the optimization variables \(\texttt{d[t]}\) and \(\texttt{d[t-1]}\) before the optimization starts. This is not possible as optimization variables don't have values during model building.

    You can model the \(\texttt{if-else}\)-statement via the introduction of additional variables. You would have to drop the function \(\texttt{discharge_tracker}\), add an optimization variable \(\texttt{discharge_track[t]}\) for each \(\texttt{t}\), and subsequently model the \(\texttt{if-else}\)-statement. A very good description of how to model an \(\texttt{if-else}\)-statement via MIPs can be found in the stackexchange post How to write if else statement in Linear Programming?

    Best regards,
    Jaromił

    0
  • Sheng Hui Tan
    • Gurobi-versary
    • First Comment
    • First Question

    Hi Jaromił,

    Thanks for your help. The code is working.

    Regards,

    Sheng Hui

    0

投稿コメントは受け付けていません。