I am currently implementing my own heuristic to solve MILP problems in gurobi python interface. All my variables are continuous in the beginning and at some point of time the solution becomes integeral which is my terminating condition.
Right now, to check for the binaryness of the variables I loop through all the variables and check if they are 0 or 1 like this:
for var in model.getVars():
if (var.x >= intFeasTol and var.x <= 1.0-intFeasTol):
break
where intFeasTol = 10^-4
. I am trying to see if any variable value in the optimal solution is either not 0 or 1 within tolerance.
Is there a better way or an api call to check if the solution is binary?
You can do this more efficiently and without (python) loops with numpy.
intFeasTol = 1e-4
xs = np.array(model.getAttr('X', model.getVars())
int_inf = (xs > intFeasTol) & (xs < 1.0 - intFeasTol)
If only some variables are binary, you keep them in a list (instead of calling model.getVars()), or filter them for vtype "B".