Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# checking if a solution was found
if model.num_solutions:
out.write('route with total distance %g found: %s'
% (model.objective_value, places[0]))
nc = 0
while True:
nc = [i for i in V if x[nc][i].x >= 0.99][0]
out.write(' -> %s' % places[nc])
if nc == 0:
break
out.write('\n')
# sanity tests
from mip import OptimizationStatus
assert model.status == OptimizationStatus.OPTIMAL
assert round(model.objective_value) == 547
model.check_optimization_results()
self.solver_name = "osi"
existing_solver = osi_ptr != ffi.NULL
self.solver = SolverOsi(self, osi_ptr)
# list of constraints and variables
self.constrs = VConstrList(self)
self.vars = VVarList(self)
# if a fractional solution is being processed
self.fractional = True
if existing_solver:
self._status = self.solver.get_status()
else:
self._status = OptimizationStatus.LOADED
# initializing additional control variables
self.__cuts = -1
self.__cut_passes = -1
self.__clique = -1
self.__preprocess = -1
self.__constrs_generator = None
self.__lazy_constrs_generator = None
self.__start = None
self.__threads = 0
self.__n_cols = 0
self.__n_rows = 0
self.__gap = INF
self.__store_search_progress_log = False
def check_optimization_results(self):
"""Checks the consistency of the optimization results, i.e., if the
solution(s) produced by the MIP solver respect all constraints and
variable values are within acceptable bounds and are integral when
requested"""
if self.status in [
mip.OptimizationStatus.FEASIBLE,
mip.OptimizationStatus.OPTIMAL,
]:
assert self.num_solutions >= 1
if self.num_solutions or self.status in [
mip.OptimizationStatus.FEASIBLE,
mip.OptimizationStatus.OPTIMAL,
]:
if self.sense == mip.MINIMIZE:
assert self.objective_bound <= self.objective_value + 1e-10
else:
assert self.objective_bound + 1e-10 >= self.objective_value
for c in self.constrs:
if c.expr.violation >= self.infeas_tol + self.infeas_tol * 0.1:
raise mip.InfeasibleSolution(
"Constraint {}:\n{}\n is violated."
"Computed violation is {}."
self.__x = ffi.new("double[{}]".format(self.num_cols()))
self.__obj_val = self.get_dbl_attr("ObjVal")
attr = "X".encode("utf-8")
st = GRBgetdblattrarray(
self._model, attr, 0, self.num_cols(), self.__x
)
if st:
raise ParameterNotAvailable("Error querying Gurobi solution")
return OptimizationStatus.FEASIBLE
return OptimizationStatus.NO_SOLUTION_FOUND
if status == 1: # LOADED
return OptimizationStatus.LOADED
if status == 2: # OPTIMAL
if isinstance(self.__x, EmptyVarSol):
self.__obj_val = self.get_dbl_attr("ObjVal")
self.__x = ffi.new("double[{}]".format(self.num_cols()))
attr = "X".encode("utf-8")
st = GRBgetdblattrarray(self._model, attr, 0, self.num_cols(), self.__x)
if st:
raise ParameterNotAvailable("Error quering Gurobi solution")
if (self.num_int() + self.get_int_attr("NumSOS")) == 0 or (relax):
self.__pi = ffi.new("double[{}]".format(self.num_rows()))
attr = "Pi".encode("utf-8")
st = GRBgetdblattrarray(
self._model, attr, 0, self.num_rows(), self.__pi
)
else:
import mip.cbc
self.solver = mip.cbc.SolverCbc(self, self.name, sense)
self.solver_name = mip.CBC
# list of constraints and variables
self.constrs = mip.ConstrList(self)
self.vars = mip.VarList(self)
# initializing additional control variables
self.__cuts = 1
self.__cuts_generator = None
self.__lazy_constrs_generator = None
self.__start = []
self._status = mip.OptimizationStatus.LOADED
self.__threads = 0