Answered step by step
Verified Expert Solution
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 to n inclusive.
# we can add clauses using the addclause method.
def initself n clauses:
self.n n
self.m lenclauses
self.clauses clauses
assert self.isvalid
# isvalid
# Check if all clauses are correct.
# literals in each clause must be between and n or n and
def isvalidself:
assert self.n
assert self.m
for c in self.clauses:
for l in c:
assert l and l self.n or self.n l and l
return True
# addclause
# Add a new clause to the list of clauses
def addclauseself c:
#check the clause we are adding.
for l in c:
assert l and l self.n or self.n and l
self.clauses.appendc
## Function: evaluateliteral
# Evaluate a literal against a partial truth assignment
# return if the partial truth assignment does not have the variable corresponding to the literal
# return if the partial truth assignment has the variable and the literal is true
# return if the partial truth assignment has the variable and the literal is false
def evaluateliteralself partialtruthassignment, literal:
var absliteral # literal may be negated. First remove any negation using abs
if var not in partialtruthassignment:
return
v partialtruthassignmentvar
if literal self.n:
return if v else
else:
return if v else
## TODO: Write your code here
# Function: evaluate
# See description above: partialtruthassignment is a dictionary from n to truefalse
# since it is partial, we may have variables with no truth assignments.
# use evaluateliteral function as a useful primitive
# return if the formula is already satisfied under partialtruthassignment: ie all clauses are true
# return if formula is indeterminate under partialtruthassignment, all clauses are true or unresolved and at least one clause is unresolved.
# return if formula is already violated under partialtruthassignment, ie at least one clause is false
def evaluateself partialtruthassignment:
# your code here
## BEGIN TESTS
printtest
f SATInstance
t:True, :False
e fevaluatet
assert e f'Expected that f is satisfied by t but your code returns: e
printtest
t:False, : False
e fevaluatet
assert e f'Expected that f is indeterminate under t Your code returns: e
printtest
f SATInstance
t:True
e fevaluatet
assert e f'Expected that f is indeterminate under t Your code returns e
printtest
t: True, : False
e fevaluatet
assert e f'Expected that f is satisfied by t Your code returns e
printtest
t: False, : False, :False
e fevaluatet
assert e f'Expected that f is violated by t Your code returns e
printAll tests passed: points!
## END TESTS
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started