Constraint Issue: 'Var' object cannot be interpreted as an integer
AnsweredHello,
I have an issue where I have defined a variable as an integer, but my constraints fail to run due to this error. I have attached some pictures of my code for reference. Any ideas on what the issue could be?
Thanks in advance,
Quentin
-
Official comment
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?. -
It looks like the Variable object, \(\texttt{TT}\), is being used inside of the range() function. This function only accepts numerical values. This is what is causing your error.
It also looks like the total operation time, \(\texttt{TT}\), is quite important to your model. Unfortunately, defining whether or not constraints are added to the model based on a Variable value is not possible. These constraints are added to the model well before the value of \(\texttt{TT}\) is known in the solve. Luckily, there are a few ways that you can reformulate this.
For example, you can create a new binary variable list \(y\) that is up to \(\texttt{TTmax}\) variables long. In this case, \(\texttt{TTmax}\) is the maximum reasonable value \(\texttt{TT}\) could take. For each \(t = 0,1,2,...\textrm{TTmax}\), this new variable will be defined by,
- If \(t \leq \textrm{TT} - 1\) then \(y = 1\)
- If \(t \geq \textrm{TT}\) then \(y = 0\)
To constrain \(y\), you can use the following two constraints:
\(t - \textrm{TT} +1 \leq \textrm{TTmax}\cdot\left( 1-y(t)\right) \quad\quad t=0,1,...,\textrm{TTmax}\)
\(t - \textrm{TT} \geq -\textrm{TTmax} \cdot y(t) \quad\quad t=0,1,...,\textrm{TTmax}\)At this point, you have a binary variable that says whether or not a certain time period, \(t\), is within the operation time. Using this variable, you can create a series of indicator constraints to define the constraints you have defined over the set \(\texttt{t in range(TT)}\). These would be defined as,
\( \left(y(t) == 1\right) \rightarrow \texttt{<Your constraints>} \quad \quad t = 0,1,...\textrm{TTmax}\)
Here we have created a constraint over \(t = 0,1,...\textrm{TTmax}\). The big change we've made is that TTmax is a scalar value rather than a Variable.
For details on how to implement this, check out the Indicator Constraint documentation.
1 -
Alison,
Thank you for your help! Is there any way to make a quadratic indicator constraint? (e.g., see quadratic constraint below)
Cheers,
Quentin
0 -
Indicator constraints can only be used in conjunction with linear constraints. Fortunately, an easy workaround is to introduce a new auxiliary variable, add a constraint that this new variable is (e.g.) equal to your quadratic expression, then use that auxiliary variable in the indicator constraint.
For instance, if you want to add the indicator constraint
\begin{align*}z = 1 \implies x^\top Q x \leq 2,\end{align*}
you can do so by introducing a new variable \( y \), then modeling
\begin{align*}x^\top &Q x \leq y \\ z = 1 &\implies y \leq 2.\end{align*}
1
Post is closed for comments.
Comments
4 comments