How to add a binary variable without objective value using the Java API
回答済みDear all,
my question is how to create binary variable in Gurobi without assigning a cost/objective value to it. I use the Java API method `GRBModel.addVar(...)` to add variables to my model; this is the function signature([REF](https://www.gurobi.com/documentation/9.5/refman/java_model_addvar.html#javamethod:GRBModel.addVar)):
GRBVar addVar (double lb, double ub, double obj, char type, String name)
This method only alows adding variables with associated objective value (i.e. the `obj` argument).
However, for the problem I am trying to solve, I need to introduce an auxiliary binary variable (see below for details), which I use to "deactivate" a constraint (lets call this variable \(d\)). I do not want the model to incur a cost, when the auxiliary variable \(d\) is selected.
Is this possible and I do this?
Here is some more details of my particular optimization problem:
I have a set of "main" constraints like this one. Let's consider a single instance for simplicity:
\(\sum \beta_j \cdot b_j \leq \sum_i \alpha_i \cdot a_i + M \cdot d\)
where \(a_i\), \(b_j\) are binary variables with respective objective values \(\alpha_i\), \(\beta_j\). \(d\) is the auxiliary binary variable mentioned earlier.
I want this constraint become "inactive" when \(\sum_i a_i = 0\). To do this I add this auxiliary constraint:
\(\sum_i a_i + d = 1\)
Thus \(d=1\) when all \(a_i=0\) and vice-versa and hence the constraint above becomes:
\(\sum \beta_j \cdot b_j \leq M \cdot d\)
where in my code I set \(M\) to the maximum value allowed by double values in Java (i.e. `M=Double.MAX_VALUE`). Thus the sum \(\sum \beta_j \cdot b_j\) is unconstraint, when \(\sum_i a_i = 0\).
But (as said above) I do not want the selection of \(d\) be penalized or favored by an objective value. It should be just auxiliary variable/dummy variable without associated objective value ...
Thank you in advance,
Michael
PS: I found [this example](https://support.gurobi.com/hc/en-us/community/posts/360057913451/comments/360009559012) on the Gurobi forum, where apparantly a binary variable is created without associated cost using the Python API.
PPS: How do I format Math equations on this forum?
-
正式なコメント
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 Michael,
You can set the obj argument value to 0. Then, the variable will not be added to the objective function. Note that 0 is the default value for the obj argument in the addVar Python method, that is why it is not necessary to explicitly set it.
PPS: How do I format Math equations on this forum?
This is explained in Posting to the Community Forum. You can either use \(\texttt{\\( \\)}\) or \(\texttt{\[ \]}\) to enclose math code.
Best regards,
Jaromił0
投稿コメントは受け付けていません。
コメント
2件のコメント