Skip to content
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

Added command line input functionality #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion SATSolver.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
from copy import deepcopy
import re

assign_true = set()
assign_false = set()
Expand Down Expand Up @@ -109,5 +110,34 @@ def dpll():
print('\nResult: UNSATISFIABLE')
print()

# New function to solve a single CNF at a time when called from the command line (must use "~" for negation)
def dpll(cnf_argument):
global assign_true, assign_false, n_props, n_splits
literals = [i for i in list(set(cnf_argument)) if i.isalpha()]
if not re.match(r'^(\s*(!?[A-Z]\s*)+)$', cnf_argument):
print('Invalid input format; please submit one formula at a time in CNF (e.g., ~A B ~C). Please try again.')
return
elif solve([cnf_argument], literals):
print('\nNumber of Splits =', n_splits)
print('Unit Propogations =', n_props)
print('\nResult: SATISFIABLE')
print('Solution:')
for i in assign_true:
print('\t\t'+i, '= True')
for i in assign_false:
print('\t\t'+i, '= False')
else:
print('\nReached starting node!')
print('Number of Splitss =', n_splits)
print('Unit Propogations =', n_props)
print('\nResult: UNSATISFIABLE')
print()

# Modified main function to allow for command line input
if __name__=='__main__':
dpll()
if len(sys.argv)==2:
dpll()
elif len(sys.argv)==3 and sys.argv[1] == 'solve':
formula = sys.argv[2].replace("~", "!")
print('Solving CNF:', formula)
dpll(formula)