-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathmaximize_combinations_sat.py
75 lines (58 loc) · 2.21 KB
/
maximize_combinations_sat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
# Copyright 2010-2025 Google LLC
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Maximize the number of valid combinations of Boolean variables."""
from typing import Sequence
from absl import app
from ortools.sat.python import cp_model
def maximize_combinations_sat() -> None:
"""Maximize the number of valid combinations of Boolean variables."""
model = cp_model.CpModel()
cards: list[cp_model.IntVar] = [
model.new_bool_var("card1"),
model.new_bool_var("card2"),
model.new_bool_var("card3"),
model.new_bool_var("card4"),
]
combos: list[list[cp_model.IntVar]] = [
[cards[0], cards[1]],
[cards[0], cards[2]],
[cards[1], cards[3]],
[cards[0], cards[2], cards[3]],
]
deck_size: int = 3
model.add(sum(cards) == deck_size)
valid_combos: list[cp_model.IntVar] = []
for combination in combos:
is_valid = model.new_bool_var("")
# All true implies is_valid.
model.add_bool_and(is_valid).only_enforce_if(combination)
# is_valid implies all true.
for literal in combination:
model.add_implication(is_valid, literal)
valid_combos.append(is_valid)
model.maximize(sum(valid_combos))
solver = cp_model.CpSolver()
solver.parameters.log_search_progress = True
status = solver.solve(model)
if status == cp_model.OPTIMAL:
print(
"chosen cards:",
[card.name for card in cards if solver.boolean_value(card)],
)
def main(argv: Sequence[str]) -> None:
if len(argv) > 1:
raise app.UsageError("Too many command-line arguments.")
maximize_combinations_sat()
if __name__ == "__main__":
app.run(main)