def test_minisat(self) -> None: ip = IP() x = ip.add_boolean() y = ip.add_boolean() ip.add_objective(-x - y) solver = Solver(Type.minisat) self.assertTrue(solver.solutions(ip).get()) self.assertAlmostEqual(x.value(), 1) self.assertAlmostEqual(y.value(), 1)
def test_sudoku(self) -> None: n = 3 ip = IP() x = ip.add_boolean_cube(n * n, n * n, n * n) for i in range(n * n): for j in range(n * n): s = Sum(0) for k in range(n * n): s += x[i][j][k] ip.add_constraint(s == 1) for i in range(n * n): for k in range(n * n): s = Sum(0) for j in range(n * n): s += x[i][j][k] ip.add_constraint(s == 1) for j in range(n * n): for k in range(n * n): s = Sum(0) for i in range(n * n): s += x[i][j][k] ip.add_constraint(s == 1) for i in range(n): for j in range(n): for k in range(n * n): s = Sum(0) for i2 in range(n): for j2 in range(n): s += x[n * i + i2][n * j + j2][k] ip.add_constraint(s == 1) solver = Solver() solutions = solver.solutions(ip) self.assertTrue(solutions.get()) d = np.zeros((n * n, n * n)) for i in range(n * n): for j in range(n * n): for k in range(n * n): d[i, j] += (k + 1) * x[i][j][k].value() for i in range(n * n): self.assertEqual(len(set(d[:, i])), n * n) self.assertEqual(len(set(d[i, :])), n * n) for i in range(n): for j in range(n): self.assertEqual( len(set(d[n * i:n * i + n, n * j:n * j + n].flatten())), n * n)
def test_next(self) -> None: ip = IP() x = ip.add_boolean() y = ip.add_boolean() z = ip.add_boolean() ip.add_constraint(x + y + z >= 1) solver = Solver() solutions = solver.solutions(ip) n = 0 while solutions.get(): n += 1 self.assertEqual(n, 7)
def test_infeasible(self) -> None: ip = IP() x = ip.add_variable() y = ip.add_variable() ip.add_constraint(x + y <= 1) ip.add_constraint(x + y >= 2) ip.add_constraint(0 <= x) ip.add_constraint(x <= 3) ip.add_constraint(0 <= y) ip.add_constraint(y <= 3) ip.add_objective(-x) solver = Solver() self.assertFalse(solver.solutions(ip).get())
def test_fake_cloud(self) -> None: solver = Solver() solutions = solver.solutions(self.ip) self.assertTrue(solutions.get()) self.verify_solution() fake_server = FakeNeosServer(self.ip.solution) self.ip.solution = None cloud_solver = CloudSolver("http://localhost:8000") solutions = cloud_solver.solutions(self.ip) self.assertTrue(solutions.get()) self.verify_solution() fake_server.stop()
def test_basic(self) -> None: ip = IP() x = ip.add_variable() y = ip.add_variable() ip.add_constraint(x + y <= 1) ip.add_constraint(0 <= x) ip.add_constraint(x <= 1) ip.add_constraint(0 <= y) ip.add_constraint(y <= 1) ip.add_objective(-x) solver = Solver() self.assertTrue(solver.solutions(ip).get()) self.assertEqual(x.value(), 1) self.assertEqual(y.value(), 0)
def test_basic_variable_type(self) -> None: ip = IP() x = ip.add_variable(True) y = ip.add_boolean() ip.add_bounds(0, x, 0.5) ip.add_objective(-x - y) self.assertTrue(Solver().solutions(ip).get()) self.assertEqual(x.value(), 0) self.assertEqual(y.value(), 1)
def test_basic_bounds(self) -> None: ip = IP() x = ip.add_variable() y = ip.add_variable() ip.add_bounds(0, x, 1) ip.add_bounds(0, y, 1) ip.add_constraint(x + y <= 1) ip.add_objective(-x) self.assertTrue(Solver().solutions(ip).get()) self.assertEqual(x.value(), 1) self.assertEqual(y.value(), 0)
def find_groups(num_weeks): print("Number of weeks:", num_weeks) all_weeks = list(range(num_weeks)) start_time = time.time() ip = IP() x = ip.add_boolean_grid(len(all_groups), num_weeks) # Symmetry breaking start = set() for i in range(num_groups): start.add(tuple(range(group_size * i, group_size * (i + 1)))) second_week = tuple([group_size * i for i in range(group_size)]) for i, g in enumerate(all_groups): # First week is completely fixed. if g in start: ip.add_constraint(x[i, 0] == 1) for w in range(1, num_weeks): ip.add_constraint(x[i, w] == 0) else: ip.add_constraint(x[i, 0] == 0) # Second week 0 and 5 plays together. if num_weeks > 1: if second_week == g: ip.add_constraint(x[i, 1] == 1) else: for j in second_week: if j in g: ip.add_constraint(x[i, 1] == 0) # Everyone plays exactly once every week. for w in all_weeks: for p in all_players: s = Sum() for i, g in enumerate(all_groups): if p in g: s += x[i, w] ip.add_constraint(s == 1) # Everyone plays together no more than once. for p1, p2 in itertools.combinations(all_players, 2): s = Sum() for i, g in enumerate(all_groups): if p1 in g and p2 in g: for w in all_weeks: s += x[i, w] ip.add_constraint(s <= 1) print("Creation time:", int(time.time() - start_time)) start_time = time.time() if not Solver().solutions(ip).get(): return False print("Solve time:", int(time.time() - start_time)) for w in all_weeks: print("Week", w + 1) for i, g in enumerate(all_groups): if x[i, w].value() > 0.5: print(g, end=" ") print("") return True