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

Piecewise Objective function and Piecewise constraints

回答済み

コメント

13件のコメント

  • 正式なコメント
    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

    This post is a continuation of this previous post.

    0
  • Jaromił Najman
    • Gurobi Staff

    Hi Syed,

    For simplicity, I will assume that your \(r_t\) is given as

    \[\begin{equation}
    r_t (Y_t) = \begin{cases} 0 & \mathrm{if }\; Y_t \leq b_0 \\
     0.05 & \mathrm{if }\; b_0 < Y_t \leq b_1 \\
    0.1 & \mathrm{if }\; b_1 < Y_t \leq b_2
    \end{cases}
    \end{equation}\]

    where \(b_0=50\), \(b_1=100\), \(b_2=150\).

    The function \(r_t \cdot y\) then looks as depicted in this plot.

    To model this function, we can use Gurobi's piecewise-linear feature described in the documentation on piecewise-linear objectives.

    The Python code would look something like:

    model.setPWLObj(y, [0, 50, 50, 100, 100, 150], [0, 0, 0.05*50, 0.05*100, 0.1*100, 0.1*150])

    The piecewise-linear function is defined by 6 points

    \[(0,0), (50,0), (50,0.05\cdot 50), (100, 0.05\cdot 100), (100, 0.1\cdot 100), (150, 0.1\cdot 150)\]

    with 2 jumps between the points \( (50,0), (50,0.05\cdot 50)\) and \((100, 0.05\cdot 100), (100, 0.1\cdot 100)\).

    The default optimization sense is minimization in Gurobi so you don't have to adjust anything regarding the optimization sense.

    The constraints can be modeled in a similar way.

    Best regards,
    Jaromił

    0
  • Syed Saad Ali
    • Gurobi-versary
    • Conversationalist
    • First Question

    Hi Jaromil,

    Many thanks for the answer. It surely helps a lot in understanding how to formulate the piecewise objective function but I still have one confusion. What does this $y$ value indicates actually? 

    What I understood is it searches for the value of $y$ in the second argument (i.e. in which intervals does it lie) and then takes the corresponding value from the third argument to minimize the function? Please confirm if I understood it right.

    If that is the case so, then would it be right if I set the function like this (shown by the third term of the third bracket) as an example:

    model.setPWLObj(y, [0, 50, 50, 100, 100, 150], [0, 0, p_t * (1+0.05)*50, 0.05*100, 0.1*100, 0.1*150])
    0
  • Jaromił Najman
    • Gurobi Staff

    Hi Syed,

    What I understood is it searches for the value of $y$ in the second argument (i.e. in which intervals does it lie) and then takes the corresponding value from the third argument to minimize the function? Please confirm if I understood it right.

    In simple words, yes this describes what happens. To add more technical details. Gurobi uses the 6 provided points to automatically construct the piecewise-linear function depicted in this plot. Gurobi will add binary variables to model the 3 pieces and the jumps. For more details on how piecewise-linear functions are handled by optimization solvers, please have a look the the blog on Piecewise-linear functions in MIP modeling.

    If that is the case so, then would it be right if I set the function like this (shown by the third term of the third bracket) as an example:

    If \(\texttt{p_t}\) is a constant value then yes, this will work.

    Best regards,
    Jaromił

    0
  • Syed Saad Ali
    • Gurobi-versary
    • Conversationalist
    • First Question

    Hi Jaromil,

    Yes, $p_t$ is a constant and it's working.

    But there is another term added to the objective function which contains another variable. So, in that case shall I state separate objective function and add both of them or it could also be integrated into the same piecewise objective function?

    0
  • Jaromił Najman
    • Gurobi Staff

    Hi Syed,

    But there is another term added to the objective function which contains another variable. So, in that case shall I state separate objective function and add both of them or it could also be integrated into the same piecewise objective function?

    You will have to add this objective function part using the setPWLObj function. This will then be automatically added to the objective function. You can use the write() function to write an LP file and see what your objective function looks like.

    Best regards,
    Jaromił

    0
  • Syed Saad Ali
    • Gurobi-versary
    • Conversationalist
    • First Question

    Hi Jaromil,

    I tried to formulate the first constraint like this:

    for t in T:
    \begin{equation}
    model.addGenConstPWL(y_t, r, [0, 50, 50, 100, 100, 150],[0, 0, 0.05, 0.05, 0.1, 0.1])
    \end{equation}
    \begin{equation}
    model.addConstrs(M * y_t > P_t - p_t * (1+r))
    \end{equation}

    If it is correct, then wouldn't I have to declare the $r$ as a variable which would then make the objective function quadratic? I am highly doubtful if I understood it right. 

    Waiting for your reply. 

    BR

    Saad

     



    0
  • Syed Saad Ali
    • Gurobi-versary
    • Conversationalist
    • First Question

    Hi! Somehow I have removed the errors and my program is working and optimizing but it is making one of my variable equal to zero for all values. (Screenshot is attached )

    What could be the possible reason?

    0
  • Jaromił Najman
    • Gurobi Staff

    Hi Syed,

    Sorry for not getting back to you for so long.

    If it is correct, then wouldn't I have to declare the $r$ as a variable which would then make the objective function quadratic?

    Yes, this is correct, you have to declare \(r\) as an optimization variable. If \(p_t\) is an optimization variable as well then your problem is a quadratic one.

    Somehow I have removed the errors and my program is working and optimizing but it is making one of my variable equal to zero for all values. (Screenshot is attached )

    What could be the possible reason?

    There can be several reasons. Without any reproducible example possible help here is only limited.

    Best regards,
    Jaromił

    0
  • Syed Saad Ali
    • Gurobi-versary
    • Conversationalist
    • First Question

    Hi Jaromil,

    Many thanks for the responses. 

    Please if you could help in understanding the reason why does it makes the values of all beta equal to zero. 

    Kind regards,

    Saad

    0
  • Jaromił Najman
    • Gurobi Staff

    Hi Syed,

    I am not able to execute your code. The indentation is incorrect and I get the error

    NameError: name 'demand' is not defined

    Could you please write an MPS or an LP file using the write() function. You can share files in the Forum as described in Posting to the Community Forum.

    Best regards,
    Jaromił

    0
  • Syed Saad Ali
    • Gurobi-versary
    • Conversationalist
    • First Question

    Hi Jaromil,

    Would it be fine if I just send it to you all the files as described in this link below due to confidentiality matters?

    https://support.gurobi.com/hc/en-us/articles/360030716132-How-do-I-upload-or-send-files-to-Gurobi-Support-

    BR

    Saad 

    0

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