Conditional (piecewise) constraints modeling in Gurobi
回答済みHi All,
I have face to model a optimization problem with conditional (piecewise) constraints. My problem is stated as follows:

Here, both y_1 and y_2 are decision variables, \theta_1 and \theta_2 are also calculated by y_1 and y_2. d is given as an input parameter.
I am thinking how to write this kind of piecewise constraints in the optimization model? It seems like if we satisfy constraint A, then we need add a set of constraint B into the model?
-
正式なコメント
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 try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Jingfeng,
To model the piecewise expression, you can first define auxiliary variable \(z_1 = y_1 + y_2, z_2 = |y_1-y_2|\) and thren follow the steps described in How do I model conditional statements in Gurobi? For the absolute value, you will need the addGenConstrAbs method. Once you have introduced two binary variables \(b_1,b_2\) for the first two conditions, you can check for the 3rd condition (the "otherwise" condition) by introducing a third binary variable and using Gurobi's or method to model \(b_3 = b_1 \lor b_2\).
Then, there is the issue with all the nonlinear terms in your expressions. You will need to have a look at How do I divide by a variable in Gurobi? and How do I model multilinear terms in Gurobi?. Additionally, you will require the addGenConstrSin method to model the \(\sin\) term. Unfortunately, \(\arccos\) is not supported in Gurobi. Thus, you will need to construct a piecewise-linear approximation of this function by hand. You can use the addGenConstrPWL method for this.
Given all the extra steps you have to make to model this highly nonlinear term in Gurobi, I would recommend to try a different solver, which is dedicated to such nonlinear expressions and possibly directly supports the \(\arccos\) function, multilinear terms, and variable division.
Best regards,
Jaromił0 -
Hi Jaromił,
Thank you for your detailed explanation here as well on the post How do I model constraints in Gurobi? I found it to be very helpful to also implement some conditional constraints in my model. Perhaps it might be useful to briefly mention somewhere that when working exclusively with negative variables, the parameter M should take on a different value -- not the upper bound of x and y, but the lower bound instead. It took me some time to figure out that this was the root cause behind a TypeError that kept popping up.
Thanks!
Best regards,
Purva
0 -
Hi Purva,
Thanks you for the suggestion. We improved the Knowledge Base article.
Best regards,
Jaromił0
投稿コメントは受け付けていません。
コメント
4件のコメント