def test_mip_file(solver: str, instance: str): """Tests optimization of MIP models stored in .mps or .lp files""" m = Model(solver_name=solver) # optional file for optimal LP basis bas_file = "" iname = "" for ext in EXTS: if instance.endswith(ext): bas_file = instance.replace(ext, ".bas") if not exists(bas_file): bas_file = "" iname = basename(instance.replace(ext, "")) break assert iname in BOUNDS.keys() lb = BOUNDS[iname][0] ub = BOUNDS[iname][1] assert lb <= ub + TOL has_opt = abs(ub - lb) <= TOL max_dif = max(max(abs(ub), abs(lb)) * 0.01, TOL) m.read(instance) if bas_file: m.verbose = True m.read(bas_file) m.optimize(relax=True) print("Basis loaded!!! Obj value: %f" % m.objective_value) m.optimize(max_nodes=MAX_NODES) if m.status in [OptimizationStatus.OPTIMAL, OptimizationStatus.FEASIBLE]: assert m.num_solutions >= 1 m.check_optimization_results() assert m.objective_value >= lb - max_dif if has_opt and m.status == OptimizationStatus.OPTIMAL: assert abs(m.objective_value - ub) <= max_dif elif m.status == OptimizationStatus.NO_SOLUTION_FOUND: assert m.objective_bound <= ub + max_dif else: assert m.status not in [ OptimizationStatus.INFEASIBLE, OptimizationStatus.INT_INFEASIBLE, OptimizationStatus.UNBOUNDED, OptimizationStatus.ERROR, OptimizationStatus.CUTOFF, ] assert m.objective_bound <= ub + max_dif
for (i, j) in product(V - {0}, V - {0}): if i != j: model += y[i] - (n + 1) * x[i][j] >= y[j] - n # optimizing model.threads = 4 model.optimize(max_nodes=75) # checking if a solution was found if model.num_solutions: out.write("route with total distance %g found: %s" % (model.objective_value, 0)) nc = 0 while True: nc = [i for i in V if x[nc][i].x >= 0.99][0] out.write(" -> %s" % nc) if nc == 0: break out.write("\n") # sanity tests from mip import OptimizationStatus if model.status == OptimizationStatus.OPTIMAL: assert round(model.objective_value) == 7013 elif model.status == OptimizationStatus.FEASIBLE: assert round(model.objective_value) >= 7013 else: assert model.objective_bound <= 7013 + 1e-7 model.check_optimization_results()
for i in N: m += xsum(x[i][c] for c in U) == r[i] for i, j, c1, c2 in product(N, N, U, U): if i != j and c1 <= c2 < c1 + d[i][j]: m += x[i][c1] + x[j][c2] <= 1 for i, c1, c2 in product(N, U, U): if c1 < c2 < c1 + d[i][i]: m += x[i][c1] + x[i][c2] <= 1 for i, c in product(N, U): m += z >= (c + 1) * x[i][c] m.optimize(max_nodes=30) if m.num_solutions: for i in N: print('Channels of node %d: %s' % (i, [c for c in U if x[i][c].x >= 0.99])) # sanity tests from mip import OptimizationStatus assert m.objective_bound <= 41 + 1e-10 if m.status == OptimizationStatus.OPTIMAL: assert round(m.objective_value) == 41 elif m.status == OptimizationStatus.FEASIBLE: assert m.objective_value >= 41 - 1e-10 m.check_optimization_results()
# checking if columns with negative reduced cost were produced and # adding them into the restricted master problem if pricing.objective_value < -1e-5: pattern = [a[i].x for i in range(m)] column = Column(constraints, pattern) lambdas.append( master.add_var(obj=1, column=column, name='lambda_%d' % (len(lambdas) + 1))) print('new pattern = {pattern}'.format(**locals())) # if no column with negative reduced cost was produced, then linear # relaxation of the restricted master problem is solved else: new_vars = False pricing.write('pricing.lp') # printing the solution print('') print('Objective value: {master.objective_value:.3}'.format(**locals())) print('Solution: ', end='') for v in lambdas: if v.x > 1e-6: print('{v.name} = {v.x:.3} {v.column}'.format(**locals())) print(' ', end='') # sanity checks master.check_optimization_results()