Gurobi Absolute Value Constraints
Awaiting user inputHi,
How to formulate the absolute value constraints in the Gurobi Python API?
I have the constraint and I implements it but gives strange results by violating the constraints. How to fix it?
Thanks

Hi Muhammad,
Could you please share what you tried so far and try to explain in more detail what you mean by
it but gives strange results by violating the constraints
Best regards,
Jaromił0 
Hi Jaromił,
Thanks for your response, please see the below formulation, I implemented it on the Gurobi Python API, but It gives an optimal solution with violation in constraints.
Here decision variables are 'x', 'x^k', and 'w' are the free variables, 'y' is binary, and 't' and 'z' have nonnegative values to be taken.
The input I use is matrix 'a'.
\epsilon = 1e4.
The output I got has a violation of one of the constraints,
How to fix this issue?
I use several parameters including FeasibilityTol=1e6, Presolve=0, and NumericFocus=3 but the violation remains...
Regard, Arslan
0 
Moreover, when given a fixed bound to 'x^k', e.g. 1e6 to 1e6, (26) constraint is violated which absolute value constraint and performs this strange behavior:
t_2_1 should be xp_2_1 but it gives a strange value, how to fix it?
0 
Hi Muhammad,
How to fix this issue?
A constraint violation of ~2e11 is nothing to be worried about. Note that Gurobi uses numerical algorithm so it is basically impossible to achieve a 0 violation for models with continuous variables.
There is no fix to this issue. You should rather take the optimal solution point and check whether it is good enough for your application. If not, you could try to polish it yourself, i.e., round some variable values while pertaining feasibility of the model.
Moreover, when given a fixed bound to 'x^k', e.g. 1e6 to 1e6, (26) constraint is violated which absolute value constraint and performs this strange behavior:
This may happen for (absolute value) general constraints, because variable bounds are used to reformulate the absolute value constraints in a MIP fashion. To avoid this, you can do the following in the ordering given
 You should try tightening the bounds of \(x^k\) as much as possible.
 You could try experimenting with the PreSOS1BigM and PreSOS2BigM parameters.
 You could also try tightening the FeasibilityTol and IntFeasTol parameters to tighten feasibility tolerances.
Note that even with tighter bounds and tolerances, it may be inevitable that a small violation persists, e.g., \(x^p_{21} = 10^{8}, t_{21} = 2\cdot 10^{8}\). As mentioned above, this is unavoidable due to machine precision and the numerical algorithms involved and the best way is to polish the optimal solution point after the optimization process is finished.
Best regards,
Jaromił0 
Hi Jaromił,
Thanks for your response.
There is no fix to this issue. You should rather take the optimal solution point and check whether it is good enough for your application. If not, you could try to polish it yourself, i.e., round some variable values while pertaining feasibility of the model.
The optimal value is correct but the variable values that are taken for optimal solution by model are not satisfying. Also, how to set the variable precision that is to be taken up to 7 decimal places.
I have to find set zero violations for better results and the results I got have some numerical issues, so how to handle precision and use precise variable values when solving the model?
Thanks, Arslan
0 
The optimal value is correct but the variable values that are taken for optimal solution by model are not satisfying. Also, how to set the variable precision that is to be taken up to 7 decimal places.
As I pointed out in my previous comment
 You should try tightening the bounds of as much as possible.
 You could try experimenting with the PreSOS1BigM and PreSOS2BigM parameters.
 You could also try tightening the FeasibilityTol and IntFeasTol parameters to tighten feasibility tolerances.
Additionally, you might want to experiment with the NumericFocus and IntegralityFocus parameters.
I have to find set zero violations for better results and the results I got have some numerical issues, so how to handle precision and use precise variable values when solving the model?
This will not be possible as long as your model has continuous variables (unless you get very lucky). The only way is to use the solution point provided by Gurobi and try to manually polish it, i.e., try to perturb some variable values to reduce the violation, while maintaining feasibility and optimality. However, since the violation is so small, I cannot imagine that this is an easy task.
0 
Thanks for your responses.
I used the parameters that you mentioned but NO change in the solution, even though I tried tight bounds.
Can I put the bound on the precision of the variable so that it only takes the desired values after the decimal places for solving the model? e.g x= 2.9001000000000015 but if I have to allow this to take only up to 7 decimal places, like x = 2.9001001? can it be possible to set the precision of the variable in Gurobi?
Regards, Arslan
0 
Hi Arslan,
Can I put the bound on the precision of the variable so that it only takes the desired values after the decimal places for solving the model? e.g x= 2.9001000000000015 but if I have to allow this to take only up to 7 decimal places, like x = 2.9001001? can it be possible to set the precision of the variable in Gurobi?
Unfortunately, it is currently not possible to set individual variable tolerances.
You could try the following:
 Solve the model and extract the solution point
 Manually polish the solution point, e.g., round 2.9001000000000015 to 2.9001
 Collect all constraints that have a too large violation
 For each violated constraint, fix some of the variables to their polished solution value
 Reoptimize and check whether constraint violations improved without degrading the optimal solution value
 Reiterate with different variables if needed
As already mentioned, there is no good way to reduce constraint violation way below a 1e10 threshold by just adjusting parameters or tightening bounds. It is in the nature of numerical algorithms that they are inaccurate up to some digits. May I ask why you need a such high precision solution?
You could also try the Quad parameter but I doubt that it will help.
Best regards,
Jaromił0 
You could also share the model you generated so I can take a closer look. Note that uploading files in the Community Forum is not possible but we discuss an alternative in Posting to the Community Forum.
0 
Appreciate your prompt responses.
I modeled the formulation to check the linear independence of columns of the input I used which is an adjacency matrix of a simple graph "a" and its linear dependence of columns of its adjacency matrix after deletion of one vertex. I got an optimal solution but I faced violations and infeasibility in variable values in the linear dependence constraints. So, I tried to take precise values that can satisfy the linear dependence case, maybe the issue is in the precision...
0
Please sign in to leave a comment.
Comments
10 comments