Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement evaluate so all tests pass: class SATInstance: # Constructor: provide n the number of variables and # an initial list of clauses. # Note

Implement "evaluate" so all tests pass:
class SATInstance:
# Constructor: provide n the number of variables and
# an initial list of clauses.
# Note that variable numbers will go from 1 to n inclusive.
# we can add clauses using the add_clause method.
def __init__(self, n, clauses):
self.n = n
self.m = len(clauses)
self.clauses = clauses
assert self.is_valid()
# is_valid
# Check if all clauses are correct.
# literals in each clause must be between 1 and n or -n and -1
def is_valid(self):
assert self.n >=1
assert self.m >=0
for c in self.clauses:
for l in c:
assert (1= l and l = self.n) or (-self.n = l and l =-1)
return True
# add_clause
# Add a new clause to the list of clauses
def add_clause(self, c):
#check the clause we are adding.
for l in c:
assert (1= l and l = self.n) or (-self.n =1 and l =-1)
self.clauses.append(c)
## Function: evaluate_literal
# Evaluate a literal against a partial truth assignment
# return 0 if the partial truth assignment does not have the variable corresponding to the literal
# return 1 if the partial truth assignment has the variable and the literal is true
# return -1 if the partial truth assignment has the variable and the literal is false
def evaluate_literal(self, partial_truth_assignment, literal):
var = abs(literal) # literal may be negated. First remove any negation using abs
if var not in partial_truth_assignment:
return 0
v = partial_truth_assignment[var]
if 1= literal = self.n:
return 1 if v else -1
else:
return -1 if v else 1
## TODO: Write your code here
# Function: evaluate
# See description above: partial_truth_assignment is a dictionary from 1.. n to true/false.
# since it is partial, we may have variables with no truth assignments.
# use evaluate_literal function as a useful primitive
# return +1 if the formula is already satisfied under partial_truth_assignment: i.e, all clauses are true
# return 0 if formula is indeterminate under partial_truth_assignment, all clauses are true or unresolved and at least one clause is unresolved.
# return -1 if formula is already violated under partial_truth_assignment, i.e, at least one clause is false
def evaluate(self, partial_truth_assignment):
# your code here
## BEGIN TESTS
print('-test1-')
f1= SATInstance(4,[[1,2,-4],[-2,-3,1],[-1,-2,-3]])
t1={1:True, 2:False}
e1= f1.evaluate(t1)
assert e1==1, f'Expected that f1 is satisfied by t1 but your code returns: {e1}'
print('-test2-')
t2={1:False, 2: False}
e2= f1.evaluate(t2)
assert e2==0, f'Expected that f1 is indeterminate under t2. Your code returns: {e2}'
print('-test3-')
f2= SATInstance(5,[[1,2,-5],[-4,-2,-1],[1,3,5],[-1,-5,-2],[1,2,-4]])
t3={1:True}
e3= f2.evaluate(t3)
assert e3==0, f'Expected that f2 is indeterminate under t3. Your code returns {e3}'
print('-test4-')
t4={1: True, 2: False}
e4= f2.evaluate(t4)
assert e4==1, f'Expected that f2 is satisfied by t4. Your code returns {e4}'
print('-test5-')
t5={1: False, 3: False, 5:False}
e5= f2.evaluate(t5)
assert e5==-1, f'Expected that f2 is violated by t5. Your code returns {e5}'
print('All tests passed: 10 points!')
## END TESTS
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Marketing Database Analytics

Authors: Andrew D. Banasiewicz

1st Edition

0415657881, 978-0415657884

More Books

Students also viewed these Databases questions