def test_solver(): instance = _get_instance() for mode in ["exact", "heuristic"]: for internal_solver in ["cplex", "gurobi"]: solver = LearningSolver(time_limit=300, gap_tolerance=1e-3, threads=1, solver=internal_solver, mode=mode) solver.solve(instance) assert instance.solution["x"][0] == 1.0 assert instance.solution["x"][1] == 0.0 assert instance.solution["x"][2] == 1.0 assert instance.solution["x"][3] == 1.0 assert instance.lower_bound == 1183.0 assert instance.upper_bound == 1183.0 assert round(instance.lp_solution["x"][0], 3) == 1.000 assert round(instance.lp_solution["x"][1], 3) == 0.923 assert round(instance.lp_solution["x"][2], 3) == 1.000 assert round(instance.lp_solution["x"][3], 3) == 0.000 assert round(instance.lp_value, 3) == 1287.923 solver.fit([instance]) solver.solve(instance)
def test_learning_solver(): instance = _get_instance() for mode in ["exact", "heuristic"]: for internal_solver in ["cplex", "gurobi", GurobiSolver]: solver = LearningSolver(time_limit=300, gap_tolerance=1e-3, threads=1, solver=internal_solver, mode=mode) solver.solve(instance) assert instance.solution["x"][0] == 1.0 assert instance.solution["x"][1] == 0.0 assert instance.solution["x"][2] == 1.0 assert instance.solution["x"][3] == 1.0 assert instance.lower_bound == 1183.0 assert instance.upper_bound == 1183.0 assert round(instance.lp_solution["x"][0], 3) == 1.000 assert round(instance.lp_solution["x"][1], 3) == 0.923 assert round(instance.lp_solution["x"][2], 3) == 1.000 assert round(instance.lp_solution["x"][3], 3) == 0.000 assert round(instance.lp_value, 3) == 1287.923 assert instance.found_violations == [] assert len(instance.solver_log) > 100 solver.fit([instance]) solver.solve(instance) # Assert solver is picklable with tempfile.TemporaryFile() as file: pickle.dump(solver, file)
def test_subtour(): n_cities = 6 cities = np.array([ [0., 0.], [1., 0.], [2., 0.], [3., 0.], [0., 1.], [3., 1.], ]) distances = squareform(pdist(cities)) instance = TravelingSalesmanInstance(n_cities, distances) for solver_name in ['gurobi', 'cplex']: solver = LearningSolver(solver=solver_name) solver.solve(instance) x = instance.solution["x"] assert x[0, 1] == 1.0 assert x[0, 4] == 1.0 assert x[1, 2] == 1.0 assert x[2, 3] == 1.0 assert x[3, 5] == 1.0 assert x[4, 5] == 1.0 solver.fit([instance]) solver.solve(instance)