I've recently installed the Docplex optimization package on my Python to solve a problem. Then, I defined some integer and binary variables for the problem. But I've got approximate float values for these variables after running! I will explain more about the problem in the following lines.
I've mentioned a part of my code including some integer and binary variables.
from docplex.mp.model import Model
from datetime import datetime
model = Model(name='Logistics')
T = 3
P = 5
A = 13
B = 2
R = 2
QB = [[[[[model.integer_var(name=f"QB_{t}_{p}_{q}_{a}_{b}") for b in
range(B)] for a in range(A)] for q in range(Qproduct)] for p in range(P)] for t in range(T)]
QBR = [[[[[model.integer_var(name=f"QBR_{t}_{p}_{q}_{b}_{r}") for r in
range(R)] for b in range(B)] for q in range(Qproduct)] for p in range(P)] for t in range(T)]
LR = [[model.binary_var(name=f"LR_{t}_{r}") for r in range(R)] for t in range(T)]
As you can see in above mentioned codes, I've represented QB and QBR as two integer variables and LR as a binary one. I ran the code and got this result.
Model: Logistics
number of variables: 60941
number of constraints: 14596
parameters: defaults
objective: maximize
problem type is: MILP
QB = 8314.999999999944
QBR = 5565.999999999924
LR = 6.000000000003007
profit: 391970.3199999964
time: 0:24:03.586190
I want to know why I got float values instead of integer? Please help me to fix it if there is any mistakes in my definitions. Thank you!
Very common question. Technote is a very good answer
This is a consequence of the integrality tolerance parameter, CPX_PARAM_EPINT (default value: 1e-5).
This parameter specifies the amount by which an integer variable can violate its integrality requirement. The default value of the integrality tolerance is fine for most MIPs.
You can use round after solve.
number = 11.99
rounded_number = int(round(number))
print(rounded_number)