-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Solutions to the second round of homework #3
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d970762
add: proposed solutions to the second round of homework
elblasco 38eb090
fix: typo
elblasco 344a518
fix: Or instead of ExactlyOne
elblasco d319824
fix: constraints, now constraint 2 works
elblasco 9fafc44
feat: complexity reduced to O(n^3)
elblasco 97737c5
fix: typo
elblasco 7bb6a4f
fix: yet another col_diag copied and pasted without thinking
elblasco 47f52d1
fix: constraint to prevent Nick from buying peacock
elblasco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
02-sat-advanced/homework-students-solutions/elblasco_hw_21.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from pysmt.shortcuts import And, ExactlyOne, Solver, Symbol, BOOL, TRUE | ||
|
||
vars = { | ||
f"x{i}{j}{k}": Symbol(f"x{i}{j}{k}", BOOL) | ||
for i in range(1, 10) | ||
for j in range(1, 10) | ||
for k in range(1, 10) | ||
} | ||
|
||
assertions = [] | ||
|
||
# Each number is contained exactly once in each row | ||
for number in range(1, 10): | ||
for row in range(1, 10): | ||
assertions.append( | ||
ExactlyOne( | ||
vars[f"x{row}{col}{number}"] | ||
for col in range(1, 10) | ||
) | ||
) | ||
|
||
# Each number is contained exactly once in each column | ||
for number in range(1, 10): | ||
for col in range(1, 10): | ||
assertions.append( | ||
ExactlyOne( | ||
vars[f"x{row}{col}{number}"] | ||
for row in range(1, 10) | ||
) | ||
) | ||
|
||
# Each number is contained exactly once in each 3x3 square | ||
for number in range(1, 10): | ||
for srow in range(1, 10, 3): | ||
for scol in range(1, 10, 3): | ||
assertions.append( | ||
ExactlyOne( | ||
vars[f"x{col}{row}{number}"] | ||
for row in range(srow, srow + 3) | ||
for col in range(scol, scol + 3) | ||
) | ||
) | ||
|
||
# Each number should appear exactly once in the first diagonal | ||
for number in range(1, 10): | ||
assertions.append( | ||
ExactlyOne( | ||
vars[f"x{row_col}{row_col}{number}"] | ||
for row_col in range(1, 10) | ||
) | ||
) | ||
|
||
# Each number should appear once exactly once in the second diagonal | ||
for number in range(1, 10): | ||
assertions.append( | ||
ExactlyOne( | ||
vars[f"x{row_col}{10 - row_col}{number}"] | ||
for row_col in range(1, 10) | ||
) | ||
) | ||
|
||
# Hidden: Exactly one number for each cell | ||
for col in range(1, 10): | ||
for row in range(1, 10): | ||
assertions.append( | ||
ExactlyOne( | ||
vars[f"x{row}{col}{number}"] | ||
for number in range(1, 10) | ||
) | ||
) | ||
|
||
# Numbers on the homework slide | ||
assertions.append( | ||
And( | ||
vars["x135"], vars["x171"], vars["x244"], vars["x259"], vars["x262"], | ||
vars["x319"], vars["x393"], vars["x423"], vars["x486"], vars["x529"], | ||
vars["x581"], vars["x622"], vars["x687"], vars["x711"], vars["x798"], | ||
vars["x846"], vars["x858"], vars["x867"], vars["x933"], vars["x974"] | ||
) | ||
) | ||
|
||
|
||
def print_sudoku_table(model: dict): | ||
for row in range(1, 10): | ||
if (row - 1) % 3 == 0: | ||
print("") | ||
for col in range(1, 10): | ||
if (col - 1) % 3 == 0: | ||
print(" ", end='') | ||
for num in range(1, 10): | ||
if model[vars[f"x{row}{col}{num}"]] is TRUE(): | ||
print(num, end='') | ||
print('') | ||
|
||
|
||
with Solver("msat") as solver: | ||
solver.add_assertions(assertions) | ||
if solver.solve(): | ||
model = solver.get_model() | ||
print_sudoku_table(model) | ||
else: | ||
print("UNSAT") |
114 changes: 114 additions & 0 deletions
114
02-sat-advanced/homework-students-solutions/elblasco_hw_22.py
masinag marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# https://logic.puzzlebaron.com/play.php?u2=9db6a5914e4ef572fe50bc57bac9efd5 | ||
|
||
from pysmt.shortcuts import Or, And, ExactlyOne, Solver, Symbol, BOOL, TRUE | ||
|
||
months = ["jan", "feb", "mar", "apr"] | ||
names = ["guadalupe", "keith", "ollie", "yvette"] | ||
cultures = ["ashdage", "fujishiro", "stosam", "yersim"] | ||
|
||
nvars = { | ||
f"{month}{name}": Symbol(f"{month}{name}", BOOL) | ||
for month in months | ||
for name in names | ||
} | ||
|
||
cvars = { | ||
f"{month}{culture}": Symbol(f"{month}{culture}", BOOL) | ||
for month in months | ||
for culture in cultures | ||
} | ||
|
||
assertions = [] | ||
|
||
# 1.Ollie has been studying the Stosam culture. | ||
assertions.append( | ||
Or( | ||
And(nvars[f"{month}ollie"], cvars[f"{month}stosam"]) | ||
for month in months | ||
) | ||
) | ||
|
||
# 2. Keith will publish 1 month after the person who has been studying the | ||
# Stosam culture. | ||
|
||
assertions.append( | ||
Or( | ||
And(nvars["febkeith"], cvars["janstosam"]), | ||
And(nvars["markeith"], cvars["febstosam"]), | ||
And(nvars["aprkeith"], cvars["marstosam"]) | ||
) | ||
) | ||
|
||
# 3. The student who will publish in March is either the person who has been | ||
# studying the Yersim culture or Guadalupe. | ||
|
||
assertions.append( | ||
ExactlyOne( | ||
cvars["maryersim"], nvars["marguadalupe"] | ||
) | ||
) | ||
|
||
# 4. The student who has been studying the Fujishiro culture will publish 1 | ||
# month after Yvette. | ||
|
||
assertions.append( | ||
ExactlyOne( | ||
And(nvars["janyvette"], cvars["febfujishiro"]), | ||
And(nvars["febyvette"], cvars["marfujishiro"]), | ||
And(nvars["maryvette"], cvars["aprfujishiro"]) | ||
) | ||
) | ||
|
||
# Each month must be associated with exactly one name | ||
for month in months: | ||
assertions.append( | ||
ExactlyOne( | ||
nvars[f"{month}{name}"] for name in names | ||
) | ||
) | ||
|
||
# Each month must be associated with exactly one culture | ||
for month in months: | ||
assertions.append( | ||
ExactlyOne( | ||
cvars[f"{month}{culture}"] for culture in cultures | ||
) | ||
) | ||
|
||
# Each name must be associated with exactly one month | ||
for name in names: | ||
assertions.append( | ||
ExactlyOne( | ||
nvars[f"{month}{name}"] for month in months | ||
) | ||
) | ||
|
||
# Each culture must be associated with exactly one month | ||
for culture in cultures: | ||
assertions.append( | ||
ExactlyOne( | ||
cvars[f"{month}{culture}"] for month in months | ||
) | ||
) | ||
|
||
with Solver("msat") as solver: | ||
solver.add_assertions(assertions) | ||
if solver.solve(): | ||
# print("SAT", solver.get_model()) | ||
model = solver.get_model() | ||
for month in months: | ||
name_out = [ | ||
name | ||
for name in names | ||
if model[nvars[f"{month}{name}"]] is TRUE() | ||
][0] | ||
culture_out = [ | ||
culture | ||
for culture in cultures | ||
if model[cvars[f"{month}{culture}"]] is TRUE() | ||
][0] | ||
print(f"Month {month}: {name_out} {culture_out}") | ||
else: | ||
print("UNSAT") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, well done 🚀